<template>
  <div class="lendConfirm-box">
    <div class="head-container">
      <el-button size="mini" class="iconfont icon-jiechu-fanbai" :disabled="!selections.length" @click="handleLendBtn">借出</el-button>
      <el-button size="mini" class="iconfont icon-dengji-fanbai" :disabled="!selections.length" @click="handelReRecord">重新登记</el-button>
      <el-button size="mini" class="iconfont icon-yichu-fanbai" :disabled="!selections.length" @click="handleRemove">移出</el-button>
      <el-button size="mini" icon="el-icon-download" :disabled="!selections.length" :loading="crud.downloadLoading" @click="downloadApi">导出</el-button>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      :row-key="getRowKey"
      style="min-width: 100%"
      @select-all="selectAll"
      @selection-change="selectionChangeHandler"
      @row-click="clickRowHandler"
      @row-dblclick="handleDbClick"
      @select="handleCurrentChange"
    >
      <el-table-column type="selection" :reserve-selection="true" align="center" width="55" />
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column prop="orderNo" align="center" label="单据号" min-width="100" />
      <el-table-column prop="borrowNum" align="center" label="数量" min-width="50" />
      <el-table-column prop="borrowerName" align="center" label="借阅人" min-width="60" />
      <el-table-column prop="department" align="center" label="所属部门" min-width="85" />
      <el-table-column prop="cardType" align="center" label="证件类型" min-width="85" />
      <el-table-column prop="idcard" align="center" label="证件号码" min-width="120" />
      <el-table-column prop="phone" align="center" label="电话号码" min-width="85" />
      <el-table-column prop="lendDates" align="center" label="借阅时间" min-width="150">
        <template slot-scope="scope">
          <div>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') + ' 至 ' + parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="purpose" align="center" label="借阅目的" min-width="70" />
      <el-table-column prop="borrowType" align="center" label="借阅状态" min-width="70">
        <template slot-scope="scope">
          <!-- 待借阅 -->
          <span class="cell-lend wait-lend" style="width:76px">{{ scope.row.borrowType | borrowStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" align="center" label="操作时间" min-width="120">
        <template slot-scope="scope">
          <div>{{ scope.row.createTime | parseTime }}</div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination />
    <!--借出弹框-->
    <releaseAlarm ref="releaseAlarmDom" />
    <!-- 移出确认弹框 -->
    <delConfirm ref="delConfirmDom" :list-name="listName" :is-list-type="isListType" />
    <!-- 档案详情 -->
    <archiveDetail ref="archiveDetailDom" />
  </div>
</template>

<script>
import { FetchReRegister } from '@/api/archivesManage/lendManage'
import pagination from '@crud/Pagination'
import CRUD, { presenter } from '@crud/crud'
import { lendingCrud } from '../mixins/lending'
import delConfirm from '../components/delConfirm'
import archiveDetail from '../components/archiveDetail'
import releaseAlarm from '../components/releaseAlarm'
export default {
  name: 'LendConfirm',
  components: { pagination, archiveDetail, delConfirm, releaseAlarm },
  mixins: [presenter(), lendingCrud],
  cruds() {
    return CRUD({
      url: 'api/borrow/initWaitBorrowList',
      // crudMethod: caseCrudMethod,
      title: '借出确认',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        group: false
      }
    })
  },
  data() {
    return {
      selections: [],
      listName: '借出确认',
      isListType: 2, // 移出框类型判断 待借1 / 借阅2
      lendDates: null
    }
  },
  created() {
    this.getBorrowRule()
  },
  methods: {
    // table - 全选
    selectAll(val) {
      this.selections = val
      if (this.selections.length === 1) {
        this.$emit('getSelections', this.selections[0])
      } else {
        this.$emit('getSelections', null)
      }
    },
    // 触发单选
    handleCurrentChange(selection, row) {
      this.selections = selection
      if (selection.length === 1) {
        this.$emit('getSelections', selection[0])
      } else {
        this.$emit('getSelections', null)
      }
    },
    clickRowHandler(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
      this.selections = []
      this.selections.push(row)
      this.$emit('getSelections', row)
    },
    // 重新登记返回上一级
    handelReRecord() {
      const params = this.selections.map(item => item.orderNo)
      FetchReRegister(params).then(data => {
        console.log(data)
        if (data) {
          this.$message({
            message: '重新登记成功',
            type: 'success'
          })
          this.$emit('callBack', { index: 0 })
          this.crud.refresh()
        }
      })
    },
    // 移出
    handleRemove() {
      if (this.selections.length > 0) {
        this.$refs.delConfirmDom.deleteVisible = true
        this.$refs.delConfirmDom.deltSelections = this.selections
      }
    },
    // 借出
    handleLendBtn() {
      if (this.selections.length > 0) {
        if (this.lineStateVal === 'offline') {
          // 离线
          this.$refs.releaseAlarmDom.lendSelections = this.selections
          this.$refs.releaseAlarmDom.getLendTid(0)
        } else {
          // 在线
          const params = this.selections.map(item => item.orderNo)
          this.confirmLendOrReturn(0, params, this.selections)
        }
        this.$refs.table.clearSelection()
        this.$emit('getSelections', null)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/styles/lend-manage.scss';
</style>
