<template>
	<el-dialog
		:title="modeTitle"
		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="检验类型">
								<el-select
									v-model="form.type2"
									placeholder="检验类型"
									style="width: 100%"
								>
								<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="物料批次" prop="batch">
								<el-input v-model="form.batch" disabled></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="总数">
								<el-input
									v-model="batchCount"
									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="batchCount"
									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"
									@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_ok_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>
					<el-divider style="margin-top:0"></el-divider>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="厚度">
								<el-input-number
									controls-position="right"
									v-model="count_notok_json.count_n_hd"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_wj"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_yd"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_txd"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_qp"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_swen"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_bb"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_xbb"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_hs"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_md"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_ps"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></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_notok_json.count_n_qt"
									style="width: 100%"
									precision="0"
									@change="handleCountChange"
								></el-input-number>
							</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"],
	props: {
		mgroup: {
			type: String,
			default: "",
		},
		itemObj:{
			type: Object,
			default: null,
		}
	},
	data() {
		return {
			mode:'',
			modeTitle: '',
			loading: false,
			form: {
				type:'process',
				type2:10,
				test_date: "",
				batch: "",
				count: null,
				count_ok: 0,
				count_sampling_ok:'',
				count_notok: 0,
				count_notok_json:{}
			},
			count_ok_rate:100,
			count_notok_json:{
				//尺寸
				count_n_wj: 0,
				count_n_yd: 0,
				count_n_txd: 0,
				count_n_hd: 0,
				//外观和内质
				count_n_qp: 0,
				count_n_swen: 0,
				count_n_hs: 0,
				count_n_md: 0,
				count_n_bb:0,
				count_n_xbb:0,
				count_n_ps: 0,
				count_n_qt:0,
			},
			rules: {
				test_date: [{required: true,message: "请选择检验日期",trigger: "blur"}],
				batch: [{required: true,message: "请选择物料批次",trigger: "blur"}],
				test_user: [{required: true,message: "请选择物料批次",trigger: "blur"}],
			},
			options: [],
			userList : [],
			typeOption:[
				{name:'全检',value:20},
				{name:'抽检',value:10},
			],
			selectionFilters: [],
			batchCount:null,
			formCount:null,
			visible: false,
			supplier:null,
			isSaveing: false,
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.batchCount = this.itemObj.count;
		this.form.batch = this.itemObj.batch;
		this.form.wm = this.itemObj.id;
		this.form.count = this.itemObj.count;
		this.form.supplier = this.itemObj.supplier;
		this.deptID = this.$TOOL.data.get('gx_deptID');
		this.getUsers();
	},
	methods: {
		//显示
		open(mode = "抽检") {
			this.mode = mode;
			this.modeTitle = mode+'抽检';
			this.visible = true;
			
			return this;
		},
		getUsers(){
			let that = this;
			let userList = [];
			that.$API.system.user.list
			.req({ page: 0, posts__code__contains: "check" })
			.then((res) => {
				userList = res;
				that.$API.system.user.list.req({ depts: that.deptID, page: 0 })
				.then((res2) => {
					res2.forEach((item) => {
						userList.push(item);
					});
					that.userList = userList ;
				});
			});
		},
		handleCountChange(type){
			this.form.count_notok = 
			this.count_notok_json.count_n_wj+
			this.count_notok_json.count_n_yd+
			this.count_notok_json.count_n_txd+
			this.count_notok_json.count_n_hd+
			this.count_notok_json.count_n_qp+
			this.count_notok_json.count_n_swen+
			this.count_notok_json.count_n_hs+
			this.count_notok_json.count_n_bb+
			this.count_notok_json.count_n_xbb+
			this.count_notok_json.count_n_md+
			this.count_notok_json.count_n_ps+
			this.count_notok_json.count_n_qt;
			this.handleCountNotokChange();
		},
		handleCountNotokChange(){
			if(this.form.type2==10){//抽检
				this.form.count_sampling_ok = this.form.count_sampling - this.form.count_notok;
				this.count_ok_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_ok_rate = ((this.form.count_ok/this.form.count)*100).toFixed(2);
			}
		},
		//获取物料批次
		getMaterialBatch() {
			let that = this;
			let obj = {page: 0, count__gte: 0, count_xtest__gte: 0};
			//工段
			obj.mgroupx = that.mgroup;
			that.$API.wpm.wmaterial.list
				.req(obj)
				.then((res) => {
					that.options = res;
				});
		},
		//提交
		submit() {
			let that = this;
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					let count_notok = 
					this.count_notok_json.count_n_wj+
					this.count_notok_json.count_n_yd+
					this.count_notok_json.count_n_txd+
					this.count_notok_json.count_n_hd+
					this.count_notok_json.count_n_qp+
					this.count_notok_json.count_n_swen+
					this.count_notok_json.count_n_hs+
					this.count_notok_json.count_n_bb+
					this.count_notok_json.count_n_xbb+
					this.count_notok_json.count_n_md+
					this.count_notok_json.count_n_ps+
					this.count_notok_json.count_n_qt;
					if(count_notok>that.form.count_notok){
						that.$notify.error("不合格数量有问题");
						that.isSaveing = false;
					}else{
						if(count_notok<that.form.count_notok){
							that.count_notok_json.count_n_qt = that.form.count_notok-count_notok;
						}
						if(that.supplier!== null&&that.form.type2==10){
							that.form.need_update_wm = false;
						}
						// that.form.count_ok = that.form.count-that.form.count_notok;
						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;
							})
					}
					
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.formCount = this.form.count;
			// this.form.test_group = this.form.split(",");
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
