<template>
	<el-container>
		<el-header>
			<div class="left-panel"></div>
			<div class="right-panel">
                <el-date-picker
					v-model="query.start_date"
					type="date"
					value-format="YYYY-MM-DD"
					placeholder="开始时间"
					style="width: 150px"
				/>
				<el-date-picker
					v-model="query.end_date"
					type="date"
					value-format="YYYY-MM-DD"
					placeholder="结束时间"
					style="margin-left: 2px; width: 150px"
				/>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:data="tableData"
				id="exportDiv"
				row-key="id"
				hidePagination
				hideDo
				stripe
			>
				<el-table-column type="index" width="50"  fixed="left"/>
                <el-table-column label="员工" prop="员工" :filters="userFilters" :filter-method="filterUser" filter-placement="bottom-end" >
				</el-table-column>
				<el-table-column label="切片后缀" prop="切片后缀" :filters="houzhuiFilters" :filter-method="filterHouzhui" filter-placement="bottom-end">
				</el-table-column>
				<el-table-column label="总数">
					<template #default="scope">
						<span v-if="scope.row.总切片数" @click="handleClick(scope.row)" style="color:#0052d9;">{{ scope.row.总切片数 }}</span>
						<span v-else @click="handleClick(scope.row)" style="color:#0052d9;">{{ scope.row.总生产数 }}</span>
					</template>
				</el-table-column>
				<el-table-column label="剪切">
					<template #default="scope">
						<span v-if="scope.row.剪切合格">{{((scope.row.剪切合格/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
				<el-table-column label="剪切加工率">
					<template #default="scope">
						<span v-if="scope.row['剪切￠18.3mm不合格']">{{ (((scope.row.总切片数-scope.row['剪切￠18.3mm不合格'].含)/scope.row.总切片数)*100).toFixed(2) }}%</span>
						<span v-else>100%</span>
					</template>
				</el-table-column>
				<el-table-column label="暗点">
					<template #default="scope">
						<span v-if="scope.row.暗点合格">{{((scope.row.暗点合格.含/scope.row.总切片数)*100).toFixed(2) }}%</span>
						<span v-else>0%</span>
					</template>
				</el-table-column>
				<el-table-column label="长点率">
					<template #default="scope">
						<span>{{((scope.row.长点数/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
				<el-table-column label="加工率">
					<template #default="scope">
						<span>{{((scope.row.加工率数/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
				<el-table-column label="合格率" prop="合格率">
					<!-- 暗点合格并且剪切合格的板段数：在暗点合格含里刨除剪切可加工和剪切不合格 -->
					<template #default="scope">
						<span>{{((scope.row.合格率数/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-container v-if="visible" style="height:100%;overflow-y: scroll;">
			<el-main class="nopadding">
				<div style="padding: 10px;">
					<div style="height: 35px;font-size: 18px;">
						<span>{{ userName }}-{{ banduanNum }}板段列表 </span>
						<el-button type="primary" style="position: absolute;right: 20px;" @click="listClose">关闭</el-button>
					</div>
					<div v-for="(item,index) in detailRow" :key="item" style="display: inline-block;width:150px;margin-bottom: 5px;">
						<span style="margin-right:10px;display: inline-block;">{{ index+1 }}、{{ item }} </span>
					</div>
				</div>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
export default {
	name: "workerTimes",
	data() {
		return {
			visible:false,
			userName:'',
			banduanNum:'',
			query:{
				end_date:'',
                mgroup_name:'排板',
				start_date:'',
				select_cols: ", ep.总生产数, ep.切片编号列表"
			},
			users:[],
			detailRow:[],
			userFilters:[],
			houzhuiFilters:[],
			tableData:[],
		};
	},
    mounted() {
        let that = this;
        let date = new Date();
        that.query.end_date = that.$TOOL.dateFormat2(date);
		that.query.start_date =  that.query.end_date.split('-')[0] + '-' + that.query.end_date.split('-')[1] + '-01';
		that.getData();
    },
	methods: {
        getData(){
            let that = this;
            let params = {};
			let userList = [],users = [];
			let houzhuiList = [],houzhuis=[];
            params.query = that.query;
			that.$API.bi.dataset.exec.req('product_defect_pb_hz1', params).then((res) => {
				let data = res.data2.ds0;
				data.forEach(item=>{
					if(users.indexOf(item.员工)>-1){}else{
						users.push(item.员工);
						userList.push({text:item.员工,value:item.员工})
					}
					if(houzhuis.indexOf(item.切片后缀)>-1){}else{
						houzhuis.push(item.切片后缀);
						houzhuiList.push({text:item.切片后缀,value:item.切片后缀})
					}
					if(item.缺陷分布!==null){
						let defect = JSON.parse(item.缺陷分布);
						for(let key in defect){
							item[key]=defect[key];
						}
					}
					item.长点数 = item.暗点不合格?item.暗点重?item.暗点不合格.含 + item.暗点重.含:item.暗点不合格.含:item.暗点重?item.暗点重.含:0;
					item.剪切合格 = item['剪切￠18.3mm不合格']?item['剪切￠18.3mm可加工']?item.总切片数 - item['剪切￠18.3mm可加工'].含- item['剪切￠18.3mm不合格'].含:item.总切片数 - item['剪切￠18.3mm不合格'].含:item['剪切￠18.3mm可加工']?item.总切片数 - item['剪切￠18.3mm可加工'].含:item.总切片数;
					let count_hgls = item.暗点合格?item.暗点合格.含:0;
					let count1 = item['剪切￠18.3mm不合格']?item.总切片数 - item.长点数 - item['剪切￠18.3mm不合格'].含:item.总切片数 - item.长点数;
					if(item['剪切￠18.3mm不合格']&&item['剪切￠18.3mm不合格'].组!==null){
						if(item['剪切￠18.3mm不合格'].组['剪切￠18.3mm不合格+暗点不合格']){
							count1 = count1 + item['剪切￠18.3mm不合格+暗点不合格'];
						}
						if(item['剪切￠18.3mm不合格'].组['剪切￠18.3mm不合格+暗点重']){
							count1 = count1 + item['剪切￠18.3mm不合格+暗点重'];
						}
						if(item['剪切￠18.3mm可加工'].组['剪切￠18.3mm可加工+暗点合格']){
							count_hgls = count_hgls - item['剪切￠18.3mm可加工'].组['剪切￠18.3mm可加工+暗点合格'];
						}
						if(item['剪切￠18.3mm可加工'].组['剪切￠18.3mm不合格+暗点合格']){
							count_hgls = count_hgls - item['剪切￠18.3mm不合格+暗点合格'];
						}
					}
					item.加工率数 = count1;//总数-暗点不合格.含-暗点重.含-剪切不合格.含+两个组合数；
					item.合格率数 = count_hgls;//：在暗点合格含里刨除剪切可加工和剪切不合格
				})
				 that.$nextTick(() => {
                  	that.userFilters = userList;
					that.houzhuiFilters = houzhuiList;
					that.tableData = data;
                })
			});
        },
		filterUser(value, row) {
			return row.员工 == value;
		},
		filterHouzhui(value, row) {
			return row.切片后缀 == value;
		},
		handleClick(row){
			let that = this;
			that.detailRow = [];
			that.userName = row.员工;
			that.banduanNum = row.切片后缀;
			that.detailRow = row.切片编号列表;
			that.visible = true;
		},
		listClose(){
			let that = this;
			that.visible = false;
			that.detailRow = [];
		},
		deptChange(){
			this.getData();
		},
		handleQuery(){
			let that = this;
			console.log('that.query',that.query)
			that.getData();
		},
	},
};
</script>

<style scoped>
</style>
