<template>
	<el-dialog :close-on-click-modal="false" :title="dialogTitle" draggable v-model="visible" width="1400px" @close="handleClose">
		<!-- 表格 -->
		<el-table
			:data="paymentList"
			v-loading="loading"
			border
			:cell-style="tableStyle.cellStyle"
			:header-cell-style="tableStyle.headerCellStyle"
			style="width: 100%"
			max-height="500"
		>
			<el-table-column label="通道名称" prop="channelName" show-overflow-tooltip />
			<el-table-column label="通道编码" prop="channelCode" show-overflow-tooltip />
			<el-table-column label="支付类型名称" prop="paymentName" show-overflow-tooltip />
			<el-table-column label="支付类型代码" prop="paymentCode" show-overflow-tooltip />
			<el-table-column label="启用状态" prop="status" show-overflow-tooltip>
				<template #default="scope">
					<el-tag :type="scope.row.status ? 'success' : 'info'">
						{{ scope.row.status ? '启用' : '禁用' }}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="代理商手续费类型" prop="agentFeeType" show-overflow-tooltip>
				<template #default="scope">
					<el-tag v-if="scope.row.agentFeeType === 'PERCENTAGE'" type="primary">百分比</el-tag>
					<el-tag v-else-if="scope.row.agentFeeType === 'FIXED'" type="success">固定</el-tag>
					<el-tag v-else-if="scope.row.agentFeeType === 'MIXED'" type="warning">百分比+固定</el-tag>
					<el-tag v-else type="info">未设置</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="代理商手续费比例(%)" prop="agentFeeRate" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.agentFeeRate !== undefined && scope.row.agentFeeRate !== null ? scope.row.agentFeeRate.toFixed(2) : '-' }}
				</template>
			</el-table-column>
			<el-table-column label="代理商手续费固定金额" prop="agentFeeEvery" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.agentFeeEvery !== undefined && scope.row.agentFeeEvery !== null ? scope.row.agentFeeEvery : '-' }}
				</template>
			</el-table-column>
			<!-- 代收：显示商户费率 -->
			<el-table-column v-if="paymentType === 'HARVEST'" label="商户费率(%)" prop="merchantUserRate" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.merchantUserRate ? scope.row.merchantUserRate.toFixed(2) : '0.00' }}
				</template>
			</el-table-column>
			<!-- 代付：显示商户手续费类型、手续费比例、手续费固定金额 -->
			<template v-if="paymentType === 'PAY'">
				<el-table-column label="商户手续费类型" prop="merchantUserFeeType" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.merchantUserFeeType === 'PERCENTAGE'" type="primary">百分比</el-tag>
						<el-tag v-else-if="scope.row.merchantUserFeeType === 'FIXED'" type="success">固定</el-tag>
						<el-tag v-else-if="scope.row.merchantUserFeeType === 'MIXED'" type="warning">百分比+固定</el-tag>
						<el-tag v-else type="info">未设置</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="商户手续费比例(%)" prop="merchantUserRate" show-overflow-tooltip>
					<template #default="scope">
						<template v-if="scope.row.merchantUserFeeType === 'PERCENTAGE' || scope.row.merchantUserFeeType === 'MIXED'">
							{{ scope.row.merchantUserRate !== undefined && scope.row.merchantUserRate !== null ? scope.row.merchantUserRate.toFixed(2) : '-' }}
						</template>
						<template v-else>-</template>
					</template>
				</el-table-column>
				<el-table-column label="商户手续费固定金额" prop="merchantUserFeeEvery" show-overflow-tooltip>
					<template #default="scope">
						<template v-if="scope.row.merchantUserFeeType === 'FIXED' || scope.row.merchantUserFeeType === 'MIXED'">
							{{ scope.row.merchantUserFeeEvery !== undefined && scope.row.merchantUserFeeEvery !== null ? scope.row.merchantUserFeeEvery : '-' }}
						</template>
						<template v-else>-</template>
					</template>
				</el-table-column>
			</template>
			<el-table-column label="单笔最小金额" prop="singleMinAmount" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.singleMinAmount || 0 }}
				</template>
			</el-table-column>
			<el-table-column label="单笔最大金额" prop="singleMaxAmount" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.singleMaxAmount || 0 }}
				</template>
			</el-table-column>
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<el-button icon="Setting" @click="handleConfig(scope.row)" text type="primary" v-auth="'merchant_payment_type_edit'">配置 </el-button>
				</template>
			</el-table-column>
		</el-table>

		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">关闭</el-button>
			</span>
		</template>
	</el-dialog>

	<!-- 配置弹窗 -->
	<config-dialog ref="configDialogRef" @refresh="loadPaymentList" />
</template>

<script lang="ts" name="MerchantPaymentDialog" setup>
import { fetchMerchantPaymentTypeList } from '/@/api/merchant';
import { useMessage } from '/@/hooks/message';

// 引入配置弹窗组件
const ConfigDialog = defineAsyncComponent(() => import('./merchantPaymentConfigDialog.vue'));

// 定义表格样式
const tableStyle = {
	cellStyle: { textAlign: 'center' },
	headerCellStyle: { textAlign: 'center', background: '#f5f7fa' },
};

// 定义变量
const visible = ref(false);
const loading = ref(false);
const configDialogRef = ref();
const merchantUserId = ref('');
const paymentType = ref<'PAY' | 'HARVEST'>('HARVEST');
const dialogTitle = ref('');
const paymentList = ref<any[]>([]);

// 打开弹窗
const openDialog = (userId: string, type: 'PAY' | 'HARVEST') => {
	visible.value = true;
	merchantUserId.value = userId;
	paymentType.value = type;
	dialogTitle.value = type === 'HARVEST' ? '商户代收配置' : '商户代付配置';

	// 加载数据
	loadPaymentList();
};

// 加载支付通道列表
const loadPaymentList = async () => {
	try {
		loading.value = true;
		const res = await fetchMerchantPaymentTypeList(merchantUserId.value, paymentType.value);
		paymentList.value = res.data || [];
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 配置
const handleConfig = (row: any) => {
	configDialogRef.value.openDialog(row, merchantUserId.value, paymentType.value);
};

// 关闭弹窗
const handleClose = () => {
	paymentList.value = [];
};

// 暴露方法
defineExpose({
	openDialog,
});
</script>
