<template>
  <div>
    <!-- 列表 -->
    <div>
      <div class="mjy" v-show="!showDetail">
        <el-form :model="queryParams" ref="queryForm" class="search-area" :inline="true" label-width="88px">
          <!-- 名称/代码 -->
          <el-form-item :label="$t('page.business.resource.gsp.mc') + '：'" prop="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') + '：'" prop="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.ph') + '：'" prop="ph">
            <el-input v-model="queryParams.ph" clearable @change="handleQuery"
              :placeholder="$t('form.placeholderInput')" />
          </el-form-item>
          <!-- 借阅状态 -->
          <el-form-item :label="$t('page.business.resource.gsp.jyzt') + '：'" prop="jyzt">
            <el-select v-model="queryParams.jyzt" :placeholder="$t('form.placeholderSelect')" clearable
              @change="handleQuery">
              <el-option :label="$t('page.business.resource.jyzt.wjy')" :value="1" />
              <el-option :label="$t('page.business.resource.jyzt.djy')" :value="3" />
              <el-option :label="$t('page.business.resource.jyzt.jyz')" :value="5" />
            </el-select>
          </el-form-item>
          <!-- 制剂状态 -->
          <el-form-item :label="$t('page.business.resource.gsp.zjzt') + '：'" prop="zjzt">
            <el-select v-model="queryParams.zjzt" :placeholder="$t('form.placeholderSelect')" clearable
              @change="handleQuery">
              <el-option :label="$t('page.business.resource.zjzt.rk')" :value="1" />
              <el-option :label="$t('page.business.resource.zjzt.yff')" :value="3" />
              <el-option :label="$t('page.business.resource.zjzt.ysd')" :value="5" />
              <el-option :label="$t('page.business.resource.zjzt.dgd')" :value="7" />
              <el-option :label="$t('page.business.resource.zjzt.gd')" :value="9" />
              <el-option :label="$t('page.business.resource.zjzt.djd')" :value="11" />
            </el-select>
          </el-form-item>
          <!-- 失效日期 -->
          <el-form-item :label="$t('page.business.resource.gsp.yxq') + '：'">
            <el-date-picker v-model="daterange" 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-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <!-- 更新物资库 -->
            <el-button type="primary" @click="handlePlff" v-hasPermi="['business:resource:gsp:gxwzk']">{{
              $t('page.business.resource.gsp.gxwzk') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 新增 -->
            <el-button type="primary" @click="handleXz" v-hasPermi="['business:resource:gsp:xz']">{{
              $t('form.add') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量发放 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlff" v-hasPermi="['business:resource:gsp:ff']">{{
              $t('page.business.resource.gsp.plff') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量处置 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlcz" v-hasPermi="['business:resource:gsp:cz']">{{
              $t('page.business.resource.gsp.plcz') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 钥匙发放 -->
            <el-button type="primary" :disabled="multiple" @click="handleYsff" v-hasPermi="['business:resource:gsp:ff']">{{
              $t('page.business.resource.gsp.ysff') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量锁定 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlsd" v-hasPermi="['business:resource:gsp:ysff']">{{
              $t('page.business.resource.gsp.plsd') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 批量归档 -->
            <el-button type="primary" :disabled="multiple" @click="handlePlgd" v-hasPermi="['business:resource:gsp:gd']">{{
              $t('page.business.resource.gsp.plgd') }}</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- 导出 -->
            <el-button type="primary" @click="exportExcel" v-hasPermi="['business:resource:gsp:dc']">{{
              $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.bh')" align="center" prop="bh"
            :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.kcl')" align="center">
            <template slot-scope="scope">
              {{ scope.row.kc }}{{ scope.row.kcdw }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('page.business.resource.gsp.ph')" align="center" prop="ph" width="150px" />
          <el-table-column :label="$t('page.business.resource.gsp.jsrq')" align="center" prop="jsrq" width="150px" />
          <el-table-column :label="$t('page.business.resource.gsp.cctj')" align="center" prop="cctj" width="130px" />

          <el-table-column :label="$t('page.business.resource.gsp.zjzt')" align="center" width="100px" fixed="right">
            <template slot-scope="scope">
              <span v-if="scope.row.zjzt == 1">{{ $t('page.business.resource.resource.zjzt.rk') }}</span>
              <span v-if="scope.row.zjzt == 3">{{ $t('page.business.resource.resource.zjzt.yff') }}</span>
              <span v-if="scope.row.zjzt == 5">{{ $t('page.business.resource.resource.zjzt.ysd') }}</span>
              <span v-if="scope.row.zjzt == 7">{{ $t('page.business.resource.resource.zjzt.dgd') }}</span>
              <span v-if="scope.row.zjzt == 9">{{ $t('page.business.resource.resource.zjzt.gd') }}</span>
              <span v-if="scope.row.zjzt == 11">{{ $t('page.business.resource.resource.zjzt.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">
              <template
                v-if="scope.row.zjzt == 1 || scope.row.zjzt == 3 || scope.row.zjzt == 5 || (scope.row.zjzt == 9 && scope.row.jyzt == 5)">
                <!-- 详情 -->
                <el-button type="text" @click="handleXq(scope.row)" v-hasPermi="['business:resource:gsp:xq']">{{
                  $t('page.business.resource.gsp.detail') }}</el-button>
              </template>
              <template v-if="scope.row.zjzt == 1">
                <!-- 发放 -->
                <el-button type="text" @click="handleFf(scope.row)" v-hasPermi="['business:resource:gsp:ff']">{{
                  $t('page.business.resource.gsp.fafang') }}</el-button>
                <!-- 处置 -->
                <el-button type="text" @click="handleCz(scope.row)" v-hasPermi="['business:resource:gsp:cz']">{{
                  $t('page.business.resource.gsp.cz') }}</el-button>
                <!-- 编辑 -->
                <el-button v-if="scope.row.bjzt == 1 && scope.row.zjzt == 1" type="text" @click="handleBj(scope.row)"
                v-hasPermi="['business:resource:gsp:bj']">{{
                  $t('form.edit') }}</el-button>
                <!-- 修改库存 -->
                <el-button v-if="scope.row.kcbjzt == 1" type="text" @click="handleXgkc(scope.row)" v-hasPermi="['business:resource:gsp:xgkc']" >{{
                    $t('page.business.resource.gsp.xgkc') }}</el-button>
                <!-- 锁定 -->
                <el-button type="text" @click="handleSd(scope.row)" v-hasPermi="['business:resource:gsp:sd']">{{
                  $t('page.business.resource.gsp.lock') }}</el-button>
              </template>
              <template v-if="scope.row.bjzt == 3 && (scope.row.zjzt == 1 || scope.row.zjzt == 3)">
                <!-- 审核编辑 -->
                <el-button type="text" @click="handleShbj(scope.row)" v-hasPermi="['business:resource:gsp:shbj']" >{{
                  $t('page.business.resource.gsp.shbj') }}</el-button>
              </template>
              <template v-if="scope.row.kcbjzt == 3 && (scope.row.zjzt == 1 || scope.row.zjzt == 3)">
                <!-- 审核修改库存 -->
                <el-button type="text" @click="handleShxgkc(scope.row)" v-hasPermi="['business:resource:gsp:xgkc']">{{
                  $t('page.business.resource.gsp.shxgkc') }}</el-button>
              </template>
              <template v-if="scope.row.zjzt == 5">
                <!-- 解锁 -->
                <el-button type="text" @click="handleJs(scope.row)" v-hasPermi="['business:resource:gsp:js']">{{
                  $t('page.business.resource.gsp.unlock') }}</el-button>
                <!-- 归档 -->
                <el-button type="text" @click="handleGd(scope.row)" v-hasPermi="['business:resource:gsp:gd']">{{
                  $t('page.business.resource.gsp.guidang') }}</el-button>
              </template>
              <template v-if="scope.row.zjzt == 3">
                <!-- 归还 -->
                <el-button type="text" @click="handleGh(scope.row)" v-hasPermi="['business:resource:gsp:gh']">{{
                  $t('page.business.resource.gsp.guihuan') }}</el-button>
              </template>
              <template v-if="scope.row.zjzt == 9 && scope.row.jyzt == 1">
                <!-- 解档 -->
                <el-button type="text" @click="handleJd(scope.row)" v-hasPermi="['business:resource:gsp:jd']">{{
                  $t('page.business.resource.gsp.jiedang') }}</el-button>
                  <!-- 借阅 -->
                <el-button type="text" @click="handleJy(scope.row)" v-hasPermi="['business:resource:gsp:jy']">{{
                  $t('page.business.resource.gsp.jieyue') }}</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
      <!-- 新增 -->
      <Xz key="Xz" ref="Xz" @callback="handleQuery" />
      <!-- 锁定 -->
      <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" />
      <!-- 编辑 -->
      <Bj key="Bj" ref="Bj" @callback="handleQuery" />
      <!-- 审核编辑 -->
      <Shbj key="Shbj" ref="Shbj" @callback="handleQuery" />
      <!-- 修改库存 -->
      <Xgkc key="Xgkc" ref="Xgkc" @callback="handleQuery" />
      <!-- 审核修改库存 -->
      <Shxgkc key="Shxgkc" ref="Shxgkc" @callback="handleQuery" />
      <!-- 借阅 -->
      <Jy key="Jy" ref="Jy" @callback="handleQuery" />
      <!-- 归还 -->
      <Gh key="Gh" ref="Gh" @callback="handleQuery" />
      <!-- 发放 -->
      <Ff key="Ff" ref="Ff" @callback="handleQuery" />
      <!-- 处置 -->
      <Cz key="Cz" ref="Cz" @callback="handleQuery" />
      <!-- 批量处置 -->
      <CzBatch key="CzBatch" ref="CzBatch" @callback="handleQuery" />
      <!-- 钥匙发放 -->
      <Ysff key="Ysff" ref="Ysff" @callback="handleQuery" />
      <!-- 详情 -->
      <Xq v-show="showDetail" key="Xq" ref="Xq" @callback="closeXq" />
    </div>
  </div>
</template>

<script>
import { list } from "@/api/business/gsp/gsp"
import Sd from "./gsp/Sd";
import Js from "./gsp/Js";
import Gd from "./gsp/Gd";
import Jd from "./gsp/Jd";
import Xq from "./gsp/Xq";
import Bj from "./gsp/Bj";
import Xz from "./gsp/Xz";
import Shbj from "./gsp/Shbj";
import Xgkc from "./gsp/Xgkc";
import Shxgkc from "./gsp/Shxgkc";
import Jy from "./gsp/Jy";
import Gh from "./gsp/Gh";
import Ff from "./gsp/Ff";
import Cz from "./gsp/Cz";
import CzBatch from "./gsp/CzBatch";
import Ysff from "./gsp/Ysff";
import { deepClone } from "@/utils/index";
export default {
  name: "GspList",
  components: { Xz, Sd, Js, Gd, Xq, Jd, Bj, Shbj, Xgkc, Shxgkc, Jy, Gh, Cz, CzBatch, Ff, Ysff },
  data() {
    return {
      daterange: [],
      loading: true,
      single: true,
      multiple: true,
      showDetail: false,
      total: 0,
      list: [],
      //勾选列表
      selectList: [],
      //查询条件
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mc: null,
        jyzt: null,
        zjzt: null,
        bh: null,
        startDate: null,
        endDate: 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.bh') + '</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.kcl') + '</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.jsrq') + '</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.zjzt') + '</th>' +
          '<th style="text-align: center;">' + this.$t('page.business.resource.gsp.jyzt') + '</th>' +
          ' </tr>'
        let _datastr = ''
        _.forEach(response.rows, function (value) {
          let zjzt = ''
          switch (value.zjzt) {
            case 1:
              zjzt = that.$t('page.business.resource.resource.zjzt.rk')
              break
            case 2:
              zjzt = that.$t('page.business.resource.resource.zjzt.wrk')
              break
            case 3:
              zjzt = that.$t('page.business.resource.resource.zjzt.yff')
              break
            case 5:
              zjzt = that.$t('page.business.resource.resource.zjzt.ysd')
              break
            case 7:
              zjzt = that.$t('page.business.resource.resource.zjzt.dgd')
              break
            case 9:
              zjzt = that.$t('page.business.resource.resource.zjzt.gd')
              break
            case 11:
              zjzt = that.$t('page.business.resource.resource.zjzt.djd')
              break
            default: zjzt = ''
          }
          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.bh +
            '</td>' +
            ' <td>' +
            value.gg + value.ggdw +
            '</td>' +
            ' <td>' +
            value.kc + value.kcdw +
            '</td>' +
            ' <td>' +
            value.ph +
            '</td>' +
            ' <td>' +
            value.jsrq +
            '</td>' +
            ' <td>' +
            value.cctj +
            '</td>' +
            ' <td>' +
            zjzt +
            '</td>' +
            ' <td>' +
            jyzt +
            '</td>' +
            '</tr>'
        })
        tabelStr = tabelStr + _datastr + ' <table>'
        // Worksheet名
        var worksheet = that.$t('page.business.resource.gsp.gspgl')
        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()
      })
    },
    // 新增
    handleXz() {
      this.$refs.Xz.show()
    },
    //钥匙发放
    handleYsff() {
      this.$refs.Ysff.showBatch(this.selectList)
    },
    //批量发放
    handlePlff() {
      this.$refs.Ff.showBatch(this.selectList)
    },
    //发放
    handleFf(row) {
      this.$refs.Ff.show(row)
    },
    //处置
    handleCz(row) {
      this.$refs.Cz.show(row)
    },
    //批量处置
    handlePlcz() {
      this.$refs.CzBatch.showBatch(this.selectList)
    },
    //编辑
    handleBj(row) {
      this.$refs.Bj.show(row)
    },
    //审核编辑
    handleShbj(row) {
      this.$refs.Shbj.show(row)
    },
    //修改库存
    handleXgkc(row) {
      this.$refs.Xgkc.show(row)
    },
    //审核修改库存
    handleShxgkc(row) {
      this.$refs.Shxgkc.show(row)
    },
    //借阅
    handleJy(row) {
      this.$refs.Jy.show(row)
    },
    //归还
    handleGh(row) {
      this.$refs.Gh.show(row)
    },
    //是否可勾选
    checkSelectable(row) {
      return true;
    },
    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)
    },
    //批量解锁
    handlePljs() {
      this.$refs.Js.showBatch(this.selectList)
    },
    //解锁
    handleJs(row) {
      this.$refs.Js.show(row)
    },
    //批量归档
    handlePlgd() {
      this.$refs.Gd.showBatch(this.selectList)
    },
    //归档
    handleGd(row) {
      this.$refs.Gd.show(row)
    },
    //解档
    handleJd(row) {
      this.$refs.Jd.show(row)
    },
    //批量锁定
    handlePlsd() {
      this.$refs.Sd.showBatch(this.selectList)
    },
    //锁定
    handleSd(row) {
      this.$refs.Sd.show(row)
    },
    //获取列表
    getList() {
      if (this.daterange != null && this.daterange.length > 0) {
        this.queryParams.startDate = this.daterange[0] + ' 00:00:00'
        this.queryParams.endDate = this.daterange[1] + ' 23:59:59'
      } else {
        this.queryParams.startDate = ''
        this.queryParams.endDate = ''
      }
      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.dateRange = []
      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">
.mjy {
  background: #fff;
  padding: 10px 10px;
}
</style>