<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="$t('merchantUser.merchantName')" prop="query">
						<el-input :placeholder="$t('merchantUser.searchMerchantAccount')" style="max-width: 180px" v-model="state.queryForm.query" />
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="search" type="primary">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button @click="resetQuery" icon="Refresh">{{ $t('common.resetBtn') }} </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="'merchant_admin'">
						{{ $t('common.addBtn') }}
					</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="$t('merchantUser.merchantId')" prop="userId" show-overflow-tooltip />
				<el-table-column :label="$t('merchantUser.merchantName')" prop="merchantName" show-overflow-tooltip />
				<el-table-column :label="$t('merchantUser.agentName')" prop="agentName" show-overflow-tooltip />
				<el-table-column label="可用余额" prop="availableAmount" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.availableAmount ? `¥${scope.row.availableAmount.toFixed(2)}` : '¥0.00' }}
					</template>
				</el-table-column>
				<el-table-column label="冻结金额" prop="freezeAmount" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.freezeAmount ? `¥${scope.row.freezeAmount.toFixed(2)}` : '¥0.00' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('merchantUser.withdrawnAmount')" prop="withdrawnAmount" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.withdrawnAmount ? `¥${scope.row.withdrawnAmount.toFixed(2)}` : '¥0.00' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('merchantUser.status')" 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="'merchant_admin'"
						/>
					</template>
				</el-table-column>
				<el-table-column :label="$t('merchantUser.operation')" width="480">
					<template #default="scope">
						<el-button icon="View" @click="handleView(scope.row)" text type="primary" v-auth="'merchant_admin'">{{
							$t('merchantUser.viewBtn')
						}}</el-button>
						<el-button icon="edit-pen" @click="formDialogRef.openDialog(scope.row.userId)" text type="primary" v-auth="'merchant_admin'"
							>{{ $t('common.editBtn') }}
						</el-button>
						<el-button icon="Key" @click="handleChangePwd(scope.row)" text type="warning" v-auth="'merchant_admin'">{{
							$t('merchantUser.changePwdBtn')
						}}</el-button>
						<el-button icon="RefreshRight" @click="handleResetKey(scope.row)" text type="danger" v-auth="'merchant_admin'">{{
							$t('merchantUser.resetKeyBtn')
						}}</el-button>
						<el-button icon="Setting" @click="handleMerchantPayment(scope.row, 'HARVEST')" text type="success" v-auth="'merchant_payment_type_query'"
							>代收
						</el-button>
						<el-button icon="Setting" @click="handleMerchantPayment(scope.row, 'PAY')" text type="warning" v-auth="'merchant_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" />

		<!-- 修改密码 -->
		<change-pwd-dialog @refresh="getDataList(false)" ref="changePwdDialogRef" />

		<!-- 密钥显示 -->
		<key-dialog ref="keyDialogRef" />

		<!-- 商户支付通道配置 -->
		<merchant-payment-dialog ref="merchantPaymentDialogRef" />
	</div>
</template>

<script lang="ts" name="systemMerchantUser" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, changeStatus, resetMd5Key } from '/@/api/merchant';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const ChangePwdDialog = defineAsyncComponent(() => import('./changePwd.vue'));
const KeyDialog = defineAsyncComponent(() => import('./keyDialog.vue'));
const MerchantPaymentDialog = defineAsyncComponent(() => import('./merchantPaymentDialog.vue'));

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

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

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.userId);
	multiple.value = !objs.length;
};

// 状态切换
const handleStatusChange = async (row: any) => {
	try {
		await changeStatus(row.userId);
		useMessage().success(t('merchantUser.statusChangeSuccess'));
		getDataList(false);
	} catch (err: any) {
		useMessage().error(err.msg);
		// 恢复原状态
		row.status = !row.status;
	}
};

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

// 修改密码
const handleChangePwd = (row: any) => {
	changePwdDialogRef.value.openDialog(row.userId);
};

// 重置密钥
const handleResetKey = async (row: any) => {
	try {
		await useMessageBox().confirm(t('merchantUser.resetKeyConfirm'));
		const res = await resetMd5Key(row.userId);
		// 显示密钥信息
		keyDialogRef.value.openDialog(res.data);
	} catch (err: any) {
		if (err !== 'cancel') {
			useMessage().error(err.msg);
		}
	}
};

// 打开商户支付通道配置
const handleMerchantPayment = (row: any, type: 'PAY' | 'HARVEST') => {
	merchantPaymentDialogRef.value.openDialog(row.userId, type);
};
</script>
