<template>
	<el-dialog
		:title="modeTitle"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="close"
	>
		<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">
							<el-form-item label="抽检数量">
								<el-input-number
									:max="batchCount"
									controls-position="right"
									v-model="form.count"
									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-row>
						<el-col :md="12" :sm="24" v-for="item in qct_defects" :key="item.id">
							<el-form-item :label="item.name">
								<el-input-number
									:precision="0"
									style="width: 100%;"
									v-model="item.count"
									placeholder="请输入数量"
								></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: "",
		},
		mgroupName:{
			type: String,
			default: "",
		},
		itemObj:{
			type: Object,
			default: null,
		}
	},
	data() {
		return {
			mode:'',
			modeTitle: '抽检',
			loading: false,
			form: {
				type:'process',
				type2:10,
				test_date: "",
				batch: "",
				count: 0,
				wm: "",
				test_user: "",
				qct:"",
				need_update_wm:true,
				ftestworkdefect:[],
			},
			count_ok_rate:100,
			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},
			],
			qct_defects: [],
			defectlist:[],
			batchCount:null,
			formCount:null,
			visible: false,
			supplier:null,
			isSaveing: false,
			addTemplate:{},
		};
	},
	mounted() {
		this.form.count =this.batchCount = this.itemObj.count;
		this.form.batch = this.itemObj.batch;
		this.form.wm = this.itemObj.id;
		this.deptID = this.$TOOL.data.get('bx_deptID');
		this.getUsers();
		if(this.itemObj.material_.tracking==10){
			this.getdefects();
		}
	},
	methods: {
		//显示
		open(mode = "抽检") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		getUsers(){
			let that = this;
			let userList = [];
			that.$API.system.user.list.req({ depts: that.deptID, page: 0 }).then((res2) => {
				res2.forEach((item) => {
					userList.push(item);
				});
				that.userList = userList ;
			});
		},
		//获取不合格项
		getdefects(){
			let that = this;
			that.$API.qm.qct.list.req({ page: 0, qctmat__material: that.itemObj.material }).then((res) => {
				if(res.length>0){
					that.form.qct = res[0].id;
					that.$API.qm.qct.item.req(res[0].id).then((res) => {
						let qct_defects= [];
						res.qct_defects.forEach((item) => {
						    let obj = {};
						    obj.name = item.defect_name;
							obj.defect = item.defect;
						    obj.count = 0;
						    qct_defects.push(obj);
						})
						that.qct_defects = qct_defects;
						console.log('that.qct_defects',that.qct_defects);
					})
				}
			});
		},
		handleCountNotokChange(){
			if(this.form.type2==10){//抽检
				this.form.count_ok = this.form.count;
			}else if(this.form.type2==20){//全检
				this.form.count_ok = this.form.count;
			}
		},
		//提交
		submit() {
			let that = this;
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.ftestworkdefect = [];
					that.qct_defects.forEach((item) => {
					    let obj = {};
					    obj.defect = item.defect;
						obj.count = item.count;
					    that.form.ftestworkdefect.push(obj);
					})
					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;
					})
				}
			});
		},
		close(){
			this.visible = false;
			this.$emit('closed')
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.formCount = this.form.count;
			// this.form.test_group = this.form.split(",");
		},
	},
};
</script>

<style></style>
