<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		style="width: 80%;"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 0 20px 0">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-position="right"
					label-width="100px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="关联任务">
								<el-select
									v-model="form.mtask"
									placeholder="关联任务"
									clearable
									style="width: 100%"
									@change="getMaterial"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.number"
										:value="item.id"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="批次号" prop="wm_in">
								<el-select
									v-model="form.wm_in"
									placeholder="物料"
									clearable
									filterable
									class="width-100"
									@change="materialBatchChange"
								>
									<el-option
										v-for="item in materialOptions"
										:key="item.id"
										:label="item.batch"
										:value="item.id"
									>
										<div
											style="
												display: flex;
												justify-content: space-between;
											"
										>
											<span>{{ item.batch }}</span
											><span>{{ item.count }}</span>
										</div>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="mgroupName=='黑化'||mgroupName=='退火'">
							<el-form-item label="进炉数">
								<el-input-number
									v-model="batch_count"
									class="width-100"
									:disabled="true"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="mgroupMtype==10">
							<el-form-item :label="mgroupMtype==10?'领用数量':'批次总数'">
								<el-input-number
									v-model="form.count_use"
									:min="0"
									class="width-100"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="mgroupMtype==10">
							<el-form-item label="加工前不良" prop="count_pn_jgqbl">
								<el-input-number
									v-model="form.count_pn_jgqbl"
									:min="0"
									:disabled="true"
									style="width: 100%"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="备注">
								<el-input v-model="form.note"/>
							</el-form-item>
						</el-col>
					</el-row>
					<el-divider v-if="(mgroupName=='黑化'||mgroupName=='退火')&&cellsList.length>0"></el-divider>
					<el-button 
						v-if="mgroupName=='黑化'||mgroupName=='退火'" 
						icon="el-icon-plus"
						type="primary"
						@click="cellsAdd"
					></el-button>
					<template v-if="mgroupName=='黑化'||mgroupName=='退火'">
						<el-row v-for="(item,$index) in cellsList" :key="item">
							<el-col style="display: flex;">
								<el-form-item label="第" label-width="50">
									<el-input-number
										v-model="item.start"
										:min="1"
										placeholder="层数"
										controls-position="right"
									/>
								</el-form-item>
								<el-form-item label="层到第" label-width="65">
									<el-input-number
										v-model="item.end"
										:min="1"
										placeholder="层数"
										controls-position="right"
									/>
								</el-form-item>
								<el-form-item label="层，总数:" label-width="80">
									<el-input-number
									v-model="item.count_t"
									:min="0"
									placeholder="总数量"
									controls-position="right"
									@change="countTotalChange"
									/>
								</el-form-item>
								<el-button
									type="danger" 
									style="margin: 0 20px;" 
									@click="cellsDel($index)">删除</el-button>
							</el-col>
						</el-row>
					</template>
					<el-divider v-if="qct_defects.length>0"></el-divider>
					<el-row>
						<el-col :md="12" :sm="24" v-for="item in qct_defects" :key="item.id">
							<el-form-item :label="item.defect_name">
								<el-input-number
									v-model="defectinform[item.defect_name]"
									:min="0"
									style="width: 100%"
									controls-position="right"
									@change="countChange"
								/>
							</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>
const defaultForm = {
	mlog: "",
	mtask: "",
	batch: "",
	count_pn_jgqbl:0,
	count_use: 0,
};

