<template>
  <div class="collect-no-tab">
    <CollectHeader ref="collectHeaderRef" :is-title-type="isTitleType" :selected-category="selectedCategory" :arc-id="arcId" :selections="selections" :is-recycle="isRecycle" />
    <!-- <el-button type="text" @click="openAnjuan">打开案卷的Drawer</el-button> -->
    <div class="collect-table">
      <el-table
        ref="table"
        v-loading="getTableDisplayFieldsLoading"
        class="archives-table"
        :data="projectData"
        highlight-current-row
        style="width: 100%;"
        height="calc(100vh - 364px)"
        :row-key="rowKey"
        :row-class-name="tableRowClassName"
        border
        @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="案卷" 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>
      <!--分页组件-->
      <el-pagination
        v-if="projectData.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="false" />
  </div>
</template>

<script>
import { manageLibraryCrud } from '../mixins/index'
import { header, form } from '@crud/crud'
import CollectHeader from '../module/collectHeader.vue'
import ArchivesInfo from '../module/archivesInfo/index'

export default {
  name: 'Project',
  components: { CollectHeader, ArchivesInfo },
  mixins: [
    header(),
    form({}),
    manageLibraryCrud
  ],
  props: {
    data: {
      type: String,
      default: ''
    },
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    },
    isRecycle: {
      type: Boolean,
      default: false
    },
    smartQuery: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  inject: ['parentsData'],
  data() {
    return {
      isTitleType: 2,
      categoryId: '',
      arcId: '',
      activeIndex: '1',
      selections: [],
      yearData: [],
      parentId: null,
      currentPage: 1,
      mousedownTime: 0,
      mousedownX: 0,
      mousedownY: 0
    }
  },
  watch: {
    selectedCategory: function(newValue, oldValue) {
    },
    tableDisplayFields(val) {
      this.doLayout()
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    getCommonData(categoryLevel, parentId, type) {
      this.getViewTable(categoryLevel, parentId, type)
    },
    sendYearDataToParent() {
      this.$emit('myYearEvent', this.yearData)
    },
    openAnjuan(data, parentId, parentRow) {
      // this.$emit('openAnjuan', '传值')
      this.$emit('openAnjuan', data, parentId, parentRow)
    },
    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
    },
    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(() => {
        this.$refs.archivesInfo.detailTitle = '项目详情'
        this.$refs.archivesInfo.archivesInfoVisible = true
        this.$refs.archivesInfo.archivesTabIndex = 0
        this.$refs.archivesInfo.getDetial(1, row.id)
      })
    },
    // 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)
      }

      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
          this.openAnjuan('所属项目：' + row.project_no, this.parentId, row)
          this.getDictsList(1)
        }, 300)
        this.selections = this.crud.selections
      }
    },
    // 触发单选
    handleCurrentChange(selection, row) {
      this.selections = selection
    },
    handleSizeChange(size) {
      this.currentPage = 1
      this.page.size = size
      this.page.page = 0
      localStorage.setItem('currentPageSize', size)
      this.getViewTable(1)
    },
    handleCurrentPage(pageVal) {
      this.currentPage = pageVal
      this.page.page = pageVal - 1
      localStorage.setItem('currentPage', JSON.stringify(this.page.page))
      this.getViewTable(1)
    }
  }
}
</script>

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