<template>
    <a-card :bordered="false">
  
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="时间">
                <a-range-picker
                    style="width: 100% !important"
                    v-model="DateTime"
                    format="YYYY-MM"
                    valueFormat="YYYY-MM"
                    :mode="mode2"
                    :open="open"
                    :placeholder="['开始时间', '结束时间']"
                    @panelChange="handlePanelChange2"
                    @openChange="onDateChange"
                    @change="changeData"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
            </a-col>
  
          </a-row>
        </a-form>
      </div>
  
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="UploadAttendanceSheet" type="primary" icon="plus">上传考勤表</a-button>
        <a-button @click="addPayroll" v-has="'salaryAttendance:importExcel:flj'" type="primary" icon="plus">导入薪金</a-button>
      </div>
  
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :scroll="{x: 1000}"
          :loading="loading"
          @change="handleTableChange">
           <span slot="operation" slot-scope="text, record">
                <a @click="handleDetail(record)" >查看详情</a>
                <a-divider type="vertical" />
                <a @click="handleDownload(record)" >下载</a>

           </span>
        </a-table>
      </div>
      <!-- table区域-end -->
  
      <!-- 表单区域 -->
      <AttendancelDetail ref="AttendancelDetail"></AttendancelDetail>
      <OvertimeDetail ref="OvertimeDetail"></OvertimeDetail>
      <AnnualLeave ref="AnnualLeave"></AnnualLeave>
      <uploadPayroll ref="uploadPayroll" @ok="searchQuery"></uploadPayroll>
      <uploadModal ref="uploadModal" @ok="searchQuery"></uploadModal>
    </a-card>
  </template>
  
  <script>
    import JEllipsis from '@/components/jeecg/JEllipsis'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import { getAction,downFile} from '@/api/manage'
    import moment from 'moment'
    import AttendancelDetail from './modules/AttendancelDetail.vue'
    import OvertimeDetail from './modules/OvertimeDetail.vue'
    import AnnualLeave from './modules/AnnualLeave.vue'
    import uploadPayroll from '../personnelSalary/modules/uploadPayroll.vue'
    import uploadModal from './modules/uploadModal.vue'
    export default {
      name: "attendanceManagement",
      mixins: [JeecgListMixin],
      components: {
        JEllipsis,
        AttendancelDetail,
        AnnualLeave,
        OvertimeDetail,
        uploadModal,uploadPayroll
      },
      data () {
        let ellipsis = (v, l) => <j-ellipsis value={v} length={l} />
        return {
          description: '考勤管理',
          DateTime:[],
          yearWithMonth:'',
          open:false,
          mode2: ['month', 'month'],
          // 表头
          columns: [
            {
              title: '序号',
              width: 80,
              align:"center",
              dataIndex: 'index',
              customRender:function (t, r, index) {
                    return parseInt(index)+1;
              }
            },
            {
              title: '月份',
              align:"center",
              dataIndex: 'yearWithMonth', 
            },
            {
              title: '类型',
              align:"center",
              dataIndex: 'type', 
            },
            {
              title: '名称   ',
              align:"center",
              dataIndex: 'name'
            },
            {
              title: '上传时间',
              align:"center",
              dataIndex: 'createTime'
            },
            {
              title: '备注',
              align:"center",
              width: 350,
              customRender: (t) => ellipsis(t,22),
              dataIndex: 'remarks'
            },
            {
              title: '版本',
              align:"center",
              dataIndex: 'version'
            },
            {
              title: '操作',
              align:"center",
              dataIndex: 'operation',
              width: 200,
              scopedSlots: { customRender: 'operation' } 
            },
          ],
          queryParam:{},
          dataSource:[{}],
          ipagination:{}
          }
        },
      computed: {},
      created () {
        this.getTableList()
      },
      methods: {
        searchQuery(){
          this.queryParam.pageNo =1
          this.getTableList()
        },
        searchReset(){
          
          this.queryParam={}
          this.DateTime = []
          this.getTableList()
        },
        getTableList(){
          this.loading = true
          getAction('/salary/salaryAttendance/list',this.queryParam).then(res=>{
            this.loading = false
            if(res.success){
                this.dataSource =res.result.records
                this.dataSource.map(item=>item.version=item.version+'.0')
                this.ipagination = {
                  total: res.result.total,
                  current: res.result.current,
                  pageSize: res.result.size
                }
            }else{
                this.$message.error(res.message);
            }
          })
        },
        addPayroll(){
          this.$refs.uploadPayroll.visible = true
        },
        UploadAttendanceSheet(){
          this.$refs.uploadModal.visible = true
        },
        handleDetail(record){
          if(record.type=='考勤表'){
            this.$refs.AttendancelDetail.visible = true
            this.$refs.AttendancelDetail.detail(record.id)         
          }else if(record.type=='年休表'){
            this.$refs.AnnualLeave.visible = true
            this.$refs.AnnualLeave.detail(record.id)        
          }
            
        },
        changeData(value){
            this.DateTime = value
            if(value == null || value.length == 0){
              this.queryParam.yearWithMonth_begin = ''
              this.queryParam.yearWithMonth_end = ''
            }
        },
        onDateChange(status) {
            if(status){
                this.open = true;
            }else{
                this.open = false
            }
        },
        handlePanelChange2(value,mode){
            if (this.DateTime[1] && this.DateTime[1]._d != value[1]._d) {
                this.open = false;
            }
            this.DateTime = value
            this.queryParam.yearWithMonth_begin = this.DateTime[0]&&this.DateTime[0]!==''?moment(this.DateTime[0]).format('YYYY-MM'):''
            this.queryParam.yearWithMonth_end = this.DateTime[1]&&this.DateTime[1]!==''?moment(this.DateTime[1]).format('YYYY-MM'):''
            this.mode2 = [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]];
        },
        handleDownload(record){
           this.$message.info('下载中，请稍后...')
          downFile('/salary/salaryAttendance/exportXls',{id:record.id,type:record.type}).then(data => {
            if (!data) {
              this.$message.warning('文件下载失败')
              return
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }),  record.type + '.xlsx')
            } else {
              let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
              let link = document.createElement('a')
              link.style.display = 'none'
              link.href = url
              link.setAttribute('download',  record.type+'.xlsx')
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link) // 下载完成移除元素
              window.URL.revokeObjectURL(url) // 释放掉blob对象
            }
             this.$message.success('下载完成，请查看')
          })

        },
        onChange(data){
          this.yearWithMonth = data
          this.queryParam.yearWithMonth = this.yearWithMonth?moment(this.yearWithMonth).format('YYYY-MM'):''
        },
        handleTableChange(pagination, filters, sorter) {
            this.queryParam.pageNo = pagination.current
            this.queryParam.pageSize = pagination.pageSize
            this.getTableList()
        },
      }
    }
  </script>
  <style scoped>
    @import '~@assets/less/common.less'
  </style>