<template>
  <div>
    <collectHeader ref="collectHeaderRef" :is-title-type="isTitleType" :selected-category="selectedCategory" :arc-id="arcId" :selections="selections" :active-index="activeIndex" :is-recycle="isRecycle" :test="test" />
    <div class="collect-table">
      <el-table
        ref="table"
        v-loading="getTableDisplayFieldsLoading"
        class="archives-table"
        :data="anjuanData"
        highlight-current-row
        style="width: 100%;"
        height="calc(100vh - 418px)"
        :row-class-name="tableRowClassName"
        :row-key="rowKey"
        @select-all="selectAll"
        @selection-change="crud.selectionChangeHandler"
        @row-click="clickRowHandler"
        @row-dblclick="tableDoubleClick"
        @select="handleCurrentChange"
        @mousedown.native="onMouseDown"
        @mouseup.native="onMouseUp"
      >
        <el-table-column type="selection" :reserve-selection="true" width="55" align="center" />
        <el-table-column label="序号" width="55" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ page.page * page.size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column type="index" label="序号" width="55" align="center" /> -->
        <el-table-column :label="selectedCategory.arrangeType === 1 || (selectedCategory.arrangeType !==1 && activeIndex===1) ? '原文':'卷内'" prop="child" width="55" align="center">
          <template slot-scope="scope">
            {{ scope.row.child === '' ? 0 : scope.row.child }}
          </template>
        </el-table-column>
        <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-column v-if="!isRecycle" label="装盒" width="88" align="center" :fixed="parentsData.fixedStatusBar ? false : 'right' ">
          <template slot-scope="scope">
            <span :class="['row-state', 'row-packing', scope.row.case_no ? 'state-active' : '' ]">{{ scope.row.case_no ? '已装': '未装' }}</span>
          </template>
        </el-table-column> -->
        <el-table-column v-if="!isRecycle && !(isTitleType === 3 && activeIndex === 1 && selectedCategory.arrangeType !== 1 )" label="审批锁定" width="88" align="center" :fixed="parentsData.fixedStatusBar ? false : 'right' ">
          <template slot-scope="scope">
            <span :class="['row-state', 'row-warehousing', scope.row.collect_formal===2 ? 'state-active' : '' ]">{{ scope.row.collect_formal===2 ? '归档': '空闲' }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        v-if="anjuanData.length !== 0"
        :current-page="currentPage"
        :total="page.total"
        :page-size="page.size"
        :pager-count="5"
        layout="total, prev, pager, next, sizes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentPage"
      />
    </div>
    <!-- 档案详情 -->
    <ArchivesInfo ref="archivesInfo" :selected-category="selectedCategory" :arc-id="arcId" :is-title-type="isTitleType" :is-collect="true" />
  </div>
</template>

<script>
import { header, form } from '@crud/crud'
import { collectionLibraryCrud } from '../mixins/index'
import ArchivesInfo from '../module/archivesInfo/index'
import collectHeader from '../module/collectHeader'
export default {
  name: 'Sorted',
  components: { ArchivesInfo, collectHeader },
  mixins: [
    header(),
    form({}),
    collectionLibraryCrud
  ],
  props: {
    isTitleType: {
      type: Number,
      default: 3
    },
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    },
    activeIndex: {
      type: Number,
      default: 0
    },
    isRecycle: {
      type: Boolean,
      default: false
    },
    smartQuery: {
      type: Object,
      default: function() {
        return {}
      }
    },
    test: {
      type: String,
      default: ''
    }
  },
  inject: ['parentsData'],
  data() {
    return {
      categoryId: '',
      arcId: '',
      title: '',
      selections: [],
      yearData: [],
      parentId: null,
      currentPage: 1,
      mousedownTime: 0,
      mousedownX: 0,
      mousedownY: 0
    }
  },
  watch: {
    selectedCategory: function(newValue, oldValue) {
      this.selections = []
      this.$refs.table.clearSelection()
      if (newValue.arrangeType !== 1) {
        this.title = '案卷'
      } else {
        this.title = '文件'
      }
    },
    tableDisplayFields(val) {
      this.doLayout()
    },
    activeIndex(newValue) {
      if (newValue === 1) {
        this.title = '文件'
        this.getCommonData(3, null)
      } else {
        this.title = '案卷'
        if (this.selectedCategory.arrangeType === 3) {
          this.getCommonData(2, this.parentsData.parentsProjectId)
        } else {
          this.getCommonData(2, null)
        }
      }
      this.selections = []
      this.$refs.table.clearSelection()
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    getCommonData(categoryLevel, parentId, type) {
      this.getViewTable(categoryLevel, parentId, type)
    },
    rowKey(row) {
      return row.id
    },
    sendYearDataToParent() {
      this.$parent.$parent.$emit('myYearEvent', this.yearData)
    },
    openJuannei(data, parentId, parentRow) {
      // this.$emit('openJuannei', '这是来自案卷的通知')
      if (this.selectedCategory.arrangeType === 3) {
        this.$parent.$parent.$parent.$emit('openJuannei', data, parentId, parentRow)
      } else {
        this.$parent.$parent.$emit('openJuannei', data, parentId, parentRow)
      }
    },
    // table选中加上选中状态
    tableRowClassName({ row, rowIndex }) {
      let color = ''
      this.selections.forEach(item => {
        if (item.id === row.id) {
          color = 'rowStyle'
        }
      })
      return color
    },
    // table - 全选
    selectAll(val) {
      this.selections = val
    },
    onMouseDown(event) {
      this.mousedownTime = Date.now()
      this.mousedownX = event.clientX
      this.mousedownY = event.clientY
    },
    onMouseUp() {
      // 可以在这里添加更多逻辑，比如清除临时数据等
    },
    // table - 双击查看详情
    tableDoubleClick(row) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.arcId = row.id
      this.$nextTick(() => {
        if (this.selectedCategory.arrangeType !== 1) {
          this.$refs.archivesInfo.isHasFile = false
          if (this.activeIndex === 1) {
            this.$refs.archivesInfo.detailTitle = '文件详情'
            this.$refs.archivesInfo.isHasFile = true
            this.$refs.archivesInfo.getDetial(3, row.id)
          } else {
            this.$refs.archivesInfo.detailTitle = '案卷详情'
            this.$refs.archivesInfo.getDetial(2, row.id)
          }
        } else {
          this.$refs.archivesInfo.isHasFile = true
          this.$refs.archivesInfo.detailTitle = '文件详情'
          this.$refs.archivesInfo.getDetial(3, row.id)
        }
        this.$refs.archivesInfo.isFourTest = true
        this.$refs.archivesInfo.archivesInfoVisible = true
        this.$refs.archivesInfo.archivesTabIndex = 0
      })
    },
    // table - 当前选中得row
    clickRowHandler(row) {
      // this.parentsData.smartQuery = {
      //   'retention': null,
      //   'security_class': null,
      //   'doc_type': null,
      //   'medium_type': null,
      //   'archive_year': null,
      //   'fonds_no': null
      // }
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (this.selectedCategory.arrangeType === 1) {
        if (this.activeIndex === 0) {
          this.title = '文件'
        }
      } else {
        if (this.activeIndex === 0) {
          this.title = '案卷'
        }
      }
      const clickTime = Date.now()
      const clickX = event.clientX
      const clickY = event.clientY
      const timeDiff = clickTime - this.mousedownTime
      const distance = Math.sqrt(
        Math.pow(clickX - this.mousedownX, 2) + Math.pow(clickY - this.mousedownY, 2)
      )
      // 时间差较短且鼠标移动距离较小，判定为点击操作
      if (timeDiff < 300 && distance < 10) {
        this.timer = setTimeout(() => {
          this.parentId = row.id
          localStorage.removeItem('currentPageSize')
          localStorage.removeItem('currentPage')
          this.openJuannei('所属' + this.title + '：' + row.archive_no, this.parentId, row)
        }, 300)
      }
    },
    // 触发单选
    handleCurrentChange(selection, row) {
      this.selections = selection
    },
    handleSizeChange(size) {
      this.currentPage = 1
      this.page.size = size
      this.page.page = 0
      localStorage.setItem('currentPageSize', size)
      if (this.activeIndex === 1) {
        this.getViewTable(3, null)
      } else {
        if (this.selectedCategory.arrangeType === 3) {
          this.getViewTable(2, this.parentsData.parentsProjectId)
        } else if (this.selectedCategory.arrangeType === 1) {
          this.getViewTable(3, null)
        } else {
          this.getViewTable(2, null)
        }
      }
    },
    handleCurrentPage(pageVal) {
      this.currentPage = pageVal
      this.page.page = pageVal - 1
      localStorage.setItem('currentPage', JSON.stringify(this.page.page))
      if (this.activeIndex === 1) {
        this.getViewTable(3, null)
      } else {
        if (this.selectedCategory.arrangeType === 3) {
          this.getViewTable(2, this.parentsData.parentsProjectId)
        } else if (this.selectedCategory.arrangeType === 1) {
          this.getViewTable(3, null)
        } else {
          this.getViewTable(2, null)
        }
      }
    }
  }
}
</script>

<style lang='scss' scoped>
@import "~@/assets/styles/collect-reorganizi.scss";
@mixin management-fixed-style{
  [data-theme="dark"] & {
    background-color: #031435 !important;
    -webkit-box-shadow: -5px 5px 10px 1px rgba(15,164,222,.16);
    box-shadow: -5px 5px 10px 1px rgba(15,164,222,.16);
  }
  [data-theme="light"] & {
    background-color: #fff;
  }
}
.el-table {
  ::v-deep .el-table__fixed-right {
    @include management-fixed-style;
  }
}
</style>
