<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		width="1000px"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			ref="dialogForm"
			:model="form"
			:rules="rules"
			label-width="80px"
		>
			<el-row>
				<el-col :md="12" :sm="24">
					<el-form-item label="交接物料" prop="wm">
						<el-select
							v-model="form.wm"
							placeholder="交接物料"
							clearable
							style="width: 100%"
							@change="materialChange"
						>
							<el-option
								v-for="item in materialOptions"
								:key="item.id"
								:label="item.batch"
								:value="item.id"
							>
								<span>{{ item.batch }}</span>
								<span v-if="item.notok_sign !== null"
									>-{{ item.notok_sign }}</span
								>
								<span>-[{{ item.count }}]</span>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="交送日期" prop="send_date">
						<el-date-picker
							v-model="form.send_date"
							type="date"
							placeholder="选择时间"
							format="YYYY-MM-DD"
							value-format="YYYY-MM-DD"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="交接数量" width="100">
						<el-input-number
							v-model="form.count"
							controls-position="right"
							:min="0"
							step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="交接数量"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="交送人" prop="send_user">
						<el-select
							v-model="form.send_user"
							placeholder="交送人"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in userList"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="接收工段" prop="recive_mgroup">
						<el-select
							v-model="form.recive_mgroup"
							placeholder="接收工段"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in mgroupOptions"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="接收人" prop="recive_user">
						<el-select
							v-model="form.recive_user"
							placeholder="接收人"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in userList2"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-footer>
			<el-button type="primary" v-loading="isSaveing" @click="submit"
				>确定</el-button
			>
			<el-button @click="visible = false">取消</el-button>
		</el-footer>
	</el-dialog>
</template>
<script>
export default {
	emits: ["success", "closed"],
	props: {
		type: {
			type: Number,
			default: 10,
		},
		mgroupId: {
			type: String,
			default: "",
		},
	},
	data() {
		return {
			yseorno: ["是", "否"],
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增交接记录",
				edit: "编辑交接记录",
				show: "查看交接记录",
			},
			handle_user: [],
			form: {
				batch: "",
				count: 0, //送料数
				wm: "",
				send_date: "",
				send_user: "",
				send_mgroup: "",
				recive_user: "",
				recive_mgroup: "",
			},
			rules: {
				batch: [
					{
						required: true,
						message: "请输入批次号",
						trigger: "blur",
					},
				],
				wm: {
					required: true,
					message: "请选择物料",
					trigger: "blur",
				},
				send_date: [
					{
						required: true,
						message: "请选择送料日期",
						trigger: "blur",
					},
				],
				send_user: [
					{
						required: true,
						message: "请选择交送人",
						trigger: "blur",
					},
				],
				send_mgroup: [
					{
						required: true,
						message: "请选择交送工段",
						trigger: "blur",
					},
				],
				recive_user: [
					{
						required: true,
						message: "请选择接收人",
						trigger: "blur",
					},
				],
				recive_mgroup: [
					{
						required: true,
						message: "请选择接收工段",
						trigger: "blur",
					},
				],
			},
			userList: [],
			userList2: [],
			mgroupOptions: [],
			materialOptions: [],
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.form.type = this.type;
		let materialObj = this.$TOOL.data.get("MATERIAL_OBJECT");
		this.materialObj = materialObj;
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		this.form.handle_date = year + "-" + month + "-" + day;
		this.form.send_mgroup = this.mgroupId;
		this.getMaterial();
		this.getUserList();
		this.getMgroupOptions();
	},
	methods: {
		//获取部门列表
		getMgroupOptions() {
			this.$API.mtm.mgroup.list
				.req({ page: 0, type__in: "dept" })
				.then((res) => {
					this.mgroupOptions = res;
				});
		},

		//获取车间物料
		getMaterial() {
			let that = this;
			this.$API.wpm.wmaterial.list
				.req({ mgroup: that.mgroupId, page: 0 })
				.then((res) => {
					that.materialOptions = res;
				});
		},
		//获取切片工段人员和活化工段人员
		getUserList() {
			let that = this;
			this.$API.system.user.list
				.req({ mgroup__name: "切片", page: 0 })
				.then((res) => {
					that.userList2 = res;
				});
			this.$API.system.user.list
				.req({ mgroup__name: "活化", page: 0 })
				.then((res) => {
					that.userList = res;
				});
		},

		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		materialChange(val) {
			let data = this.materialOptions.filter((item) => {
				return item.id == val;
			});
			this.form.batch = data[0].batch;
		},
		//提交
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					if (this.mode == "add") {
						this.$API.wpm.handover.create
							.req(this.form)
							.then((res) => {
								this.isSaveing = false;
								this.$emit("success", this.form, this.mode);
								this.visible = false;
								this.$message.success("操作成功");
							})
							.catch((err) => {
								//可以处理校验错误
								this.isSaveing = false;
								return err;
							});
					} else if (this.mode == "edit") {
						this.$API.wpm.handover.update
							.req(this.form.id, this.form)
							.then((res) => {
								this.isSaveing = false;
								this.$emit("success", this.form, this.mode);
								this.visible = false;
								this.$message.success("操作成功");
							})
							.catch((err) => {
								//可以处理校验错误
								this.isSaveing = false;
								return err;
							});
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>
<style scoped>
.formTitle {
	margin-bottom: 10px;
	font-weight: 600;
}
</style>
