<template>
	<div class="snapshot-re-audit-citizen-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef" @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="gridOptions.loading">
						<el-form-item label="审批状态" prop="Status">
							<el-select v-model="state.queryParams.Status" class="w100" placeholder="请选择审批状态" @change="handleQuery">
								<el-option v-for="item in statusOptions" :key="item.key" :label="item.value" :value="item.key" />
							</el-select>
						</el-form-item>
						<el-form-item label="工单编码" prop="No">
							<el-input v-model="state.queryParams.No" placeholder="请填写工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="工单标题" prop="Title">
							<el-input v-model="state.queryParams.Title" placeholder="请填写工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
							<el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
						</el-form-item>
					</el-form>
				</template>
				<template #toolbar_buttons>
					<!--
						v-if="state.queryParams.Status === 1"-->
					<!--					<el-button type="primary" @click="onRemark" :disabled="isChecked" v-auth="'snapshot:reAudit:citizen:remark'">
						<SvgIcon name="ele-Plus" class="mr5" />添加备注
					</el-button>
					<el-button type="primary" @click="onSupply" v-auth="'snapshot:reAudit:citizen:supply'" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="ele-Plus" class="mr5" />补充发放
					</el-button>-->
				</template>
				<template #statusTxt="{ row }">
					<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.status)">{{ row.statusTxt }}</el-text>
					<span v-else>{{ row.statusTxt }}</span>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="{ id: row.orderId }" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<template #action="{ row }">
					<template v-if="state.queryParams.Status === 0">
						<el-button link type="primary" @click="onAudit(row)" v-auth="'snapshot:reAudit:citizen:audit'"> 审批 </el-button>
						<el-button link type="danger" @click="onReturn(row)" v-auth="'snapshot:reAudit:citizen:return'"> 退回 </el-button>
					</template>
					<template v-if="state.queryParams.Status === 1">
						<el-button link type="primary" @click="onRemark(row)" v-auth="'snapshot:reAudit:citizen:remark'"> 添加备注 </el-button>
						<el-button link type="primary" @click="onSupply(row)" v-auth="'snapshot:reAudit:citizen:supply'"> 补充发放 </el-button>
					</template>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="110px">
				<el-form-item label="受理时间" prop="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="审批时间" prop="spTime">
					<el-date-picker
						v-model="state.queryParams.spTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="办结时间" prop="bjTime">
					<el-date-picker
						v-model="state.queryParams.bjTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="来电人电话" prop="FromPhone">
					<el-input v-model="state.queryParams.FromPhone" placeholder="请填写来电人电话" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="网格员是否办理" prop="IsDeal">
					<el-select v-model="state.queryParams.IsDeal" class="w100" placeholder="请选择网格员是否办理" @change="handleQuery" clearable>
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="部门是否属实" prop="IsTruthDepartment">
					<el-select v-model="state.queryParams.IsTruthDepartment" class="w100" placeholder="请选择部门是否属实" @change="handleQuery" clearable>
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="网格员是否属实" prop="IsTruth">
					<el-select v-model="state.queryParams.IsTruth" class="w100" placeholder="请选择网格员是否属实" @change="handleQuery" clearable>
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否发放红包" prop="IsIssued">
					<el-select v-model="state.queryParams.IsIssued" class="w100" placeholder="请选择是否发放红包" @change="handleQuery" clearable>
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="行业类型" prop="IndustryId">
					<el-select v-model="state.queryParams.IndustryId" class="w100" placeholder="请选择行业类型" @change="handleQuery" clearable>
						<el-option v-for="item in industryOptions" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="配置金额" prop="ConfigAmount">
					<el-select v-model="state.queryParams.ConfigAmount" class="w100" placeholder="请选择配置金额" @change="handleQuery">
						<el-option v-for="item in configAmountOptions" :key="item" :label="item" :value="item" />
					</el-select>
				</el-form-item>
				<el-form-item label="实发金额" prop="AcutalAmount">
					<el-input v-model="state.queryParams.AcutalAmount" placeholder="请填写实发金额" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="审批（应发）金额" prop="ApprovedAmount" label-width="130px">
					<el-input v-model="state.queryParams.ApprovedAmount" placeholder="请填写审批（应发）金额" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="是否安全生产" prop="IsIssued">
					<el-select v-model="state.queryParams.IsDanger" class="w100" placeholder="请选择是否安全生产" @change="handleQuery" clearable>
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
		<!--	红包审批	-->
		<re-audit ref="reAuditRef" @updateList="queryList" />
		<!--	红包退回	-->
		<re-return ref="reReturnRef" @updateList="queryList" />
		<!--	红包补充	-->
		<re-supply ref="reSupplyRef" @updateList="queryList" />
		<!--	红包备注	-->
		<re-remark ref="reRemarkRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotReAuditCitizen">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { getCitizenRedEnvelopeApprovalBaseData, getCitizenRedEnvelopeApprovalList } from '@/api/snapshot/reAudit';
