<template>
	<el-container>
		<el-header>
			<div class="left-panel"></div>
			<div class="right-panel">
				<el-select v-model="query.mgroup_name" placeholder="工段" clearable style="width: 150px">
                  <el-option v-for="item in mgroups" :key="item.id" :label="item.name" :value="item.name"></el-option>
                </el-select>
                <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="工段">{{ query.mgroup_name }}
				</el-table-column>
                <el-table-column label="员工" prop="员工">
				</el-table-column>
				<el-table-column label="总数">
					<template #default="scope">
						<span v-if="scope.row.总切片数">{{ scope.row.总切片数 }}</span>
						<span v-else>{{ 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 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 v-if="scope.row.总切片完全合格数">{{((scope.row.加工率数/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
				<el-table-column label="合格率" prop="合格率">
					<!-- 暗点合格并且剪切合格的板段数：在暗点合格含里刨除剪切可加工和剪切不合格 -->
					<template #default="scope">
						<span v-if="scope.row.总切片完全合格数">{{((scope.row.暗点合格.仅/scope.row.总切片数)*100).toFixed(2) }}%</span>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
</template>

<script>
export default {
	name: "workerTimes",
	data() {
		return {
			query:{
				end_date:'',
                mgroup_name:'排板',
				start_date:'',
			},
            mgroups: [],
			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();
		this.deptMgroup();
    },
	methods: {
		 deptMgroup() {
            this.$API.mtm.mgroup.list.req({ page: 0,belong_dept__name:'拉丝排板班组' }).then(res => {
                this.mgroups = res;
            });
        },
        getData(){
            let that = this;
            let params = {};
            params.query = that.query;
			that.$API.bi.dataset.exec.req('prodect_defect', params).then((res) => {
				let data = res.data2.ds0;
				data.forEach(item=>{
					if(item.缺陷分布!==null){
						let defect = JSON.parse(item.缺陷分布);
						for(let key in defect){
							item[key]=defect[key];
						}
					}
					item.不合格数 = 0;
					item.可加工数 = 0;
					item.加工率数 = item.总切片合格数?item.总切片合格数:0-item.暗点不合格.含?item.暗点合格.含:0;
					if(item['剪切￠18.3mm不合格']){
						if(item['剪切￠18.3mm可加工']){
							item.剪切合格 = item.总切片数 - item['剪切￠18.3mm可加工'].含- item['剪切￠18.3mm不合格'].含;
						}else{
							item.剪切合格 = item.总切片数 - item['剪切￠18.3mm不合格'].含;
						}
					}else{
						if(item['剪切￠18.3mm可加工']){
							item.剪切合格 = item.总切片数 - item['剪切￠18.3mm可加工'].含;
						}else{
							item.剪切合格 = item.总切片数;
						}
					}
				})
				that.tableData = data;
			});
        },
		deptChange(){
			this.getData();
		},
		handleQuery(){
			let that = this;
			console.log('that.query',that.query)
			that.getData();
		},
	},
};
</script>

<style scoped>
</style>
