<template>
  <!-- 插件 -->
  <el-dialog class="insertFile-dialog" title="插件" :close-on-click-modal="false" :modal-append-to-body="false" append-to-body :visible.sync="insertFileVisible" :before-close="handleCloseDialog">
    <div class="head-search">
      <el-input v-model="query.search" clearable size="small" placeholder="输入题名搜索" prefix-icon="el-icon-search" style="width: 200px; margin-right: 10px;" class="filter-item" @keyup.enter.native="toQuery" @clear="toQuery" />
      <el-button class="filter-item filter-search" style="margin-right: 10px;" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <el-button class="filter-item filter-refresh" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery">重置</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="tableLoading"
      :data="tableData"
      highlight-current-row
      style="width: 100%;"
      :row-key="rowKey"
      @select-all="selectAll"
      @selection-change="selectionChangeHandler"
      @row-click="clickRowHandler"
      @select="handleCurrentChange"
    >
      <el-table-column type="selection" :reserve-selection="true" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column v-for="field in tableDisplayFields" :key="field.id" :label="field.fieldCnName" :align="field.displayformatType" :width="field.displayLength" show-overflow-tooltip>
        <template slot="header">
          <el-tooltip
            class="item"
            effect="dark"
            :content="field.fieldCnName"
            placement="top-start"
          >
            <span>{{ field.fieldCnName }}</span>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          {{ scope.row[field.fieldName] }}
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      v-if="tableData.length !== 0"
      :current-page="page.page"
      :total="page.total"
      :page-size="page.size"
      :pager-count="5"
      layout="total, prev, pager, next, sizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentPage"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleCloseDialog">取消</el-button>
      <el-button type="primary" @click.native="handleComfireInsertFile">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { FetchInitCategoryViewTable, FetchInitCategoryView, FetchInsertSingle } from '@/api/collect/collect'
export default {
  name: 'InsertFile',
  props: {
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    },
    collectLevel: {
      type: Number,
      default: function() {
        return null
      }
    },
    selections: {
      type: Array,
      default: () => []
    }
  },
  inject: ['parentsData'],
  data() {
    return {
      query: {
        search: null
      },
      insertFileVisible: false,
      tableLoading: false,
      insertSelections: [],
      tableData: [],
      tableDisplayFields: [],
      arrySort: [],
      parentsId: null,
      categoryLevel: null,
      fileNoSelectionData: [],
      page: {
        page: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
  },
  mounted() {

  },
  methods: {
    rowKey(row) {
      return row.id
    },
    setInsertBase() {
      if (this.collectLevel === 2) {
        // 该门类下的"未整理"文件列表
        this.parentsId = null
        this.categoryLevel = 3
      } else if (this.collectLevel === 3) {
        // 该门类下的"已整理"案卷列表
        this.categoryLevel = 2
        if (this.selectedCategory.arrangeType === 3) {
          this.parentsId = this.parentsData.parentsProjectId
        } else {
          this.parentsId = null
        }
      }
    },
    toQuery() {
      this.setInsertBase()
      this.getInsertList(this.categoryLevel, this.parentsId)
    },
    resetQuery() {
      this.query.search = null
      this.toQuery()
    },
    getInsertViewTable() {
      this.tableDisplayFields = []
      this.setInsertBase()
      FetchInitCategoryViewTable({ categoryId: this.selectedCategory.id, categoryLevel: this.categoryLevel }).then((res) => {
        if (res) {
          this.arrySort = []
          this.tableDisplayFields = res
          const orderSortArry = this.tableDisplayFields.filter(item => item.displayOrder).sort((a, b) => a.displayOrder - b.displayOrder)
          orderSortArry.forEach(item => {
            if (item.displayOrderBy) {
              this.arrySort.push(item.fieldName + ',' + item.displayOrderBy)
            }
          })
          this.$nextTick(() => {
            this.getInsertList(this.categoryLevel, this.parentsId)
          })
        }
      })
    },
    getInsertList(categoryLevel, parentsId) {
      this.tableLoading = true
      const params = {
        'parentId': parentsId,
        'categoryId': this.selectedCategory.id,
        'categoryLevel': categoryLevel,
        'search': this.query.search,
        'page': this.page.page - 1,
        'size': this.page.size,
        'sort': this.arrySort
      }
      FetchInitCategoryView(params).then((res) => {
        if (res.code !== 500) {
          this.insertFileVisible = true
          this.tableData = res.list.content
          this.page.total = res.list.totalElements
          setTimeout(() => {
            this.tableLoading = false
          }, 500)
        }
      })
    },
    // table - 全选
    selectAll(val) {
      if (this.categoryLevel === 2) {
        val.splice(0, val.length)
        this.insertSelections = val
      } else {
        this.insertSelections = val
      }
    },
    clickRowHandler(row) {
      console.log('row', row)
      if (this.categoryLevel === 2) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(row)
        this.insertSelections = []
        this.insertSelections.push(row)
      } else {
        this.$refs.table.toggleRowSelection(row)
      }
    },
    selectionChangeHandler(selection) {
      if (this.categoryLevel === 2) {
        if (selection.length > 1) {
          selection.splice(0, selection.length - 1)
          this.insertSelections = selection
        }
      } else {
        this.insertSelections = selection
      }
    },
    handleCurrentChange(selection, row) {
      if (this.categoryLevel === 2) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(row)
        this.insertSelections = []
        this.insertSelections.push(row)
      } else {
        this.insertSelections = selection
      }
    },
    handleComfireInsertFile() {
      console.log('insertSelections', this.insertSelections)
      let archivesIds
      let params
      if (this.collectLevel === 2) {
        archivesIds = this.insertSelections.map(item => item.id)
        params = {
          'categoryId': this.selectedCategory.id, // 门类id
          'archivesIds': archivesIds, // 卷内级档案id
          'parentsId': this.selections[0].id // 案卷级档案id
        }
      } else {
        if (this.selections.length === 0) {
          archivesIds = this.fileNoSelectionData.map(item => item.id)
        } else {
          archivesIds = this.selections.map(item => item.id)
        }
        params = {
          'categoryId': this.selectedCategory.id, // 门类id
          'archivesIds': archivesIds, // 卷内级档案id
          'parentsId': this.insertSelections[0].id // 案卷级档案id
        }
      }
      console.log(params)
      FetchInsertSingle(params).then((res) => {
        if (res.code !== 500) {
          this.$message.success('插件成功')
          this.$emit('close-dialog')
        } else {
          this.$message.error('插件失败')
        }
        this.insertFileVisible = false
      })
    },
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.toQuery()
    },
    handleCurrentPage(val) {
      this.page.page = val
      this.toQuery()
    },
    handleCloseDialog(done) {
      // 重置表单数据
      this.query.search = null
      this.tableDisplayFields = []
      this.tableData = []
      this.$refs.table.clearSelection()
      this.insertSelections = []
      this.insertFileVisible = false
      // 关闭弹框
      // done()
    }
  }
}
</script>

<style lang='scss' scoped>
.insertFile-dialog{
  ::v-deep .el-dialog{
    width: 815px;
    .search-btn-box{
      .el-button{
        margin-left: 10px;
      }
    }
  }
}
.dialog-footer{
  margin-top: 20px;
}
</style>
