<template>
	<div class="snapshot-statistics-order-mark-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-radio-group v-model="state.queryParams.Status" @change="handleQuery">
								<el-radio :value="1">待标注</el-radio>
								<el-radio :value="2">已标注</el-radio>
								<el-radio :value="0">全部</el-radio>
							</el-radio-group>
						</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="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
						</el-form-item>
					</el-form>
				</template>
				<template #toolbar_buttons>
					<el-button type="primary" @click="onAdd" v-auth="'snapshot:handle:orderMark:add'" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="ele-Plus" class="mr5" />添加标注<span v-if="checkTable.length">({{ checkTable.length }})</span>
					</el-button>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="{ id: row.orderId }" @updateList="handleQuery">{{ row.title }}</order-detail>
				</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>
		<!-- 添加标注 -->
		<order-mark-add ref="orderMarkAddRef" @updateList="handleQuery" />
	</div>
</template>

<script lang="tsx" setup name="snapshotHandleOrderMark">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { getOrderMarkList } from '@/api/snapshot/handle';
import Other from '@/utils/other';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const OrderMarkAdd = defineAsyncComponent(() => import('@/views/snapshot/handle/components/Order-mark-add.vue')); // 添加标注

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Status: 1, // 默认待标注
		No: null, // 工单编码
		Title: 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: 'snapshotReSendList',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			field: 'no',
			title: '工单编码',
			width: 140,
		},
		{
			field: 'content',
			title: '受理内容',
			width: 300,
		},
		{
			field: 'title',
			title: '工单标题',
			slots: { default: 'order_detail' },
			minWidth: 300,
		},
		{
			field: 'creationTime',
			title: '受理时间',
			formatter: 'formatDate',
			width: 160,
		},
		{
			field: 'county',
			title: '区域',
			width: 150,
		},
		{
			field: 'isSafetyDepartment',
			title: '标注状态',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isSafetyDepartment === null ? '' : row.isSafetyDepartment ? '是' : '否';
				},
			},
		},
		{
			field: 'networkRemark',
			title: '回复内容',
			minWidth: 200,
		},
		{
			field: 'replyDate',
			title: '回复时间',
			formatter: 'formatDate',
			width: 160,
		},
		{
			field: 'networkENumber',
			title: '编号',
			width: 180,
		},
		{
			field: 'labelUserName',
			title: '标注人员',
			width: 120,
		},
		{
			field: 'labelTime',
			title: '标注时间',
			formatter: 'formatDate',
			width: 160,
		},
	],
	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);
	getOrderMarkList(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
			gridOptions.loading = false;
		})
		.finally(() => {
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const ruleFormRef = ref<any>(null); // 表单ref
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 orderMarkAddRef = ref<RefType>();
const onAdd = () => {
	const ids = checkTable.value.map((item: any) => item.orderId);
	orderMarkAddRef.value.openDialog(ids);
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
