<template>
  <div>
    <CollectHeader :is-title-type="isTitleType" :selected-category="selectedCategory" :arc-id="arcId" :selections="selections" :active-index="activeIndex" :test="test" :is-recycle="isRecycle" />
    <el-table
      ref="table"
      v-loading="crud.loading || getTableDisplayFieldsLoading"
      class="archives-table"
      :data="anjuanData"
      highlight-current-row
      style="width: 100%;"
      :row-class-name="tableRowClassName"
      :row-key="rowKey"
      @select-all="selectAll"
      @selection-change="crud.selectionChangeHandler"
      @row-click="clickRowHandler"
      @cell-dblclick="tableDoubleClick"
      @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 :label="selectedCategory.arrangeType === 1 || (selectedCategory.arrangeType !==1 && activeIndex===1) ? '原文':'卷内'" prop="children_num" 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
      :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"
    />
    <!-- 档案详情 -->
    <ArchivesInfo ref="archivesInfo" :category-id="categoryId" :arc-id="arcId" />
  </div>
</template>

<script>
import { header, form } from '@crud/crud'
import ArchivesInfo from '../module/archivesInfo/index'
import CollectHeader from '../module/collectHeader.vue'
import tableFields from './tableFields.json'
import tableData from './tableData.json'
export default {
  name: 'Sorted',
  components: { ArchivesInfo, CollectHeader },
  mixins: [
    header(),
    form({})
  ],
  props: {
    isTitleType: {
      type: Number,
      default: 3
    },
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    },
    activeIndex: {
      type: Number,
      default: 0
    },
    test: {
      type: String,
      default: ''
    },
    isRecycle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      anjuanData: [],
      tableDisplayFields: [], // table-list-title字段
      getTableDisplayFieldsLoading: false, // table-loading
      selections: [],
      categoryId: 'F0F59CC713C83AE4BAB99B',
      arcId: '256E752BC0280618840600',
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      timer: null
    }
  },
  watch: {
    selectedCategory: function(newValue, oldValue) {
      this.selections = []
      this.$refs.table.clearSelection()
    },
    tableDisplayFields(val) {
      this.doLayout()
    },
    activeIndex(newValue) {
      this.selections = []
      this.$refs.table.clearSelection()
    }
  },
  created() {
    this.tableDisplayFields = tableFields.data
    this.anjuanData = tableData.data.list.content
  },
  mounted() {
  },
  methods: {
    openJuannei(data) {
      // this.$emit('openJuannei', '这是来自案卷的通知')
      if (this.selectedCategory.arrangeType === 3) {
        this.$parent.$parent.$parent.$emit('openJuannei', data)
      } else {
        this.$parent.$parent.$emit('openJuannei', data)
      }
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath)
    },
    rowKey(row) {
      return row.id
    },
    // table选中加上选中状态
    tableRowClassName({ row, rowIndex }) {
      // console.log('添加类名', row, rowIndex)
      let color = ''
      this.selections.forEach(item => {
        if (item.id === row.id) {
          color = 'rowStyle'
        }
      })
      return color
    },
    // table - 全选
    selectAll(val) {
      this.selections = val
    },
    // table - 双击查看详情
    tableDoubleClick(row) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      console.log('tableDoubleClick', row)
      this.arcId = row.id

      if (this.selectedCategory.arrangeType !== 1) {
        this.$refs.archivesInfo.isHasFile = false
        if (this.activeIndex === 1) {
          this.$refs.archivesInfo.detailTitle = '文件详情'
          this.$refs.archivesInfo.isHasFile = true
        } else {
          this.$refs.archivesInfo.detailTitle = '案卷详情'
        }
      } else {
        this.$refs.archivesInfo.isHasFile = true
        this.$refs.archivesInfo.detailTitle = '文件详情'
      }

      // if (this.selectedCategory.arrangeType === 2) {
      //   if (this.activeIndex === 1) {
      //     this.$refs.archivesInfo.detailTitle = '文件详情'
      //     this.$refs.archivesInfo.isHasFile = true
      //   } else {
      //     this.$refs.archivesInfo.detailTitle = '案卷详情'
      //   }
      // } else {
      //   this.$refs.archivesInfo.detailTitle = '文件详情'
      // }
      this.$refs.archivesInfo.archivesInfoVisible = true
      this.$refs.archivesInfo.archivesTabIndex = 0
      // this.$refs.archivesInfo.getDetial(row.id)
    },
    // table - 当前选中得row
    clickRowHandler(row) {
      console.log('clickRowHandler', row)
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        this.openJuannei(row.archive_no)
      }, 300)
      this.selections = this.crud.selections
    },
    // 触发单选
    handleCurrentChange(selection, row) {
      console.log('触发单选', row)
      this.selections = selection
    },
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
    },
    handleCurrentPage(val) {
      this.page.page = val
    },
    /* 重新渲染table组件 防止table-fixed 错位 配合watch-table数据 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
