﻿<template>
	<div class="mesWorkSite-container">
		<el-row :gutter="5" style="width: 100%; flex: 1">
			<el-col :span="4" :xs="24">
				<OrgTree ref="orgTreeRef" @node-click="nodeClick" :lv="4" />
			</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.workSiteCode" 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.workSiteName" 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.mesWorkStationCode" 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="X坐标">
									<el-input v-model="queryParams.siteX" clearable="" placeholder="请输入X坐标" />
								</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="Y坐标">
									<el-input v-model="queryParams.siteY" clearable="" placeholder="请输入Y坐标" />
								</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="Z坐标">
									<el-input v-model="queryParams.siteZ" clearable="" placeholder="请输入Z坐标" />
								</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 :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb10" v-if="showAdvanceQueryUI">-->
							<!--                <el-form-item label="创建者部门名称">-->
							<!--                  <el-input v-model="queryParams.createOrgName" clearable="" placeholder="请输入创建者部门名称"/>-->
							<!--                </el-form-item>-->
							<!--              </el-col>-->
							<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" 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="'mesWorkSite/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="openAddMesWorkSite" v-auth="'mesWorkSite/add'"> 新增 </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="activeChild">
									<el-tab-pane label="关联员工" name="staff"></el-tab-pane>
									<el-tab-pane label="关联设备" name="equipment"></el-tab-pane>
								</el-tabs>
								<!--   员工  -->
								<el-table :data="scope.row.staffTableData || []" style="width: 100%" v-loading="loading"
									tooltip-effect="light" row-key="id" border="" v-show="activeChild == 'staff'">
									<el-table-column type="index" label="序号" width="55" align="center" />
									<el-table-column prop="account" label="账号" show-overflow-tooltip="" />
									<el-table-column prop="realName" label="姓名" show-overflow-tooltip="" />
								</el-table>
								<!--   设备  -->
								<el-table :data="scope.row.equipmentTableData || []" style="width: 100%"
									v-loading="loading" tooltip-effect="light" row-key="id" border=""
									v-show="activeChild == 'equipment'">
									<el-table-column type="index" label="序号" width="55" align="center" />
									<el-table-column prop="equipmentCode" label="设备编码" show-overflow-tooltip="" />
									<el-table-column prop="equipmentName" label="设备名称" show-overflow-tooltip="" />
								</el-table>
							</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="workSiteCode" label="工位编码" show-overflow-tooltip="" />
						<el-table-column prop="workSiteName" label="工位名称" show-overflow-tooltip="" />
						<!-- <el-table-column prop="mesWorkStationCode" label="所属工站编码" show-overflow-tooltip="" /> -->
						<el-table-column prop="productionLineCode" label="所属产线" show-overflow-tooltip="">
							<template #default="scope">
								<span v-show="scope.row.productionLineCode">[{{ scope.row.productionLineCode
									}}]</span><span>{{ scope.row.productionLineName }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="workShopCode" label="所属车间" show-overflow-tooltip="">
							<template #default="scope">
								<span v-show="scope.row.workShopCode">[{{ scope.row.workShopCode }}]</span><span>{{
									scope.row.workShopName }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="factoryCode" label="所属工厂" show-overflow-tooltip="">
							<template #default="scope">
								<span v-show="scope.row.factoryCode">[{{ scope.row.factoryCode }}]</span><span>{{
									scope.row.factoryName }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="mesWorkStationName" label="所属工站" show-overflow-tooltip="">
							<template #default="scope">
								<span v-show="scope.row.mesWorkStationCode">[{{ scope.row.mesWorkStationCode }}]{{
									scope.row.workStationName}}</span>
							</template>
						</el-table-column>
						<el-table-column prop="siteX" label="X坐标" show-overflow-tooltip="" />
						<el-table-column prop="siteY" label="Y坐标" show-overflow-tooltip="" />
						<el-table-column prop="siteZ" label="Z坐标" show-overflow-tooltip="" />
						<el-table-column prop="remark" label="备注" show-overflow-tooltip="" />
						<!--            <el-table-column prop="createOrgName" label="创建者部门名称"  show-overflow-tooltip="" />-->
						<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip=""
							v-if="auth('mesWorkSite/update') || auth('mesWorkSite/delete')">
							<template #default="scope">
								<el-button icon="ele-Edit" size="small" text="" type="primary"
									@click="openEditMesWorkSite(scope.row)" v-auth="'mesWorkSite/update'"> 编辑
								</el-button>
								<el-button icon="ele-Delete" size="small" text="" type="primary"
									@click="delMesWorkSite(scope.row)" v-auth="'mesWorkSite/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="printMesWorkSiteTitle" @reloadTable="handleQuery" />
					<editDialog ref="editDialogRef" :title="editMesWorkSiteTitle" @reloadTable="handleQuery" />
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script lang="ts" setup="" name="mesWorkSite">
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/mesWorkSite/component/editDialog.vue';
import { pageMesWorkSite, deleteMesWorkSite, detailMesWorkSite } from '/@/api/mes/mesWorkSite';
// import TeamTree from "/@/views/mes/mesWorkSite/component/teamTree.vue";
import OrgTree from '/@/views/mes/mesProductionLine/component/orgTree1.vue';
import { updateworksit } from '/@/api/simple/mesVirtualPackLog';
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 activeChild = ref('staff');
const printMesWorkSiteTitle = ref('');
const editMesWorkSiteTitle = ref('');

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

// 下拉展开
const expandRowKeys = ref([]);
const expandChange = async (row: any, expandedRows: any) => {
	if (expandedRows.length) {
		expandRowKeys.value = [];
		if (row) {
			loading.value = true;
			expandRowKeys.value = expandedRows.map((item) => item.id);
			let detail = await detailMesWorkSite(row.id);
			console.log(detail);
			let data = detail?.data?.result || {};
			loading.value = false;
			// 员工
			row.staffTableData = (data.personCodes || '')
				.split(',')
				.filter((item) => item)
				.map((item, index) => {
					return {
						account: item || '',
						realName: (data.personNames || '').split(',').filter((item) => item)?.[index] || '',
					};
				});
			// 设备
			row.equipmentTableData = (data.equipmentCodes || '')
				.split(',')
				.filter((item) => item)
				.map((item, index) => {
					return {
						equipmentCode: item || '',
						equipmentName: (data.equipmentNames || '').split(',').filter((item) => item)?.[index] || '',
					};
				});
		}
	} else {
		expandRowKeys.value = [];
	}
};

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

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

// 打开新增页面
const openAddMesWorkSite = () => {
	if (!queryParams.value.MesWorkStationCode) return ElMessage.warning('请选择所属工站');
	editMesWorkSiteTitle.value = '添加工位档案';
	editDialogRef.value.openDialog({
		mesWorkStationCode: queryParams.value.MesWorkStationCode
	});
};

// 打开打印页面
const openPrintMesWorkSite = async (row: any) => {
	printMesWorkSiteTitle.value = '打印工位档案';
};

// 打开编辑页面
const openEditMesWorkSite = (row: any) => {
	editMesWorkSiteTitle.value = '编辑工位档案';
	editDialogRef.value.openDialog(row);
};

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

// 树组件点击
const nodeClick = async (node: any) => {
	// 只根据产线筛选列表
	console.log(node);
	queryParams.value.MesWorkStationCode = null;
	if (!node.sign) {
		queryParams.value.MesWorkStationCode = null;
		handleQuery();
		return;
	}
	if (node.sign !== '4') return;
	queryParams.value.MesWorkStationCode = node.code;
	handleQuery();
};

// 改变页面容量
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('工位40');
	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,
	});
};

//新增方法，添加工位时同时添加或修改仓库中对应工位信息
const onScanAccount = async (val:any,code:any,name:any) => {
	 var res = await updateworksit({ val:val,code:code,name:name });
	 let data = res.data.result || {};
	// state.ruleForm.people = data.account+"-"+data.realName;
	// state.ruleForm.peopleCode=undefined;
	// ElMessage.success('扫描成功');
};

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