<script lang="ts" setup>
/**
 * @Name: DataTableSlot.vue
 * @Description: 商品监控-表格插槽
 * @Author: Cheney
 */

import { useCountryInfoStore } from '/@/stores/countryInfo';
import { Delete, InfoFilled, Operation, Tickets, Timer } from '@element-plus/icons-vue';
import { getTagType } from '/@/utils/useTagColor';
import PermissionButton from '/@/components/PermissionButton/index.vue';
import ProductInfo from '/@/views/product-manage/component/ProductInfo.vue';
import ProgressBar from '/@/views/product-manage/product-monitor/component/ProgressBar.vue';
import router from '/@/router';


const props = defineProps<{
  row: any,
  field: any
}>();
const { row, field } = props;

const emit = defineEmits([ 'edit-row', 'handle-delete' ]);

const countryInfoStore = useCountryInfoStore();
const country = countryInfoStore.Countries.find(c => c.code == row.country_code);
const color = country ? country.color : '#3875F6';

const statusText = row.status === 1 ? '在售' : '停售';
const statusType = row.status === 1 ? 'success' : 'info';

function handleEdit() {
  emit('edit-row', row);
}

function onConfirm() {
  emit('handle-delete', row);
}

function handleNavigate(path: string) {
  console.log('row=> ', row);
  router.push({
    path,
    query: {
      img: row.goods.img,
      title: row.goods.title,
      url: row.goods.url,
      asin: row.goods.asin
    }
  });
}

</script>

<template>
  <div class="font-medium">
    <div v-if="field === 'product_info'">
      <ProductInfo :img-width="50" :item="row.goods"/>
    </div>
    <div v-else-if="field === 'country_code'">
      <el-tag :disable-transitions="true" :style="{ color: color, borderColor: color }" effect="plain" round>
        {{ country ? country.name : '-' }}
      </el-tag>
    </div>
    <div v-else-if="field === 'shop_name'">
      <el-tag v-if="row.goods.shop_name" :disable-transitions="true" :type=getTagType(row.shop_name)>
        {{ row.shop_name }}
      </el-tag>
      <div v-else>-</div>
    </div>
    <div v-else-if="field === 'tag'">
      <el-tag v-if="row.goods.tag" :disable-transitions="true" :type=getTagType(row.goods.tag)>
        {{ row.goods.tag }}
      </el-tag>
      <div v-else>-</div>
    </div>
    <div v-else-if="field === 'brand'">
      <el-tag v-if="row.goods.brand" :disable-transitions="true" :type=getTagType(row.goods.brand) effect="plain" round>
        {{ row.goods.brand }}
      </el-tag>
      <div v-else>-</div>
    </div>
    <div v-else-if="field === 'price_info'">
      <div v-if="row.goods.price >= 0" class="font-medium">
        <p>现 价：{{ row.goods.currency_code + '‎' + row.goods.price }}</p>
        <p>折 扣：{{ row.goods.discount > 0 ? row.goods.discount + '%' : '-' }}</p>
        <p>优惠劵：{{ !row || row.goods.coupon <= 0 ? '-' : row.goods.currency_code + '‎' + row.goods.coupon }}</p>
      </div>
    </div>
    <div v-else-if="field === 'show_price'">
      <div class="font-medium">
        {{ row.goods.show_price ? row.goods.currency_code + row.goods.show_price : '-' }}
      </div>
    </div>
    <div v-else-if="field === 'activity_price'">
      <div class="font-medium">
        {{ row.goods.activity_price ? row.goods.currency_code + row.goods.activity_price : '-' }}
      </div>
    </div>
    <div v-else-if="field === 'minimum_price'">
      <div class="font-medium">
        {{ row.goods.minimum_price ? row.goods.currency_code + row.goods.minimum_price : '-' }}
      </div>
    </div>
    <div v-else-if="field === 'score'">
      <template v-if="row.goods.score !== null && row.goods.score !== undefined && row.goods.score !== ''">
        <el-rate
            v-if="row.goods.score > 0"
            v-model="row.goods.score"
            :colors="['#FF0000', '#FF9900', '#67C23A']"
            disabled
            show-score
            text-color="#1e293b"
        />
        <span v-else>{{ row.goods.score }}</span> <!-- 值为0时显示0 -->
      </template>
      <template v-else>
        <span>-</span> <!-- 无值时显示'--' -->
      </template>
    </div>
    <div v-else-if="field === 'all_score'">
      <template v-if="row.goods.all_score !== null && row.goods.all_score !== undefined && row.goods.all_score !== ''">
        <el-rate
            v-if="row.goods.all_score > 0"
            v-model="row.goods.all_score"
            :colors="['#FF0000', '#FF9900', '#67C23A']"
            disabled
            show-score
            text-color="#1e293b"
        />
        <span v-else>{{ row.goods.all_score }}</span>
      </template>
      <template v-else>
        <span>-</span>
      </template>
    </div>
    <div v-else-if="field === 'stars'" class="flex flex-col font-normal" style="min-width: 170px">
      <ProgressBar :row="row" percentage="ratings"/>
    </div>
    <div v-else-if="field === 'all_stars'" class="flex flex-col font-normal" style="min-width: 170px">
      <ProgressBar :row="row" percentage="all_rate"/>
    </div>
    <div v-else-if="field === 'status'">
      <el-tag :disable-transitions="true" :type=statusType>
        {{ statusText }}
      </el-tag>
    </div>
    <div v-else-if="field === 'operate'">
      <div class="flex justify-center gap-2 mb-2">
        <el-tooltip :enterable="false" :show-arrow="false" content="评论详情" hide-after="0"
                    placement="top" popper-class="custom-btn-tooltip">
          <PermissionButton circle plain type="success" @click="handleNavigate('/product/comment')">
            <el-icon>
              <Tickets/>
            </el-icon>
          </PermissionButton>
        </el-tooltip>
        <el-tooltip :enterable="false" :show-arrow="false" content="历史详情" hide-after="0"
                    placement="top" popper-class="custom-btn-tooltip-2">
          <PermissionButton :color="'#6466F1'" circle plain type="success" @click="handleNavigate('/product/history')">
            <el-icon>
              <Timer/>
            </el-icon>
          </PermissionButton>
        </el-tooltip>
      </div>
      <div class="flex justify-center gap-2">
        <PermissionButton circle plain type="warning" @click="handleEdit">
          <el-icon>
            <Operation/>
          </el-icon>
        </PermissionButton>
        <el-popconfirm
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="你确定要删除此项吗?"
            width="220"
            @confirm="onConfirm"
        >
          <template #reference>
            <PermissionButton circle plain type="danger">
              <el-icon>
                <Delete/>
              </el-icon>
            </PermissionButton>
          </template>
          <template #actions="{ confirm, cancel }">
            <el-button size="small" @click="cancel">No!</el-button>
            <el-button
                size="small"
                type="danger"
                @click="confirm"
            >
              Yes?
            </el-button>
          </template>
        </el-popconfirm>
      </div>
    </div>
    <div v-else>
      {{ row.goods[field] || '-' }}
    </div>
  </div>
</template>

<style scoped>
:deep(.flex-1 .el-progress__text) {
  font-size: 14px !important;
}

</style>

<style lang="scss">
.custom-btn-tooltip {
  background-color: #EFF9EB !important;
  color: #606266 !important;
  border: 1px solid #67C23A !important;
  font-size: 14px;
}
.custom-btn-tooltip-2 {
  background-color: #F0F0FE !important;
  color: #606266 !important;
  border: 1px solid #6466F1 !important;
  font-size: 14px;
}
</style>
