<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-select v-model="searchParam._mineIndex" placeholder="所属矿山" clearable size="small" class="filter-item" style="width: 200px;">
          <el-option v-for="mine in mineList" :key="mine.index" :label="mine.name" :value="mine.index" />
        </el-select>
        <el-select v-model="searchParam.auditState" placeholder="审核状态" clearable size="small" class="filter-item" style="width: 200px;">
          <el-option label="未审核" :value="1" />
          <el-option label="审核通过" :value="2" />
          <el-option label="审核不通过" :value="3" />
        </el-select>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="searchSubmit">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="searchParamReset">重置</el-button>
        <el-tooltip class="item" effect="dark" content="按当前查询条件导出" placement="top">
          <el-button :loading="exporting" class="filter-item" size="mini" type="info" plain icon="el-icon-download" @click="exportSubmit">导出</el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="打印当前页面内容" placement="top">
          <el-button class="filter-item" size="mini" type="info" plain icon="el-icon-printer" @click="print">打印</el-button>
        </el-tooltip>
      </div>
    </div>
    <!--弹出框1-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog1Show" :title="'确认审核'" width="580px">
      <div v-if="dialog1Show">
        <span>确认要</span>
        <span v-show="dialog1Data.auditState===2" style="color:#66cb71;font-weight:bold;">通过</span>
        <span v-show="dialog1Data.auditState===3" style="color:#e75851;font-weight:bold;">拒绝</span>
        <span style="font-weight:bold;">{{ dialog1Data.scheduling.schedulingDate }}</span>
        <span style="font-weight:bold;">{{ dialog1Data.leaderClassesName }}</span>
        <span>的变更申请吗？</span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialog1Show=false">取消</el-button>
        <el-button :loading="dialog1Saving" type="primary" @click="dialog1Submit">确认</el-button>
      </div>
    </el-dialog>
    <!--表格-->
    <el-table ref="table" v-loading="searching" :data="tableData" row-key="id">
      <el-table-column :show-overflow-tooltip="true" prop="id" label="编号" width="100px" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="mineName" label="所属矿山" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="staffName" label="带班领导姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.staffName ? scope.row.staffName : '【取消带班】' }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="scheduling.schedulingDate" label="带班日期" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="leaderClassesName" label="班次" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="note" label="备注" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="auditState" label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.auditState === 1 ? '未审核' : scope.row.auditState === 2 ? '审核通过' : '审核不通过' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button v-show="scope.row.auditState==1" size="mini" round @click="doAudit(scope.row,2)">通过</el-button>
          <el-button v-show="scope.row.auditState==1" size="mini" round @click="doAudit(scope.row,3)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      :current-page.sync="searchParam.page"
      :page-size.sync="searchParam.count"
      :total="searchResult.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="searchSubmit"
      @current-change="searchSubmit"
    />
  </div>
</template>

<script>
import leaderSchedulingApi from '@/api/kd/leaderScheduling'
import { mapGetters } from 'vuex'
import mineApi from '@/api/kd/mine'
import { downloadFile } from '@/utils'

const searchParam = { // 默认搜索条件
  auditState: 1,
  page: 1,
  count: 10,
  searchCount: true
}
export default {
  name: 'CLeaderSchedulingChange', // 本页面名
  components: { },
  data() {
    return {
      mineList: [],
      searching: false,
      exporting: false,
      searchParam: {},
      searchResult: {
        total: 0,
        results: [] // 搜索结果
      },
      dialog1Show: false,
      dialog1Saving: false,
      dialog1Data: {}
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'baseApi'
    ]),
    tableData() {
      // 此处对返回值进行算法重组
      return this.searchResult.results
    }
  },
  mounted() {
    // 矿山选项卡
    mineApi.getEnableMine().then(response => {
      let i = 1
      response.results.forEach(x => { x.index = i++ })
      this.mineList = response.results
    })
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      if (this.searchParam._mineIndex) {
        const mine = this.mineList[this.searchParam._mineIndex - 1]
        this.searchParam.mineType = mine.mineType
        this.searchParam.mineId = mine.id
      } else {
        this.searchParam.mineType = null
        this.searchParam.mineId = null
      }
      leaderSchedulingApi.getChangePage(this.searchParam).then(response => {
        this.searching = false
        this.searchResult = response
      }).catch(() => {
        this.searching = false
      })
    },
    exportSubmit() {
      this.exporting = true
      if (this.searchParam._mineIndex) {
        const mine = this.mineList[this.searchParam._mineIndex - 1]
        this.searchParam.mineType = mine.mineType
        this.searchParam.mineId = mine.id
      } else {
        this.searchParam.mineType = null
        this.searchParam.mineId = null
      }
      leaderSchedulingApi.exportChange(this.searchParam).then(response => {
        downloadFile(response, '排班变更', 'xlsx')
        this.exporting = false
      }).catch(() => {
        this.exporting = false
      })
    },
    doAudit(changeInfo, auditState) {
      this.dialog1Data = JSON.parse(JSON.stringify(changeInfo))
      this.dialog1Data.auditState = auditState
      this.dialog1Show = true
    },
    dialog1Submit() {
      this.dialog1Saving = true
      leaderSchedulingApi.auditChange(this.dialog1Data).then(() => {
        this.$message.success('审核完成')
        this.dialog1Show = false
        this.dialog1Saving = false
        this.searchSubmit()
      })
    },
    print() {
      window.localStorage.setItem('kd.print.model', 'c/leader/change')
      window.localStorage.setItem('kd.print.data', JSON.stringify(this.tableData))
      window.open(location.protocol + '//' + window.location.host + '/#/print')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
