<!--特殊考勤日列表-->
<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item">
            <el-input v-model.trim="listQuery.name" placeholder="名称" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item" />
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        <el-col :span="12" :offset="6" class="edit_btns">
          <!--批量导入-->
          <el-upload
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFile"
            :file-list="fileList"
            action="string"
            accept=".xls,.xlsx"
            class="edit_btn"
            style="margin-top: 0">
            <el-button slot="trigger" class="edit_btn" size="small">批量导入</el-button>
          </el-upload>
          <download-template ref="downloadTemplate" :filename="filename" />
          <el-button class="edit_btn" size="small" @click="del">删除</el-button>
          <el-button class="edit_btn" size="small" @click="add">新增</el-button>
          <!--<el-button class="edit_btn" size="small" @click="downloadTemplate">模板下载</el-button>-->
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" row-class-name="small-row-class" size="small" border @selection-change="handleSelectionChange">
        <!--批量选择勾选框列-->
        <el-table-column align="center" type="selection" width="55"/>
        <!--序号列-->
        <el-table-column :index="indexMethod" label="#" align="center" type="index"/>
        <!--内容列-->
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row[column.value] }}</span>
            <span v-if="column.ext">{{ scope.row.ext[column.value] }}</span>
          </template>
        </el-table-column>
        <!--操作列-->
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
    <edit-special-day ref="editSpecialDay" @watchChild="fetchData"/>
  </div>
</template>
<script>

import { getSpecialDayList, delSpecialDay, batchImportSpecialDay } from '@/api/attendance'
import EditSpecialDay from './editSpecialDay' // getRegular, updateRegular, downloadTemplate, getSpecialDayList  ,
import DownloadTemplate from '@/components/DownloadTemplate/index'

export default {
  name: 'SpecialDay',
  components: { EditSpecialDay, DownloadTemplate },
  data() {
    return {
      listQuery: {
        name: '', // 节假日名称
        offset: 1,
        limit: 20,
        sort: 'beginDate',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '名称',
          value: 'name',
          align: 'center'
        },
        {
          text: '开始日期',
          value: 'beginDate',
          align: 'center'
        },
        {
          text: '结束日期',
          value: 'endDate',
          align: 'center'
        },
        {
          text: '是否需要考勤',
          value: 'isKaoqinName',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      fileList: [], // 上传文件列表
      listLoading: true, // 列表加载动画
      multipleSelection: [], // 多选选中项
      filename: 'specialDayTemp.xlsx'
    }
  },
  watch: {},
  created() {
    this.fetchData()// 获取数据
  },
  activated() { // 页面激活刷新数据
    this.fetchData(false)
  },
  methods: {
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 编辑特殊考勤日
    edit(row) {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editSpecialDay.initDialog('update', this.dialogFormVisible, row)
    },
    // 新增
    add() {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editSpecialDay.initDialog('create', this.dialogFormVisible)
    },
    // 删除
    del() {
      if (this.checkSelection()) {
        const ids = []
        this.multipleSelection.forEach(function(value, index) {
          ids.push(value.id)
          console.log(value.id)
        })
        // wellNames = wellNames.slice(0, wellNames.length - 1)
        this.$confirm(
          '确定要删除所选特殊考勤日吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delSpecialDay(ids).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取列表
    fetchData(isNowPage = true) {
      this.listLoading = false
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getSpecialDayList(this.listQuery).then(response => {
        if (response.code === 200) {
          this.list = response.data.rows
          this.total = parseInt(response.data.total)
        } else {
          this.$message.error(response.message)
        }
        this.listLoading = false
      })
    },
    // 批量导入
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 发起导入请求
      batchImportSpecialDay(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fileList = []
          this.fetchData(false)
        }
      }).catch(() => {
        loading.close()
      })
      this.fileList = []
    },
    // 序号计算
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:20px
  }
  .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>
