<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-select
					v-model="query.warehouse"
					clearable
					placeholder="所在仓库"
					@change="handleQuery"
					style="width: 150px"
				>
					<el-option
						v-for="item in warehouseOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</div>
			<div class="right-panel">
				<el-input
					v-model="query.search"
					placeholder="物料名"
					clearable
					style="margin-right: 5px"
				></el-input>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				stripe
				:params="params"
			>
				<el-table-column type="index" width="50" />
				<el-table-column label="物料批次" prop="batch">
				</el-table-column>
				<el-table-column label="物料名称" prop="material_name">
				</el-table-column>
				<el-table-column label="类型" min-width="100"  show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="getType(scope.row.state)">{{ state_[scope.row.state] }}</el-tag>
						<el-tag type="info" v-if="scope.row.defect_name">{{ scope.row.defect_name }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="仓库" width="80" prop="warehouse_name">
				</el-table-column>
				<el-table-column label="物料存量">
					<template #default="scope">
						<el-link @click="handleWpr(scope.row)" type="primary">{{ scope.row.count }}</el-link>
					</template>
				</el-table-column>
				<el-table-column label="有效期" prop="expiration_date">
				</el-table-column>
				<el-table-column label="更新时间" prop="update_time">
				</el-table-column>
				<el-table-column width="80">
					<template #default="scope">
						<!-- <el-button type="primary" link v-if="project_code == 'gx'" @click="mbCheck(scope.row)">检验</el-button>
						<el-button type="success" link v-if="project_code == 'gx'" @click="mbCheckList(scope.row)">检验记录</el-button> -->
						<el-button type="text"  @click="printMaterial(scope.row,'mb')">打签</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<print-dialog
			v-if="print_m"
			ref="printmaterial"
			:mId="wmId"
			:mtype="wmtype"
			:apiObj="apiObjPrint"
		></print-dialog>
		<!-- 关联产品 -->
		<el-drawer v-model="wprVisibleDrawer" :size="'70%'">
			<template #header>
				<h4>关联产品</h4>
			</template>
			<template #default>
				<scTable ref="wprTable" hidePagination hideDo :data="wprList">
					<el-table-column type="index" width="50"></el-table-column>
					<el-table-column label="产品编号" prop="number" min-width="100px"></el-table-column>
					<el-table-column label="缺陷项"  min-width="200px">
						<template #default="scope">
							<span v-for="item in scope.row.wprdefect" :key="item.id">
								<el-tag type="warning">{{ item.defect_name }}</el-tag>
							</span>
						</template>
					</el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="120"
					>
						<template #default="scope">
							<el-link :underline="false"  @click="printMaterial(scope.row,'wpr')" type="primary">打签</el-link>
						</template>
					</el-table-column>
				</scTable>
			</template>
			<template #footer>
			<div style="flex: auto">
				<el-button @click="wprVisibleDrawer=false">关闭</el-button>
			</div>
			</template>
		</el-drawer>
		<!-- 检验记录 -->
		<el-drawer v-model="checkVisibleDrawer" :size="'70%'">
			<template #header>
				<h4>检验记录</h4>
			</template>
			<template #default>
				<scTable ref="wprTable" hidePagination hideDo :data="checkList">
					<el-table-column type="index" width="50"></el-table-column>
					<el-table-column label="物料名称" prop="material_name" min-width="100px"></el-table-column>
					<el-table-column label="批次号" prop="batch"></el-table-column>
					<el-table-column label="总数" prop="count"></el-table-column>
					<el-table-column label="合格数" prop="count_ok"></el-table-column>
					<el-table-column label="不合格数" prop="count_notok"></el-table-column>
					<el-table-column label="检验员" prop="test_user_name"></el-table-column>
					<el-table-column label="检验日期" prop="test_date"></el-table-column>
					<el-table-column label="备注" prop="note"></el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="80"
					>
						<template #default="scope">
							<el-link :underline="false"  @click="checkDelete(scope.row)" type="danger">删除</el-link>
						</template>
					</el-table-column>
				</scTable>
			</template>
			<template #footer>
			<div style="flex: auto">
				<el-button type="warning" @click="checkVisibleDrawer=false">关闭</el-button>
			</div>
			</template>
		</el-drawer>
		<!-- 入厂检验 -->
		<el-dialog
			title="检验"
			v-model="visible"
			:size="1200"
			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="物料名称:">
									{{ objitem.material_name }}
								</el-form-item>
							</el-col>
							<el-col :md="12" :sm="24">
								<el-form-item label="批次号:">
									{{ objitem.batch }}
								</el-form-item>
							</el-col>
							<el-col :md="12" :sm="24">
								<el-form-item label='总数'>
									<el-input-number
										v-model="form.count"
										disabled
										style="width: 100%"
										controls-position="right"
									>
									</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="合格数">
									<el-input-number
										v-model="form.count_ok"
										disabled
										style="width: 100%"
										controls-position="right"
									>
									</el-input-number>
								</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-col v-for="item in qct_defects" :key="item.id" :md="12" :sm="24">
								<el-form-item :label="item.defect_name">
									<el-input-number
										v-model="form[item.defect_name]"
										:min="0"
										style="width: 100%"
										@change="changeDefect(item)"
										controls-position="right"
									>
									</el-input-number>
								</el-form-item>
							</el-col>
							<el-col :md="12" :sm="24">
								<el-form-item label="备注">
									<el-input v-model="form.note"> </el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" :loading="isSaveing" @click="mbCheckSubmit">提交</el-button>
					<el-button @click="visible = false">取消</el-button>
				</el-footer>
			</el-container>
		</el-dialog>
	</el-container>
</template>
<script>
import printDialog from "./../template/printmaterial.vue";
export default {
	name: "materials",
	components: {
		printDialog
	},
	data() {
		return {
			apiObj: this.$API.inm.warehouse.batch,
			params: { count__gte: 1, material__type__in: "30" },
			selection: [],
			wprList:[],
			checkList:[],
			userList:[],
			rules: {
				test_date: [
					{ required: true, message: "请选择检验日期", trigger: "blur" },
				],
				test_user: [
					{ required: true, message: "请选择检验员", trigger: "blur" },
				],
			},
			form: {
				mb: "",
				type: "purin",
				test_date: "",
				count: 0,
				count_ok: 0,
				count_notok: 0,
				test_user: "",
				note: "",
			},
			query: {},
			objitem: {},
			warehouseOptions: [],
			wmtype:0,
			visible: false,
			print_m: false,
			checkVisibleDrawer:false,
			wprVisibleDrawer:false,
			materialsVisible:false,
			apiObjPrint:this.$API.cm.labelmat.fromMb,
			wmId:'',
			state_:{
				10:'合格',
				20:'不合格',
				30:'返工',
			},
			wprParams:{
				page:0
			},
			project_code:''
		};
	},
	mounted() {
		this.getIQCUser();
		this.getWarehouse();
		this.getQctDetail();
		this.project_code = this.$TOOL.data.get("BASE_INFO").base.base_code;
	},
	methods: {
		getType(type){
			if(type==10){
				return 'success'
			}else if(type==20){
				return 'danger'
			}else if(type==30){
				return 'warning'
			}
		},
		getWarehouse() {
			this.$API.inm.warehouse.list.req({ page: 0 }).then((res) => {
				this.warehouseOptions = res;
			});
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		handleWpr(row){
			let that = this;
			that.wprParams.mb = row.id;
			that.wprVisibleDrawer = true;
			that.$API.wpm.wpr.list.req(that.wprParams).then((res) => {
			    that.wprList = res;
			})
		},
		getIQCUser(){
			let that = this;
			that.$API.system.user.list.req({ page: 0, belong_dept__name: "IQC" }).then((res) => {
				that.userList = res;
			});
		},
		
		//根据物料关联的检测表获取检测项和不合格项
		getQctDetail(){
			let that = this;
			that.qct_defects = [];
			that.$API.qm.qct.list.req({ page:0 }).then((res) => {
				if(res.length>0){
					res.forEach((item) => {
						if(item.name=='入库检验表'){
							that.qctId = item.id;
							that.$API.qm.qct.item.req(that.qctId).then((res0) => {
								if(res0.qct_defects.length>0){
									that.qct_defects = res0.qct_defects;
									res0.qct_defects.forEach((item) => {
									    that.form[item.defect_name] = 0;
									})
								}
							})
						}
					})
				}
			});
		},
		changeDefect(item){
			let that = this;
			let count_notok = 0;
			that.form.count_notok = 0;
			that.qct_defects.forEach((item) => {
				if(that.form[item.defect_name]!==null){
				    count_notok += that.form[item.defect_name];
				}
			})
			that.form.count_notok = count_notok;
			that.form.count_ok = that.form.count - that.form.count_notok;
		},
		//原料库存检验
		mbCheck(row){
			let that = this;
			let date = new Date();
			let currentDate = that.$TOOL.dateFormat2(date);
			that.form.test_date = that.currentDate = currentDate;
			that.visible = true;
			that.objitem = row;	
			that.form.mb = row.id;
			that.form.count = that.form.count_ok = row.count;
		},
		mbCheckSubmit(){
			let that = this;
		    that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					let objitem = {} ;
					objitem.mb = that.form.mb;
					objitem.type = that.form.type;
					objitem.test_date = that.form.test_date;
					objitem.test_user = that.form.test_user;
					objitem.count = that.form.count;
					objitem.count_ok = that.form.count_ok;
					objitem.count_notok = that.form.count_notok;
					objitem.note = that.form.note;
					objitem.ftestworkdefect = [];
					that.qct_defects.forEach((item) => {
						if(that.form[item.defect_name]!==null){
						    objitem.ftestworkdefect.push({defect:item.defect,count:that.form[item.defect_name]});
						}
					})
					that.$API.qm.ftestwork.create.req(objitem).then((res) => {
						that.$API.qm.ftestwork.submit.req(res.id).then((res1) => {
						    that.isSaveing = false;
							that.visible = false;
							that.$message.success("检验成功");
						}).catch(err => {that.isSaveing = false;});
					}).catch(err => {that.isSaveing = false;});
				}
			})
		},
		getCheckList(){
			let that = this;
			that.$API.qm.ftestwork.list.req({ page:0,mb:that.mbId }).then((res) => {
				that.checkList = res;
			})
		},
		mbCheckList(row){
			let that = this;
			that.checkList = [];
			that.mbId = row.id;
			that.getCheckList();
			that.checkVisibleDrawer = true;
		},
		checkDelete(row){
			let that = this;
			that.$API.qm.ftestwork.delete.req(row.id).then((res) => {
				that.getCheckList();
			})
		},
		//打印打签
		printMaterial(row,type){
			let that = this;
			let printer_name=localStorage.getItem("printer_name");
			if(printer_name!==''&&printer_name!==null&&printer_name!==undefined){
				if(that.project_code!=='gz'){
					if(type=='mb'){
						let params = {};
						params.tid = row.id;
						params.label_template_name = '库存标签模板';
						params.extra_data={count:row.count};
						that.$API.cm.labelmat.fromMb.req(params).then((res) => {
							let obj = {};
							obj.printer_commands = res.commands;
							obj.printer_name = printer_name;
							let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
								that.$message.success("打印成功");
							});
						})
					}else{
						let params = {};
						let name = row.material_name.split('|')[0];
						params.label_template_name = '单件打印模板';
						params.data = {number:row.number,name:name};
						that.$API.cm.labeltemplate.commands.req(params).then((res) => {
							let obj = {};
							obj.printer_commands = res.commands;
							obj.printer_name = printer_name;
							let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
								that.$message.success("打印成功");
							});
						});
					}
				}else{
					that.wmId = row.id;
					that.wmtype = row.material_.type;
					that.print_m = true;
					this.$nextTick(() => {
						this.$refs.printmaterial.open();
					})
				}
			}else{
				that.$message.error("请先设置打印机");
			}
		},
	},
};
</script>
