﻿<template>
	<div class="mesFactory-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="factoryCode">
							<el-input v-model="ruleForm.factoryCode" 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="factoryName">
							<el-input v-model="ruleForm.factoryName" 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="factoryTypeCode">
							<el-select clearable filterable v-model="ruleForm.factoryTypeCode" placeholder="请选择工厂类别">
								<el-option v-for="(item, index) in mesFactoryFactoryTypeCodeDropdownList" :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 v-model="ruleForm.manageName" @click="chooseProduct()" placeholder="请输入负责人"
								maxlength="10" 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="country">
							<el-select clearable v-model="ruleForm.country" placeholder="请选择国家">
								<el-option v-for="(item, index) in dl('Country')" :key="index" :value="item.code"
									:label="`[${item.code}] ${item.value}`"></el-option>

							</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="province">
							<el-select clearable v-model="ruleForm.province" placeholder="请选择省份">
								<el-option v-for="(item, index) in dl('Province')" :key="index" :value="item.code"
									:label="`[${item.code}] ${item.value}`"></el-option>

							</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="orgCode">
							<el-cascader :options="sysOrgTreeData"
								:props="{ checkStrictly: true, emitPath: false, value: 'code', label: 'name' }"
								placeholder="请选择所属公司" clearable="" class="w100" v-model="ruleForm.orgCode">
								<template #default="{ node, data }">
									<span>{{ data.name }}</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="address">
							<el-input v-model="ruleForm.address" placeholder="请输入地址" maxlength="300" 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="mobile">
							<el-input v-model="ruleForm.mobile" 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="post">
							<el-input v-model="ruleForm.post" 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="fax">
							<el-input v-model="ruleForm.fax" placeholder="请输入传真" maxlength="100" 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-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 { addMesFactory, updateMesFactory, detailMesFactory } from "/@/api/mes/mesFactory";
import { getMesFactoryFactoryTypeCodeDropdown } from '/@/api/mes/mesFactory';
import { getSysOrgTree } from '/@/api/mes/mesFactory';
import materialDialog from '/@/views/mes/mesFactory/component/chooseDialog.vue';
const materialDialogRef = ref();
//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: "",
	},
});
let currentRow = {};
let fullScreen = ref(false);
//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
//自行添加其他规则
const rules = ref<FormRules>({
	factoryCode: [{ required: true, message: '请输入工厂编码！', trigger: 'blur', },],
	factoryName: [{ required: true, message: '请输入工厂名称！', trigger: 'blur', },],
	orgCode: [{ required: true, message: '请选择所属公司！', trigger: 'change', },],
});
//打开产品选择页面
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.nickName;
		ruleForm.value.manageId = data.id;
	}
}
// 打开弹窗
const openDialog = async (row: any) => {
	// ruleForm.value = JSON.parse(JSON.stringify(row));
	// 改用detail获取最新数据来编辑
	let rowData = JSON.parse(JSON.stringify(row));
	if (rowData.id)
		{
			ruleForm.value = (await detailMesFactory(rowData.id)).data.result;
			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 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 addMesFactory(values);
			} else {
				await updateMesFactory(values);
			}
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

const mesFactoryFactoryTypeCodeDropdownList = ref<any>([]);
const getMesFactoryFactoryTypeCodeDropdownList = async () => {
	let list = await getMesFactoryFactoryTypeCodeDropdown();
	mesFactoryFactoryTypeCodeDropdownList.value = list.data.result ?? [];
};
getMesFactoryFactoryTypeCodeDropdownList();


const sysOrgTreeData = ref<any>([]);
const getSysOrgTreeData = async () => {
	let list = await getSysOrgTree();
	sysOrgTreeData.value = list.data.result ?? [];
};
getSysOrgTreeData();






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

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