<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="form">
                <el-form :rules="rules" ref="form" label-width="110px" :model="form" style="width:100%">
                <el-form-item label="周号：" prop="weekNum">
                    <el-select
                      v-model="form.weekNum"
                      filterable
                      allow-create
                      clearable
                      default-first-option
                      @change="weekNumChange"
                      @clear="weekNumclear"
                      placeholder="请选择周号">
                      <el-option
                        v-for="item in weekNumAndFromAndTo"
                        :key="item.weekNum"
                        :label="item.weekNum"
                        :value="item.weekNum">
                      </el-option>
                    </el-select>
                    <span style="padding-left:6%">起日： {{from}}</span>
                    <span style="padding-left:6%">止日： {{to}}</span>
                </el-form-item>
                <el-form-item label="项目归属：" prop="region">
                    <el-input placeholder="请输入项目归属"  style="width:19%" v-model="form.region"></el-input>
                </el-form-item>
                <el-form-item label="单元负责人：" prop="unitManagerName">
                    <el-input placeholder="请输入单元负责人"  style="width:19%" v-model="form.unitManagerName"></el-input>
                    <span style="padding-left:4%;font-weight:bold">项目审核状态：</span>
            <span style="color:red;padding-left:2%;font-weight:bold">{{projectStatus}}</span>
                      <el-button type="primary" style="margin-left:4%" :loading="selectBtnLoading" @click="getProjectForm('form')">查询日报数据</el-button>
                      <el-popconfirm
                          confirm-button-text='确定'
                          cancel-button-text='等一下再说'
                          icon="el-icon-info"
                          icon-color="red"
                          @confirm="delWeek('form')"
                          style="margin-left:2%"
                          title="确定要删除当前周报吗？"
                        >
                        <el-button slot="reference" type="danger" :disabled="deldisabled">删除周报</el-button>
                        </el-popconfirm>
                </el-form-item>
              </el-form>
          </div>
          <div class="table">
              <el-table
                :data="dailyReportStatus.dailtReportList.slice((currentPage-1)*pagesize,currentPage*pagesize)"
                stripe
                v-loading="loading"
                style="width: 100%">
                <el-table-column
                  prop="userId"
                  label="姓名">
                </el-table-column>
                <el-table-column
                  prop="dateAt"
                  label="提报日期">
                </el-table-column>
                <el-table-column
                  prop="projectId"
                  label="项目归属">
                </el-table-column>
                <el-table-column
                  prop="dailyReportStatus"
                  label="是否通过">
                </el-table-column>
              </el-table>
              <div class="pagination"><el-pagination
                    @size-change="handleSizeChange"
                    @current-change="current_change"
                    :current-page="currentPage"
                    :page-sizes="pageSizes"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                  </el-pagination>
                </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    </div>
</template>

<script>
//import { getProject } from "@/api/daily/daily";
import { getReauditDailys,getAllWeekInfo,delWeekReport } from "@/api/week/week";
//import { getUserProfile } from "@/api/system/user";


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

        table:"",
        currentPage:1,
        weekNumAndFromAndTo:[],
        from:"",
        to:"",
        total:0,
        pagesize:5,
        pageSizes:[5,10,20,30,50],
        deldisabled: true,

        projectStatus:"请先查询",
        selectBtnLoading:false,



        form:{
            region:"",
            weekNum:"",
            unitManagerName:""
        },



        dailyReportStatus:{
          dailtReportList:[]  
        },


        rules: {
          region: [
            { required: true, message: '请输入项目编号', trigger: 'blur' },
            { min : 10,max: 10,message:'请输入正确的项目编号'}
          ],
          weekNum: [
              { required: true, message: '请选择周号', trigger: 'change' }
          ],
          unitManagerName: [
              { required: true, message: '请输入单元负责人账号名', trigger: 'blur' },
              { min : 1,max: 6,message:'请输入正确的单元负责人账号名'}
          ]
        }
    };
  },
  created(){
    this.getAllWeekInfo();

  },
  methods :{
    delWeek(formName){
        this.$refs[formName].validate((valid) => {
            this.deldisabled = true;
          if (valid) {
              delWeekReport(this.form.unitManagerName,this.form.weekNum,this.form.region,this.from,this.to).then(res => {
                this.deldisabled = false;
                if(res.msg == "操作成功"){
                this.$message({
                  showClose: true,
                  message: '已成功删除周报信息！',
                  type: 'success'
                });
                this.getProjectForm('form');
                }else{
                    this.$message({
                  showClose: true,
                  message: '删除周报信息失败！请联系管理员',
                  type: 'error'
                });
                }
              })
          }else{
              return false;
          }
    });
    },
    weekNumclear(){
        this.from = "";
        this.to = "";
    },
    weekNumChange(){
        for(let i = 0;i < this.weekNumAndFromAndTo.length ;i++){
            if(this.form.weekNum == this.weekNumAndFromAndTo[i].weekNum){
                this.from = this.weekNumAndFromAndTo[i].from;
                this.to = this.weekNumAndFromAndTo[i].to;
            }
        }
    },
    getAllWeekInfo(){
        getAllWeekInfo().then(res => {
            this.weekNumAndFromAndTo = res.data;
        })
    },
    current_change:function(currentPage){
        this.currentPage = currentPage;
      },
      handleSizeChange(val) {
        this.pagesize = Number(`${val}`);
      },
      getProjectForm(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            // this.errorLog.open = true;
            this.selectBtnLoading = true;
            getReauditDailys(this.form.unitManagerName,this.form.region,this.form.weekNum,this.from,this.to).then(response =>{
            this.selectBtnLoading = false;
  
            this.exanmineFlag = response.weekReportStatus;
            this.staff = response.staffList;
            this.dailyReportStatus.dailtReportList = response.dailyReportList;
            for(let i = 0;i < this.dailyReportStatus.dailtReportList.length;i++){
                if(this.dailyReportStatus.dailtReportList[i].dailyReportStatus == "0"){
                    this.dailyReportStatus.dailtReportList[i].dailyReportStatus = "未审核";
                }
                if(this.dailyReportStatus.dailtReportList[i].dailyReportStatus == "1"){
                    this.dailyReportStatus.dailtReportList[i].dailyReportStatus = "通过";
                }
                if(this.dailyReportStatus.dailtReportList[i].dailyReportStatus == "2"){
                    this.dailyReportStatus.dailtReportList[i].dailyReportStatus = "已删除";
                }
            }
            this.total = response.dailyReportList.length;
            if(this.exanmineFlag == "" || this.exanmineFlag == null || this.exanmineFlag == "0"){
            this.projectStatus = "未审核";
            if(this.staff.length != 0){
            this.deldisabled = true;
            }else{
            this.projectStatus = "未查询到周报信息";
            this.deldisabled = true;
            }
            }else{
              this.projectStatus = "已审核";
              this.deldisabled = false;
            }
        })
          } else {
            return false;
          }
        });
      },
    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>
.pagination{
  margin:20px auto;
  padding-left: 36%;
}
.table{
    margin-top:20px;
}
</style>