﻿<template>
	<div class="mesEquipment-container">
		<el-row :gutter="5" style="width: 100%; flex: 1;height:100%">
			<el-col :span="4" :xs="24" style="height:100%">
				<Tree ref="TreeRef" @node-click="nodeClick" />
			</el-col>
			<el-col :span="20" :xs="24" style="display: flex; flex-direction: column">
				<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
					<el-form :model="queryParams" ref="queryForm" labelWidth="90">
						<el-row>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10">
								<el-form-item label="关键字">
									<el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备编码">
									<el-input v-model="queryParams.equipmentCode" clearable="" placeholder="请输入设备编码" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备名称">
									<el-input v-model="queryParams.equipmentName" clearable="" placeholder="请输入设备名称" />
								</el-form-item>
							</el-col>
							<!-- <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备IP">
									<el-input v-model="queryParams.ip" clearable="" placeholder="请输入设备IP" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备网关">
									<el-input v-model="queryParams.getway" clearable="" placeholder="请输入设备网关" />
								</el-form-item>
							</el-col> -->
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备型号">
									<el-input v-model="queryParams.model" clearable="" placeholder="请输入设备型号" />
								</el-form-item>
							</el-col>
							<!-- <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="一保">
									<el-select clearable="" v-model="queryParams.guar1" placeholder="请选择一保">
										<el-option v-for="(item, index) in dl('YiBao')" :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`" />
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="二保">
									<el-select clearable="" v-model="queryParams.guar2" placeholder="请选择二保">
										<el-option v-for="(item, index) in dl('ErBao')" :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`" />
									</el-select>
								</el-form-item>
							</el-col> -->
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="设备制造商">
									<el-input v-model="queryParams.maker" clearable="" placeholder="请输入设备制造商" />
								</el-form-item>
							</el-col>
							<!-- <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="制造日期">
									<el-date-picker placeholder="请选择制造日期" value-format="YYYY/MM/DD" type="daterange" v-model="queryParams.makeDateRange" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="使用日期">
									<el-date-picker placeholder="请选择使用日期" value-format="YYYY/MM/DD" type="daterange" v-model="queryParams.useDateRange" />
								</el-form-item>
							</el-col> -->
							<!-- <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
								<el-form-item label="备注">
									<el-input v-model="queryParams.remark" clearable="" placeholder="请输入备注" />
								</el-form-item>
							</el-col> -->
							<el-col :span="showAdvanceQueryUI ? 24 : 8" class="mb10">
								<el-form-item>
									<el-button-group style="display: flex; align-items: center">
										<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'mesEquipment/page'"> 查询 </el-button>
										<el-button icon="ele-Refresh" @click="() => (queryParams = {})"> 重置 </el-button>
										<el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI" style="margin-left: 5px"> 高级查询 </el-button>
										<el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI" style="margin-left: 5px"> 隐藏 </el-button>
										<el-button type="primary" style="margin-left: 5px" icon="ele-Plus" @click="openAddMesEquipment" v-auth="'mesEquipment/add'"> 新增 </el-button>

										<el-button type="primary" icon="ele-RefreshRight" @click="syncClick"> 同 步 </el-button>
										<el-button type="primary" icon="ele-Histogram" @click="historySync"> </el-button>

										<el-button icon="ele-Printer" size="small" type="primary" @click="openPrint()"> 批量打印 </el-button>
									</el-button-group>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</el-card>
				<el-card class="full-table" shadow="hover" style="margin-top: 5px">
					<el-table
						ref="tabRef"
						:data="tableData"
						style="width: 100%"
						v-loading="loading"
						tooltip-effect="light"
						row-key="id"
						@sort-change="sortChange"
						@expand-change="expandChange"
						:expand-row-keys="expandRowKeys"
						border=""
					>
						<!--            <el-table-column type="expand">-->
						<!--              <template #default="scope">-->
						<!--                <el-tabs v-model="scope.row.activeChild">-->
						<!--                  <el-tab-pane label="设备工艺参数" name="params"></el-tab-pane>-->
						<!--                  <el-tab-pane label="执行指令" name="instruction"></el-tab-pane>-->
						<!--                </el-tabs>-->
						<!--                <el-table :data="scope.row.paramsTableData || []" style="width: 100%" tooltip-effect="light" row-key="id" border="" v-show="scope.row.activeChild == 'params' || !scope.row.activeChild">-->
						<!--                  <el-table-column type="index" label="序号" width="55" align="center" />-->
						<!--                  <el-table-column prop="params" label="参数编码"  show-overflow-tooltip="" />-->
						<!--                  <el-table-column prop="paramNames" label="参数名称"  show-overflow-tooltip="" />-->
						<!--                </el-table>-->
						<!--                <mes-equipment-command v-show="scope.row.activeChild == 'instruction'" :equipmentCode="scope.row.equipmentCode" style="max-height:40vh" ></mes-equipment-command>-->
						<!--              </template>-->
						<!--            </el-table-column>-->
						<el-table-column type="index" label="序号" width="55" align="center" />
						<el-table-column type="selection" width="55" align="center" />
						<el-table-column prop="equipmentCode" label="设备编码" show-overflow-tooltip="" />
						<el-table-column prop="equipmentName" label="设备名称" show-overflow-tooltip="" />
						<el-table-column prop="ip" label="设备IP" show-overflow-tooltip="" />
						<el-table-column prop="getway" label="设备网关" show-overflow-tooltip="" />
						<el-table-column prop="equipmentTypeCode" label="设备类别" width="120" show-overflow-tooltip="">
							<template #default="scope">
								<span>{{ scope.row.equipmentTypeCodeEquipmentTypeName }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="model" label="设备型号" show-overflow-tooltip="" />
						<!-- <el-table-column prop="guar1" label="一保" show-overflow-tooltip="">
							<template #default="scope"> [{{ scope.row.guar1 }}]{{ scope.row.guar1Info?.workTeamName }} </template>
						</el-table-column>
						<el-table-column prop="guar2" label="二保" show-overflow-tooltip="">
							<template #default="scope"> [{{ scope.row.guar2 }}]{{ scope.row.guar2Info?.workTeamName }} </template>
						</el-table-column> -->
						<el-table-column prop="maker" label="设备制造商" show-overflow-tooltip="" />
						<el-table-column prop="makeDate" label="制造日期" show-overflow-tooltip="" />
						<el-table-column prop="useDate" label="使用日期" show-overflow-tooltip="" />
						<el-table-column prop="isStop" label="是否停用" show-overflow-tooltip="">
							<template #default="scope">
								<el-tag v-if="scope.row.isStop"> 是 </el-tag>
								<el-tag type="danger" v-else> 否 </el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="productionLineCode" label="产线编码" show-overflow-tooltip="">
							<template #default="scope">
								<span>{{ scope.row.productionLineCodeProductionLineName }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="isQa" label="是否质保期" show-overflow-tooltip="">
							<template #default="scope">
								<el-tag v-if="scope.row.isQa"> 是 </el-tag>
								<el-tag type="danger" v-else> 否 </el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="img" label="设备图片" show-overflow-tooltip="">
							<template #default="scope">
								<el-image
									v-if="scope.row.img"
									style="width: 60px; height: 60px"
									:src="scope.row.img"
									:lazy="true"
									:hide-on-click-modal="true"
									:preview-src-list="[scope.row.img]"
									:initial-index="0"
									fit="scale-down"
									preview-teleported=""
								/>
							</template>
						</el-table-column>
						<el-table-column prop="remark" label="备注" show-overflow-tooltip="" />
						<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('mesEquipment/update') || auth('mesEquipment/delete')">
							<template #default="scope">
								<el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditMesEquipment(scope.row)" v-auth="'mesEquipment/update'"> 编辑 </el-button>
								<el-button icon="ele-Delete" size="small" text="" type="primary" @click="delMesEquipment(scope.row)" v-auth="'mesEquipment/delete'"> 删除 </el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						v-model:currentPage="tableParams.page"
						v-model:page-size="tableParams.pageSize"
						:total="tableParams.total"
						:page-sizes="[10, 20, 50, 100, 200, 500]"
						small=""
						background=""
						@size-change="handleSizeChange"
						@current-change="handleCurrentChange"
						layout="total, sizes, prev, pager, next, jumper"
					/>
					<printDialog ref="printDialogRef" :title="printTitle" @reloadTable="handleQuery" />
					<editDialog ref="editDialogRef" :title="editMesEquipmentTitle" @reloadTable="handleQuery" />

					<syncProcess ref="syncProcessRef" />
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script lang="ts" setup="" name="mesEquipment">
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { formatDate } from '/@/utils/formatTime';

import printDialog from '/@/views/system/print/component/hiprint/preview.vue';
import editDialog from '/@/views/mes/mesEquipment/component/editDialog.vue';
import { pageMesEquipment, deleteMesEquipment, syncDevice } from '/@/api/mes/mesEquipment';
import Tree from '/@/views/mes/mesEquipment/component/equipmentTypeTree.vue';

import syncProcess from '/@/views/mes/components/syncProcess/index.vue';
const syncProcessRef = ref();
const historySync = () => {
	syncProcessRef.value.openDialog();
};
const syncClick = () => {
	ElMessageBox.confirm(`确定要同步数据吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'success',
	})
		.then(async () => {
			await syncDevice();
			ElMessage.success('同步完成');
		})
		.catch(() => {});
};

const showAdvanceQueryUI = ref(false);
const printDialogRef = ref();
const editDialogRef = ref();
const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});
const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
});

const editMesEquipmentTitle = ref('');

// 改变高级查询的控件显示状态
const changeAdvanceQueryUI = () => {
	showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
};

// 树组件点击
const nodeClick = async (node: any) => {
	queryParams.value.equipmentTypeCode = node.code;
	handleQuery();
};

// 查询操作
const handleQuery = async () => {
	loading.value = true;
	var res = await pageMesEquipment(Object.assign(queryParams.value, tableParams.value));
	tableData.value = (res.data.result?.items ?? []).map((item) => ({
		...item,
		activeChild: 'params',
		paramsTableData: [],
	}));
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
};

const expandRowKeys = ref([]);
const expandChange = (row: any, expandedRows: any) => {
	if (expandedRows.length) {
		expandRowKeys.value = [];
		if (row) {
			expandRowKeys.value = expandedRows.map((item) => item.id);
			// 获取详情
			row.paramsTableData = (row.params || '')
				.split(',')
				.filter((item) => item)
				.map((item, index) => {
					return {
						params: item || '',
						paramNames: (row.personNames || '').split(',').filter((item) => item)?.[index] || '',
					};
				});
		}
	} else {
		expandRowKeys.value = [];
	}
};

// 列排序
const sortChange = async (column: any) => {
	queryParams.value.field = column.prop;
	queryParams.value.order = column.order;
	await handleQuery();
};

// 打开新增页面
const openAddMesEquipment = () => {
	editMesEquipmentTitle.value = '添加设备档案';
	editDialogRef.value.openDialog({});
};

// 打开打印页面
const openPrintMesEquipment = async (row: any) => {
	printMesEquipmentTitle.value = '打印设备档案';
};

// 打开编辑页面
const openEditMesEquipment = (row: any) => {
	editMesEquipmentTitle.value = '编辑设备档案';
	editDialogRef.value.openDialog(row);
};

// 删除
const delMesEquipment = (row: any) => {
	ElMessageBox.confirm(`确定要删除吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await deleteMesEquipment(row);
			handleQuery();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	tableParams.value.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	tableParams.value.page = val;
	handleQuery();
};

const printTitle = ref('');
import { hiprint } from 'vue-plugin-hiprint';
import { SysPrintApi } from '/@/api-services/api';
import { getAPI } from '/@/utils/axios-utils';
const tabRef = ref();
const openPrint = async () => {
	var rows = tabRef.value.getSelectionRows();

	if (!rows || rows.length == 0) {
		return ElMessage.warning('请选择至少一条数据！');
	}

	let printRes = await getAPI(SysPrintApi).apiSysPrintPrintNameGet('设备信息打印');
	let template = JSON.parse(printRes.data.result?.template);
	printTitle.value = '设备信息打印';
	let templates: any = [];
	rows.forEach((el: any) => {
		templates.push({ template: new hiprint.PrintTemplate({ template: template }), data: el });
	});
	hiprint.print({
		templates: templates,
	});
};

handleQuery();
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
