<script lang="ts" setup>
/**
 * @Name: DataTableSlot.vue
 * @Description: 审批查看（直接销售）数据表格插槽
 * @Author: xinyan
 */

import { hasPermission } from '/@/utils/hasPermission';
import { Delete, InfoFilled, Operation } from '@element-plus/icons-vue';
import PermissionButton from '/@/components/PermissionButton/index.vue';
import { useCountryInfoStore } from '/@/stores/countryInfo';

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

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

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

const currency = countryInfoStore.CurrencyCodes.find((c) => c.code == row.currency_code);
const currencyColor = currency ? currency.color : '#626AEF';

// 日常活动销售利润 = 日常活动售价（人民币）-出口报关价-头程运费-尾程费用-转发费-广告费-退货成本-VAT-仓储费-佣金
const totalCosts =
	parseFloat(row.export_tax) +
	parseFloat(row.first_cost) +
	parseFloat(row.final_cost) +
	parseFloat(row.forwarding_fee) +
	parseFloat(row.ad_budget) +
	parseFloat(row.return_or_refurbishment) +
	parseFloat(row.storage_charges) +
	parseFloat(row.brokerage);
const routine_activity_profit = computed(() => row.price_daily_rmb - totalCosts);

// 日常活动毛利率 = 日常活动销售利润/日常活动售价（人民币）
const gross_margin_daily = computed(() => routine_activity_profit.value / row.price_daily_rmb);
// console.log("=>(DataTableSlot.vue:44) gross_margin_daily", gross_margin_daily.value);

// 平均毛利 = 0.8*日常活动毛利率 + 0.2*毛利率
const average_gross_profit = computed(() => 0.8 * gross_margin_daily.value + 0.2 * row.gross_profit_margin);


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

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

<template>
	<div class="font-semibold">
		<div v-if="field === 'operate'">
			<div class="flex justify-center gap-2">
				<div v-if="hasPermission('PRICE_DIRECT_UPDATE')">
					<PermissionButton circle plain type="warning" @click="handleEdit">
						<el-icon>
							<Operation />
						</el-icon>
					</PermissionButton>
				</div>
				<div v-if="hasPermission('PRICE_DIRECT_DEL')">
					<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-if="field === 'average_gross_profit'">
			{{ average_gross_profit && average_gross_profit !== -Infinity ? average_gross_profit.toFixed(2) : '-' }}
		</div>
		<div v-else-if="field === 'routine_activity_profit'">
			{{ routine_activity_profit && routine_activity_profit !== -Infinity ? routine_activity_profit .toFixed(2): '-' }}
		</div>
		<div v-else-if="field === 'gross_margin_daily'">
			{{ gross_margin_daily && gross_margin_daily !== -Infinity ? gross_margin_daily.toFixed(2) : '-' }}
		</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 === 'currency_code'">
			<el-tag :disable-transitions="true" :style="{ color: currencyColor, borderColor: currencyColor }" effect="plain" round>
				{{ currency ? currency.code : '-' }}
			</el-tag>
		</div>
		<div v-else>
			{{ row[field] || '-' }}
		</div>
	</div>
</template>

<style scoped></style>
