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

import { useCountryInfoStore } from '/@/stores/countryInfo';
import { CopyDocument, Delete, InfoFilled, Message, Operation } 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 { handleCopy } from '/@/utils/useCopyText';
import { hasPermission } from '/@/utils/hasPermission';
// import { BtnPermissionStore } from '/@/stores/btnPermission';
//
// const x = BtnPermissionStore();
// console.log("(DataTableSlot.vue: 17)=> x", x.data);

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

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

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: 'info' },
	10: { text: '下架', type: 'danger' }
};
const { text: statusText, type: statusType } = statusMap[row.status] || { text: '未知', type: 'info' };


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

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

function handleNotice() {
  emit('handle-notice', row);
}

function handleMonitor() {
  emit('handle-monitor', row);
}
</script>

<template>
  <div class="font-medium">
    <div v-if="field === 'is_monitor'">
      <el-switch v-model=row.is_monitor @change="handleMonitor" />
    </div>
    <div v-else-if="field === 'product_info'">
      <ProductInfo :displayCompetitor="true" :img-width="60" :item="row" />
    </div>
    <div v-else-if="field === 'sku'" style="color: #1d2129;">
      <div v-if="row.sku || row.sku2 || row.sku3" class="flex flex-col items-center justify-center">
        <div v-if="row.sku" class="flex max-h-5">
          <div>
            {{ row.sku }}
          </div>
          <el-button :icon="CopyDocument" class="ml-1 cursor-pointer" link
                     @click="handleCopy(row.sku || '')"></el-button>
        </div>
        <div v-if="row.sku2" class="flex max-h-5">
          <div>
            {{ row.sku2 }}
          </div>
          <el-button :icon="CopyDocument" class="ml-1 cursor-pointer" link
                     @click="handleCopy(row.sku2 || '')"></el-button>
        </div>
        <div v-if="row.sku3" class="flex max-h-5">
          <div>
            {{ row.sku3 }}
          </div>
          <el-button :icon="CopyDocument" class="ml-1 cursor-pointer" link
                     @click="handleCopy(row.sku3 || '')"></el-button>
        </div>
      </div>
      <div v-else>
        -
      </div>
    </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.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.tag" :disable-transitions="true" :type=getTagType(row.tag)>
        {{ row.tag }}
      </el-tag>
      <div v-else>-</div>
    </div>
    <div v-else-if="field === 'brand'">
      <el-tag v-if="row.brand" :disable-transitions="true" :type=getTagType(row.brand) effect="plain" round>
        {{ row.brand }}
      </el-tag>
      <div v-else>--</div>
    </div>
    <div v-else-if="field === 'price_info'">
      <div v-if="row.price >= 0" class="font-medium">
        <p>现 价：{{ row.currency_code + '‎' + row.price }}</p>
        <p>折 扣：{{ row.discount > 0 ? row.discount + '%' : '-' }}</p>
        <p>优惠劵：{{ !row || row.coupon <= 0 ? '-' : row.currency_code + '‎' + row.coupon }}</p>
      </div>
    </div>
    <div v-else-if="field === 'show_price'">
      <div class="font-medium">
        <p>展示价格：{{ row.show_price ? row.currency_code + row.show_price : '-' }}</p>
        <p>平时售价：{{ row.activity_price ? row.currency_code + row.activity_price : '-' }}</p>
        <p>最低售价：{{ row.minimum_price ? row.currency_code + row.minimum_price : '-' }}</p>
      </div>
    </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">
        <div>
          <PermissionButton :disabled="row.is_competitors" circle plain type="warning" @click="handleEdit">
            <el-icon>
              <Operation />
            </el-icon>
          </PermissionButton>
        </div>
				<div v-if="hasPermission('GoodsDelete')">
					<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>
          <el-tooltip :enterable="false" :show-arrow="false" content="变更通知" hide-after="0"
                      placement="top" popper-class="custom-btn-tooltip-info">
            <PermissionButton circle plain type="info" @click="handleNotice">
              <el-icon>
                <Message />
              </el-icon>
            </PermissionButton>
          </el-tooltip>
        </div>
      </div>
    </div>
    <div v-else>
      {{ row[field] || '-' }}
    </div>
  </div>
</template>

<style scoped>

</style>

<style lang="scss">
.custom-btn-tooltip-info {
  background-color: #F4F4F5 !important;
  color: #606266 !important;
  border: 1px solid #C8C9CC !important;
  font-size: 14px;
}
</style>
