<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-width="120px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="物料类别" prop="type">
								<el-select
									v-model="form.type"
									placeholder="物料类别"
									clearable
									style="width: 100%"
									@change="typeChange"
								>
									<el-option
										v-for="item in options"
										: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="number">
								<el-input
									v-model="form.number"
									placeholder="物料编号"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" prop="name">
							<el-form-item label="物料名称">
								<el-input
									v-model="form.name"
									placeholder="物料名称"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" prop="cate">
							<el-form-item label="物料系列">
								<el-select
									v-model="form.cate"
									placeholder="物料系列"
									clearable
									allow-create
									filterable
									default-first-option
									:reserve-keyword="false"
									style="width: 100%"
								>
									<el-option
										v-for="item in cateOptions"
										:key="item"
										:label="item"
										:value="item"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="规格">
								<el-input
									v-model="form.specification"
									placeholder="规格"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="型号">
								<el-input
									v-model="form.model"
									placeholder="型号"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="关联物料">
								<el-select
									v-model="brothers"
									placeholder="关联物料"
									clearable
									multiple
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in materialOptions"
										:key="item.id"
										:label="item.full_name"
										:value="item.id"
									/>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="已到工序" prop="process">
								<el-select
									v-model="form.process"
									placeholder="已到工序"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in processOptions"
										: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="need_route">
								<el-switch v-model="form.need_route" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="是否隐藏" prop="is_hidden">
								<el-switch v-model="form.is_hidden" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="安全库存" prop="count_safe">
								<el-input-number
									v-model="form.count_safe"
									:min="0"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="组合件" prop="is_assemb">
								<el-switch v-model="form.is_assemb" />
								<el-button
									v-if="form.is_assemb"
									type="primary"
									icon="el-icon-plus"
									style="margin-left: 20px"
									@click="addComponent"
								></el-button>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row v-if="form.is_assemb">
						<template
							v-for="(item, $index) in componentList"
							:key="item.id"
						>
							<el-col :md="11" :sm="24">
								<el-form-item label="物料名称">
									<el-select
										v-model="item.id"
										placeholder="物料名称"
										clearable
										filterable
										style="width: 100%"
									>
										<el-option
											v-for="item in materialOptions"
											:key="item.id"
											:label="item.full_name"
											:value="item.id"
										/>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :md="11" :sm="24">
								<el-form-item label="数量">
									<el-input-number
										v-model="item.count"
										controls-position="right"
										placeholder="数量"
										clearable
										style="width: 100%"
									></el-input-number>
								</el-form-item>
							</el-col>
							<el-col :md="2">
								<div style="text-align: center">
									<el-icon
										class="deleteIcons"
										@click="delComponent($index)"
									>
										<CircleCloseFilled />
									</el-icon>
								</div>
							</el-col>
						</template>
					</el-row>
				</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>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增物料",
				edit: "编辑物料",
				show: "查看物料",
			},
			brothers: [],
			form: {},
			rules: {
				name: [
					{
						required: true,
						message: "请输入物料名称",
						trigger: "blur",
					},
				],
				type: [
					{
						required: true,
						message: "请选择物料类别",
						trigger: "blur",
					},
				],
				number: [
					{
						required: true,
						message: "请输入物料编号",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			cateOptions:[],
			// cateOptions: [
			// 	{ id: "bang", name: "棒" },
			// 	{ id: "guan", name: "管" },
			// 	{ id: "bangguan", name: "棒管" },
			// 	{ id: "huiliao", name: "混料" },
			// ],
			options: [
				{ id: 0, name: "电/水/气" },
				{ id: 10, name: "成品" },
				{ id: 20, name: "半成品" },
				{ id: 30, name: "主要原料" },
				{ id: 40, name: "辅助材料" },
				{ id: 50, name: "加工工具" },
				{ id: 60, name: "辅助工装" },
				{ id: 70, name: "办公用品" },
			],
			handle_user: [],
			selectionFilters: [],
			materialOptions: [],
			setFiltersVisible: false,
			processOptions: [],
			componentList: [{ id: "", count: 1 }],
		};
	},
	mounted() {
		this.getProcessOptions();
		this.getMaterialCate();
	
	},
	methods: {
		getMaterialCate() {
		    this.$API.mtm.material.cates.req({ page: 0 }).then((res) => {
				this.cateOptions = res;
			});
		},
		getProcessOptions() {
			this.$API.mtm.process.list.req({ page: 0 }).then((res) => {
				this.processOptions = res;
			});
		},
		//显示
		open(mode = "add", type) {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		getMaterial(val) {
			let that = this;
			let query = {};
			query.type = val;
			query.page = 0;
			query.is_hidden = false;
			that.$API.mtm.material.list.req(query).then((res) => {
				that.materialOptions = [];
				res.forEach((item) => {
					item.label =
						item.name + "—" + item.specification + "/" + item.model;
				});
				that.materialOptions = res;
			});
		},
		typeChange(val) {
			this.getMaterial(val);
		},
		//提交
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.brothers = that.brothers;
					if (that.form.is_assemb) {
						let obj = {};
						that.componentList.forEach((item) => {
							obj[item.id] = item.count;
						});
						that.form.components = obj;
					}
					if (that.mode == "add") {
						that.$API.mtm.material.create.req(that.form).then((res) => {
							that.isSaveing = false;
							that.$emit("success", that.form, that.mode);
							that.visible = false;
							that.$message.success("操作成功");
						}).catch(() => {
							that.isSaveing = false;
							that.visible = false;
						});
					} else if (that.mode == "edit") {
						that.$API.mtm.material.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("操作成功");
						}).catch(() => {
							that.isSaveing = false;
						});
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			if(this.mode == 'add'){
				delete this.form.id;
			}
			this.brothers = data.brothers;
			let arr = [];
			for (let key in data.components) {
				let obj = {};
				obj.id = key;
				obj.count = data.components[key];
				arr.push(obj);
			}
			this.componentList = arr;
			this.getMaterial(data.type);
		},
		addComponent() {
			let obj = {};
			obj.id = "";
			obj.count = 1;
			this.componentList.push(obj);
		},
		delComponent(index) {
			this.componentList.splice(index, 1);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>
<style>
.deleteIcons {
	color: #f56c6c;
	font-size: 25px;
	line-height: 34px;
	height: 34px;
}
</style>
