<template>
	<el-dialog
		title="中检"
		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="100px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验类型">
								<el-select
									v-model="form.type2"
									placeholder="检验类型"
									style="width: 100%"
									@testType="handleTypeChange"
								>
								<el-option
										v-for="item in typeOption"
										:key="item.value"
										:label="item.name"
										:value="item.value"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="物料批次">
								<el-input v-model="wm_batch" disabled></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type2==20">
							<el-form-item label="检验数量">
								<el-input-number
									:max="originData.count"
									controls-position="right"
									v-model="form.count"
									style="width: 100%"
									:precision="0"
									@change="handleCountNotokChange"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type2==20">
							<el-form-item label="合格数量">
								<el-input v-model="form.count_ok" disabled></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type2==10">
							<el-form-item label="抽检数量">
								<el-input-number
									:max="batchCount"
									controls-position="right"
									v-model="form.count_sampling"
									style="width: 100%"
									:precision="0"
									@change="handleCountNotokChange"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="form.type2==10">
							<el-form-item label="抽检合格数">
								<el-input-number
									controls-position="right"
									v-model="form.count_sampling_ok"
									style="width: 100%"
									disabled
									:precision="0"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="不合格数量">
								<el-input-number
									controls-position="right"
									v-model="form.count_notok"
									style="width: 100%"
									:precision="0"
									disabled
									@change="handleCountNotokChange"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="合格率(%)">
								<el-input-number
									controls-position="right"
									v-model="count_rate"
									style="width: 100%"
									:precision="0"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验日期" prop="test_date">
								<el-date-picker
									v-model="form.test_date"
									type="date"
									value-format="YYYY-MM-DD"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验人" prop="test_user">
								<el-select
									v-model="form.test_user"
									placeholder="检验人"
									clearable
									filterable
									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-row>
					<div class="formTitle">不合格原因及数量:</div>
					<!-- 7车间 -->
					<el-row v-if="deptName == '7车间'">
						<el-col :md="12" :sm="24">
							<el-form-item label="炸纹">
								<el-input-number
									v-model="count_notok_json.count_n_zw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="炸纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="炸头">
								<el-input-number
									v-model="count_notok_json.count_n_zt"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="炸头"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="条纹">
								<el-input-number
									v-model="count_notok_json.count_n_tw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="条纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="气泡">
								<el-input-number
									v-model="count_notok_json.count_n_qp"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="气泡"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						
						<el-col :md="12" :sm="24">
							<el-form-item label="扁">
								<el-input-number
									v-model="count_notok_json.count_n_b"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="扁"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="其他">
								<el-input-number
									v-model="count_notok_json.count_n_qt"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="其他"
									@change="handleCountChange"
								>
								</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>
					<!-- 10车间 -->
					<el-row v-if="deptName == '10车间'">
						<el-col :md="12" :sm="24">
							<el-form-item label="椭圆/弯曲">
								<el-input-number
									v-model="count_notok_json.count_n_wq"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="椭圆/弯曲"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="条纹">
								<el-input-number
									v-model="count_notok_json.count_n_tw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="条纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="断裂">
								<el-input-number
									v-model="count_notok_json.count_n_dl"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="断裂"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						
						<el-col :md="12" :sm="24">
							<el-form-item label="偏壁">
								<el-input-number
									v-model="count_notok_json.count_n_pb"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="偏壁"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="大小头">
								<el-input-number
									v-model="count_notok_json.count_n_dxt"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="大小头"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="结石">
								<el-input-number
									v-model="count_notok_json.count_n_js"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="结石"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="气线">
								<el-input-number
									v-model="count_notok_json.count_n_qx"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="气线"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="其他">
								<el-input-number
									v-model="count_notok_json.count_n_qt"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="其他"
									@change="handleCountChange"
								>
								</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>
					<!-- 6车间 -->
					<el-row v-if="deptName == '6车间'">
						<el-col :md="12" :sm="24">
							<el-form-item label="划伤">
								<el-input-number
									v-model="count_notok_json.count_n_hs"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="划伤"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="挫伤">
								<el-input-number
									v-model="count_notok_json.count_n_cs"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="挫伤"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="杂质">
								<el-input-number
									v-model="count_notok_json.count_n_zz"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="杂质"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="条纹">
								<el-input-number
									v-model="count_notok_json.count_n_tw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="条纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="锥度大">
								<el-input-number
									v-model="count_notok_json.count_n_zdd"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="锥度大"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="短">
								<el-input-number
									v-model="count_notok_json.count_n_d"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="短"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="炸纹">
								<el-input-number
									v-model="count_notok_json.count_n_zw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="炸纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="断裂">
								<el-input-number
									v-model="count_notok_json.count_n_dl"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="断裂"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="气泡">
								<el-input-number
									v-model="count_notok_json.count_n_qp"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="气泡"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="不亮">
								<el-input-number
									v-model="count_notok_json.count_n_bl"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="不亮"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="横纹">
								<el-input-number
									v-model="count_notok_json.count_n_hw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="横纹"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="有皮">
								<el-input-number
									v-model="count_notok_json.count_n_yp"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="有皮"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="爆皮">
								<el-input-number
									v-model="count_notok_json.count_n_bp"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="爆皮"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="色差">
								<el-input-number
									v-model="count_notok_json.count_n_sc"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="色差"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="直径大">
								<el-input-number
									v-model="count_notok_json.count_n_zjd"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="直径大"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="直径小">
								<el-input-number
									v-model="count_notok_json.count_n_zjx"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="直径小"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="气线">
								<el-input-number
									v-model="count_notok_json.count_n_qx"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="气线"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="结石">
								<el-input-number
									v-model="count_notok_json.count_n_js"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="结石"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="椭圆度大">
								<el-input-number
									v-model="count_notok_json.count_n_tydd"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="椭圆度大"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="水雾">
								<el-input-number
									v-model="count_notok_json.count_n_sw"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="水雾"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="壁厚偏差大">
								<el-input-number
									v-model="count_notok_json.count_n_bhpcd"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="壁厚偏差大"
									@change="handleCountChange"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="弯">
								<el-input-number
									v-model="count_notok_json.count_n_wq"
									controls-position="right"
									:min="0"
									:step="1"
									:step-strictly="true"
									style="width: 100%"
									placeholder="弯"
									@change="handleCountChange"
								>
								</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-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 {
			deptName: "",
			mode:'',
			loading: false,
			form: {
				type:'process',
				type2:20,
				test_date: "",
				count: 0,
				count_ok: 0,
				count_notok: 0,
				count_notok_json:{},
				ftestworkdefect:[],
			},
			count_rate:100,
			count_notok_json:{
				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_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_zjd: 0,
				count_n_zjx: 0,
				count_n_qx: 0,
				count_n_js: 0,
				count_n_tydd: 0,
				count_n_sw: 0,
				count_n_bhpcd: 0,
				count_n_wq: 0,
				//7
				count_n_zt:0,
				count_n_b:0,
				count_n_qt:0,
				//10
				count_n_pb:0,
				count_n_dxt:0,
			},
			rules: {
				test_date: [{required: true,message: "请选择检验日期",trigger: "blur"}],
				batch: [{required: true,message: "请选择物料批次",trigger: "blur"}],
				test_user: [{required: true,message: "请选择物料批次",trigger: "blur"}],
			},
			typeOption:[
				{name:'全检',value:20},
				{name:'抽检',value:10},
			],
			originData:{},
			userList : [],
			selectionFilters: [],
			batchCount:null,
			formCount:null,
			visible: false,
			supplier:null,
			isSaveing: false,
		};
	},
	mounted() {
		this.getUsers();
	},
	methods: {
		//显示
		open(deptName) {
			this.deptName = deptName;
			this.visible = true;
			return this;
		},
        //表单注入数据
		setData(data,) {
			let that = this;
			Object.assign(that.originData, data);
			that.form.wm = data.id;
            that.count_rate = 100;
			that.wm_batch = data.batch;
			that.form.count_notok = 0;
			that.form.count = that.form.count_ok = data.count;
		},
		handleTypeChange(){
			if(this.form.type2==10){//抽检
				this.form.count_sampling = this.originData.count;
				this.form.count_sampling_ok = this.originData.count;
			}else if(this.form.type2==20){
				this.form.count = this.originData.count;
				this.form.count_ok = this.originData.count;
			}
		},
		getUsers(){
			let that = this;
			that.$API.system.user.list.req({ page: 0, posts__code__contains: "check" }).then((res) => {
				that.userList = res ;
			});
		},
		handleCountChange(){
			this.form.count_notok = 
			this.count_notok_json.count_n_hs+
			this.count_notok_json.count_n_cs+
			this.count_notok_json.count_n_zz+
			this.count_notok_json.count_n_tw+
			this.count_notok_json.count_n_zdd+
			this.count_notok_json.count_n_d+
			this.count_notok_json.count_n_zw+
			this.count_notok_json.count_n_dl+
			this.count_notok_json.count_n_qp+
			this.count_notok_json.count_n_bl+
			this.count_notok_json.count_n_hw+
			this.count_notok_json.count_n_yp+
			this.count_notok_json.count_n_bp+
			this.count_notok_json.count_n_sc+
			this.count_notok_json.count_n_zjd+
			this.count_notok_json.count_n_zjx+
			this.count_notok_json.count_n_qx+
			this.count_notok_json.count_n_js+
			this.count_notok_json.count_n_tydd+
			this.count_notok_json.count_n_sw+
			this.count_notok_json.count_n_bhpcd+
			this.count_notok_json.count_n_wq
			this.count_notok_json.count_n_zt
			this.count_notok_json.count_n_b
			this.count_notok_json.count_n_qt
			this.count_notok_json.count_n_pb
			this.count_notok_json.count_n_dxt;
			this.handleCountNotokChange();
		},
		handleCountNotokChange(){
			if(this.form.type2==10){//抽检
				this.form.count_sampling_ok = this.form.count_sampling - this.form.count_notok;
				this.count_rate = ((this.form.count_sampling_ok/this.form.count_sampling)*100).toFixed(2);
			}else if(this.form.type2==20){//全检
				this.form.count_ok = this.form.count - this.form.count_notok;
				this.count_rate = ((this.form.count_ok/this.form.count)*100).toFixed(2);
			}
		},
		//提交
		submit() {
			let that = this;
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					let count_notok = 
					that.count_notok_json.count_n_hs+
					that.count_notok_json.count_n_cs+
					that.count_notok_json.count_n_zz+
					that.count_notok_json.count_n_tw+
					that.count_notok_json.count_n_zdd+
					that.count_notok_json.count_n_d+
					that.count_notok_json.count_n_zw+
					that.count_notok_json.count_n_dl+
					that.count_notok_json.count_n_qp+
					that.count_notok_json.count_n_bl+
					that.count_notok_json.count_n_hw+
					that.count_notok_json.count_n_yp+
					that.count_notok_json.count_n_bp+
					that.count_notok_json.count_n_sc+
					that.count_notok_json.count_n_zjd+
					that.count_notok_json.count_n_zjx+
					that.count_notok_json.count_n_qx+
					that.count_notok_json.count_n_js+
					that.count_notok_json.count_n_tydd+
					that.count_notok_json.count_n_sw+
					that.count_notok_json.count_n_bhpcd+
					that.count_notok_json.count_n_wq+
					that.count_notok_json.count_n_zt
					that.count_notok_json.count_n_b
					that.count_notok_json.count_n_qt
					that.count_notok_json.count_n_pb
					that.count_notok_json.count_n_dxt;
					if(count_notok>that.form.count_notok){
						that.$notify.error("不合格数量有问题");
						that.isSaveing = false;
					}else{
						if(that.form.type2==10){//抽检
							that.form.count_sampling_ok = that.form.count_sampling - that.form.count_notok;
						}else{//全检
							that.form.count_ok = that.form.count - that.form.count_notok;
						}
						that.form.count_notok_json = that.count_notok_json;
						console.log('that.form',that.form)
						that.$API.qm.ftestwork.create.req(that.form).then((res) => {
							that.$API.qm.ftestwork.submit.req(res.id).then((res) => {
								that.isSaveing = false;
								that.visible = false;
								that.$emit("success");
								that.$message.success("操作成功");
							})	
						}).catch( err=>{
							that.isSaveing = false;
                        })
					}
				}
			});
		},
	},
};
</script>

<style></style>