import Other from '@/utils/other';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const ReAudit = defineAsyncComponent(() => import('@/views/snapshot/reAudit/components/Re-audit.vue')); // 红包审批
const ReReturn = defineAsyncComponent(() => import('@/views/snapshot/reAudit/components/Re-return.vue')); // 红包退回
const ReRemark = defineAsyncComponent(() => import('@/views/snapshot/reAudit/components/Re-remark.vue')); // 红包备注
const ReSupply = defineAsyncComponent(() => import('@/views/snapshot/reAudit/components/Re-supply.vue')); // 红包补充

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Status: 0, // 审批状态 默认审批
		No: null, // 工单编码
		Title: null, // 工单标题
		slTime: [], // 受理时间
		BeginCreationTime: null,
		EndCreationTime: null,
		spTime: [], // 审批时间
		BeginAuditTime: null,
		EndAuditTime: null,
		bjTime: [], // 办结时间
		BeginFiledTime: null,
		EndFiledTime: null,
		FromPhone: null, // 来电人电话
		IsDeal: null, // 网格员是否办理
		IsTruth: null, // 网格员是否属实
		IsTruthDepartment: null, // 部门是否属实
		IsIssued: null, // 是否发放红包
		IndustryId: null, // 行业
		CaseName: null, // 线索名称
		IndustryName: null, // 行业类型
		AcutalAmount: null, // 实发金额
		ApprovedAmount: null, // 审批金额
		IsDanger: null, // 是否安全生成
		ConfigAmount: null, // 配置金额
	},
	total: 0, // 总条数
});

