<template>
  <d2-container>
    <div class="d2-table-view-full">
      <div class="d2-table-view-header">
        <el-form size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline">
          <el-form-item label="考勤月份">
            <el-date-picker
						        :editable="false"
                    :clearable="false"
                    :default-value="filter.startTime"
                    v-model="filter.startTime"
                    type="month"
                    value-format="yyyy-MM">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="部门">
            <department-tree @clearInput="clearDepartment" v-model="filter.depId" :isClear="isClear"/>
          </el-form-item>
          <el-form-item label="教工工号">
            <el-input v-model="filter.userNo" placeholder="教工工号" clearable/>
          </el-form-item>
          <!-- <el-form-item label="学生类型">
            <custom-select :vallue="filter.empType" :isAll="true" v-model="filter.empType" typeName="学生类型"/>
          </el-form-item> -->
          <!-- <el-form-item label="教工状态">
            <custom-select :vallue="filter.empStatus" :isAll="true" v-model="filter.empStatus" typeName="员工状态"/>
          </el-form-item> -->
          <el-form-item>
            <el-button   type="primary" @click="handleList('1')">查询</el-button>
            <el-button v-if="func.hrAnalyse" :loading="AnaLoading" type="success" @click="deal()">考勤分析</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="d2-table-view-content">
        <d2-crud
                title="考勤月报表列表"
                ref="d2Crud"
                :options="tableOptions"
                :loading="loading"
                :columns="columns"
                :data="data">
				<el-button v-if="func.out" slot="headerButton" type="primary" size="small" @click="exportFile()">导出</el-button>
        </d2-crud>
      </div>
      <div class="d2-table-view-footer">
        <el-pagination
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                :current-page.sync="currentPage"
                :page-size="tableOptions.pageList[0]"
                :page-sizes="tableOptions.pageList"
                layout="total,sizes, prev, pager, next,jumper"
                :total="pagination.total">
        </el-pagination>
      </div>
			 <el-dialog
					title="分析进度"
					:show-close="false"
					:close-on-click-modal="false"
					:close-on-press-escape="false"
					:visible.sync="progressVisible"
					width="20%">
			  <el-progress :percentage="progress"/>
			</el-dialog>
    </div>
  </d2-container>
</template>

