<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="120px"
		>
			<el-row>
				<el-col :md="12" :sm="24">
					<el-form-item label="批次号">
						<el-input
							disabled
							v-if="mode == 'edit'"
							v-model="form.batch"
						></el-input>
						<el-select
							v-else
							v-model="form.batch"
							style="width: 100%"
							filterable
							:disabled="mode == 'edit'"
						>
							<el-option
								v-for="item in wmaterialOptions"
								:key="item"
								:label="item"
								:value="item"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item prop="type" label="操作人">
						<el-input
							disabled
							v-if="mode == 'edit'"
							v-model="form.handle_user_name"
						></el-input>
						<el-select
							v-else
							v-model="form.handle_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="material">
						<el-select
							v-model="form.shift"
							placeholder="班次"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in shiftOptions"
								: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="领用数量">
						<el-input-number
							v-model="form.count_use"
							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="加工数量">
						<el-input-number
							v-model="form.count_real"
							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="合格数">
						<el-input-number
							v-model="form.count_ok"
							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="生产设备">
						<el-select
							v-model="form.equipments"
							style="width: 100%"
							filterable
							multiple
							clearable
						>
							<el-option
								v-for="item in equipmentOptions"
								: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="handle_date">
            <el-date-picker
              v-model="form.handle_date"
              type="date"
              placeholder="操作日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
            />
          </el-form-item>
        </el-col> -->
			</el-row>
			<div class="formTitle">不合格原因及数量:</div>
			<el-row>
				<el-col :md="12" :sm="24">
					<el-form-item label="划伤">
						<el-input-number
							v-model="form.count_n_hs"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="划伤"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="挫伤">
						<el-input-number
							v-model="form.count_n_cs"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="挫伤"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="杂质">
						<el-input-number
							v-model="form.count_n_zz"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="杂质"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="条纹">
						<el-input-number
							v-model="form.count_n_tw"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="条纹"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="锥度大">
						<el-input-number
							v-model="form.count_n_zdd"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="锥度大"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="短">
						<el-input-number
							v-model="form.count_n_d"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="短"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="炸纹">
						<el-input-number
							v-model="form.count_n_zw"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="炸纹"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="断裂">
						<el-input-number
							v-model="form.count_n_dl"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="断裂"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="气泡">
						<el-input-number
							v-model="form.count_n_qp"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="气泡"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="不亮">
						<el-input-number
							v-model="form.count_n_bl"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="不亮"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="横纹">
						<el-input-number
							v-model="form.count_n_hw"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="横纹"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="有皮">
						<el-input-number
							v-model="form.count_n_yp"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="有皮"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="爆皮">
						<el-input-number
							v-model="form.count_n_bp"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="爆皮"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="色差">
						<el-input-number
							v-model="form.count_n_sc"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="色差"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="直径大">
						<el-input-number
							v-model="form.count_n_zjd"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="直径大"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="直径小">
						<el-input-number
							v-model="form.count_n_zjx"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="直径小"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="气线">
						<el-input-number
							v-model="form.count_n_qx"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="气线"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="结石">
						<el-input-number
							v-model="form.count_n_js"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="结石"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="椭圆度大">
						<el-input-number
							v-model="form.count_n_tydd"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="椭圆度大"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="水雾">
						<el-input-number
							v-model="form.count_n_sw"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="水雾"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="壁厚偏差大">
						<el-input-number
							v-model="form.count_n_bhpcd"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="壁厚偏差大"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="弯">
						<el-input-number
							v-model="form.count_n_wq"
							controls-position="right"
							:min="0"
							:step="1"
							:step-strictly="true"
							style="width: 100%"
							placeholder="弯"
							@change="countNotOkSun"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="合计">
						<el-input-number
							v-model="form.count_notok"
							controls-position="right"
							style="width: 100%"
							placeholder="合计"
							disabled
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="备注">
						<el-input
							v-model="form.note"
							tyle="width:100%"
							placeholder="备注"
						></el-input>
					</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: {
		mgroup: { type: String, default: "" },
		mtask: { type: String, default: "" },
		handledate: { type: String, default: "" },
		material_in: { type: String, default: "" },
		material_out: { type: String, default: "" },
		material_name: { type: String, default: "" },
	},
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增记录",
				edit: "编辑记录",
			},
			form: {
				shift: "",
				batch: "",
				count_ok: 0,
				count_real: 0,
				count_use: 0,
				count_notok: 0,
				handle_date: "",
				//共有
				count_n_hs: 0, //划伤
				count_n_cs: 0, //挫伤
				count_n_zz: 0, //杂质
				count_n_tw: 0, //条纹
				count_n_zdd: 0, //锥度大
				count_n_d: 0, //短
				count_n_zw: 0, //炸文
				count_n_tydd: 0, //椭圆度大          共8个
				//棒
				count_n_dl: 0, //断裂
				count_n_qp: 0, //气泡
				count_n_bl: 0, //不亮
				count_n_hw: 0, //横纹
				count_n_yp: 0, //有皮
				count_n_bp: 0, //爆皮
				count_n_sc: 0, //色差
				count_n_zjx: 0, //直径大
				count_n_zjd: 0, //直径小       共9个
				//管
				count_n_qx: 0, //气线
				count_n_js: 0, //结石
				count_n_sw: 0, //水雾
				count_n_bhpcd: 0, //壁厚偏差大
				count_n_wq: 0, //弯   共5个

				handle_user: "",
			},
			mlogb: [],
			userList: [],
			batchOptions: [],
			equipmentOptions: [],
			wmaterialOptions: [],
			shiftOptions: [],
			routeOptions: [],
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
			handle_name: "",
			leader_name: "",
			count_n_all: null,
			rules: {},
		};
	},
	mounted() {
		this.getBatchs();
		this.getUserList();
		this.getEquipment();
		this.getWMaterial();
		this.getShiftOptions();
		this.form.material_out = this.material_out;
		this.form.handle_date = this.handle_date;
		this.form.mgroup = this.mgroup;
	},
	methods: {
		getRouteOptions() {
			this.$API.mtm.route.list
				.req({ page: 0, mgroup: this.mgroup })
				.then((res) => {
					for (var i = 0; i < res.length; i++) {
						if (res[i].material_in == null) {
							res[i].material_in_name = "";
						}
						if (res[i].material_out == null) {
							res[i].material_out_name = "";
						}
						res[i].label =
							res[i].material_in_name +
							"->" +
							res[i].material_out_name;
					}
					this.routeOptions = res;
				});
		},
		getShiftOptions() {
			this.$API.mtm.shift.list.req({ page: 0 }).then((res) => {
				this.shiftOptions = res;
			});
		},
		getWMaterial() {
			let that = this;
			that.$API.wpm.wmaterial.list
				.req({
					material__in: that.material_in,
					belong_dept__name: "6车间",
					page: 0,
				})
				.then((res) => {
					if (res.length > 0) {
						let arr = [];
						res.forEach((item) => {
							arr.push(item.batch);
						});
						that.wmaterialOptions = arr;
					}
				});
		},
		//获取车间物料批次号
		getBatchs() {
			let that = this;
			this.$API.wpm.wmaterial.batchs
				.req({ belong_dept_name: "6车间" })
				.then((res) => {
					that.batchOptions = res;
				});
		},
		getUserList() {
			let that = this;
			this.$API.system.user.list
				.req({ belong_dept__name: "6车间", page: 0 })
				.then((res) => {
					that.userList = res;
				});
		},

		//获取设备列表
		getEquipment() {
			this.$API.em.equipment.list
				.req({ page: 0, type: 10 })
				.then((res) => {
					this.equipmentOptions = res;
				});
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		countNotOkSun() {
			this.form.count_notok =
				Number(this.form.count_n_hs) +
				Number(this.form.count_n_cs) +
				Number(this.form.count_n_zz) +
				Number(this.form.count_n_tw) +
				Number(this.form.count_n_zdd) +
				Number(this.form.count_n_d) +
				Number(this.form.count_n_dl) +
				Number(this.form.count_n_qp) +
				Number(this.form.count_n_bl) +
				Number(this.form.count_n_hw) +
				Number(this.form.count_n_yp) +
				Number(this.form.count_n_bp) +
				Number(this.form.count_n_sc) +
				Number(this.form.count_n_zjd) +
				Number(this.form.count_n_zjx) +
				Number(this.form.count_n_qx) +
				Number(this.form.count_n_js) +
				Number(this.form.count_n_tydd) +
				Number(this.form.count_n_sw) +
				Number(this.form.count_n_bhpcd) +
				Number(this.form.count_n_wq) +
				Number(this.form.count_n_zw);
		},
		//提交
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let sun = this.form.count_ok + this.form.count_notok;
					if (sun == this.form.count_real) {
						if (this.mode == "add") {
							this.form.mtask = this.mtask;
							this.form.handle_date = this.handledate;
							this.$API.wpm.mlog.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.mlog.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;
								});
						}
					} else {
						this.isSaveing = false;
						this.$message.error(
							"合格总数与不合格总数与加工数量不对等"
						);
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.getRouteOptions();
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
		//
		countChange() {
			if (
				this.form.count_real !== null &&
				this.form.count_real !== "" &&
				this.form.count_real !== 0
			) {
				if (
					this.form.count_ok !== null &&
					this.form.count_ok !== "" &&
					this.form.count_ok !== 0
				) {
					this.form.rate =
						(this.form.count_ok / this.form.count_real) * 100 + "%";
				} else {
					this.form.rate = null;
				}
			} else {
				this.form.rate = null;
			}
		},
	},
};
</script>
<style scoped>
.formTitle {
	margin-bottom: 10px;
	font-weight: 600;
}
</style>
