<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">
							<el-form-item label="物料名称" prop="name">
								<el-input
									v-model="form.name"
									placeholder="物料名称"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type==40">
							<el-form-item label="辅料类别" prop="cate">
								<el-select
									v-model="form.cate"
									placeholder="辅料类别"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in mCateEnum.list"
										: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-input
									v-model="form.unit"
									placeholder="计量单位"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type==10||form.type==20">
							<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" v-if="project_code == 'bxerp'||project_code == 'tcerp'">
							<el-form-item label="追踪方式" prop="tracking">
								<el-select
									v-model="form.tracking"
									placeholder="追踪方式"
									clearable
									style="width: 100%"
								>
									<el-option label="批次" :value="10"></el-option>
									<el-option label="单件" :value="20"></el-option>
								</el-select>
							</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="count_safe_upper">
								<el-input-number
									v-model="form.count_safe_upper"
									:min="0"
								/>
							</el-form-item>
						</el-col>
						<!-- 光芯 -->
						<el-col :md="12" :sm="24" v-if="form.type == 40">
							<el-form-item label="仓库位号">
								<el-input v-model="form.bin_number_main"/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type !== 20">
							<el-form-item label="物料单价">
								<el-input-number
									v-model="form.unit_price"
									:min="0"
									controls-position="right"
								/>
							</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" v-if="form.type == 40">
							<el-form-item label="是否进入车间库存">
								<el-switch v-model="form.into_wm" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type==10||form.type==20">
							<el-form-item label="图纸编号">
								<el-input v-model="form.photo_number" placeholder="图纸编号"/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="备注">
								<el-input v-model="form.note" placeholder="备注"/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type==10||form.type==20||(form.type==40&&project_code == 'gx')">
							<el-form-item label="物料照片">
								<sc-upload v-model="form.img" :modelValue="form.img" title="物料照片"></sc-upload>
							</el-form-item>
						</el-col>
					</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>
import { mCateEnum } from "@/utils/enum.js";
export default {
	emits: ["success", "closed"],
	props: {
	    type: {
			type: Number,
	        default: 10,
	    }
	},
	data() {
		return {
			mCateEnum,
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增物料",
				edit: "编辑物料",
				show: "查看物料",
			},
			form: {},
			rules: {
				name: [
					{
						required: true,
						message: "请输入物料名称",
						trigger: "blur",
					},
				],
				type: [
					{
						required: true,
						message: "请选择物料类别",
						trigger: "blur",
					},
				],
				cate: [
					{
						required: true,
						message: "请选择辅料类别",
						trigger: "blur",
					},
				],
				number: [
					{
						required: true,
						message: "请输入物料编号",
						trigger: "blur",
					},
				],
				process: [
					{
						required: true,
						message: "请选择已到工序",
						trigger: "blur",
					},
				],
				tracking: [
					{
						required: true,
						message: "请选择追踪方式",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,

			options: [
				{ id: 10, name: "成品" },
				{ id: 20, name: "半成品" },
				{ id: 30, name: "主要原料" },
				// { id: 40, name: "辅助材料" },
			],
			handle_user: [],
			selectionFilters: [],
			materialOptions: [],
			setFiltersVisible: false,
			processOptions: [],
			componentList: [{ id: "", count: 1 }],
			project_code : this.$TOOL.data.get("BASE_INFO").base.base_code,
		};
	},
	mounted() {
		if(this.project_code !== 'bxerp'){
			this.options.push({ name: "辅助材料", id: 40 });
		}
		this.form.type = this.type;
		this.getProcessOptions();
	},
	methods: {
		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;
			this.$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);
			if(this.form.type == 40) {
				this.form.into_wm = false;
			}
		},
		//提交
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					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) {
			let that = this;
			if(that.mode=="add"){
				that.form.name = data.name;
				that.form.type = data.type;
				that.form.number = data.number;
				that.form.specification = data.specification;
				that.form.model = data.model;
				that.form.unit = data.unit;
				that.form.process = data.process;
				that.form.tracking = data.tracking;
				that.form.count_safe = data.count_safe;
				that.form.count_safe_upper = data.count_safe_upper;
				that.form.is_hidden = data.is_hidden;
				that.form.unit_price = data.unit_price;
				that.form.photo = data.photo;
				that.form.into_wm = data.into_wm;
				that.form.photo_number = data.photo_number;
			}else{
				Object.assign(this.form, data);
			}
			this.getMaterial(data.type);
		},

		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>
<style>
.deleteIcons {
	color: #f56c6c;
	font-size: 25px;
	line-height: 34px;
	height: 34px;
}
</style>
