<template>
	<el-dialog
		:title="titleMap[form.type]"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			ref="dialogForm"
			:model="form"
			:rules="rules"
			label-width="120px"
		>
			<el-form-item label="编号">
				<el-input v-model="form.number" placeholder="编号" />
			</el-form-item>
			<el-form-item label="出入库日期">
				<el-date-picker
					v-model="form.inout_date"
					type="date"
					placeholder="出入库日期"
					value-format="YYYY-MM-DD"
					style="width: 100%"
				>
				</el-date-picker>
			</el-form-item>
			<el-form-item label="采购订单" v-if="form.type == 'pur_in'">
				<el-select
					v-model="form.pu_order"
					clearable
					filterable
					style="width: 100%"
				>
					<el-option
						v-for="item in puOrderOptions"
						:key="item.id"
						:label="item.number"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="供应商"
				v-if="form.type == 'other_in' || form.type == 'pur_in'||form.type == 'pur_out'"
			>
				<el-select
					v-model="form.supplier"
					clearable
					filterable
					style="width: 100%"
				>
					<el-option
						v-for="item in supplierOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="销售订单" v-if="form.type == 'sale_out'">
				<el-select v-model="form.order" clearable filterable style="width: 100%">
					<el-option
						v-for="item in orderOptions"
						:key="item.id"
						:label="item.number"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="客户名称" v-if="form.type == 'sale_out'">
				<el-select
					v-model="form.customer"
					clearable
					style="width: 100%"
				>
					<el-option
						v-for="item in customerOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="客户" v-if="form.type == 'other_out'">
				<el-select
					v-model="form.customer"
					clearable
					filterable
					style="width: 100%"
				>
					<el-option
						v-for="item in customerOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="操作工段"
				v-if="form.type == 'do_in' || form.type == 'do_out'|| form.type == 'borrow_out' || form.type == 'return_in'"
			>
				<el-select
					v-model="form.mgroup"
					clearable
					filterable
					style="width: 100%"
					placeholder="为空时代表领料/入库到车间"
					@change="mgroupChange"
				>
					<el-option
						v-for="item in mgroupOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="操作部门"
				v-if="(form.type == 'do_in' || form.type == 'do_out' || form.type == 'borrow_out' || form.type == 'return_in')&& htype!='baofei'"
			>
				<el-select
					v-model="form.belong_dept"
					clearable
					filterable
					style="width: 100%"
					@change="getgetDeptUsers"
				>
					<el-option
						v-for="item in deptOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="部门执行人" prop="do_user"
				v-if="(form.type == 'do_in' || form.type == 'do_out'|| form.type == 'borrow_out' || form.type == 'return_in')&& htype!='baofei'"
			>
				<el-select v-model="form.do_user" filterable clearable style="width: 100%">
					<el-option
						v-for="item in userOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="仓库执行人" prop="mio_user">
				<el-select
					v-model="form.mio_user"
					clearable
					filterable
					style="width: 100%"
				>
					<el-option
						v-for="item in userOption"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.note" placeholder="备注" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">
				保存
			</el-button>
			<el-button @click="visible = false">取消</el-button>
		</template>
	</el-dialog>
