<template>
	<div class="auxiliary-industry-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="行业名称" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" 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>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
				<template #buttons>
					<el-button type="primary" @click="onAdd" v-auth="'auxiliary:industry:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增行业 </el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true, useKey: true }"
					:row-config="{ isCurrent: true, isHover: true, useKey: true, height: 30 }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="auxiliaryIndustry"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: exportAuxIndustry, exportParams: requestParams }"
				>
					<vxe-column field="industrialName" title="行业名称" min-width="150"></vxe-column>
					<vxe-column field="orgCount" title="关联部门" min-width="150">
						<template #default="{ row }">
							<el-button type="primary" link @click="showOrg(row)">{{ row.orgCount }}</el-button>
						</template>
					</vxe-column>
					<vxe-column field="updateTime" title="更新时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.updateTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="120" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onUpdate(row)" v-auth="'auxiliary:industry:edit'" title="编辑行业"> 编辑 </el-button>
							<el-button link type="danger" @click="onDelete(row)" v-auth="'auxiliary:industry:delete'" title="删除行业"> 删除 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!--  新增行业  -->
		<industry-add ref="industryAddRef" @updateList="queryList" />
		<!--  编辑行业  -->
		<industry-edit ref="industryEditRef" @updateList="queryList" />
		<!--  关联部门  -->
		<connect-org ref="connectOrgRef" />
	</div>
</template>

<script lang="tsx" setup name="auxiliaryIndustry">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { deleteAuxIndustry, exportAuxIndustry, getAuxIndustryList } from '@/api/auxiliary/industry';
import Other from '@/utils/other';
// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const IndustryAdd = defineAsyncComponent(() => import('@/views/auxiliary/industry/components/Industry-add.vue')); // 新增行业
const IndustryEdit = defineAsyncComponent(() => import('@/views/auxiliary/industry/components/Industry-edit.vue')); // 编辑行业
const ConnectOrg = defineAsyncComponent(() => import('@/views/auxiliary/industry/components/Connect-org.vue')); // 关联部门

// 定义变量内容
const state = reactive({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 行业名称
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	getAuxIndustryList(requestParams.value)
		.then((res) => {
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增行业
const industryAddRef = ref<RefType>();
const onAdd = () => {
	industryAddRef.value.openDialog();
};
// 修改行业
const industryEditRef = ref<RefType>();
const onUpdate = (row: any) => {
	industryEditRef.value.openDialog(row);
};
// 删除行业
const onDelete = (row: any) => {
	ElMessageBox.confirm(`是否确认删除该行业？`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			deleteAuxIndustry(row.id).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 展示关联部门
const connectOrgRef = ref<RefType>();
const showOrg = (row: any) => {
	connectOrgRef.value.openDialog(row);
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
// 页面加载时
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
