<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam" :inline="true">
        <el-form-item label="考生姓名：">
          <el-input
            v-model="querys[1].value"
            clearable
            placeholder="请输入考生姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="年份：">
          <el-date-picker
            v-model="querys[2].value"
            placeholder="选择年份"
            type="year"
            value-format="yyyy"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评价等级：">
          <el-select
            v-model="querys[3].value"
            clearable
            placeholder="请选择评价等级"
          >
            <el-option
              v-for="item in pjdjeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="loadData(1)"
            >搜索
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-row :gutter="10" class="fullheight">
      <el-col :span="5" class="fullheight">
        <div class="common-tree-container">
          <div class="container-head">
            <el-input
              v-model="filterText"
              clearable
              placeholder="输入关键字进行过滤"
            >
            </el-input>
            <img
              v-if="expandAll"
              src="@/assets/img/quanjusuoxiao2.png"
              @click="expandHandle"
            />
            <img v-else src="@/assets/img/quanping.png" @click="expandHandle" />
            <i class="el-icon-refresh" @click="getTreen"></i>
          </div>
          <el-tree
            v-if="treeData.length"
            ref="tree"
            :current-node-key="currentKey"
            :data="treeData"
            :filter-node-method="filterNode"
            :props="defaultProps"
            expand-on-click-node="expandAll"
            highlight-current
            node-key="id"
            @node-click="handleNodeClick"
          >
          </el-tree>
        </div>
      </el-col>

      <el-col :span="19" class="fullheight">
        <div ref="contentPanel" class="bbsl-table-content">
          <div ref="btnPanel" class="bbsl-btn-content">
            <el-button type="primary" @click="openGrade"
              >批量设置成绩
            </el-button>
            <el-upload
              ref="upload"
              :auto-upload="false"
              :file-list="fileList"
              :http-request="upLoadFile"
              :on-change="e => handleChange(e, fileList)"
              :show-file-list="false"
              accept="."
              action="no"
              class="avatar-uploader"
            >
              <el-button
                icon="el-icon-download"
                style="margin-right: 10px;"
                type="primary"
                >导入
              </el-button>
            </el-upload>
            <!-- <el-button icon="el-icon-download" type="primary" v-else @click="importData">导入
						</el-button> -->
            <el-button icon="el-icon-upload2" type="warning">
              <!-- {{this.portal+'/exam/user/evaluation/export?positionId='+this.currentKey}} -->
              <a
                :href="
                  this.portal +
                    '/exam/user/evaluation/export?orgId=' +
                    this.parentId
                "
                target="_blank"
                >导出</a
              >
            </el-button>
            <el-button icon="el-icon-document" type="info">
              <a
                :href="this.portal + '/exam/user/evaluation/downModel'"
                target="_blank"
                >下载模板</a
              >
            </el-button>
          </div>
          <el-table
            ref="multipleSelection"
            v-loading="loading"
            :cell-style="{'text-align': 'center'}"
            :data="tableData"
            :header-cell-style="{'text-align': 'center'}"
            :max-height="tableMaxHeight"
            :style="{width: '100%'}"
            size="mini"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              fixed
              label="序号"
              type="index"
              width="55"
            ></el-table-column>
            <el-table-column
              label="考生"
              min-width="150"
              prop="userName"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="年份"
              min-width="100"
              prop="year"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="总科目数"
              min-width="100"
              prop="totalCount"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="考核课目"
              min-width="150"
              prop="subjectName"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="已考科目数"
              min-width="100"
              prop="inCount"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="未考科目数"
              min-width="100"
              prop="notCount"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="评价等级"
              min-width="100"
              prop="levelName"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="最后评定时间"
              min-width="150"
              prop="evaluationTime"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              min-width="120"
              show-overflow-tooltip
            >
              <template #default="scope">
                <el-button @click="handleClick(scope.row)">评定详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            ref="paginationPanel"
            :current-page="queryParam.pageBean.page"
            :page-size="queryParam.pageBean.pageSize"
            :page-sizes="[20, 30, 40, 50]"
            :total="total"
            align="right"
            background
            class="bbsl-pagination"
            layout="prev, pager, next,total, sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
    <gradeDialog ref="gradeDialog"></gradeDialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import moment from 'moment'
import ndbbMenu from '@/api/examination/ndbbMenu.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import gradeDialog from './components/gradeDialog.vue'