</template>
<script>
const defaultForm = {
	state: 10,
	type: "do_in",
};
export default {
	emits: ["success", "closed"],
	props: {},
	data() {
		return {
			type: "do_in",
			loading: false,
			mode: "add",
			titleMap: {
				do_out: "生产领料",
				sale_out: "销售发货",
				pur_in: "采购入库",
				pur_out: "退货",
				do_in: "生产入库",
				other_in: "其他入库",
				other_out: "其他出库",
				borrow_out: "领用出库",
				return_in: "退还入库",
			},
			form: {},
			rules: {
				number: [
					{required: true,message: "请填写编号",trigger: "blur"},
				],
				do_user: [
					{required: true,message: "请选择部门执行人",trigger: "blur"},
				],
				mio_user: [
					{required: true,message: "请选择仓库执行人",trigger: "blur"},
				],
			},
			htype:"",
			visible: false,
			isSaveing: false,
			options: [],
			setFiltersVisible: false,
			deptOptions: [],
			mgroupOptions: [],
			userOptions: [],
			userOption: [],
			orderOptions: [],
			puOrderOptions: [],
			supplierOptions: [],
			customerOptions: [],
		};
	},
	mounted() {
		let that = this;
		this.form.inout_date = this.$TOOL.dateFormat2(new Date());
		//原料库
		if(that.htype=='baofei'){
			//操作工段：废品库
			//部门操作人员：原料库
			that.$API.mtm.mgroup.list.req({ name:'废品库',page: 0}).then((res) => {
				that.form.recive_mgroup = item.id;
				that.getCkUserList();//废品库接收人
			});
		}
	},
	methods: {
		init() {
			let that = this;
			this.getDeptUser();
			var type = this.form.type;
			if (type == "pur_in") {
				this.getPuOrderOptions();
				this.getSupplierOptions();
			} else if (type == "sale_out") {
				this.getOrderOptions();
				this.getCustomerOptions();
			} else if (type == "do_in" || type == "do_out" || type == "borrow_out" || type == "return_in") {
				if(that.htype=='baofei'){
					that.$API.mtm.mgroup.list.req({ name:'废品库',page: 0}).then((res) => {
						that.mgroupOptions = res;
						that.form.mgroup = res[0].id;
					});
				}else{
					this.getDeptOptions();
					this.getmgroupOptions();
					this.getgetDeptUsers();
				}
			} else if (type == "other_in") {
				this.getSupplierOptions();
			} else if (type == "other_out") {
				this.getCustomerOptions();
			}
		},
		mgroupChange(){
			let that = this;
			that.mgroupOptions.forEach(item=>{
				if(item.id == that.form.mgroup){
				    this.form.belong_dept = item.belong_dept;
				}
			})
		},
		getmgroupOptions() {
			this.$API.mtm.mgroup.list.req({page: 0}).then(res=>{
				this.mgroupOptions = res;
			})
		},
		getSupplierOptions() {
			this.$API.pum.supplier.list.req({ page: 0 }).then((res) => {
				this.supplierOptions = res;
			});
		},
		getCustomerOptions() {
			this.$API.sam.customer.list.req({ page: 0 }).then((res) => {
				this.customerOptions = res;
			});
		},
		getDeptOptions() {
			this.$API.system.dept.list
				.req({ page: 0, type__in: "dept" })
				.then((res) => {
					this.deptOptions = res;
				});
		},
		getPuOrderOptions() {
			this.$API.pum.order.list
				.req({ page: 0, type__in: "20,30" })
				.then((res) => {
					this.puOrderOptions = res;
				});
		},
		getOrderOptions() {
			this.$API.sam.order.list
				.req({ page: 0, state__in: "20,30" })
				.then((res) => {
					this.orderOptions = res;
				});
		},
		getgetDeptUsers(){
			this.$API.system.user.list.req({ depts: this.form.belong_dept, page: 0 }).then((res) => {
				this.userOptions = res;
			});
		},
		getDeptUser() {
			let that = this;
			let userOption = [];
			that.$API.system.user.list.req({ posts__code__contains: "inm", page: 0 }).then((res) => {
				userOption = Array.from(new Map(res.map(item => [item.id, item])).values());
				that.userOption = userOption;
				// console.log('userOption',userOption);
			});
		},
		//新增时清空表单,避免上次保存的数据残留(与mioitem_form一致,防v-if未及时重建时复用旧实例)
		resetForm() {
			this.form = { state: 10, inout_date: this.$TOOL.dateFormat2(new Date()) };
		},
		//显示
		open(mode = "add", type = "",htype="") {
			this.mode = mode;
			this.htype = htype;
			if (mode == "add") {
				this.resetForm();
			}
			this.form.type = type;
			this.init();
			this.visible = true;
			return this;
		},
		//提交
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					try {
						var res;
						if(this.htype=='baofei'){
							this.form.do_user = this.form.mio_user
						}
						if (this.mode == "add") {
							res = await this.$API.inm.mio.create.req(this.form);
						} else if (this.mode == "edit") {
							res = await this.$API.inm.mio.update.req(
								this.form.id,
								this.form
							);
						}
						this.isSaveing = false;
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					} catch (err) {
						console.log(err);
						//可以处理校验错误
						this.isSaveing = false;
						return err;
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>
<style></style>
