<!--
  1.rows cut from 2100 to 500
  2.tables columns head request from mysql,not write in this file,
    so if change table columns,not need fix this file.
  3.unitid not need request from mysql , unitid save to cookie at login,
    when this page need use ,then get from cookie.
  4.{__ob__: Observer }，occur this error
     ---by Paul 2022-11-11
-->
<!--
  1.tables columns head request not from mysql,
    from the file "check.xlsx.full.js" use method "getObjColumnsAndProps"
     ---by Paul 2022-11-17
-->
<!--
//watch，也可以解决 {__ob__: Observer }，不用延时等了
     ---by Paul 2022-11-18
-->

<!--
只有开放日，才 激活“查询审核状态”按键，因为，当周期跨月时，查询下个月的数据，会出错
     ---by Paul 2022-12-06
-->

<!--
审核时，组成项目号的年份，不能取服务器日期的年份，因为最后1-2周可能出问题，改为取周号的年份
     ---by Paul 2023-1-1
-->

<template>
    <div class="app-container">
    <el-row :gutter="5">
    <el-col :span="22" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>周报审核</span>
          </div>
          <div class="date">
            <span style="padding-left:0%">本机日期：</span><span>{{SystemDate}}</span>
            <span style="padding-left:2%">服务器日期：</span><span>{{ServerDate}}</span>
            <span style="padding-left:2%">周号：</span><span>{{weekInfo.weekNum}}</span>
            <span style="padding-left:2%">起日：</span><span>{{weekInfo.from}}</span>
            <span style="padding-left:2%">止日：</span><span>{{weekInfo.to}}</span>
            <span style="padding-left:2%">开放日：</span><span><i class="el-icon-caret-right" v-if="this.ServerDate==this.weekInfo.kfDay1"></i>{{weekInfo.kfDay1}}&nbsp;&nbsp;</span><span><i class="el-icon-caret-right" v-if="this.ServerDate==this.weekInfo.kfDay2"></i>{{weekInfo.kfDay2}}</span>
          </div>
          <div class="form">
                <el-form :rules="rules" ref="form" :model="form" label-width="120px" style="width:66%">
                <el-form-item label="项目归属："  style="margin-bottom:30px">
                        <el-select
                        value-key="item"
                        v-model="form.region"
                        placeholder="请选择项目归属"
                        >
                          <el-option
                          v-for="item of projectFrom"
                            :key="item"
                            :label="item"
                            :value="item"
                          />
                        </el-select>
                        <span style="padding-left:8%;font-weight:bold;font-size:15px">审核状态：</span>
                        <span style="padding-left:5%;color:red;font-weight: bold;font-size:15px">{{projectStatus}}</span>
                        <el-button style="float:right" :disabled ="disabled2" :loading="queryprojectLoading" type="primary" @click="getProjectForm('form')">查询审核状态</el-button>
                </el-form-item>
              </el-form>
          </div>
          <div class="staff">
            <el-form ref="staff" label-width="120px" style="width:100%">
              <el-form-item label="不予确认人员：">
                    <el-select ref="staff" v-model="filter2" @change="filterChange()" filterable clearable multiple placeholder="请选择需要过滤的人员">
                      <el-option
                        v-for="item in staff"
                        :key="item"
                        :label="item"
                        :value="item">
                      </el-option>
                    </el-select>
                <el-button :disabled ="disabled1" :loading="assignByFilterBtnLoading" style="margin-left:5%" type="primary" @click="assignByFilter()">开始确认</el-button>
              </el-form-item>
            </el-form>
          </div>

          <div class="dailyReport" style="margin-top:40px">
              <div style="text-align:center">
                <el-tag>本周日报内容</el-tag>
              </div>

                <div style="text-align:center">
                    <el-button-group>
                        <el-button type="primary"  @click="btnb1click" :loading="b1loading" :disabled ="disabled2">B1</el-button>
                        <el-button type="primary"  @click="btnb2click" :loading="b2loading" :disabled ="disabled2">B2</el-button>
                        <el-button type="primary"  @click="btnb3click" :loading="b3loading" :disabled ="disabled2">B3</el-button>
                        <el-button type="primary"  @click="btnb4click" :loading="b4loading" :disabled ="disabled2">B4</el-button>
                        <el-button type="primary"  @click="btnb6click" :loading="b6loading" :disabled ="disabled2">B6</el-button>
                        <el-button type="primary"  @click="btnb7click" :loading="b7loading" :disabled ="disabled2">B7</el-button>
                        <el-button type="primary"  @click="btnb8click" :loading="b8loading" :disabled ="disabled2">B8</el-button>
                        <el-button type="primary"  @click="btnb9click" :loading="b9loading" :disabled ="disabled2">B9</el-button>
                        <el-button type="primary"  @click="btnb10click" :loading="b10loading" :disabled ="disabled2">B10</el-button>
                        <el-button type="primary"  @click="btnb11click" :loading="b11loading" :disabled ="disabled2">B11</el-button>
                        <el-button type="primary"  @click="btnb12click" :loading="b12loading" :disabled ="disabled2">B12</el-button>
                        <el-button type="primary"  @click="btnb13click" :loading="b13loading" :disabled ="disabled2">B13</el-button>
                        <el-button type="primary"  @click="btnb14click" :loading="b14loading" :disabled ="disabled2">B14</el-button>
                        <el-button type="primary"  @click="btnb15click" :loading="b15loading" :disabled ="disabled2">B15</el-button>
                    </el-button-group>
              </div>

              <el-table
                        :data="BX"
                        border
                        v-loading="bxloading"
                        stripe
                        lazy
                        style="width: 100%">
                        <el-table-column
                            v-for="(index,item) in tmpfields"
                            :key="index"
                            :label="item"
                            :prop="index"
                            :show-overflow-tooltip="true">
                        </el-table-column>
              </el-table>

          </div>
        </el-card>
      </el-col>
    </el-row>
    </div>
