<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('order.payOrder.mchOrderNo')" prop="mchOrderNo">
						<el-input
							:placeholder="$t('order.payOrder.mchOrderNoPlaceholder')"
							style="max-width: 180px"
							v-model="state.queryForm.mchOrderNo"
							clearable
						/>
					</el-form-item>
					<el-form-item :label="$t('order.payOrder.transactionId')" prop="transactionId">
						<el-input
							:placeholder="$t('order.payOrder.transactionIdPlaceholder')"
							style="max-width: 180px"
							v-model="state.queryForm.transactionId"
							clearable
						/>
					</el-form-item>
					<el-form-item :label="$t('order.payOrder.merchantName')" prop="merchantName">
						<el-input
							:placeholder="$t('order.payOrder.merchantNamePlaceholder')"
							style="max-width: 180px"
							v-model="state.queryForm.merchantName"
							clearable
						/>
					</el-form-item>
					<el-form-item :label="$t('order.payOrder.orderStatus')" prop="orderStatus">
						<el-select
							v-model="state.queryForm.orderStatus"
							:placeholder="$t('order.payOrder.orderStatusPlaceholder')"
							style="max-width: 180px"
							clearable
						>
							<el-option :label="$t('order.payOrder.createOrder')" value="CREATE_ORDER" />
							<el-option :label="$t('order.withdrawOrder.withdrawSuccess')" value="WITHDRAW_SUCCESS" />
							<el-option :label="$t('order.withdrawOrder.withdrawFail')" value="WITHDRAW_FAIL" />
						</el-select>
					</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%">
					<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('order.payOrder.mchOrderNo')" prop="mchOrderNo" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.mchOrderNo || '--' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.transactionId')" prop="transactionId" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.transactionId || '--' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.merchantName')" prop="merchantName" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.merchantName || '--' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.channelName')" prop="paymentChannelName" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.paymentChannelName || '--' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.submitAmount')" prop="amount" show-overflow-tooltip>
					<template #default="scope">
						<!-- 橙色 -->
						<span style="color: #f56c6c; font-weight: bold">¥{{ scope.row.amount ? scope.row.amount.toFixed(2) : '0.00' }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.actualAmount')" prop="noticeAmount" show-overflow-tooltip>
					<template #default="scope">
						<span style="color: #f56c6c; font-weight: bold">¥{{ scope.row.noticeAmount ? scope.row.noticeAmount.toFixed(2) : '0.00' }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.orderStatus')" prop="orderStatus" show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="orderStatusMap[scope.row.orderStatus]?.type || 'info'" size="small">
							{{ orderStatusMap[scope.row.orderStatus]?.label || scope.row.orderStatus }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.createTime')" prop="createTime" show-overflow-tooltip width="180">
					<template #default="scope">
						{{ scope.row.createTime || '--' }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('order.payOrder.operation')">
					<template #default="scope">
						<!-- <el-button icon="View" @click="handleView(scope.row)" text type="primary" size="small">查看</el-button> -->
						<el-button icon="Document" @click="handleNotifyLog(scope.row)" text type="warning" size="small">{{
							$t('order.payOrder.notifyLog')
						}}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 回调日志弹窗 -->
		<notify-log-dialog ref="notifyLogDialogRef" />
	</div>
</template>

<script lang="ts" name="orderWithdrawOrder" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchWithdrawOrderList } from '/@/api/order';

// 引入组件
const NotifyLogDialog = defineAsyncComponent(() => import('../payOrder/notifyLog.vue'));

import { useI18n } from 'vue-i18n';

const { t } = useI18n();

// 订单状态映射
const orderStatusMap = computed(() => ({
	CREATE_ORDER: { label: t('order.payOrder.createOrder'), type: 'info' },
	WITHDRAW_SUCCESS: { label: t('order.withdrawOrder.withdrawSuccess'), type: 'success' },
	WITHDRAW_FAIL: { label: t('order.withdrawOrder.withdrawFail'), type: 'danger' },
}));

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

// 定义表格查询、变更
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		mchOrderNo: '',
		transactionId: '',
		merchantName: '',
		agentName: '',
		orderStatus: '',
	},
	pageList: fetchWithdrawOrderList,
	descs: ['create_time'],
	createdIsNeed: true, // 使用自动加载
});

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 handleNotifyLog = (row: any) => {
	notifyLogDialogRef.value.openDialog(row.appId, row.mchOrderNo);
};
</script>
