<template>
  <div>
    <div>
      <!-- 入库记录列表 -->
      <div class="ffjl" v-show="!showDetail">
        <el-form :model="queryParams" ref="queryForm" class="search-area" :inline="true" label-width="88px">
          <el-row>
            <el-col :span="24">
              <!-- 名称 -->
              <el-form-item :label="$t('page.business.resource.gsp.mc') + '：'">
                <el-input v-model="queryParams.mc" clearable @change="handleQuery"
                  :placeholder="$t('form.placeholderInput')" />
              </el-form-item>
              <!-- 编号 -->
              <el-form-item :label="$t('page.business.resource.gsp.bh') + '：'">
                <el-input v-model="queryParams.bh" clearable @change="handleQuery"
                  :placeholder="$t('form.placeholderInput')" />
              </el-form-item>
              <!-- 规格 -->
              <el-form-item :label="$t('page.business.resource.gsp.gg') + '：'">
                <el-input v-model="queryParams.gg" clearable @change="handleQuery"
                  :placeholder="$t('form.placeholderInput')" />
              </el-form-item>
              <!-- 记录状态 -->
              <el-form-item :label="$t('page.business.resource.gsp.jlzt') + '：'">
                <el-select v-model="queryParams.jlzt" :placeholder="$t('form.placeholderSelect')" clearable
                  @change="handleQuery">
                  <el-option :label="$t('page.business.resource.resource.jlzt.wsd')" :value="1" />
                  <el-option :label="$t('page.business.resource.resource.jlzt.ysd')" :value="3" />
                  <el-option :label="$t('page.business.resource.resource.jlzt.dgd')" :value="5" />
                  <el-option :label="$t('page.business.resource.resource.jlzt.gd')" :value="7" />
                  <el-option :label="$t('page.business.resource.resource.jlzt.djd')" :value="9" />
                </el-select>
              </el-form-item>
              <!-- 借阅状态 -->
              <el-form-item :label="$t('page.business.resource.gsp.jyzt') + '：'">
                <el-select v-model="queryParams.jyzt" :placeholder="$t('form.placeholderSelect')" clearable
                  @change="handleQuery">
                  <el-option :label="$t('page.business.resource.resource.jyzt.wjy')" :value="1" />
                  <el-option :label="$t('page.business.resource.resource.jyzt.djy')" :value="3" />
                  <el-option :label="$t('page.business.resource.resource.jyzt.jyz')" :value="5" />
                </el-select>
              </el-form-item>
              <!-- 入库日期 -->
              <el-form-item :label="$t('page.business.resource.mjy.rksj') + '：'">
                <el-date-picker v-model="daterangeRk" class="chat-histogram-daterange" type="daterange"
                  range-separator="-" :start-placeholder="$t('form.startDate')" :end-placeholder="$t('form.endDate')"
                  value-format="yyyy-MM-dd" @change="handleQuery" style="width: 250px;" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('form.search') }}</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('form.reset') }}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <!-- 新增 -->
            <el-button type="primary" @click="handleXz" v-hasPermi="['business:resource:gsp:rkxz']">{{
              $t('form.add') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 导入 -->
            <el-button type="primary" @click="handleXz" v-hasPermi="['business:resource:gsp:rkdr']">{{
              $t('page.business.resource.gsp.dr') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量归档 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlgd"
              v-hasPermi="['business:resource:gsp:rkgd']">{{
                $t('page.business.resource.gsp.plgd') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量锁定 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlsd"
              v-hasPermi="['business:resource:gsp:rksd']">{{
                $t('page.business.resource.gsp.plsd') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 导出 -->
            <el-button type="primary" @click="exportExcel"
              v-hasPermi="['business:resource:gsp:rkdc']">{{
                $t('form.export') }}</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
          <el-table-column type="selection" fixed="left" width="55" align="center" :selectable="checkSelectable" />
          <el-table-column :label="$t('page.business.resource.gsp.mc')" align="left" prop="mc"
            :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.resource.gsp.ph')" align="center" prop="ph"
            :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.resource.gsp.gg')" align="center">
            <template slot-scope="scope">
              {{ scope.row.gg }} {{ scope.row.ggdw }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('page.business.resource.gsp.rksj')" align="center" prop="rksj" width="150px"
            :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.resource.gsp.rkl')" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.rkl }} {{ scope.row.rkdw }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('page.business.resource.gsp.cctj')" align="center" prop="cctj" />
          <el-table-column :label="$t('page.business.resource.gsp.zysx')" align="center" prop="zysx"
            :show-overflow-tooltip="true" />
          <el-table-column :label="$t('page.business.resource.gsp.jlzt')" align="center" width="100px" fixed="right">
            <template slot-scope="scope">
              <span v-if="scope.row.jlzt == 1">{{ $t('page.business.resource.resource.jlzt.wsd') }}</span>
              <span v-if="scope.row.jlzt == 3">{{ $t('page.business.resource.resource.jlzt.ysd') }}</span>
              <span v-if="scope.row.jlzt == 5">{{ $t('page.business.resource.resource.jlzt.dgd') }}</span>
              <span v-if="scope.row.jlzt == 7">{{ $t('page.business.resource.resource.jlzt.gd') }}</span>
              <span v-if="scope.row.jlzt == 9">{{ $t('page.business.resource.resource.jlzt.djd') }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('page.business.resource.gsp.jyzt')" align="center" width="100px" fixed="right">
            <template slot-scope="scope">
              <span v-if="scope.row.jyzt == 1">{{ $t('page.business.resource.resource.jyzt.wjy') }}</span>
              <span v-if="scope.row.jyzt == 3">{{ $t('page.business.resource.resource.jyzt.djy') }}</span>
              <span v-if="scope.row.jyzt == 5">{{ $t('page.business.resource.resource.jyzt.jyz') }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('form.operate')" fixed="right" align="center"
            class-name="small-padding fixed-width" width="250px">
            <template slot-scope="scope">
              <!-- 编辑 -->
              <el-button v-if="scope.row.jlzt == 1" type="text" @click="handleBj(scope.row)"
                v-hasPermi="['business:resource:gsp:rkbj']">{{
                  $t('form.edit') }}</el-button>
              <!-- 详情 -->
              <el-button v-if="scope.row.jlzt == 1 || scope.row.jlzt == 3 || scope.row.jlzt == 7" type="text"
                @click="handleXq(scope.row)" v-hasPermi="['business:resource:gsp:rkxq']">{{
                  $t('page.business.resource.gsp.detail') }}</el-button>
              <!-- 锁定 -->
              <el-button type="text" v-if="scope.row.jlzt == 1" @click="handleSd(scope.row)"
                v-hasPermi="['business:resource:gsp:rksd']">{{
                  $t('page.business.resource.gsp.lock') }}</el-button>
              <!-- 解锁 -->
              <el-button type="text" v-if="scope.row.jlzt == 3" @click="handleJs(scope.row)"
                v-hasPermi="['business:resource:gsp:rkjs']">{{
                  $t('page.business.resource.gsp.unlock') }}</el-button>
              <!-- 归档 -->
              <el-button type="text" v-if="scope.row.jlzt == 3" @click="handleGd(scope.row)"
                v-hasPermi="['business:resource:gsp:rkgd']">{{
                  $t('page.business.resource.gsp.guidang') }}</el-button>
              <!-- 解档 -->
              <el-button type="text" v-if="scope.row.jlzt == 7" @click="handleJd(scope.row)"
                v-hasPermi="['business:resource:gsp:rkjd']">{{
                  $t('page.business.resource.gsp.jiedang') }}</el-button>
              <!-- 借阅 -->
              <el-button type="text" v-if="scope.row.jlzt == 7 && scope.row.jyzt == 1" @click="handleJy(scope.row)"
                v-hasPermi="['business:resource:gsp:rkjy']">{{
                  $t('page.business.resource.gsp.jieyue') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
      <!-- 锁定 -->
      <Sd key="Sd" ref="Sd" @callback="handleQuery" />
      <!-- 解锁 -->
      <Js key="Js" ref="Js" @callback="handleQuery" />
      <!-- 归档 -->
      <Gd key="Gd" ref="Gd" @callback="handleQuery" />
      <!-- 解档 -->
      <Jd key="Jd" ref="Jd" @callback="handleQuery" />
      <!-- 借阅 -->
      <Jy key="Jy" ref="Jy" @callback="handleQuery" />
      <!-- 借阅 -->
      <Xq key="Xq" v-show="showDetail" ref="Xq" @callback="closeXq" />
      <!-- 编辑 -->
      <Bj key="Bj" ref="Bj" @callback="handleQuery" />
      <!-- 新增 -->
      <Xz key="Xz" ref="Xz" @callback="handleQuery" />
    </div>
  </div>
</template>

<script>
import { list } from "@/api/business/gsp/gspRkjl"
import Sd from "./rkjl/Sd";
import Jy from "./rkjl/Jy";
import Js from "./rkjl/Js";
import Gd from "./rkjl/Gd";
import Jd from "./rkjl/Jd";
import Xq from "./rkjl/Xq";
import Xz from "./rkjl/Xz";
import Bj from "./rkjl/Bj";
import { deepClone } from "@/utils/index";
export default {
  name: "FfjlList",
  components: { Sd, Js, Gd, Jd, Jy, Xq, Xz, Bj },
  data() {
    return {
      daterangeCk: [],
      daterangeRk: [],
      loading: true,
      showDetail: false,
      single: true,
      multiple: true,
      total: 0,
      list: [],
      //勾选列表
      selectList: [],
      //查询条件
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mc: null,
        mdId: null,
        jlzt: null,
        jyzt: null,
        bh: null,
        startDateCk: null,
        endDateCk: null,
        startDateRk: null,
        endDateRk: null,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //导出
    exportExcel() {
      let that = this
      that.$modal.loading()
      let params = deepClone(this.queryParams)
      params.pageSize = 999999
      list(params).then(response => {
        var tabelStr =
          '<table border="1"  class="html-tabel">' +
          '<tr style="background:#eee;">' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.mc') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.ph') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.gg') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.rksj') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.kcl') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.cctj') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.zysx') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.jlzt') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.jyzt') + '</th>' +
          ' </tr>'
        let _datastr = ''
        _.forEach(response.rows, function (value) {
          let jlzt = ''
          switch (value.jlzt) {
            case 1:
              jlzt = that.$t('page.business.resource.resource.jlzt.wsd')
              break
            case 3:
              jlzt = that.$t('page.business.resource.resource.jlzt.ysd')
              break
            case 5:
              jlzt = that.$t('page.business.resource.resource.jlzt.dgd')
              break
            case 7:
              jlzt = that.$t('page.business.resource.resource.jlzt.gd')
              break
            case 9:
              jlzt = that.$t('page.business.resource.resource.jlzt.djd')
              break
            default: jlzt = ''
          }
          let jyzt = ''
          switch (value.jyzt) {
            case 1:
              jyzt = that.$t('page.business.resource.resource.jyzt.wjy')
              break
            case 3:
              jyzt = that.$t('page.business.resource.resource.jyzt.djy')
              break
            case 5:
              jyzt = that.$t('page.business.resource.resource.jyzt.jyz')
              break
            default: jyzt = ''
          }
          _datastr =
            _datastr +
            '<tr style="text-align: center;">' +
            ' <td>' +
            value.mc +
            '</td>' +
            ' <td>' +
            '&nbsp;' + value.ph +
            '</td>' +
            ' <td>' +
            value.gg + value.ggdw +
            '</td>' +
            ' <td>' +
            value.rksj +
            '</td>' +
            ' <td>' +
            value.rkl + value.rkdw +
            '</td>' +
            ' <td>' +
            value.cctj +
            '</td>' +
            ' <td>' +
            value.zysx +
            '</td>' +
            ' <td>' +
            jlzt +
            '</td>' +
            ' <td>' +
            jyzt +
            '</td>' +
            '</tr>'
        })
        tabelStr = tabelStr + _datastr + ' <table>'
        // Worksheet名
        var worksheet = that.$t('page.business.resource.gsp.gsprkjl')
        var uri = 'data:application/vnd.ms-excel;base64,'
        // 真正要导出（下载）的HTML模板
        var exportTemplate = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
                    xmlns="http://www.w3.org/TR/REC-html40">
                    <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
                        <x:Name>${worksheet}</x:Name>
                            <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
                        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
                    </head>
                    <body>
                        ${tabelStr}
                    </body>
                    </html>`
        var a = document.createElement('a')
        a.download = worksheet + '.xls'
        a.href = uri + window.btoa(unescape(encodeURIComponent(exportTemplate)))
        a.click()
        that.$modal.closeLoading()
      }).finally(() => {
        that.$modal.closeLoading()
      })
    },
    handleBj(row) {
      this.$refs.Bj.show(row)
    },
    handleXz() {
      this.$refs.Xz.show()
    },
    //加签
    handleJq(row) {
      this.$refs.Jq.show(row)
    },
    //锁定
    handleSd(row) {
      this.$refs.Sd.show(row)
    },
    //解锁
    handleJs(row) {
      this.$refs.Js.show(row)
    },
    //批量归档
    handlePlgd() {
      this.$refs.Gd.showBatch(this.selectList)
    },
    //批量锁定
    handlePlsd() {
      this.$refs.Sd.showBatch(this.selectList)
    },
    closeXq() {
      this.showDetail = false
      this.$emit('showDetail', this.showDetail)
      this.handleQuery()
    },
    //详情
    handleXq(row) {
      this.showDetail = true
      this.$emit('showDetail', this.showDetail)
      this.$refs.Xq.show(row)
    },
    //归档
    handleGd(row) {
      this.$refs.Gd.show(row)
    },
    //借阅
    handleJy(row) {
      this.$refs.Jy.show(row)
    },
    //解档
    handleJd(row) {
      this.$refs.Jd.show(row)
    },
    //是否可勾选
    checkSelectable(row) {
      return true;
    },
    //获取列表
    getList() {
      if (this.daterangeCk != null && this.daterangeCk.length > 0) {
        this.queryParams.startDateCk = this.daterangeCk[0]
        this.queryParams.endDateCk = moment().add(this.daterangeCk[1], 'days').format('YYYY-MM-DD');
      } else {
        this.queryParams.startDateCk = ''
        this.queryParams.endDateCk = ''
      }
      if (this.daterangeRk != null && this.daterangeRk.length > 0) {
        this.queryParams.startDateRk = this.daterangeRk[0]
        this.queryParams.endDateRk = moment().add(this.daterangeRk[1], 'days').format('YYYY-MM-DD');
      } else {
        this.queryParams.startDateRk = ''
        this.queryParams.endDateRk = ''
      }
      this.loading = true
      list(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    //查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    //重置
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    //勾选回调
    handleSelectionChange(selection) {
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selectList = selection
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.ffjl {
  background: #fff;
  padding: 10px 10px;
}
</style>