<template>
  <div class="app-container">
    <!--查询结果Table显示-->
    <div>
      <el-divider content-position="left" >请选择生成报表的考勤月或起止日期</el-divider>
      <el-form ref="dataForm" :rules="rules" :model="form" label-well-code="right" label-width="120px">
        <el-row style="margin-top: 60px">
          <el-col :span="8">
            <el-form-item label="考勤月" prop="month">
              <el-date-picker
                v-model="form.month"
                :picker-options="pickerOptions"
                type="month"
                format="yyyy-MM"
                value-format="yyyy-MM"
                placeholder="考勤月"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="开始日期" prop="beginDateS">
              <el-date-picker
                v-model="form.beginDateS"
                :picker-options="beginDateOptions"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="开始日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="- 截止日期" prop="endDateS">
              <el-date-picker
                v-model="form.endDateS"
                :picker-options="endDateOptions"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="截止日期"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 20px;">
          <el-col :offset="17" :span="5">
            <el-button type="primary" size="medium" style="width: 180px" @click="makeReport">生成报表</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { makeReport } from '@/api/attendance'
import { downloadFile } from '@/utils/downloadUtils'

export default {
  name: 'MakeReport',
  data() {
    const validateMonth = (rule, value, callback) => {
      if (this.form.beginDateS === '' && this.form.endDateS ===  '' && value === '') {
          callback(new Error('请选择考勤月或起止日期'))
      } else {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        callback()
      }
    }
    const validateBeginDate = (rule, value, callback) => {
      if (this.form.month === '' && this.form.endDateS ===  '' && value === '') {
        callback(new Error('请选择考勤月或起止日期'))
      } else {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        callback()
      }
    }
    const validateEndDate = (rule, value, callback) => {
      if (this.form.beginDateS === '' && this.form.month ===  '' && value === '') {
          callback(new Error('请选择考勤月或起止日期'))
      } else {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        callback()
      }
    }
    return {
      beginDateOptions: {
        disabledDate: (time) => {
          if( this.form.endDateS !== '' ) return time.getTime() > new Date(this.form.endDateS)
          else return false
        }
      },
      endDateOptions: {
        disabledDate: (time) => {
          if( this.form.beginDateS !== '' ) return (time.getTime() < new Date(this.form.beginDateS) || time.getTime() > Date.now())
          else return time.getTime() > Date.now()
        }
      },
      form: {
        month: '',
        beginDateS: '',
        endDateS: ''
      },
      rules: {
        month: [{ validator: validateMonth, trigger: ['blur', 'change'] }],
        beginDateS: [{ validator: validateBeginDate, trigger: ['blur', 'change'] }],
        endDateS: [{ validator: validateEndDate, trigger: ['blur', 'change'] }]
      },
      pickerOptions: {
        disabledDate: (time) => {
          // const v = 24 * 60 * 60 * 1000 * 30
          return time.getTime() > Date.now()
        }
      }
    }
  },
  mounted() {},
  methods: {
    makeReport() {
      // 表单校验
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // 全屏加载动画
          const loading = this.$loading({
            lock: true,
            text: '数据处理中,请稍后...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          makeReport(this.form).then(response => {
            loading.close()
            downloadFile(response.data, '考勤报表')
            this.$message.success('生成报表成功')
          }).catch((res) => {
            loading.close()
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
