<template>
  <div>
    <el-dialog class="lendDetail" title="借阅详情" :visible.sync="detailVisible">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <div class="dpflex">
          <p><span class="color-blue">单据号:</span><span class="color-white">{{ otherInfo.id }}</span></p>
          <p><span class="color-blue">借阅人:</span><span class="color-white">{{ borrowerInfo.borrowerName }}</span></p>
          <p><span class="color-blue">所属部门:</span><span class="color-white">{{ borrowerInfo.department }}</span></p>
          <!-- <p><span class="color-blue">证件类型:</span><span class="color-white">{{ borrowerInfo.cardType }}</span></p>
          <p><span class="color-blue">证件号码:</span><span class="color-white">{{ borrowerInfo.idcard }}</span></p>
          <p><span class="color-blue">电话号码:</span><span class="color-white">{{ borrowerInfo.phone }}</span></p> -->
          <p><span class="color-blue">借阅目的:</span><span class="color-white">{{ otherInfo.purpose }}</span></p>
          <p><span class="color-blue">借阅日期:</span><span class="color-white">{{ lendDatesInfo }}</span></p>
          <p><span class="color-blue">借阅状态:</span><span :class="borrowStyle(otherInfo.borrow_type)">{{ otherInfo.borrow_type | borrowStatus }}</span></p>
          <p><span class="color-blue">操作时间:</span><span class="color-white">{{ otherInfo.create_time | parseTime }}</span></p>
        </div>
        <el-table :data="tableData" max-height="400px" style="margin-top:15px;">
          <el-table-column type="index" label="序号" align="center" width="55" />
          <el-table-column prop="categoryName" label="门类名称" align="center" min-width="160" />
          <el-table-column prop="archiveNo" label="档号" align="center" width="160" show-overflow-tooltip />
          <el-table-column prop="maintitle" label="题名" align="center" width="180" show-overflow-tooltip />
          <el-table-column prop="caseName" label="盒名称" align="center" min-width="120" show-overflow-tooltip />
          <el-table-column prop="folderLocationDetails" label="存放位置" align="center" min-width="300">
            <template v-if="scope.row.folderLocationDetails !== null" slot-scope="scope">
              <div v-if="scope.row.folderLocationDetails.includes(',')">
                <el-tag
                  v-for="(item,index) in scope.row.folderLocationDetails.split(',')"
                  :key="index"
                  :type="item"
                  effect="dark"
                >
                  {{ item }}
                </el-tag>
              </div>
              <div v-else>
                <el-tag effect="dark">{{ scope.row.folderLocationDetails }}</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="borrowType" label="借阅状态" align="center" min-width="100">
            <template slot-scope="scope">
              <!-- 待借阅 / 逾期 have-lend / 待归还 / 已归还 has-return -->
              <span :class="borrowStyle(scope.row.borrowType)" style="width:76px">{{ scope.row.borrowType | borrowStatus }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column v-if="isReturn" prop="borrowType" align="center" label="开架操作" min-width="100">
            <template slot-scope="scope">
              <el-button v-if="scope.row.folderLocationDetails" size="mini" type="primary" @click="openCol(scope.row.folderLocationDetails)">开架</el-button>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
    </el-dialog>

    <!-- 是否查看视频监控 -->
    <el-dialog title="提示" :visible.sync="videoTipVisible" :close-on-click-modal="false" :before-close="handleClose">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <div class="dialog-delt">
          <p><span>是否需要查看相关视频监控？</span></p>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click.native="handleLookVideo">确定</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 摄像头视频 -->
    <Video ref="camera" :dialog-open.sync="open" :camera-data="cameraData" />
  </div>
</template>

<script>
import { FetchInitBillDetails } from '@/api/archivesManage/lendManage'
import CallExternal from '@/api/storeManage/deviceManage/device'
import { lendingCrud } from '../mixins/lending'
import { parseTime } from '@/utils/index.js'
import Video from './video'
import displayConfigApi from '@/api/storeManage/displayConfig'
export default {
  name: 'ArchiveDetail',
  components: { Video },
  mixins: [lendingCrud],
  props: {
    isReturn: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      detailVisible: false,
      rowData: {},
      borrowerInfo: {},
      otherInfo: {},
      tableData: [],
      lendDatesInfo: null,
      deviceData: null,
      videoTipVisible: false,
      camConfigData: [],
      cameraData: [],
      open: false
    }
  },
  created() {
    displayConfigApi.list({ storeroomId: 'D6490DA3D4261E8C26D0E3' }).then((data) => {
      if (data) {
        this.camConfigData = data
      }
    })
  },
  methods: {
    getBillDetails() {
      const params = {
        'orderNo': this.rowData.orderNo
      }
      FetchInitBillDetails(params).then(data => {
        this.otherInfo = data
        this.borrowerInfo = data.borrower
        this.tableData = data.borrowArchives
        this.lendDatesInfo = parseTime(data.borrow_start, '{y}-{m}-{d}') + ' 至 ' + parseTime(data.borrow_end, '{y}-{m}-{d}')
        this.getDeviceListAll()
      })
    },
    // 获取密集架相关信息
    getDeviceListAll() {
      const params = {
        sort: 'sequence,asc',
        storeroomId: 'D6490DA3D4261E8C26D0E3'
      }
      CallExternal.getDeviceList(params).then(data => {
        data.content.map(item => {
          if (item.deviceTypeId.name === '密集架') { // 写死状态
            this.deviceData = item
          }
        })
      })
    },
    // 开架
    openCol(data) {
      const loactionArray = data.split(' ')
      const location = data.split(' ')[loactionArray.length - 1]
      const pattern = /(\d+)区(\d+)列(\d+)节(\d+)层/
      const matches = location.match(pattern)
      let areaNumber
      let colNumber
      let leNumber
      let divNumber
      let zyNumber
      if (matches !== null) {
        areaNumber = matches[1]
        colNumber = matches[2]
        leNumber = matches[3]
        divNumber = matches[4]
      }
      const last_char = location.length - 1
      if (location[last_char] === '右') {
        zyNumber = '2'
      } else {
        zyNumber = '1'
      }
      const params = {
        deviceId: this.deviceData.id,
        quNo: areaNumber, // 区
        colNo: colNumber, // 列
        leNo: leNumber, // 节
        divNo: divNumber, // 层
        zyNo: zyNumber // 左右  1左 2右
      }
      CallExternal.FetchCallExternalOpenCol(params).then(res => {
        if (res.success && res.success === '0') {
          this.$message.success('开架执行成功')
          this.videoTipVisible = true
        } else {
          this.$message.error(res.msg)
        }
      }).catch((error) => {
        console.log(error)
        this.$message.error('连接失败')
      })
    },
    handleLookVideo() {
      this.videoTipVisible = false
      this.cameraData = this.camConfigData.filter((item) => { return item.divPosition.toLowerCase().includes('cam') })
      this.open = true
      this.$nextTick(() => {
        this.$refs.camera.camConfig = this.cameraData[0]
        this.$refs.camera.videoIndex = 0
        this.$refs.camera.play()
      })
    },
    handleClose(done) {
      this.videoTipVisible = false
      done()
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/styles/lend-manage.scss';
.lendDetail{
  ::v-deep .el-dialog{
    width: 1200px;
  }
  ::v-deep .el-dialog .el-dialog__header .el-dialog__close::before{
    position: absolute;
    right: -280px;
    bottom: -10px;
  }
  ::v-deep .el-dialog__body{
    padding: 20px 0 60px 0;
  }
}

.dpflex{
  display: flex;
  flex-wrap: wrap;
  padding: 0 30px;
  p{
    display: flex;
    align-items: center;
    width: 33%;
    height: 40px;
    line-height: 40px;
    span{
      display: block;
      line-height: 40px;
      &.color-blue{
        color: #3A99FD;
        width: 70px;
        text-align: right;
      }
      &.color-white{
        color: white;
        padding: 0 0 0 20px;
      }
      &.cell-lend{
        margin-left: 20px;
        width: 76px;
        line-height: 24px;
      }
    }
  }
}
</style>
