<template>
  <div class="app-container  row-container">
    <div class="head-container">
      <div class="head-search">
        <!-- 搜索 -->
        <el-select v-model="selectFloorVal" size="small" placeholder="楼层" class="filter-item" style="width: 80px" value-key="id" @change="changeBeforeFloor">
          <el-option v-for="(item,index) in floorOptions" :key="index" :label="item.floorName" :value="item" />
        </el-select>
        <el-select v-model="selectRegionVal" size="small" placeholder="区域" class="filter-item" style="width: 140px" value-key="id" @change="changeBeforeRegion">
          <el-option v-for="(item,index) in regionOptions" :key="index" :label="item.regionName" :value="item" />
        </el-select>
        <el-input v-model="query.search" clearable size="small" placeholder="输入检索条件检索" prefix-icon="el-icon-search" style="width: 220px;" class="filter-item" @clear="crud.toQuery" @keyup.enter.native="crud.toQuery" />
        <rrOperation>
          <template v-slot:right>
            <el-button class="filter-item  filter-refresh" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery()">重置</el-button>
          </template>
        </rrOperation>
      </div>
      <crudOperation :permission="permission">
        <template v-slot:right>
          <el-button :loading="crud.downloadLoading" size="mini" @click="doExport(crud.selections)">
            <i class="iconfont icon-daochu" />
            导出
          </el-button>
        </template>
      </crudOperation>
    </div>
    <div class="container-wrap">
      <span class="right-top-line" />
      <span class="left-bottom-line" />
      <el-table
        ref="table"
        v-loading="crud.loading"
        class="archives-table"
        :data="crud.data"
        style="width: 100%;"
        height="calc(100vh - 329px)"
        @selection-change="crud.selectionChangeHandler"
        @row-click="clickRowHandler"
      >
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="gridName" label="层位名称">
          <template slot-scope="scope">
            {{ scope.row.gridName | removeQUPrefix }}
          </template>
        </el-table-column>
        <el-table-column prop="gridCode" label="层位编码" />
        <el-table-column prop="floorName" label="所属机构" />
        <el-table-column prop="floorName" label="所属楼层" />
        <el-table-column prop="regionName" label="所属区域" />
        <el-table-column prop="createTime" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handlePosition(scope.row)">定位</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination v-if="crud.data.length!==0" />
    </div>

    <el-dialog class="positionDialog" append-to-body :close-on-click-modal="false" :modal-append-to-body="false" :before-close="handleCloseDialog" :visible="positionVisible" title="层位定位">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <ul class="book-detail">
          <li><span>所属机构</span>{{ user.fonds.fondsName }}</li>
          <li><span>所属楼层</span>{{ positionContent.floorName }}</li>
          <li><span>所属区域</span>{{ positionContent.regionName }}</li>
          <li><span>所属书架</span>{{ positionTitle }}</li>
        </ul>
        <div class="position-content">
          <div class="position-left">
            <h5>平面图</h5>
            <div class="venue-preview">
              <div v-show="currentMarkData && currentMarkData.signPoint ">
                <canvas :id="`canvasPreview${currentMarkData && currentMarkData.id}`" :width="width" :height="height" />
              </div>
              <img v-if="currentMarkData && !currentMarkData.signPoint" :src="imageUrl" :onerror="defaultImg" alt="">
              <img v-if="!currentMarkData" :src="imageUrl" :onerror="defaultImg" alt="">
            </div>
          </div>
          <div class="position-right">
            <h5>书架图</h5>
            <div class="shelf-top" :style="rowStyle">
              <p v-for="(item,index) in reversedRackNum" :key="index" :style="{width: `calc(${'100%/' + rackNum} - 4px )`}"><span>{{ item + '架' }}</span></p>
            </div>
            <ul class="data-shelf-row" :style="rowStyle">
              <li
                v-for="(cell,i) in booShelfGrid"
                :key="i"
                :class="{ active: i === cellIndex }"
                class="data-shelf-cell"
                :style="cellStyle"
              >
                <span class="cell-name">{{ cell.gridName | removeMianBeforeRefix }}</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { positionCrud } from '../../visualCheck/checkManage/positionMixins/index'
import { FetchLibraryFloorListAll } from '@/api/floor/index'
import { FetchInitLibraryRegionList } from '@/api/area/index'
import crudShelf from '@/api/shelf/index'
import CRUD, { presenter, header, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'

// import { exportFile } from '@/utils/index'
// import qs from 'qs'
import { mapGetters } from 'vuex'

export default {
  name: 'ShelfAllSearch',
  components: { crudOperation, rrOperation, pagination },
  cruds() {
    return CRUD({ title: '架位查询', url: 'api/bookShelf/initShelfGridSearch', crudMethod: { ...crudShelf }, sort: [], optShow: {
      add: false,
      edit: false,
      del: false,
      download: false,
      group: false,
      reset: false
    }})
  },
  mixins: [presenter(), header(), crud(), positionCrud],
  data() {
    return {
      permission: {
        add: ['admin', 'search:add'],
        edit: ['admin', 'search:edit'],
        del: ['admin', 'search:del']
      },
      floorOptions: [],
      regionOptions: [],
      selectFloorVal: null,
      selectRegionVal: null

    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'user'
    ])
  },
  async created() {
    this.getLibraryFloorListAll()
  },
  methods: {
    // 获取楼层数据
    getLibraryFloorListAll() {
      FetchLibraryFloorListAll().then(res => {
        this.floorOptions = res
      }).catch(() => {
      })
    },
    getInitLibraryRegionList(val) {
      const params = {
        'floorId': val
      }
      FetchInitLibraryRegionList(params).then(res => {
        this.regionOptions = res.content

        this.crud.toQuery()
      }).catch(() => {
      })
    },
    changeBeforeFloor(val) {
      if (val) {
        this.selectFloorVal = val
        this.crud.query.floorId = val.id
        this.selectRegionVal = null
        this.crud.query.regionId = null
        this.getInitLibraryRegionList(val.id)
      }
    },
    changeBeforeRegion(val) {
      if (val) {
        this.selectRegionVal = val
        this.crud.query.regionId = val.id
        this.crud.toQuery()
      }
    },
    resetQuery() {
      this.crud.query.search = ''
      this.selectFloorVal = null
      this.selectRegionVal = null
      this.crud.query.floorId = null
      this.crud.query.regionId = null
      this.crud.toQuery()
    },
    clickRowHandler(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
    },
    doExport() {
      console.log('doExport', this.crud.page.total)
      if (this.crud.page.total > 10000) {
        this.handleExport('导出数据大于10000条，时间可能较长')
      } else {
        this.handleExport('此操作将导出所有数据')
      }
    },
    handleExport(message) {
      this.crud.downloadLoading = true
      this.$confirm(message + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        const params = {
          'libcode': this.user.fonds.fondsNo,
          'floorId': this.selectFloorVal.id,
          'regionId': this.selectRegionVal.id,
          'search': this.crud.query.search
        }
        console.log('exportFile', params)
        // exportFile(this.baseApi + '/api/person/downloadFaceRecognizeLog?' + qs.stringify(params, { indices: false, allowDots: true, skipNulls: false }))
        this.crud.downloadLoading = false
      }).catch(() => {
        console.log('取消')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tag-info{
  p{
    margin-right: 20px;
  }
}
</style>