const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotReSendCitizen',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			field: 'statusTxt',
			title: '工单状态',
			width: 110,
			slots: {
				default: 'statusTxt',
			},
		},
		{
			field: 'sourceChannel',
			title: '来源渠道',
			width: 110,
		},
		{
			field: 'no',
			title: '工单编码',
			width: 140,
		},
		{
			field: 'title',
			title: '工单标题',
			slots: { default: 'order_detail' },
			minWidth: 200,
		},
		{
			field: 'industryName',
			title: '行业',
			width: 120,
		},
		{
			field: 'industryCase',
			title: '线索分类',
			width: 120,
		},
		{
			field: 'isDanger',
			title: '标记为安全生产',
			width: 120,
			slots: {
				default: ({ row }) => {
					return row.isDanger === null ? '' : row.isDanger ? '是' : '否';
				},
			},
		},
		{
			field: 'fromName',
			title: '来电人姓名',
			width: 120,
		},
		{
			field: 'fromPhone',
			title: '来电人电话',
			width: 140,
		},
		{
			field: 'auditTime',
			title: '审批时间',
			formatter: 'formatDate',
			width: 160,
		},
		{
			field: 'approvedAmount',
			title: '审批金额',
			width: 100,
		},
		{
			field: 'acutalAmount',
			title: '实发金额',
			width: 100,
		},
		{
			field: 'replenishAmount',
			title: '补充发放金额',
			width: 120,
		},
		{
			field: 'isIssued',
			title: '市民奖励发放结果',
			width: 140,
			slots: {
				default: ({ row }) => {
					return row.isIssued === null ? '' : row.isIssued ? '已发放' : '未发放';
				},
			},
		},
		{
			field: 'citizenAward',
			title: '市民奖励',
			width: 100,
		},
		{
			field: 'recordRemark',
			title: '市民奖励发放备注',
			width: 140,
		},
		{
			field: 'county',
			title: '区域',
			width: 120,
		},
		{
			field: 'isRectify',
			title: '部门是否整改完成',
			width: 140,
			slots: {
				default: ({ row }) => {
					return row.isRectify === null ? '' : row.isRectify ? '是' : '否';
				},
			},
		},
		{
			field: 'isTruthDepartment',
			title: '部门是否属实',
			width: 120,
			slots: {
				default: ({ row }) => {
					return row.isTruthDepartment === null ? '' : row.isTruthDepartment ? '是' : '否';
				},
			},
		},
		{
			field: 'isRepetition',
			title: '是否重复',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isRepetition === null ? '' : row.isRepetition ? '是' : '否';
				},
			},
		},
		{
			field: 'isDeal',
			title: '网格员是否办理',
			width: 120,
			slots: {
				default: ({ row }) => {
					return row.isDeal === null ? '' : row.isDeal ? '是' : '否';
				},
			},
		},
		{
			field: 'networkENumber',
			title: '网格E通编号',
			width: 120,
		},
		{
			field: 'creationTime',
			title: '受理时间',
			formatter: 'formatDate',
			width: 160,
		},
		{
			field: 'orgLevelOneName',
			title: '一级部门',
			width: 140,
		},
		{
			field: 'auditStatusTxt',
			title: '审批状态',
			width: 100,
		},
		{
			field: 'auditName',
			title: '审批人',
			width: 120,
		},
		{
			field: 'auditOrgName',
			title: '审批部门',
			width: 140,
		},
		{
			field: 'auditRemark',
			title: '审批意见',
			width: 150,
		},
		{
			field: 'bankCardNo',
			title: '卡号',
			width: 150,
		},
		{
			field: 'openBank',
			title: '开户行',
			width: 150,
		},
		{
			title: '操作',
			width: 160,
			fixed: 'right',
			showOverflow: false,
			align: 'center',
			slots: { default: 'action' },
		},
	],
	data: [],
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.BeginCreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[0]; // 受理时间
	requestParams.value.EndCreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); // 删除无用的参数
	requestParams.value.BeginAuditTime = state.queryParams.spTime === null ? null : state.queryParams.spTime[0]; // 审批时间
	requestParams.value.EndAuditTime = state.queryParams.spTime === null ? null : state.queryParams.spTime[1];
	Reflect.deleteProperty(requestParams.value, 'spTime'); // 删除无用的参数
	requestParams.value.BeginFiledTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[0]; // 办结时间
	requestParams.value.EndFiledTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[1];
	Reflect.deleteProperty(requestParams.value, 'bjTime'); // 删除无用的参数
	getCitizenRedEnvelopeApprovalList(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const drawerRuleFormRef = ref<RefType>();
const ruleFormRef = ref<any>(null); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};

const checkTable = ref<EmptyArrayType>([]);
const gridRef = ref<RefType>();
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});

// 审批
const reAuditRef = ref<RefType>();
const onAudit = (row: any) => {
	reAuditRef.value.openDialog(row);
};
// 退回
const reReturnRef = ref<RefType>();
const onReturn = (row: any) => {
	reReturnRef.value.openDialog(row);
};
// 添加备注
const reRemarkRef = ref<RefType>();
const onRemark = (row: any) => {
	if (checkTable.value.length > 1) {
		ElMessage.warning('只能选择1个添加备注。');
		return;
	}
	reRemarkRef.value.openDialog(row);
};
// 补充
const reSupplyRef = ref<RefType>();
const onSupply = (row: any) => {
	if (checkTable.value.length > 1) {
		ElMessage.warning('只能添加1个补充发放。');
		return;
	}
	reSupplyRef.value.openDialog(row);
};
// 获取基础信息
const statusOptions = ref<EmptyArrayType>([]); // 状态
const industryOptions = ref<EmptyArrayType>([]); // 行业
const configAmountOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await getCitizenRedEnvelopeApprovalBaseData();
		statusOptions.value = result.status ?? [];
		statusOptions.value.push({
			key: -1,
			value: '全部',
		});
		industryOptions.value = result.industry ?? [];
		configAmountOptions.value = result.configAmount ?? [];
	} catch (e) {
		console.log(e);
	}
};
// 页面加载时
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
