<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';


const router = useRouter();

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

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

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

const statusMap: { [key: number]: { text: string, type: string } } = {
  1: { text: '正常', type: 'success' },
  2: { text: '失败', type: 'danger' },
  3: { text: '暂停', type: 'warning' },
  10: { text: '下架', type: 'info' }
};
const { text: statusText, type: statusType } = statusMap[row.status] || { text: '未知', type: 'info' };

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

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

function starsPercent(goods: any) {
  const total = goods.ratings;
  const ret = [ 0, 0, 0, 0, 0 ];
  let sum = 0;
  if (total <= 0) {
    return ret;
  }
  for (const index in ret) {
    const star = parseInt(index) + 1;
    if (star === 5) {
      ret[4] = 100 - sum;
    } else {
      const v = Math.round(goods[`ratings${ star }`] / total * 100);
      ret[index] = v;
      sum += v;
    }
  }
  return ret;
}

function showDetail(detail: any) {
  emit(`${ detail }`, row);
}

function goto() {
  const routeUrl = router.resolve({
    path: '/product/comment',
    query: {
      asin: row.asin,
      title: row.goods.title,
      img: row.goods.img,
      url: row.goods.url
    }
  });
  window.open(routeUrl.href, '_blank');
}
</script>

<template>
  <div class="font-medium">
    <div v-if="field === 'product_info'">
      <ProductInfo :img-width="60" :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">
      <div v-for="(percent, index) in starsPercent(row.goods).reverse()" :key="index" class="w-full flex items-center"
           style="max-height: 15px">
        <span class="w-10 text-right mr-2 italic">{{ 5 - index }}星</span>
        <el-tooltip :content="String(row.goods[`ratings${5 - index}`])" :show-after="300" effect="dark" placement="top">
          <el-progress
              :color="'#3A8EE6'"
              :percentage="percent"
              :stroke-width="10"
              class="flex-1"
              striped
              striped-flow
          />
        </el-tooltip>
      </div>
    </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">
        <div>
          <el-tooltip :enterable="false" :show-arrow="false" content="评论详情" hide-after="0"
                      placement="top" popper-class="custom-btn-tooltip">
            <PermissionButton circle plain type="success" @click="goto">
              <el-icon>
                <Tickets />
              </el-icon>
            </PermissionButton>
          </el-tooltip>
        </div>
        <div>
          <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="showDetail('show-history')">
              <el-icon>
                <Timer />
              </el-icon>
            </PermissionButton>
          </el-tooltip>
        </div>
      </div>
      <div class="flex justify-center gap-2">
        <div>
          <PermissionButton circle plain type="warning" @click="handleEdit">
            <el-icon>
              <Operation />
            </el-icon>
          </PermissionButton>
        </div>
        <div>
          <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>
    <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>
