<template>
    <div class="app-container">
        <el-card>
            <div>
                <el-date-picker
                    v-model="listQuery.year"
                    type="year"
                    placeholder="选择年"
                    style="width:300px"
                    @change="yearChange"
                >
                </el-date-picker>
                <el-button
                    class="filter-item"
                    type="primary"
                    icon="el-icon-search"
                    @click="handleFilter"
                >搜索</el-button>
                <el-button
                    type="primary"
                    @click="handleExport"
                    >导出
                </el-button>
            </div>
        </el-card>
        <el-card style="margin-top: 6px">
            <el-table
                v-loading="listLoading"
                :data="dataList.results"
                border
                fit
                stripe
                highlight-current-row
                :height="heightTable"
                id="myTable"
            >
                <el-table-column type="index" width="50" />
                <el-table-column label="年份" prop="year" width="80"></el-table-column>
                <el-table-column label="部门名称" prop="dept_name"></el-table-column>
                <el-table-column label="重大质量事故(件)" prop="pgoal_1"></el-table-column>
                <el-table-column label="报告/证书合格率(%)" prop="pgoal_2"></el-table-column>
                <el-table-column label="报告/证书及时率(%)" prop="pgoal_3"></el-table-column>
                <el-table-column label="能力验证满意率(%)" prop="pgoal_4"></el-table-column>
                <el-table-column label="客户投诉处理满意率(%)" prop="pgoal_5"></el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script>
    import { getPgoalDeptList,taskAnalyse } from "@/api/task";
    import { getOrgList, getSubOrgList } from "@/api/org";
    import checkPermission from "@/utils/permission";
    import Pagination from "@/components/Pagination"; 
    import * as XLSX from "xlsx";
    export default {
        components:{Pagination},
        data(){
            return{
                dataList:{
                    results:[],  
                    count:0
                },
                listQuery:{
                    page: 0,
                    year:'',
                    goal_key__in:'pgoal_1,pgoal_2,pgoal_3,pgoal_4,pgoal_5',
                    belong_dept__in:''
                },
                query:{
                    year:'',
                    dept_type_name:'2级公司'
                },
                taskList:[],
                heightTable:null,
                listLoading:false
            };
        },
        mounted(){
            let height = document.getElementsByClassName('app-main')[0].clientHeight;
            this.heightTable = height-90;
            debugger;
            if (checkPermission(["record_confirm"])) {
                this.getTableData();
            } else {
                this.getOrgList();
            }
        },
        methods:{
            checkPermission,
            getOrgList() {
                let that = this;
                let ids = ''
                getSubOrgList().then((res) => {
                    let data = res.data;
                    for(let i=0;i<data.length;i++){
                        ids= ids+data[i].id+',';
                    }
                    that.listQuery.belong_dept__in=ids;
                    that.getList();
                });
            },
            getTableData(){
                debugger;
                let that = this;
                if(that.listQuery.year==''){
                    let date = new Date();
                    that.query.year = date.getFullYear();
                }else{
                    that.query.year =that.listQuery.year
                }
                taskAnalyse(that.query).then(res=>{
                    console.log(res)
                    let data = res.data;
                    let arr = [];
                    if(data.length>0){
                        let firstObj = {};
                        firstObj.year=data[0].年份;
                        firstObj.dept_name=data[0].单位;
                        firstObj.pgoal_1=data[0].重大事故数基础值;
                        let pgoal_2 = data[0].报告证书合格率基础值+'';
                        let pgoal_3=data[0].报告证书及时率基础值+'';
                        let pgoal_4=data[0].能力验证满意率基础值+'';
                        let pgoal_5=data[0].客户投诉处理满意率基础值+'';
                        firstObj.pgoal_2=pgoal_2.indexOf('.')>-1?pgoal_2:pgoal_2+'.0';
                        firstObj.pgoal_3=pgoal_3.indexOf('.')>-1?pgoal_3:pgoal_3+'.0';
                        firstObj.pgoal_4=pgoal_4.indexOf('.')>-1?pgoal_4:pgoal_4+'.0';
                        firstObj.pgoal_5=pgoal_5.indexOf('.')>-1?pgoal_5:pgoal_5+'.0';
                        arr.push(firstObj);
                        data.forEach(item=>{
                            let obj = {};
                            obj.year=item.年份;
                            obj.dept_name=item.单位;
                            obj.pgoal_1=item.重大事故数设定值;
                            let pgoal2 = item.报告证书合格率设定值+'';
                            let pgoal3=item.报告证书及时率设定值+'';
                            let pgoal4=item.能力验证满意率设定值+'';
                            let pgoal5=item.客户投诉处理满意率设定值+'';
                            obj.pgoal_2=pgoal2.indexOf('.')>-1?pgoal2:pgoal2+'.0';
                            obj.pgoal_3=pgoal3.indexOf('.')>-1?pgoal3:pgoal3+'.0';
                            obj.pgoal_4=pgoal4.indexOf('.')>-1?pgoal4:pgoal4+'.0';
                            obj.pgoal_5=pgoal5.indexOf('.')>-1?pgoal5:pgoal5+'.0';
                            arr.push(obj);
                        })
                    }
                    
                    that.dataList.results = arr;
                })
            },
            //获取列表
            getList(){
                let that = this;
                that.dataList.results = [];
                that.dataList.count =0;
                that.listLoading = true;
                if(that.listQuery.year==''){
                    let date = new Date();
                    that.listQuery.year = date.getFullYear()+'';
                }
                
                // console.log(that.listQuery)
                getPgoalDeptList(that.listQuery).then((res) => {
                    if (res.data) {
                        let data0 = res.data;
                        let data =data0.filter(item=>{
                           return item.task2do!==null;
                        });
                        debugger;
                        console.log(data)
                        that.dataList.count = data.length;
                        if(data.length>0){
                            let alreadyId = ['000'];
                            let dataList = [
                                {year:that.listQuery.year,dept_name:'集团',dept:'000',pgoal_1:'',pgoal_2:'',pgoal_3:'',pgoal_4:'',pgoal_5:''}
                            ];
                            for(let i=0;i<data.length;i++){
                                let index = alreadyId.indexOf(data[i].belong_dept)
                                if(index>-1){
                                }else{
                                    let obj = {year:that.listQuery.year,dept_name:data[i].belong_dept_name,dept:data[i].belong_dept,pgoal_1:'',pgoal_2:'',pgoal_3:'',pgoal_4:'',pgoal_5:''};
                                    dataList.push(obj)
                                    alreadyId.push(data[i].belong_dept)
                                }
                                index = alreadyId.length-1;
                                let goal_value_b = '',goal_value_a = '';
                                goal_value_a = data[i].goal_value_a+'';
                                if(goal_value_a.indexOf('.')>-1){}else{
                                    goal_value_a = goal_value_a+'.0';
                                }
                                goal_value_b = data[i].goal_value_b+'';
                                if(data[i].goal_value_b!==null){
                                    if(goal_value_b.indexOf('.')>-1){}else{
                                        goal_value_b = goal_value_b+'.0';
                                    }
                                }else{
                                    goal_value_b = ''
                                }
                                if(data[i].goal_key=='pgoal_1'){
                                    dataList[0].pgoal_1=data[i].goal_value_a;
                                    dataList[index].pgoal_1=data[i].goal_value_b!==null?data[i].goal_value_b:'';
                                }else  if(data[i].goal_key=='pgoal_2'){
                                    dataList[0].pgoal_2=goal_value_a;
                                    dataList[index].pgoal_2=goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_3'){
                                    dataList[0].pgoal_3=goal_value_a;
                                    dataList[index].pgoal_3=goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_4'){
                                    dataList[0].pgoal_4=goal_value_a;
                                    dataList[index].pgoal_4=goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_5'){
                                    dataList[0].pgoal_5=goal_value_a;
                                    dataList[index].pgoal_5=goal_value_b;
                                }
                            }
                            that.dataList.results = dataList;
                        }
                        that.listLoading = false;
                    }else{
                        that.listLoading = false;
                    }
                    
                });
                
            },
            yearChange(value){
                let year = new Date(value).getFullYear()+'';
                this.listQuery.year = year;
            },
            handleFilter(){
                if (this.checkPermission(["record_confirm"])) {
                    this.getTableData();
                } else {
                    this.getList();
                }
            },
            handleExport() {
                let filename = this.listQuery.year+'部门目标.xlsx'
                let table =document.getElementById('myTable');
                let workbook = XLSX.utils.table_to_book(table);
                XLSX.writeFile(workbook, filename);
            },
        },
    };
</script>

<style>
</style>