<template>
    <div class="app-container">
        <el-card>
            <div>
                <el-date-picker
                    v-model="listQuery.search"
                    type="year"
                    placeholder="选择年"
                    value-format="yyyy"
                    style="width:300px"
                >
                </el-date-picker>
                <!-- <el-select
                    v-model="listQuery.cycle"
                    placeholder="类型"
                    clearable
                    style="width: 300px"
                    @change="handleFilter"
                >
                    <el-option label="目标定制" value="10"/>
                    <el-option label="日常监督" value="20"/>
                </el-select> -->
                <el-button
                    class="filter-item"
                    type="primary"
                    icon="el-icon-search"
                    @click="handleFilter"
                >搜索</el-button>
                <el-button
                    class="filter-item"
                    type="primary"
                    icon="el-icon-refresh-left"
                    @click="resetFilter"
                >重置</el-button>
            </div>
        </el-card>
        <el-card style="margin-top: 10px">
            <el-table
                v-loading="listLoading"
                :data="dataList.results"
                border
                fit
                stripe
                highlight-current-row
                max-height="600"
            >
                <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',
                },
                taskList:[],
                listLoading:false
            };
        },
        mounted(){
            this.getList();
        },
        methods:{
            checkPermission,
            getOrgList() {
          let that = this;
          let ids = ''
          if (that.checkPermission(["record_confirm"])) {
            getOrgList({ can_supervision: true }).then((res) => {
              let data = res.data;
              for(let i=0;i<data.length;i++){
                ids= ids+item.id+',';
              }
              that.listQuery.belong_dept__in=ids;
              that.getList();
            });
          } else {
            getSubOrgList().then((res) => {
                let data = res.data;
                for(let i=0;i<data.length;i++){
                    ids= ids+item.id+',';
                }
                that.listQuery.belong_dept__in=ids;
                that.getList();
            });
          }
        },
            //获取任务列表===》获取任务id
            getList(){
                let that = this;
                that.listLoading = true;
                if(that.listQuery.year==''){
                    that.listQuery.year = new Date().getFullYear();
                }
                that.listQuery.page=0;
                getPgoalDeptList(that.listQuery).then((res) => {
                    debugger;
                    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++){
                                debugger;
                                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;
                        }
                    }
                    
                });
                
            },
            resetFilter(){
                this.listQuery.search = '';
                this.getList();
            },
            handleFilter(){
                this.getList();
            },
           
           
        },
    };
</script>

<style>
</style>