<script>
  import {getDayReportMonthByPage} from '../../../api/att/report'
  import {tableOptions} from '../../../assets/options'
  import departmentTree from '../../../components/department-tree/index'
	 import util from '../../../libs/util'
  export default {
    name: 'attendance-month',
    components:{
      departmentTree
    },
    data () {
      return {
				progress:0,
				progressVisible:false,
				AnaLoading:false,
				isClear:'123',
        func:{
          select:false,
					out:false,
          hrAnalyse:false
        },
        time: '',
        isExpend:true,
        tableOptions,
        operVisible:false,
        startDate:'',
        endDate:'',
        dealTime:'',
        loading:false,
        currentPage:1,
        systemRoles:[],
        filter:{
          userNo:'',
          startTime:'',
          userTyp:'0',
          depId:'',
          pageSize: 10,
          pageNum:1
        },
        pagination:{
          total: 0
        },
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '序号',
            key: 'index',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              return index+1
            }
          },
          {
            title: '教工工号',
            key: 'userNo',
            minWidth: '120',
					  showOverflowTooltip: true
          },
          {
            title: '教工姓名',
            key: 'userNm',
            minWidth: '90'
          },
          /* {
            title: '员工类型',
            key: 'empTypeName',
            minWidth: '90'
          }, */
          {
            title: '所属部门',
            key: 'depNm',
            minWidth: '150'
          },
         /* {
            title: '工作部门',
            key: 'workOrgName',
            minWidth: '150'
          }, */
          {
            title: '报到日期',
            key: 'reportDate',
            minWidth: '150',
						showOverflowTooltip: true
          },
        ],
        data: []
      }
    },
    methods: {
      handleList (val) {

        // if (!this.filter.ym) {
        //   this.$message({
        //     message: '请选择月份',
        //     type: 'error'
        //   })
		//
        //   return false
        // }

        if(val){this.filter.pageNum=1
          this.currentPage = 1}
        this.loading = true
        getDayReportMonthByPage(this.filter).then(res=>{
          this.loading = false
          this.columns = [
            {
              title: '序号',
              key: 'index',
              minWidth: '50',
              formatter (row, column, cellValue, index) {
                return index+1
              }
            },
            {
              title: '教工工号',
              key: 'userNo',
              minWidth: '120',
              showOverflowTooltip: true
            },
            {
              title: '教工姓名',
              key: 'userNm',
              minWidth: '90'
            },
            /* {
              title: '员工类型',
              key: 'empTypeName',
              minWidth: '90'
            }, */
            {
              title: '所属部门',
              key: 'depNm',
              minWidth: '130'
            },
           /* {
              title: '工作部门',
              key: 'workOrgName',
              minWidth: '130'
            }, */
            {
              title: '报到日期',
              key: 'reportDate',
              minWidth: '130',
							showOverflowTooltip: true
            },
          ]
          let temp = []
          let num = (res.thisMonthDays || 0) + 1
          for(let i = 1;i < num;i++) {
            temp.push({
              title: i,
              key: `num${i}`,
              minWidth: '100'
            })
          }
          let temp2 = [{
            title: '请假天数',
            key: 'leaveDays',
            minWidth: '100',
							formatter (row, column, cellValue, index) {
						   if(row.leaveDays){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          },{
            title: '在岗日',
            key: 'guardDay',
            minWidth: '100',
						formatter (row, column, cellValue, index) {
						   if(row.guardDay){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          },{
            title: '请假日',
            key: 'leaveDay',
            minWidth: '100',
						formatter (row, column, cellValue, index) {
						   if(row.leaveDay){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          },{
            title: '旷工日',
            key: 'absenteeismDay',
            minWidth: '100',
						formatter (row, column, cellValue, index) {
						   if(row.absenteeismDay){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          },{
            title: '出勤日',
            key: 'attendanceDay',
            minWidth: '100',
						formatter (row, column, cellValue, index) {
						   if(row.attendanceDay){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          },{
            title: '加班日',
            key: 'overDay',
            minWidth: '100',
						formatter (row, column, cellValue, index) {
						   if(row.overDay){
								 return cellValue
							 }else{
								 return '0'
							 }
						}
          }]

          temp = temp.concat(temp2)
           this.$nextTick(()=> {
           this.columns = this.columns.concat(temp)
           })
          //this.columns = this.columns.concat(temp)
          this.data = res.page.list
          this.pagination.total = res.page.totle
        }).catch(err=>{
          this.loading = false
        })
      },
      deal () {
         this.AnaLoading=true
				 this.progressVisible=true
				  let time = setInterval(()=>{
				   if (this.progress < 99) {
				     this.progress ++
				   }
				 },3000)
        GetMonthAnaly(this.filter).then(res=>{
					clearInterval(time)
					this.progress = 100
					setTimeout(()=>{
						 this.progressVisible = false
						this.progress = 0
          this.$message({
            message: '分析成功',
            type: 'success'
          })
          this.AnaLoading=false
          this.handleList('1')
				},3000)
        }).catch(err=>{
          this.AnaLoading=false
						clearInterval(time)
					this.progressVisible=false
					    this.operVisible = false
							this.progress = 0
        })
      },
      clearDepartment(){
        this.filter.depId=''
      },
      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },
      handleSizeChange (val) {
        this.filter.pageNum = 1
        this.filter.pageSize = val
        this.handleList()
      },
      dateChange (val) {
        if(val == null){
          this.filter.startDate = ''
          this.filter.endDate = ''
        }else{
          this.filter.startDate = val[0]
          this.filter.endDate = val[1]
        }
      },
      dealDateChange (val) {
        if(val == null){
          this.startDate = ''
          this.endDate = ''
        }else{
          this.startDate = val[0]
          this.endDate = val[1]
        }
      },
      clearForm () {
        this.dealTime = ''
        this.startDate = ''
        this.endDate = ''
      },
			exportFile(){
					  if (!this.data.length){
					    this.$message({
					      message: '页面无数据,无法导出',
					      type: 'error'
					    })
					    return false
					  }
					  const token = util.cookies.get('token')
					  window.open(`${process.env.VUE_APP_API}/cons/downDayReportMonthByExcel?&orgId=${this.filter.orgId}&empCode=${this.filter.empCode}&startTime=${this.filter.startTime}&empStatus=${this.filter.empStatus || '' }&empType=${this.filter.empType|| ''}&token=${token}`)
			},
   /*   getFunction () {
        this.$GetFunction({
          menuUrl:this.$route.path
        }).then(res=>{
          res.data.forEach(item=>{
            if(item.functionType === 'select') {
              if (item.remarks === 'true') {
                this.func.select = true
              } else {
                this.func.select = false
              }
            } else if(item.functionType === 'hrAnalyse') {
              if (item.remarks === 'true') {
                this.func.hrAnalyse = true
              } else {
                this.func.hrAnalyse = false
              }
            }else if(item.functionType === 'out') {
              if (item.remarks === 'true') {
                this.func.out = true
              } else {
                this.func.out = false
              }
            }
          })
        })
      } */
    },
    mounted () {
      //this.getFunction()
      this.filter.startTime = new Date().format('yyyy-MM')
      // this.time = this.$setInitTime()
      // this.filter.startDate =  this.time[0].format('yyyy-MM-dd')
      // this.filter.endDate = this.time[1].format('yyyy-MM-dd')
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions,20)


      this.handleList()
    }
  }
</script>
