<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 size="mini" :loading="crud.delAllLoading" :disabled="crud.selections.length === 0" @click="handleRemoveShelf(crud.selections)">
            <i class="iconfont icon-chuku" />
            一键下架
          </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="层位名称" />
        <el-table-column prop="floorName" label="所属楼层" />
        <el-table-column prop="regionName" label="所属区域" />
        <el-table-column prop="isCheck" label="盘点开关" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isCheck" class="row-state row-binding state-active">盘点开启</span>
            <span v-else class="row-state row-lending state-active">盘点关闭</span>
          </template>
        </el-table-column>
        <el-table-column prop="isOrder" label="有序检查" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isOrder" class="row-state row-binding state-active">是</span>
            <span v-else class="row-state row-lending state-active">否</span>
          </template>
        </el-table-column>
        <el-table-column prop="startSortmark" label="索书号范围">
          <template slot-scope="scope">
            <span>{{ getSortmark(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deptsStatus" label="盘点概况" min-width="160">
          <template slot-scope="scope">
            <div class="tag-info">
              <p class="tag-sort">错序：<i class="iconfont icon-zhuangtai2" />{{ scope.row.errorOrderNum }}</p>
              <p class="tag-place">错架：<i class="iconfont icon-zhuangtai2" />{{ scope.row.errorShelfNum }}</p>
              <p class="tag-all">在架：<i class="iconfont icon-zhuangtai2" />{{ scope.row.onShelfNum }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="操作">
          <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>{{ positionContent.gridName }}</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">{{ removeAreaPrefix(cell.gridName) }}</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { positionCrud } from '../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'

export default {
  name: 'BookSearch',
  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

    }
  },
  async created() {
    this.getLibraryFloorListAll()
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
    },
    [CRUD.HOOK.afterRefresh](crud) {
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      return false
    },
    // 获取楼层数据
    getLibraryFloorListAll() {
      FetchLibraryFloorListAll().then(res => {
        this.floorOptions = res
        // if (this.floorOptions.length > 0) {
        //   this.selectFloorVal = this.floorOptions[0]
        //   this.crud.query.floorId = this.selectFloorVal.id
        //   if (this.crud.query.floorId) {
        //     this.getInitLibraryRegionList(this.crud.query.floorId)
        //   }
        // }
      }).catch(() => {
      })
    },
    getInitLibraryRegionList(val) {
      const params = {
        'floorId': val
      }
      FetchInitLibraryRegionList(params).then(res => {
        this.regionOptions = res.content
        // if (this.regionOptions.length > 0) {
        //   this.selectRegionVal = this.regionOptions[0]
        //   this.crud.query.regionId = this.selectRegionVal.id
        // } else {
        //   this.selectRegionVal = null
        //   this.crud.query.regionId = null
        // }
        this.crud.toQuery()
      }).catch(() => {
      })
    },
    changeBeforeFloor(val) {
      if (val) {
        this.selectFloorVal = val
        this.crud.query.floorId = val.id
        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()
    },
    getSortmark(row) {
      const parts = []
      if (row.startSortmark) {
        parts.push(row.startSortmark)
      }
      if (row.endSortmark) {
        parts.push(row.endSortmark)
      }
      return parts.length > 0 ? parts.join('-') : '-'
    },
    clickRowHandler(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
    },
    handleRemoveShelf(datas) {
      console.log(datas)
      this.crud.downloadLoading = true
      this.$confirm('此操作将下架所选层位的所有图书' + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        const ids = []
        datas.forEach(val => {
          ids.push(val.id)
        })
        console.log(ids)
        crudShelf.FetchFastDownShelf(ids).then((res) => {
          console.log(res)
          this.$message({ message: '一键下架操作成功', type: 'success', offset: 8 })
          this.crud.delAllLoading = false
          this.crud.refresh()
        }).catch(err => {
          this.crud.delAllLoading = false
          console.log(err)
        })
      }).catch(() => {
        this.crud.downloadLoading = false
      })
    }

  }
}
</script>

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

</style>
