<template>
  <div style="height: 525px" v-loading="containerLoading">
    <el-input v-model="searchInput" @change="handleChange" placeholder="按ASIN搜索"></el-input>
    <hr style="margin-top: 5px" />
    <el-table :data="commodityData" :show-header="false" @selection-change="handleSelectionChange" height="450" style="width: 100%">
      <el-table-column prop="date" label="商品">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <div style="margin-right: 8px; line-height: normal">
              <el-image class="img-box" :src="scope.row.image_link" />
            </div>
            <div>
              <el-tooltip class="box-item" effect="dark" :content="scope.row.title" placement="top">
                <div class="double-line">{{ scope.row.title ? scope.row.title : '--' }}</div>
              </el-tooltip>
              <span
                >ASIN:
                <span class="data-color" style="margin-right: 8px">{{ scope.row.asin ? scope.row.asin : '--' }}</span>
              </span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="Name" width="80">
        <template #default="scope"> </template>
      </el-table-column>
      <el-table-column type="selection" width="55" />
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { defineEmits, ref } from 'vue'
import { getCommodityCard } from '../api/index'
import { useShopInfo } from '/@/stores/shopInfo'

const shopInfo = useShopInfo()
const { profile } = storeToRefs(shopInfo)

const containerLoading = ref(false)
const searchInput = ref('')
const commodityData: any = ref('')

function handleChange() {
  searchAsin()
}

async function searchAsin() {
  containerLoading.value = true
  try {
    const query = {
      profile_id: profile.value.profile_id,
      asin: searchInput.value,
    }
    const response = await getCommodityCard(query)
    commodityData.value = response.data
  } catch (error) {
    console.log('error:', error)
  } finally {
    containerLoading.value = false
  }
}

// 暴露数据给父组件
const emit = defineEmits(['updateSelected'])

function handleSelectionChange(selection) {
  // selection 是所有被选中的行数据
  emit('updateSelected', selection)
}

</script>

<style scoped>
.custom-tree-node {
  /* el-tree自定义样式 */
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.double-line {
  color: #1e2128;
  font-weight: 500;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  white-space: pre-wrap;
  word-break: break-word;
}
.data-color {
  color: rgb(30, 33, 41);
}
.img-box {
  width: 60px;
  height: 60px;
  margin-top: 5px;
  border: 1px solid rgb(194, 199, 207);
  border-radius: 4px;
}
</style>