export default {
	props: {
		mlog: {
			type: String,
			default: "",
		},
		mgroup: {
			type: String,
			default: "",
		},
		mgroupName:{
			type: String,
			default: "",
		},
		materialIn: {
			type: String,
			default: "",
		},
		codeText: {
			type: String,
			default: "",
		},
		is_fix: {
			type: Boolean,
			default: false,
		},
		mgroupMtype:{
			type: String,
			default: "",
		}
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				wm_in: [
					{
						required: true,
						message: "请填写批次号",
						trigger: "blur",
					},
				],
				count_use: [
					{
						required: true,
						message: "请填写领用数量",
						trigger: "blur",
					},
				],
			},
			// cells:0,//层数
			batch_count:null,
			// count_cell:0,//每层数量
			// cell_start:1,//开始层数
			codeBatch:"",
			options: [],
			cellsList:[],
			qct_defects:[],
			materialOptions: [],
			defectinform:{},
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
		};
	},
	mounted() {
		let that = this;
		if(that.codeText!==''){
			let id = that.codeText.split('#')[1];
			this.$API.cm.labelmat.item.req(id).then((res) => {
				that.codeBatch = res.batch;
				this.getMaterial();
			}).catch((err) => {
			    that.getMaterial();
			})
		}else{
			that.getMaterial();
		}
		this.getdefects();
		// this.getMtask();
	},
	methods: {
		open(mode="add",data) {
			this.visible = true;
			this.form.mtask = "";
			this.form.wm_in = "";
			this.form.count_use = 0;
			this.form.parent = "";
			if(data){
				this.form = data;
				this.cellsList = data.count_json_from;
			}
		},
		getMtask() {
			let that = this;
			this.$API.pm.mtask.list.req({ page: 0, mgroup: that.mgroup, state: 20 }).then((res) => {
				that.options = res;
			});
		},
		//获取车间物料
		getMaterial() {
			let that = this;
			let obj = {};
			obj.mtaskx =that.form.mtask;
			obj.mgroup = that.mgroup;
			obj.page =0;
			if(that.is_fix){
				obj.state =30;
			}else{
				obj.material =that.materialIn;
				obj.state =10;
				obj.tag = 'todo';
			}
			this.$nextTick(()=>{
				this.$API.wpm.wmaterial.list.req(obj).then((res) => {
					that.materialOptions = res;
					res.forEach(item => {
						if(item.batch==that.codeBatch){
							that.form.wm_in = item.id;
							that.form.count_use = item.count;
							that.batch_count = item.count;
						}
						if(that.form.wm_in == item.id){
							that.codeBatch=item.batch;
							that.batch_count = item.count;
						}
					});
				});
			})
		},
		getdefects(){
			let that = this;
			that.$API.qm.qct.list.req({qctmat__material:that.materialIn,page:0,tags:'process'}).then((res) => {
				if(res.length>0){
					that.$API.qm.qct.item.req(res[0].id).then((res) => {
						res.qct_defects.forEach((item) => {
							that.defectinform[item.defect_name] = 0;
						})
						that.qct_defects=res.qct_defects;
						let count_pn_jgqbl = 0;
						if(data.mlogbindefect.length>0){
							that.qct_defects.forEach(item => {
								data.mlogbindefect.forEach(item2=>{
									if(item.defect==item2.defect){
										count_pn_jgqbl+=item2.count;
										that.defectinform[item.defect_name] = item2.count;
									}
								})
							})
						}
					})
				}
			})
		},
		materialBatchChange(){
			let that = this;
			that.materialOptions.forEach(item => {
				if(item.id==that.form.wm_in){
					that.form.count_use = item.count;
					that.batch_count = item.count;
				}
			});
		},
		//添加层组
		cellsAdd(){
			let that = this;
			let obj = {start:null, end:null,count_t:null};
			that.cellsList.push(obj)
		},
		//删除层组
		cellsDel(index){
			this.cellsList.splice(index,1)
		},
		countTotalChange(){
			let that = this;
			let sum = 0,sumTotal=0;
			that.cellsList.forEach(item=>{
				sum+=item.count_t;
				if(sum>that.batch_count){
					item.count_t=0;
					that.$message.error('合计数量超过该批次总数量，请重新分配第'+item.start+'层到第'+item.end+'层的数量');
				}else{
					sumTotal+=item.count_t;
				}
			})
			that.form.count_use = sumTotal;
			
		},
		cellStartChanges(){
			let that = this;
			let remaind = (that.form.count_use-that.form.count_pn_jgqbl)%that.count_cell;
			that.cellsList = [];
			for(let i=0;i<that.cells;i++){
				let obj = {};
				obj.floor=that.cell_start+i;
				if((that.cells-i)==1&&remaind>0){
					obj.count_use = remaind;
				}else{
					obj.count_use = that.count_cell;
				}
				that.cellsList.push(obj)
			}
		},
		countCellChanges(){
			this.cells =Math.ceil((this.form.count_use-this.form.count_pn_jgqbl)/this.count_cell);
			this.cellStartChanges();
		},
		countChange() {
			let that = this;
			that.form.count_pn_jgqbl = 0;
			that.qct_defects.forEach(item => {
				if(item.defect_okcate==30){
			    	that.form.count_pn_jgqbl += that.defectinform[item.defect_name];
				}
			})
			that.countCellChanges();
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.mlog = that.mlog;
					if(that.mgroupMtype==10&&that.cellsList.length>0){
						that.form.count_json_from = that.cellsList;
					}
					let count_pn_jgqbl = 0,mlogbindefect=[];
					that.qct_defects.forEach(item => {
						count_pn_jgqbl += that.defectinform[item.defect_name] ;
						let obj = {};
						obj.defect = item.defect;
						obj.count = that.defectinform[item.defect_name];
						mlogbindefect.push(obj);
					})
					that.form.mlogbindefect = mlogbindefect;
					that.form.count_pn_jgqbl = count_pn_jgqbl;
					that.$API.wpm.mlogb.in.req(that.form).then((res) => {
						that.isSaveing = false;
						that.$emit("success");
						that.visible = false;
						that.$message.success("操作成功");
					}).catch((err) => {
						that.isSaveing = false;
					});
				}
			});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
