<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam" :inline="true">
        <el-form-item label="课目名称：">
          <el-select
            v-model="querys[1].value"
            clearable
            filterable
            placeholder="请选择课目"
          >
            <el-option
              v-for="item in subjectsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </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>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch"
            >搜索
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-row :gutter="10" class="fullheight">
      <el-col :span="6" 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"
            :expand-on-click-node="expandAll"
            :filter-node-method="filterNode"
            :props="defaultProps"
            highlight-current
            node-key="id"
            @node-click="handleNodeClick"
          >
          </el-tree>
        </div>
      </el-col>

      <el-col :span="18" class="fullheight">
        <div ref="contentPanel" class="bbsl-table-content">
          <div ref="btnPanel" class="bbsl-btn-content">
            <el-button icon="el-icon-plus" type="success" @click="handleAdd"
              >新增
            </el-button>
            <el-popconfirm title="确定删除选中数据吗？" @confirm="handleDelete">
              <el-button
                slot="reference"
                :disabled="!multipleSelection.length"
                icon="el-icon-delete"
                style="margin-left: 10px"
                type="danger"
                >删除
              </el-button>
            </el-popconfirm>
            <el-button
              icon="el-icon-sort"
              plain
              style="margin-left: 10px"
              type="primary"
              @click="savePropertyf"
            >
              保存排序号
            </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"
            @row-dblclick="handleDbClick"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column
              fixed
              label="序号"
              type="index"
              width="55"
            ></el-table-column>
            <el-table-column
              label="课目名称"
              min-width="150"
              prop="name"
              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="120"
              prop="createTime"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="排序"
              min-width="70"
              prop="sn"
              show-overflow-tooltip
              sortable
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.sn"
                  min="1"
                  size="small"
                  style="width:70px"
                  type="number"
                  @input="insort(scope.row)"
                >
                </el-input>
              </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>
    <ndpdMenuDialog ref="formModal" @reload="reload"></ndpdMenuDialog>
  </div>
</template>

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

export default {
  name: 'ndpdMenu',
  components: {
    ndpdMenuDialog,
    SubjectClassDialog
  },
  mixins: [TableMixin],
  data() {
    return {
      url: {
        delete: '/exam/year/assessmentSub/'
      },
      search: '',
      tableData: [],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      id: null,
      parentId: '', //上级id
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'po.position_id_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'subjectIds',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'year',
          relation: 'AND',
          value: moment().format('YYYY')
        }
      ],
      sortList: {},
      subjectsList: [],
      currentKey: null,
      type: '',
      filterText: '',
      expandAll: false
    }
  },
  created() {
    this.getTreen()
    this.getSubjects()
    this.TrainTypeList = dictUtils.getDictItemsFromCache('xllb')
    this.TrainLevelList = dictUtils.getDictItemsFromCache('xljb')
  },
  mounted() {},
  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
    }
  },
  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])
        }
      }
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // 保存排序
    savePropertyf() {
      ndpdMenu.updateAssess(this.sortList, res => {
        if (res.state) {
          this.$notify({
            type: 'success',
            message: res.message,
            duration: 2000
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    //获取课目
    getSubjects() {
      let params = {
        pageBean: {
          pageSize: -1,
          page: -1
        }
      }
      ndpdMenu.getSubjects(params, res => {
        if (res.rows) {
          this.subjectsList = res.rows
        }
      })
    },
    insort(val) {
      let id = val.id
      let value = val.sn
      this.sortList[id] = value
    },
    reload() {
      this.loadData(1)
    },
    handleAdd() {
      if (this.type == 2) {
        this.$refs.formModal.add(this.id, this.parentId)
      } else {
        this.$message({
          message: '请先选择岗位后再添加',
          type: 'warning'
        })
      }
    },
    // 重置
    clearQuerys() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.loadData(1)
    },
    handleNodeClick(data) {
      console.log(data)
      this.parentId = data.id
      this.type = data.type
      this.id = data.id
      if (data.id == '0') {
        this.querys[0].value = ''
        this.loadData(1)
      } else {
        this.querys[0].value = this.id
        this.loadData(1)
      }
    },
    // 获取树
    async getTreen() {
      this.currentKey = null
      await ndpdMenu.getPositionPage({}, res => {
        this.treeData = res
        this.currentKey = res[0].id
        this.querys[0].value = this.currentKey
        this.id = this.currentKey
        this.$set(this.querys[1])
        this.$set(this.treeData)
        this.loadData(1)
      })
    },
    // 获取list
    loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      this.queryParam.sorter = [
        {
          direction: 'DESC',
          property: 'createTime'
        }
      ]
      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])
      }
      ndpdMenu.queryAssess(this.queryParam, res => {
        this.total = res.total
        this.tableData = res.rows
        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;
  overflow-y: auto;
  border-radius: 20px;
  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>
