﻿<template>
	<div class="mesWorkShop-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false" :fullscreen="fullScreen">
      <template #header>
        <div style="color: #fff;display: flex;align-items: center;justify-content: space-between;">
          <span>{{ props.title }}</span>
          <el-icon size="16" @click="fullScreen = !fullScreen" style="margin-right: 16px; display: block; vertical-align: middle;cursor: pointer">
            <ele-FullScreen v-if="!fullScreen" />
            <ele-Notification v-else />
          </el-icon>
        </div>
      </template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="车间编码" prop="workShopCode">
							<el-input v-model="ruleForm.workShopCode" placeholder="请输入车间编码" maxlength="100"
								show-word-limit clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="车间名称" prop="workShopName">
							<el-input v-model="ruleForm.workShopName" placeholder="请输入车间名称" maxlength="100"
								show-word-limit clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="车间类别编码" prop="workShopTypeCode">
							<el-cascader :options="mesWorkShopTypeTreeData"
								:props="{ checkStrictly: true, emitPath: false, value: 'workShopTypeCode', label: 'workShopTypeName' }"
								placeholder="请选择车间类别编码" clearable="" class="w100" v-model="ruleForm.workShopTypeCode">
								<template #default="{ node, data }">
									<span>{{ data.workShopTypeName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属工厂编码" prop="factoryCode">
							<el-select clearable filterable v-model="ruleForm.factoryCode" placeholder="请选择所属工厂编码">
								<el-option v-for="(item, index) in mesFactoryFactoryCodeDropdownList" :key="index"
									:value="item.value" :label="item.label" />

							</el-select>

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="负责人" prop="manageName">
							<el-input @click="chooseProduct()" v-model="ruleForm.manageName" placeholder="请输入负责人"
								maxlength="10" show-word-limit clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea" maxlength="100"
								show-word-limit clearable />

						</el-form-item>

					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="sorts">
							<el-input-number v-model="ruleForm.sorts" placeholder="请输入排序" clearable />

						</el-form-item>

					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
		<materialDialog title="选择人员" ref="materialDialogRef" :isMore="false" @finish="onChooseFinish"></materialDialog>
	</div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { addMesWorkShop, updateMesWorkShop, detailMesWorkShop } from "/@/api/mes/mesWorkShop";
import { getMesWorkShopTypeTree } from '/@/api/mes/mesWorkShop';
import { getMesFactoryFactoryCodeDropdown } from '/@/api/mes/mesWorkShop';
import materialDialog from '/@/views/mes/mesFactory/component/chooseDialog.vue';
//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: "",
	},
});
const materialDialogRef = ref();
//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
//自行添加其他规则
const rules = ref<FormRules>({
	workShopCode: [{ required: true, message: '请输入车间编码！', trigger: 'blur', },],
	workShopName: [{ required: true, message: '请输入车间名称！', trigger: 'blur', },],
	workShopTypeCode: [{ required: true, message: '请选择车间类别编码！', trigger: 'change', },],
});
let fullScreen = ref(false);
// 打开弹窗
const openDialog = async (row: any) => {
	// ruleForm.value = JSON.parse(JSON.stringify(row));
	// 改用detail获取最新数据来编辑
	getMesWorkShopTypeTreeData();
	getMesFactoryFactoryCodeDropdownList();

	let rowData = JSON.parse(JSON.stringify(row));
	if (rowData.id)
	{
		ruleForm.value = (await detailMesWorkShop(rowData.id)).data.result;
		if(ruleForm.value.manageInfo){
			ruleForm.value.manageName = ruleForm.value.manageInfo.realName;
		}
	}
	else
		ruleForm.value = rowData;
	isShowDialog.value = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	isShowDialog.value = false;
};

// 取消
const cancel = () => {
	isShowDialog.value = false;
};
//打开产品选择页面
const chooseProduct = (row: any) => {
	// currentRow = row;
	materialDialogRef.value.openDialog({ codes: [] });
}
// 产品选择完成
const onChooseFinish = (e: any) => {
	if (e.rows.length == 1) {
		const data = e.rows[0]
		// currentRow.manageId = data.id;
		ruleForm.value.manageName = data.realName;
		ruleForm.value.manageId = data.id;
	}
}
// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = ruleForm.value;
			if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
				await addMesWorkShop(values);
			} else {
				await updateMesWorkShop(values);
			}
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

const mesFactoryFactoryCodeDropdownList = ref<any>([]);
const getMesFactoryFactoryCodeDropdownList = async () => {
	let list = await getMesFactoryFactoryCodeDropdown();
	mesFactoryFactoryCodeDropdownList.value = list.data.result ?? [];
};


const mesWorkShopTypeTreeData = ref<any>([]);
const getMesWorkShopTypeTreeData = async () => {
	let list = await getMesWorkShopTypeTree();
	mesWorkShopTypeTreeData.value = list.data.result ?? [];
};







// 页面加载时
onMounted(async () => {
});

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
