<template>
  <!-- 移动 -->
  <el-dialog class="move-form" append-to-body :modal-append-to-body="false" :close-on-click-modal="false" :before-close="handleClose" :visible="moveVisible" title="移动" @opened="opened">
    <span class="dialog-right-top" />
    <span class="dialog-left-bottom" />
    <div class="setting-dialog">
      <div class="move-main">
        <div class="move-left">
          <el-tree
            ref="treeMove"
            v-loading="crud.loading"
            :data="crud.data"
            node-key="id"
            default-expand-all
            :props="defaultProps"
            :expand-on-click-node="false"
            @node-click="handleMoveNodeClick"
          />
        </div>
        <div class="move-right">
          <div style="padding: 10px;">
            <el-input v-model="query.search" placeholder="输入题名或档号搜索" style="width: 200px;" />
            <el-button class="filter-item filter-search" size="mini" type="success" icon="el-icon-search" @click="getViewTableList">搜索</el-button>
          </div>
          <div class="raido-main">
            <!-- @select="handleSelect"
              @selection-change="selectionChangeHandler"
              @row-click="clickRowHandler" -->
            <el-table
              ref="table"
              v-loading="loading"
              lazy
              :data="tableData"
              style="width: 100%;"
              height="calc(100vh)"
              :row-key="getRowKey"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              highlight-current-row
              @selection-change="selectionChangeHandler"
              @row-click="clickRowHandler"
            >
              <el-table-column type="selection" align="center" width="55" />
              <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>
          </div>
          <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>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="closedDialog">取消</el-button>
        <el-button type="primary" @click="handleComfirmed">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { preLibraryCrud } from '../mixins/index'
