<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row class="ml10" v-show="showSearch">
				<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
					<el-form-item label="通道名称/编码" prop="query">
						<el-input placeholder="请输入通道名称或编码" style="max-width: 180px" v-model="state.queryForm.query" />
					</el-form-item>
					<el-form-item label="启用状态" prop="status">
						<el-select v-model="state.queryForm.status" placeholder="请选择状态" clearable style="width: 120px">
							<el-option label="启用" :value="true" />
							<el-option label="禁用" :value="false" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="search" type="primary"> 查询 </el-button>
						<el-button @click="resetQuery" icon="Refresh">重置</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button @click="formDialogRef.openDialog()" class="ml10" icon="folder-add" type="primary" v-auth="'payment_channel_save'">
						新增
					</el-button>
					<right-toolbar
						@queryTable="getDataList"
						class="ml10"
						style="float: right; margin-right: 20px"
						v-model:showSearch="showSearch"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				@selection-change="handleSelectionChange"
				@sort-change="sortChangeHandle"
				style="width: 100%"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column align="center" type="selection" width="40" />
				<el-table-column label="通道ID" prop="id" show-overflow-tooltip />
				<el-table-column label="通道名称" prop="channelName" show-overflow-tooltip />
				<el-table-column label="通道编码" prop="channelCode" show-overflow-tooltip />
				<el-table-column label="基础费率(%)" prop="baseRechargeRate" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.baseRechargeRate ? scope.row.baseRechargeRate.toFixed(2) : '0.00' }}
					</template>
				</el-table-column>
				<el-table-column label="启用状态" prop="status" show-overflow-tooltip>
					<template #default="scope">
						<el-switch
							v-model="scope.row.status"
							@change="handleStatusChange(scope.row)"
							:active-value="true"
							:inactive-value="false"
							v-auth="'payment_channel_edit'"
						/>
					</template>
				</el-table-column>
				<el-table-column label="风控状态" prop="riskControlStatus" show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="scope.row.riskControlStatus ? 'success' : 'info'">
							{{ scope.row.riskControlStatus ? '启用' : '禁用' }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="权重值" prop="roundVal" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.roundVal || 0 }}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="400">
					<template #default="scope">
						<el-button icon="View" @click="handleView(scope.row)" text type="primary" v-auth="'payment_channel_info'">查看</el-button>
						<el-button icon="edit-pen" @click="formDialogRef.openDialog(scope.row.id)" text type="primary" v-auth="'payment_channel_edit'"
							>编辑
						</el-button>
						<el-button icon="Setting" @click="handlePayTypeConfig(scope.row, 'HARVEST')" text type="success" v-auth="'payment_type_query'"
							>代收支付配置
						</el-button>
						<el-button icon="Setting" @click="handlePayTypeConfig(scope.row, 'PAY')" text type="warning" v-auth="'payment_type_query'"
							>代付支付配置
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增 -->
		<form-dialog @refresh="getDataList(false)" ref="formDialogRef" />

		<!-- 支付类型配置 -->
		<pay-type-dialog ref="payTypeDialogRef" />
	</div>
</template>

<script lang="ts" name="systemPaymentChannel" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchChannelList, changeChannelStatus } from '/@/api/payment';
import { useMessage } from '/@/hooks/message';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const PayTypeDialog = defineAsyncComponent(() => import('./payTypeDialog.vue'));

// 定义变量内容
const formDialogRef = ref();
const payTypeDialogRef = ref();
const queryRef = ref();
const showSearch = ref(true);
const selectObjs = ref([]);
const multiple = ref(true);

// 定义表格查询、变更
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		query: '',
		status: undefined,
	},
	pageList: fetchChannelList,
});

const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, tableStyle } = useTable(state);

// 重置查询
const resetQuery = () => {
	queryRef.value?.resetFields();
	getDataList();
};

// 多选事件
const handleSelectionChange = (objs: any) => {
	selectObjs.value = objs.map((val: any) => val.id);
	multiple.value = !objs.length;
};

// 状态切换
const handleStatusChange = async (row: any) => {
	try {
		await changeChannelStatus(row.id);
		useMessage().success('状态修改成功');
		getDataList(false);
	} catch (err: any) {
		useMessage().error(err.msg);
		// 恢复原状态
		row.status = !row.status;
	}
};

// 查看详情
const handleView = (row: any) => {
	formDialogRef.value.openDialog(row.id, true);
};

// 打开支付类型配置
const handlePayTypeConfig = (row: any, type: 'HARVEST' | 'PAY') => {
	payTypeDialogRef.value.openDialog(row.id, type);
};
</script>