export default {
  name: 'ndbbMenu',
  mixins: [TableMixin],
  components: {
    gradeDialog
  },
  data() {
    return {
      url: {
        delete: '/exam/year/EvaluationmentSub/'
      },
      search: '',
      tableData: [],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      parentId: null,
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'positionId',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'e.user_name_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'e.year_',
          relation: 'AND',
          value: moment().format('YYYY')
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'e.level_',
          relation: 'AND',
          value: ''
        }
      ],
      sortList: {},
      subjectsList: [],
      pjdjeList: [],
      currentKey: null,
      filterText: '',
      fileList: [],
      type: '',
      id: '',
      expandAll: false
    }
  },
  created() {
    this.pjdjeList = dictUtils.getDictItemsFromCache('pjdj')
  },

  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
    // currentKey: {
    // 	handler(value) {
    // 		if (value) {
    // 			this.$nextTick(
    // 				() => {
    // 					this.$refs.tree.setCurrentKey(this.currentKey);
    // 				});
    // 		}
    // 	},
    // 	immediate: true,
    // 	valType: false
    // },
  },
  mounted() {
    this.getTreen()
  },
  methods: {
    expandHandle() {
      console.log(this.$refs.tree.store)
      this.expandAll = !this.expandAll
      this.expandNodes(this.$refs.tree.store.root)
    },
    // 遍历树形数据，设置每一项的expanded属性，实现展开收起
    expandNodes(node) {
      node.expanded = this.expandAll
      for (let i = 0; i < node.childNodes.length; i++) {
        node.childNodes[i].expanded = this.expandAll
        if (node.childNodes[i].childNodes.length > 0) {
          this.expandNodes(node.childNodes[i])
        }
      }
    },
    importData() {
      this.$message({
        message: '请选择课目',
        type: 'warning'
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // 保存排序
    savePropertyf() {
      ndbbMenu.updateEvaluation(this.sortList, res => {
        if (res.state) {
          this.$notify({
            type: 'success',
            message: res.message,
            duration: 2000
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    openGrade() {
      console.log(1, this.currentKey)
      this.$refs.gradeDialog.open(this.currentKey)
    },
    insort(val) {
      let id = val.id
      let value = val.sn
      this.sortList[id] = value
    },
    reload() {
      this.loadData(1)
    },
    handleClick(val) {
      this.$router.push({
        name: 'ndbbMenuDetails',
        params: val
      })
    },
    toPreview() {},
    handleAdd() {},
    //导入
    handleChange(file, fileList) {
      console.log(1)
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
      if (
        file.raw.name.split('.')[1] == 'xlsx' ||
        file.raw.name.split('.')[1] == 'xls' ||
        file.raw.name.split('.')[1] == 'xlsm'
      ) {
        this.upLoadFile(file.raw)
      } else {
        this.$message.warning('请上传xls,xlsx,xlsm格式文件！')
        fileList.splice(0)
      }
    },
    //自定义上传方法（覆盖默认上传行为）
    upLoadFile(file) {
      let formData = new FormData()
      formData.append('file', file)
      formData.append('orgId', this.parentId)
      // subjectId:this.querys[1].value,
      ndbbMenu.importExcel(formData, res => {
        if (res.state) {
          this.$notify({
            type: 'success',
            message: '导入成功',
            duration: 2000
          })
          this.loadData(1)()
        } else {
          this.$notify({
            type: 'error',
            message: res.message,
            duration: 2000,
            offset: 80
          })
          this.loadData(1)
        }
      })
    },
    exportFile() {
      if (this.type == 2) {
        ndbbMenu.export(this.currentKey, res => {
          if (res) {
          }
        })
      } else {
        this.$message({
          message: '请选择课目',
          type: 'warning'
        })
      }
    },
    handleNodeClick(data) {
      console.log(data)
      this.type = data.type
      if (data.type == 1) {
        this.parentId = data.id
      } else {
        this.parentId = ''
      }
      this.currentKey = data.id
      if (data.id == '0') {
        this.querys[0].value = ''
        this.loadData(1)
      } else {
        this.querys[0].value = data.id
        this.loadData(1)
      }
      console.log(2, this.currentKey)
      sessionStorage.setItem('currentKey', data.id)
      sessionStorage.setItem('type', this.type)
    },
    // 获取树
    getTreen() {
      this.currentKey = ''
      ndbbMenu.getPositionPage({}, res => {
        this.treeData = res
        // 	if (sessionStorage.getItem('currentKey')) {
        // 		this.currentKey = sessionStorage.getItem('currentKey') //不是第一次了，取出来
        // 		console.log(this.currentKey);
        // 		this.type = sessionStorage.getItem('type')
        // 	} else {
        this.currentKey = res[0].id
        this.type = res[0].type

        // 	}
        console.log(this.currentKey)
        this.querys[0].value = this.currentKey
        this.$set(this.querys[0])
        this.$set(this.treeData)
        this.loadData(1)
      })
    },

    // 获取list
    loadData(page) {
      console.log(this.querys[0].value)
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      if (this.querys[3].value) {
        this.queryParam.querys.push(this.querys[3])
      }
      ndbbMenu.queryEvaluation(this.queryParam, res => {
        this.total = res.total
        this.tableData = res.rows
        this.tableData.forEach(item => {
          this.pjdjeList.forEach(ee => {
            if (item.level == ee.value) {
              item['levelName'] = ee.name
            }
          })
        })
        this.$set(this.tableData)
        this.loading = false
      })
    },

    // 新增
    append(data, type) {
      if (type != 'father') {
        this.$refs.SubClassModal.add(data)
      } else {
        this.$refs.SubClassModal.add(data)
      }
    },
    // 修改
    upDateinfo(data, type) {
      this.$refs.SubClassModal.edit(data)
    }
  }
}
</script>
<style lang="scss" scoped>
a {
  text-decoration: none;
  color: #ffffff;
}

.type-tag {
  margin: 0 2px;
}

.common-tree-container {
  height: calc(100% - 70px);
  margin-top: 0.52083vw;
  padding: 1.04167vw 1.04167vw;
  background-color: #fff;
  position: relative;
  box-sizing: border-box;
  border-radius: 20px;
  overflow-y: auto;
  box-shadow: 0px 0px 0.625vw 0 rgba(92, 102, 83, 0.2);
}

.avatar-uploader {
  display: inline-block;
  margin-left: 10px;
}

.title {
  width: 100%;
  margin-bottom: 10px;

  i {
    color: #1289fe;
    font-size: 25px;
  }

  span {
    font-size: 25px;
  }
}

::v-deep .el-tree {
  height: 90%;
  overflow-y: auto;
  border: none;
}

::v-deep .el-tree-node__content {
  padding: 5px 0;
}

.container-head {
  display: flex;
  align-items: center;

  img {
    width: 28px;
    height: 28px;
    margin: auto 5px;
    cursor: pointer;
  }

  i {
    font-size: 28px;
    font-weight: 600;
    cursor: pointer;
  }
}
</style>