import CRUD, { presenter, header, crud } from '@crud/crud'
import { FetchMove } from '@/api/prearchiveLibrary/prearchiveLibrary'
import { FetchInitCategoryViewTable, FetchInitCategoryView } from '@/api/collect/collect'
import Vue from 'vue'
export default {
  name: 'MoveFile',
  components: { },
  cruds() {
    return [
      CRUD({
        title: '移动', url: 'api/category/menu',
        crudMethod: {}, sort: []
      })
    ]
  },
  mixins: [presenter(), header(), crud(), preLibraryCrud],
  props: {
    selectedDocument: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      selectedCategoryMove: {},
      query: {
        search: ''
      },
      moveVisible: false,
      loading: false,
      arrySort: [],
      tableData: [],
      tableDisplayFields: [],
      selections: [],
      moveArc: [],
      defaultProps: { children: 'children', label: 'cnName' },
      page: {
        page: 1,
        size: 10,
        total: 0
      }
    }
  },
  watch: {
    selectedDocument: function(newValue, oldValue) {
    }
  },
  created() {
  },
  methods: {
    getRowKey(row) {
      return row.id
    },
    [CRUD.HOOK.afterRefresh]() {
      this.crud.data = this.filterData(this.crud.data)
    },
    opened() {
      this.refresh()
    },
    refresh() {
      if (this.$refs.treeMove) {
        let currentKey
        if (this.crud.data[0].isType === 1) {
          console.log(currentKey)
          currentKey = this.findNode(this.crud.data[0].children, (node) => {
            return node.isType !== 1
          })
        } else {
          currentKey = this.crud.data[0]
        }
        // 设置某个节点的当前选中状态
        this.$refs.treeMove.setCurrentKey(currentKey.id)
        this.$nextTick(() => {
          // 设置某个节点的父级展开
          const selectedKey = this.$refs.treeMove.getCurrentNode()
          if (this.$refs.treeMove.getNode(selectedKey) && this.$refs.treeMove.getNode(selectedKey).parent) {
            this.expandParents(this.$refs.treeMove.getNode(selectedKey).parent)
          }
          // 选中节点的门类详情
          this.handleMoveNodeClick(selectedKey)
        })
      }
    },
    // 选中门类后，设置门类详情数据
    handleMoveNodeClick(val) {
      if (val) {
        this.selectedCategoryMove = val
        if (val.pid !== '0') {
          Vue.set(this.selectedCategoryMove, 'parentName', this.$refs.treeMove.getNode(val.pid).data.cnName)
        }
        this.getViewTable()
      }
    },
    getViewTable() {
      this.loading = true
      this.tableDisplayFields = []
      FetchInitCategoryViewTable({ categoryId: this.selectedCategoryMove.id, categoryLevel: 3 }).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.getViewTableList()
          })
          this.loading = false
        }
      })
    },
    getViewTableList() {
      this.tableData = []
      this.loading = true
      const params = {
        'categoryId': this.selectedCategoryMove.id,
        'categoryLevel': 3,
        'search': this.query.search,
        'page': this.page.page - 1,
        'size': this.page.size
      }
      FetchInitCategoryView(params).then((res) => {
        console.log(res)
        if (res.code !== 500) {
          this.tableData = res.list.content
          this.page.total = res.list.totalElements
        }
        this.loading = false
      })
    },
    handleClose(done) {
      this.moveVisible = false
      this.query.search = null
      done()
    },
    closedDialog() {
      this.moveVisible = false
      this.query.search = null
    },
    clickRowHandler(row) {
      this.selections = []
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
      this.selections.push(row)
    },
    selectionChangeHandler(selection, row) {
      this.selections = selection
    },
    handleComfirmed() {
      if (this.selections.length === 0) {
        this.crud.notify('请选择要移动到收集库的档案', CRUD.NOTIFICATION_TYPE.WARNING)
        return false
      }
      const archivesIds = this.moveArc.map(item => {
        return item.id
      })
      const params = {
        'archivesId': this.selections[0].id, // 移动到的收集库档案id
        'archivesIds': archivesIds, // 移动的预归档档案信息
        'categoryId': this.selectedCategoryMove.id, // 门类id
        'documentId': this.selectedDocument.id // 预归档门类id
      }
      console.log(params)
      FetchMove(params).then((res) => {
        this.$message.success(res)
        this.moveVisible = false
        this.query.search = null
        this.selections = []
        this.$emit('refresh')
      })
    },
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getViewTableList()
    },
    handleCurrentPage(val) {
      this.page.page = val
      this.getViewTableList()
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/prearchive-library.scss";

.el-pagination{
  padding-right: 5px;
  margin: 25px 0 !important;
}

::v-deep .el-dialog{
  .el-button.filter-search{
    background-color: #0348f3;
    border-color: #0348f3;
    color: #fff !important;
  }
  .el-tree{
    .el-tree-node__content{
      color: #0C0E1E;
      &:hover{
        background-color: #E8F2FF !important;
        background-image: none !important;
      }
    }
    .el-tree-node__children{
      .el-tree-node__content{
        color: #545B65;
      }
    }
    .el-tree-node__expand-icon{
      color: #0C0E1E;
    }
    .is-current>.el-tree-node__content{
      background-color: #E8F2FF !important;
      background-image: none !important;
    }
     .el-tree-node.is-current>.el-tree-node__content .el-tree-node__label{
      background-image: none !important;
      color: #0348F3;
     }
  }
  .el-table{
    .el-table__header-wrapper,
    .el-table__header{
      background-color: #F5F9FC !important;
      th.el-table__cell{
        background-color: #F5F9FC !important;
        border-bottom: none;
        &>.cell{
          font-size: 14px;
          color: #0C0E1E !important;
        }
      }
    }
  }
  .el-table .el-table__body-wrapper td.el-table__cell,
  .el-table .el-table__fixed-right td.el-table__cell {
      border-bottom: none;
  }
  .el-table__body tr.el-table__row:hover>td.el-table__cell,
  .el-table__body tr.el-table__row:focus>td.el-table__cell,
  .el-table__body tr.current-row>td.el-table__cell,
  .el-table__body tr.current-row:hover>td.el-table__cell,
  .el-table__body tr.current-row:focus>td.el-table__cell{
    background-color: #eaf3fb;
    color: #545b65;
    border-bottom: 1px solid #f5f9fc;
  }

  .el-button--text:hover,
  .el-button--text:focus{
    border-color: #e6e8ed;
    color: #545b65;
    background-color: #E8F2FF;
  }
}
</style>
