<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<!-- 检验类型 -->
				<el-select 
					v-model="query.process_name" 
					placeholder="检验类型"
					@change="processChange">
					<el-option
						v-for="item in options"
						:key="item.value"
						:label="item.name"
						:value="item.value"
					>
					</el-option>
				</el-select>
				<!-- 检验人 -->
				<!-- <el-select v-model="query.test_user" 
					placeholder="检验人">
					<el-option
						v-for="item in userOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					>
					</el-option>
				</el-select>

				<el-select v-model="query.shift" 
					placeholder="班次">
					<el-option
						v-for="item in shiftOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					>
					</el-option>
				</el-select> -->
				<el-date-picker
					v-model="query.start_date"
					type="date"
					placeholder="查询日期"
					value-format="YYYY-MM-DD"
					style="width: 160px"
				>
				</el-date-picker>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
			<el-button @click="handleExport" type="primary">导出</el-button>
		</el-header>
		<el-main class="nopadding">
			<scTable 
			:data="tableData" 
			id="exportDiv"
			stripe
			hideDo
			hidePagination>
				<el-table-column type="index" width="50" />
				<el-table-column label="物料名称" prop="物料名"></el-table-column>
				<el-table-column label="规格" prop="规格"> </el-table-column>
				<el-table-column label="型号" prop="型号"> </el-table-column>
				<el-table-column label="工序" prop="工序"> </el-table-column>
				<el-table-column label="检验时间">
					<template #default="scope">
						{{ scope.row.年 }}-{{ scope.row.月 }}-{{ scope.row.日 }}
					</template>
				</el-table-column>
				<el-table-column label="检验数" prop="检验数"></el-table-column>
				<el-table-column label="合格数" prop="合格数量"></el-table-column>
				<el-table-column label="不合格数" align="center" v-if="query.process_name == '一次超洗'">
					<el-table-column label="黑圈内径" prop="黑圈内径"></el-table-column>
					<el-table-column label="黑圈内径圆度" prop="黑圈内径圆度"></el-table-column>
					<el-table-column label="黑圈外径" prop="黑圈外径"></el-table-column>
					<el-table-column label="黑圈外径圆度" prop="黑圈外径圆度"></el-table-column>
					<el-table-column label="产品外径" prop="产品外径"></el-table-column>
					<el-table-column label="产品圆度" prop="产品圆度"></el-table-column>
					<el-table-column label="产品同心度" prop="产品同心度"></el-table-column>
					<el-table-column label="厚度" prop="厚度"></el-table-column>
					<el-table-column label="准合格" prop="准合格"></el-table-column>
					<el-table-column label="圆准" prop="圆准"></el-table-column>
					<el-table-column label="合计" prop="不合格数"></el-table-column>
				</el-table-column>
				<el-table-column label="不合格数" align="center" v-else>
					<el-table-column label="脏" prop="脏"></el-table-column>
					<el-table-column label="气泡" prop="气泡"></el-table-column>
					<el-table-column label="水纹" prop="水纹"></el-table-column>
					<el-table-column label="崩边" prop="崩边"></el-table-column>
					<el-table-column label="棕边" prop="棕边"></el-table-column>
					<el-table-column label="棕圈" prop="棕圈"></el-table-column>
					<el-table-column label="划伤" prop="划伤"></el-table-column>
					<el-table-column label="麻点" prop="麻点"></el-table-column>
					<el-table-column label="黑圈变形" prop="黑圈变形"></el-table-column>
					<el-table-column label="倒角" prop="倒角"></el-table-column>
					<el-table-column label="破损" prop="破损"></el-table-column>
					<el-table-column label="蓝圈" prop="蓝圈"></el-table-column>
					<el-table-column label="合计" prop="不合格数"></el-table-column>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
</template>
<script>
export default {
	name: "chart",

	data() {
		return {
			query:{
				fw_type:'prod',
				end_date:'',
				start_date:'',
				process_name:'一次超洗',
				group_by_dept: ", dept.id",
				group_by_user: ", suser.id",
				group_by_shift: ", shift.id",
				select_col_dept: ", dept.name as 部门",
				select_col_user: ", suser.name as 检验人",
				select_col_shift: ", shift.name as 班次"
			},
			currentDate: "",
			tableData: [],
			options:[
				{name:'外观',value:'一次超洗'},
				{name:'尺寸',value:'二次超洗'}
			],
			userOptions:[],
			shiftOptions:[],
		};
	},
	mounted() {
		let that = this;
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		month = month < 10 ? "0" + month : month;
		day = day < 10 ? "0" + day : day;
		let queryDate = year + "-" + month + "-" + day;
		that.query.start_date = that.query.end_date = that.currentDate = queryDate;
		this.getData();
		this.getUsers();
		this.getShifts();
	},
	methods: {
		getUsers(){
			let that = this;
			this.$API.system.user.list.req({ page: 0, posts__code__contains: "check" }).then((res) => {
				that.userOptions = res;
			});
		},
		getShifts(){
			let that = this;
			that.$API.mtm.shift.list.req({ page: 0 }).then((res) => {
				that.shiftOptions = res;
			});
		},
		getData() {
			let that = this;
			let obj = {};
			obj.query = that.query;
			that.tableData = [];
			that.$API.bi.dataset.exec.req('ftestDay', obj).then((res) => {
				that.tableData = res.data2.ds0;
				console.log(that.tableData);
			});
		},
		processChange(){
			this.handleQuery();
		},
		handleQuery() {
			if(this.query.start_date){
				this.query.end_date = this.query.start_date;
			}else{
				this.query.start_date = this.query.end_date = this.currentDate;
			}
			this.getData();
		},
		handleExport() {
			this.exportLoading = true;
			this.$XLSX("#exportDiv", "成品检验统计");
			this.exportLoading = false;
		},
	},
};
</script>

<style scoped>
.tables {
	position: absolute;
	top: 4px;
	right: 6px;
	z-index: 10;
}
</style>
