<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form ref="searchForm" v-model="queryParam.querys" :inline="true">
        <el-form-item label="年份：">
          <el-date-picker
            v-model="queryParam.querys[1].value"
            placeholder="选择年份"
            type="year"
            value-format="yyyy"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="resetSearch">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="page-content">
      <div class="tree-container">
        <tree-side
          ref="treeSide"
          :default-expand-all="false"
          :defaultProps="{children: 'children', label: 'name'}"
          :tabList="['组织维度']"
          :treeData="treeData"
          isDeselect
          @currentChange="currentChange"
        ></tree-side>
      </div>
      <div ref="contentPanel" class="bbsl-table-content">
        <div ref="btnPanel" class="bbsl-btn-content">
          <el-button icon="el-icon-upload2" type="warning" @click="exportFile">
            导出
          </el-button>
        </div>
        <el-table
          ref="multipleSelection"
          v-loading="loading"
          :cell-style="{'text-align': 'center'}"
          :data="tableData"
          :default-sort="getSorterDirection(queryParam, true)"
          :header-cell-style="{'text-align': 'center'}"
          :max-height="tableMaxHeight"
          :style="{width: '100%'}"
          size="mini"
          @selection-change="handleSelectionChange"
          @sort-change="sortChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            :index="
              index =>
                index +
                1 +
                (queryParam.pageBean.page - 1) * queryParam.pageBean.pageSize
            "
            fixed
            label="序号"
            type="index"
            width="55"
          />
          <el-table-column
            :formatter="({year}) => year || '-'"
            label="年份"
            min-width="100"
            prop="year"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({orgName}) => orgName || '-'"
            label="单位名称"
            min-width="100"
            prop="orgName"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({shouldCount}) => shouldCount || '-'"
            label="应考试人数"
            min-width="100"
            prop="shouldCount"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({actualCount}) => actualCount || '-'"
            label="实际考试人数"
            min-width="100"
            prop="actualCount"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({excellentRate}) => excellentRate || '-'"
            label="优秀率"
            min-width="100"
            prop="excellentRate"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({passRate}) => passRate || '-'"
            label="及格率"
            min-width="100"
            prop="passRate"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="({referRate}) => referRate || '-'"
            label="参考率"
            min-width="100"
            prop="referRate"
            show-overflow-tooltip
          />
          <el-table-column
            :formatter="
              ({level}) =>
                dictionaryEscape('dagl-pjdj', level, {congruent: false}) || '-'
            "
            label="评定等级"
            min-width="100"
            prop="level"
            show-overflow-tooltip
          />
        </el-table>
        <el-pagination
          ref="paginationPanel"
          :current-page="queryParam.pageBean.page"
          :page-size="queryParam.pageBean.pageSize"
          :page-sizes="[20, 30, 40, 50]"
          :total="queryParam.pageBean.total"
          align="right"
          background
          class="bbsl-pagination"
          layout="prev, pager, next,total, sizes, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import {
  getSorterDirection,
  refineRequestBody,
  setQueryValue
} from '@/utils/requestWrappers'
import * as api from '@/api/examDataMenu/archivesManager/orgFiles'
import {dictionaryEscape} from '@/utils/dictionary'
import treeSide from '@/components/treeSide/index.vue'
import {treeDataByDem} from '@/api/systemInterface'
import moment from 'moment'

export default {
  name: 'OrgFiles',
  components: {treeSide},
  mixins: [TableMixin],
  data() {
    return {
      tableData: [], // 表格数据
      // 查询对象
      queryParam: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'EQUAL', property: 'org_id_'},
          {operation: 'LIKE', property: 'year_', value: moment().format('YYYY')}
        ],
        sorter: [{direction: '', property: ''}]
      },
      treeData: [],
      activeTreeItem: {},
      uploadLoading: false,
      noLoadData: true
    }
  },
  created() {
    // 获取树形目录
    this.getTreeData(treeData => {
      // 回调中默认选中第一项并将其id作为参数请求表格
      this.activeTreeItem = treeData[0] || {}
      this.$nextTick(_ => {
        this.$refs.treeSide?.setCurrentKey(treeData[0]?.id)
        this.$refs.treeSide?.expandHandle(true, 2)
        setQueryValue(this.queryParam, 'org_id_', treeData[0]?.id || '')
        this.loadData()
      })
    })
  },
  methods: {
    dictionaryEscape,
    getSorterDirection,

    //获取树
    async getTreeData(callback) {
      this.treeData = [
        {
          name: '***部队',
          id: '',
          parentId: '',
          children: await treeDataByDem()
        }
      ]
      // 保证重新刷新树数据后，选中效果不失效
      if (this.activeTreeItem.id) {
        this.$nextTick(_ => {
          this.$refs.treeSide?.setCurrentKey(this.activeTreeItem.id)
        })
      }
      callback && callback(this.treeData)
    },

    //左边树节点
    currentChange(nodeItem) {
      // 修改对应的字段值
      setQueryValue(this.queryParam, 'org_id_', nodeItem?.id || '')
      this.activeTreeItem = nodeItem || {}
      this.calcTableHeight(500)
      this.queryParam.pageBean.page = 1
      this.loadData()
    },

    // 获取表格数据
    async loadData() {
      try {
        this.loading = true
        const {total = 0, rows = []} = await api.tableData(
          refineRequestBody(this.queryParam)
        )
        this.queryParam.pageBean.total = total
        this.tableData = rows
      } finally {
        this.loading = false
      }
    },

    // 重置搜索
    resetSearch() {
      this.queryParam = this.$options.data().queryParam
      this.activeTreeItem = this.treeData[0] || {}
      this.$nextTick(_ => {
        this.$refs.treeSide?.setCurrentKey(this.activeTreeItem.id)
      })
      this.loadData()
    },

    // 导出数据
    async exportFile() {
      await api.exportData(
        refineRequestBody({
          querys: this.queryParam.querys,
          pageBean: {
            pageSize: -1
          }
        })
      )
    }
  }
}
</script>
<style lang="scss" scoped>
.bbsl-common-page {
  display: flex;
  flex-direction: column;
  row-gap: 10px;

  .page-content {
    display: flex;
    height: calc(100% - 70px);
    column-gap: 10px;

    .tree-container {
      height: 100%;
      min-width: 300px;
      background: #fff;
      border-radius: 20px;
      padding: 20px 20px;
      box-shadow: 0px 0px 12px 0 rgba($color: #000000, $alpha: 0.1);
      box-sizing: border-box;
    }

    .bbsl-table-content {
      height: 100%;
      flex: 1;
      margin-top: 0;
      box-sizing: border-box;
    }
  }
}

.avatar-uploader {
  display: inline-block;
  margin-left: 10px;
}
</style>
