<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 20px 20px 20px">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-position="right"
					label-width="80px"
					style="padding: 0 10px"
				>
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" clearable></el-input>
					</el-form-item>
					<!-- <el-form-item label="排班规则">
            <el-input v-model="form.shift_rule" clearable></el-input>
          </el-form-item> -->
					<el-form-item label="分类" prop="cate">
						<el-select
							v-model="form.cate"
							placeholder="分类"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in options"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="排班规则">
						<el-select
							v-model="form.shift_rule"
							style="width: 100%"
						>
							<el-option
								v-for="item in shiftOptions"
								:key="item"
								:label="item"
								:value="item"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="主要产品">
						<el-select
							v-model="form.product"
							placeholder="主要产品"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in materials"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="直接材料">
						<el-select
							v-model="form.input_materials"
							placeholder="直接材料"
							clearable
							multiple
							style="width: 100%"
						>
							<el-option
								v-for="item in materials"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="检测材料">
						<el-select
							v-model="form.test_materials"
							placeholder="检测材料"
							clearable
							multiple
							style="width: 100%"
						>
							<el-option
								v-for="item in materials"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="所属部门">
						<el-select
							v-model="form.belong_dept"
							placeholder="所属部门"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in group"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit"
					>保存</el-button
				>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>

<script>
const defaultForm = {
	name: "",
	cate: "",
	product: "",
	belong_dept: "",
	test_materials: "",
	input_materials: "",
};
export default {
	emits: ["success", "closed"],
	data() {
		return {
			shiftOptions: [],
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增工段",
				edit: "编辑工段",
				show: "查看工段",
			},
			//表单数据
			form: {
				shift_rule: "tkx",
			},
			//验证规则
			rules: {
				name: [
					{ required: true, message: "请输入名称", trigger: "blur" },
				],
				cate: [
					{ required: true, message: "请选择分类", trigger: "blur" },
				],
				// material: [{required: true, message: "请选择主要产品", trigger: "blur"}],
				belong_dept: [
					{
						required: true,
						message: "请选择所属部门",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			options: [
				{ label: "工段", value: "section" },
				{ label: "其他", value: "other" },
			],
			group: [],
			materials: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getShifts();
		this.getGroup(); //获取部门
		this.getMaterials(); //获取产品
	},
	methods: {
		getShifts() {
			let shiftList = [];
			this.$API.mtm.shift.list.req({ page: 0 }).then((res) => {
				for (var i = 0; i < res.length; i++) {
					if (shiftList.indexOf(res[i].rule) == -1) {
						shiftList.push(res[i].rule);
					}
				}
				this.shiftOptions = shiftList;
			});
		},
		//加载树数据
		getGroup() {
			this.$API.system.dept.list
				.req({ page_size: 3, type: "dept" })
				.then((res) => {
					this.group = res.results;
				});
		},
		//获取产品列表
		getMaterials() {
			var res = this.$API.mtm.material.list
				.req({ page: 0 })
				.then((res) => {
					this.materials = res;
				});
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		getReceptionist(data) {
			this.form.leader = data.id;
			this.form.leader_name = data.name;
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					if (that.mode === "add") {
						that.$API.mtm.mgroup.create
							.req(that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							});
					} else {
						res = that.$API.mtm.mgroup.update
							.req(that.form.id, that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							});
					}
				}
			});
		},

		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
