<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 v-if="paymentType === 'HARVEST'" label="费率(%)" prop="feeRate" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.feeRate ? scope.row.feeRate.toFixed(2) : '0.00' }}
				</template>
			</el-table-column>
			<!-- 代付：显示手续费类型、手续费比例、手续费固定金额 -->
			<template v-if="paymentType === 'PAY'">
				<el-table-column label="手续费类型" prop="feeType" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.feeType === 'PERCENTAGE'" type="primary">百分比</el-tag>
						<el-tag v-else-if="scope.row.feeType === 'FIXED'" type="success">固定</el-tag>
						<el-tag v-else-if="scope.row.feeType === 'MIXED'" type="warning">百分比+固定</el-tag>
						<el-tag v-else type="info">-</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="手续费比例(%)" prop="feeRate" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.feeRate ? scope.row.feeRate.toFixed(2) : '0.00' }}
					</template>
				</el-table-column>
				<el-table-column label="手续费固定金额" prop="feeEvery" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.feeEvery || 0 }}
					</template>
				</el-table-column>
			</template>
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<el-button icon="Setting" @click="handleConfig(scope.row)" text type="primary" v-auth="'agent_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="AgentPaymentDialog" setup>
import { fetchAgentPaymentTypeList } from '/@/api/agent';
import { useMessage } from '/@/hooks/message';

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

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

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

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

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

// 加载支付通道列表
const loadPaymentList = async () => {
	try {
		loading.value = true;
		const res = await fetchAgentPaymentTypeList(agentUserId.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, agentUserId.value, paymentType.value);
};

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

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