<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>
            </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"
            >
                <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>
            <!-- <pagination
                v-show="dataList.count > 0"
                :total="dataList.count"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.page_size"
                @pagination="getList"
            /> -->
        </el-card>
    </div>
</template>

<script>
    import { getPgoalDeptList } from "@/api/task";
    import { getOrgList, getSubOrgList } from "@/api/org";
    import checkPermission from "@/utils/permission";
    import Pagination from "@/components/Pagination"; 
    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:''
                },
                taskList:[],
                heightTable:null,
                listLoading:false
            };
        },
        mounted(){
            this.getOrgList();
            let height = document.getElementsByClassName('app-main')[0].clientHeight;
            this.heightTable = height-90;
        },
        methods:{
            checkPermission,
            getOrgList() {
          let that = this;
          let ids = ''
          if (that.checkPermission(["record_confirm"])) {
            getOrgList({ can_supervision: true }).then((res) => {
              let data = res.data;
              debugger;
              for(let i=0;i<data.length;i++){
                ids= ids+data[i].id+',';
              }
              that.listQuery.belong_dept__in=ids;
              that.getList();
            });
          } else {
            getSubOrgList().then((res) => {
                let data = res.data;
                debugger;
                for(let i=0;i<data.length;i++){
                    ids= ids+data[i].id+',';
                }
                that.listQuery.belong_dept__in=ids;
                that.getList();
            });
          }
        },
            //获取任务列表===》获取任务id
            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;
                        });
                        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;
                                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;
                                }else  if(data[i].goal_key=='pgoal_2'){
                                    dataList[0].pgoal_2=data[i].goal_value_a;
                                    dataList[index].pgoal_2=data[i].goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_3'){
                                    dataList[0].pgoal_3=data[i].goal_value_a;
                                    dataList[index].pgoal_3=data[i].goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_4'){
                                    dataList[0].pgoal_4=data[i].goal_value_a;
                                    dataList[index].pgoal_4=data[i].goal_value_b;
                                }else  if(data[i].goal_key=='pgoal_5'){
                                    dataList[0].pgoal_5=data[i].goal_value_a;
                                    dataList[index].pgoal_5=data[i].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(){
                this.getList();
            },
           
           
        },
    };
</script>

<style>
</style>