</template>

<script>
import { getNowDate} from "@/api/daily/daily";
import { examineAllDailys,getDailyByProjectAndWeekInfo,getWeekInfo,getB1,getB2,getB3,getB4,getB6,getB7,getB8,getB9,getB10,getB11,getB12,getB13,getB14,getB15 } from "@/api/week/week";
import Cookies from "js-cookie";
//import { getColumns } from "@/api/comprehensive/search";
import {getObjColumnsAndProps} from "@/api/check.xlsx.full.js";

export default {
  name: "weekIndex",
  data() {
    return {

        tmpfields:[],
        tmpcolumns:[],//by Paul 2022-11-10 current file not use
        tmpprops:[],//by Paul 2022-11-10 current file not use
        BX:[],
        Arr:[],


        SystemDate : "",//by Paul 2022-11-10 current file not use
        ServerDate : "",

        b1loading:false,
        b2loading:false,
        b3loading:false,
        b4loading:false,
        b6loading:false,
        b7loading:false,
        b8loading:false,
        b9loading:false,
        b10loading:false,
        b11loading:false,
        b12loading:false,
        b13loading:false,
        b14loading:false,
        b15loading:false,
        bxloading:false,

        disabled1: true,
        disabled2: true,


        projectStatus:"请先查询",

        form:{
            region:""
        },
        assignAllBtnLoading: false,
        assignByFilterBtnLoading:false,

        weekInfo:{
            weekNum:"",
            from:"",
            to:"",
            kfDay1:"",
            kfDay2:""
        },
        filter2:[],
        dailyReportStatus:{
          dailtReportList:[]
        },
        projectFrom:[],


        staff:[],

        errorLog:{
            open : false,
            info:""
        },
        rules: {
          region: [
            { required: true, message: '请选择项目编号', trigger: 'change' }
          ]
        }
    };
  },

  created(){
    this.getDates();
   // this.getProject();
    this.getWeekInfo();
  },
/*  mounted() {
    setTimeout(()=>{
       //这里就写你要执行的语句即可，先让数据库的数据加载进去数组中你在从数组中取值就好了
        //this.form.region = this.projectFrom;
       // console.log(this.projectFrom);
        this.getProjectIDFromCookie();
        this.form.region =  this.projectFrom[0];
    },1000)
 }, */
 //watch，也可以解决 {__ob__: Observer }，不用延时等了
watch:{
  'weekInfo.weekNum'(val){
   // console.log(val);
        this.getProjectIDFromCookie();
        this.form.region =  this.projectFrom[0];
  },
  immediate: true,
  deep: false
 },
  methods :{
    btnb1click(){
            this.b1loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b1");
            this.tmpfields = getObjColumnsAndProps(0);
            getB1(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b1loading=false;
            this.bxloading=false;
            });
    },
    btnb2click(){
            this.b2loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b2");
            this.tmpfields = getObjColumnsAndProps(1);
            getB2(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b2loading=false;
            this.bxloading=false;
            });
    },
    btnb3click(){
            this.b3loading=true;
            this.bxloading=true;
           // this.getColumnsInfo("b3");
           this.tmpfields = getObjColumnsAndProps(2);
            getB3(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b3loading=false;
            this.bxloading=false;
            });
    },
    btnb4click(){
            this.b4loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b4");
            this.tmpfields = getObjColumnsAndProps(3);
            getB4(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b4loading=false;
            this.bxloading=false;
            });
    },
    btnb6click(){
            this.b6loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b6");
            this.tmpfields = getObjColumnsAndProps(4);
            getB6(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b6loading=false;
            this.bxloading=false;
            });
    },
    btnb7click(){
            this.b7loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b7");
            this.tmpfields = getObjColumnsAndProps(5);
            getB7(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b7loading=false;
            this.bxloading=false;
            });
    },
    btnb8click(){
            this.b8loading=true;
            this.bxloading=true;
           // this.getColumnsInfo("b8");
            this.tmpfields = getObjColumnsAndProps(6);
            getB8(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b8loading=false;
            this.bxloading=false;
            });
    },
    btnb9click(){
            this.b9loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b9");
            this.tmpfields = getObjColumnsAndProps(7);
            getB9(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b9loading=false;
            this.bxloading=false;
            });
    },
    btnb10click(){
            this.b10loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b10");
            this.tmpfields = getObjColumnsAndProps(8);
            getB10(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b10loading=false;
            this.bxloading=false;
            });
    },
    btnb11click(){
            this.b11loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b11");
            this.tmpfields = getObjColumnsAndProps(9);
            getB11(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b11loading=false;
            this.bxloading=false;
            });
    },
    btnb12click(){
            this.b12loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b12");
            this.tmpfields = getObjColumnsAndProps(10);
            getB12(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b12loading=false;
            this.bxloading=false;
            });
    },
    btnb13click(){
            this.b13loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b13");
            this.tmpfields = getObjColumnsAndProps(11);
            getB13(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b13loading=false;
            this.bxloading=false;
            });
    },
    btnb14click(){
            this.b14loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b14");
            this.tmpfields = getObjColumnsAndProps(12);
            getB14(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b14loading=false;
            this.bxloading=false;
            });
    },
    btnb15click(){
            this.b15loading=true;
            this.bxloading=true;
            //this.getColumnsInfo("b15");
            this.tmpfields = getObjColumnsAndProps(13);
            getB15(this.form.region,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.Arr = response;
            this.transMeans(this.Arr);
            this.BX=this.Arr;
            this.b15loading=false;
            this.bxloading=false;
            });
    },
    getProjectIDFromCookie() {
       const unitid = Cookies.get("unitid");
      //console.log(unitid);
      const arrunitid = unitid.split("|");
      //console.log(arrunitid);

      for (let index = 0; index < arrunitid.length; index++) {
        this.projectFrom[index] = arrunitid[index]+"P" + this.weekInfo.weekNum.substr(0,4);
      }
    },

    assignByFilter(){
      this.assignByFilterBtnLoading = true;
        for(let i = 0; i < this.dailyReportStatus.dailtReportList.length;i++){
          for(let j = 0;j < this.filter2.length;j++){
            if(this.filter2[j] == this.dailyReportStatus.dailtReportList[i].userId){
              this.dailyReportStatus.dailtReportList[i].dailyReportStatus = "2";
            }
          }
        }
        examineAllDailys(this.dailyReportStatus.dailtReportList,this.weekInfo.weekNum,this.form.region).then(res => {
          this.assignByFilterBtnLoading = false;
        this.$message({
                  showClose: true,
                  //message: '已成功全部确认！',
                  message: res.msg,
                  type: 'success'
                });
        this.getProjectForm('form');
        });
    },


      getWeekInfo(){
        getWeekInfo().then(response =>{
            this.weekInfo.weekNum = response.data.weekNum;
            this.weekInfo.from = response.data.from;
            this.weekInfo.to = response.data.to;
            this.weekInfo.kfDay1 = response.data.openDay1;
            this.weekInfo.kfDay2 = response.data.openDay2;
            if(this.ServerDate == this.weekInfo.kfDay1 || this.ServerDate == this.weekInfo.kfDay2){
               this.disabled2 = false;
             };
        })
      },
      getProjectForm(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.queryprojectLoading = true;

            getDailyByProjectAndWeekInfo(this.form.region,this.weekInfo.weekNum,this.weekInfo.from,this.weekInfo.to).then(response =>{
            this.queryprojectLoading = false;
            this.filter2 = [];
            this.exanmineFlag = response.weekReportStatus;
            this.staff = response.staffList;
            this.dailyReportStatus.dailtReportList = response.dailyReportList;
            if(this.exanmineFlag == "" || this.exanmineFlag == null){
                this.projectStatus = "未审核";
                this.btnb15click();
                if(this.ServerDate == this.weekInfo.kfDay1 || this.ServerDate == this.weekInfo.kfDay2){
                  if(this.staff.length == 0){
                  this.projectStatus = "本周没有上传日报！";
                  }else{
                    this.disabled1=false;
                  }
                }
            }else if(this.exanmineFlag == "1"){
              this.projectStatus = "已审核";
              this.disabled1=true;
              this.btnb15click();
            }else{
              this.projectStatus = "审核中...";
              this.disabled1=true;
              this.btnb15click();
            }
            });

          } else {
            return false;
          }
        });
      },

      transMeans(arr){
      for(let i = 0; i < arr.length ; i++ ){
              if(arr[i].agreeSign == "2"){
                 arr[i].agreeSign = "已删除";
              }
              if(arr[i].agreeSign == "0"){
                 arr[i].agreeSign = "未同意";
              }
              if(arr[i].agreeSign == "1"){
                 arr[i].agreeSign = "同意";
              }
            }
    },

    getColumnsInfo(tableName){
      getColumns(tableName).then(res => {
       //console.log(res.data);
      this.tmpfields = res.data;
       // console.log(res.columns);
      this.tmpcolumns = res.columns;
       //console.log(res.props);
      this.tmpprops = res.props;
    });
  },
     getDates() {
        var nowDate = new Date();
        this.SystemDate = this.dateFormat("YYYY-mm-dd",nowDate);
          getNowDate().then(response =>{
            this.ServerDate = response.data;
          });

  },
    dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
  }
}
}
</script>

<style>
.date{
  margin-bottom: 35px;
  margin-top: 25px;
}
.pagination{
  margin:20px auto;
  padding-left: 36%;
}
</style>
