<template>
  <div class="lendComfirm-arcList">
    <el-button class="exception-btn" size="mini" :disabled="!selections.length" @click="handleBorrowException"><svg-icon icon-class="exception" class-name="lend-handle" />异常处理</el-button>
    <div class="container-wrap">
      <span class="right-top-line" />
      <span class="left-bottom-line" />
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="tableLoading"
        :data="tableData"
        style="min-width: 100%"
        @selection-change="selectionChangeHandler"
        @row-click="clickRowHandler"
      >
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column prop="categoryName" align="center" label="门类名称" min-width="120" show-overflow-tooltip />
        <el-table-column prop="archiveNo" align="center" label="档号" min-width="160" show-overflow-tooltip />
        <el-table-column prop="maintitle" align="center" label="题名" min-width="180" show-overflow-tooltip />
        <el-table-column prop="caseName" align="center" label="盒名称" min-width="100" show-overflow-tooltip />
        <el-table-column prop="folderLocationDetails" align="center" label="存放位置" min-width="300">
          <template 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" align="center" label="借阅状态" min-width="100">
          <template slot-scope="scope">
            <!-- 待借阅 -->
            <span :class="borrowStyle(scope.row.borrowType)" style="width:76px">{{ scope.row.borrowType | borrowStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="borrowType" align="center" label="开架操作" min-width="80">
          <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-column prop="createTime" align="center" label="操作时间" min-width="130">
          <template slot-scope="scope">
            <div>{{ scope.row.createTime | parseTime }}</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 异常处理 -->
    <el-dialog title="异常处理" :visible.sync="exceptionVisible" :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="handleExceptionConfirm">确定</el-button>
        </div>
      </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 { FetchInitArchivesByOrderNo, FetchBorrowException } from '@/api/archivesManage/lendManage'
import { lendingCrud } from '../mixins/lending'
import CallExternal from '@/api/storeManage/deviceManage/device'
import Video from './video'
import displayConfigApi from '@/api/storeManage/displayConfig'
export default {
  name: 'LendArchivesList',
  components: { Video },
  mixins: [lendingCrud],
  props: {
    archivesOrderNum: {
      type: String,
      default: function() {
        return ''
      }
    }
  },
  data() {
    return {
      selections: [],
      tableData: [],
      tableLoading: false,
      exceptionVisible: false,
      deviceData: null,
      videoTipVisible: false,
      camConfigData: [],
      cameraData: [],
      open: false
    }
  },
  watch: {
    archivesOrderNum: function(newValue, oldValue) {
      if (newValue) {
        this.getArchivesTable()
      }
    }
  },
  created() {
    displayConfigApi.list({ storeroomId: 'D6490DA3D4261E8C26D0E3' }).then((data) => {
      if (data) {
        this.camConfigData = data
      }
    })
  },
  mounted() {
    this.getArchivesTable()
  },
  methods: {
    // 档案异常处理
    handleBorrowException() {
      const index = this.selections.findIndex(item => item.borrowType === -1)
      if (index === -1) {
        this.exceptionVisible = true
      } else {
        this.$message.error('当前档案为异常状态，请勿重复操作!')
        return
      }
    },
    // 确认加入异常
    handleExceptionConfirm() {
      const params = this.selections.map(item => item.id)
      FetchBorrowException(params).then(data => {
        if (data === this.selections.length) {
          this.$message.success('已成功加入异常!')
          this.exceptionVisible = false
          this.getArchivesTable()
        }
      })
    },
    // 获取借阅确认下的档案list
    getArchivesTable() {
      this.tableLoading = true
      const params = {
        'orderNo': this.archivesOrderNum
      }
      FetchInitArchivesByOrderNo(params).then(data => {
        if (data) {
          this.tableData = data
          this.tableLoading = false
          this.getDeviceListAll()
        }
      })
    },
    selectionChangeHandler(val) {
      this.selections = val
    },
    clickRowHandler(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    handleClose(done) {
      this.exceptionVisible = false
      this.videoTipVisible = false
      done()
    },
    // 获取密集架相关信息
    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()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/styles/lend-manage.scss';
.lendComfirm-arcList{
  .exception-btn{
    margin: 20px 0;
    .lend-handle{
      margin-right: 6px;
    }
  }
  .container-wrap{
    min-height: auto;
  }
}
</style>
