<!-- 课目 -->
<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :visible="visible"
      custom-class="bbsl-location-dialog"
      title="随机出卷"
      top="5vh"
      width="75%"
    >
      <el-container>
        <el-aside class="left-aside" width="200px">
          <el-card class="box-card org-find-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>按课目查找</span>
            </div>
            <div class="common-tree-container">
              <!-- 	<el-button size="mini" @click="expandHandle">{{
                                        expandAll ? "全部收起" : "全部展开"
                                      }}</el-button> -->
              <el-input
                v-model="filterText"
                clearable
                placeholder="输入关键字进行过滤"
              >
              </el-input>
              <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-card>
        </el-aside>
        <el-container>
          <el-main style="padding:0">
            <el-table
              ref="multipleTable"
              :data="UserData"
              :row-key="binRowKey"
              border
              height="400"
              size="medium"
              stripe
              @select="manualSelection"
            >
              <el-table-column
                align="center"
                type="selection"
                width="45"
              ></el-table-column>
              <el-table-column
                label="课目名称"
                prop="subjectName"
              ></el-table-column>
              <el-table-column
                label="试题题型"
                prop="questionName"
              ></el-table-column>
              <el-table-column
                label="题型总数"
                prop="totalNumuber"
              ></el-table-column>
            </el-table>
            <!-- <el-pagination align="right" background ref="paginationPanel" @size-change="handleSizeChange"
              @current-change="handleCurrentChange" :current-page="queryParam.pageBean.page"
              :page-sizes="[20, 30, 40, 50]" :page-size="queryParam.pageBean.pageSize"
              layout="prev, pager, next,total, sizes, jumper" :total="total"></el-pagination> -->
          </el-main>
        </el-container>
        <el-aside class="select-aside" width="400px">
          <el-table
            ref="multipleSelection"
            :data="selectUser"
            border
            height="400"
            size="medium"
            stripe
          >
            <el-table-column
              label="课目名称"
              prop="subjectName"
            ></el-table-column>
            <el-table-column
              label="试题题型"
              prop="questionName"
            ></el-table-column>
            <el-table-column
              label="题型总数"
              prop="totalNumuber"
            ></el-table-column>
            <el-table-column align="center" prop="score" width="60">
              <template slot-scope="scope">
                <!-- <i class="el-icon-delete-solid" ></i> -->
                <span style="color: #36aafd;" @click="remove(scope.row)"
                  >删除</span
                >
              </template>
            </el-table-column>
          </el-table>
        </el-aside>
      </el-container>
      <span slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button size="small" type="primary" @click="dialogConfirm"
          >确认</el-button
        >
        <el-button size="small" @click="handleClose">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'

import dictUtils from '@/components/dict/DictSelectUtils.js'
import paperMenu from '@/api/examination/paperMenu.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  props: ['userTypeId'],
  data() {
    return {
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'sp.major_id_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        }
      ],
      newParam: {
        pageBean: {
          pageSize: 20,
          page: 1
        },
        querys: [
          {
            group: 'main',
            operation: 'RIGHT_LIKE',
            parentGroup: '',
            property: 'name',
            relation: 'AND',
            value: ''
          }
        ]
      },

      search: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'questionName'
      },
      nameArr: [],
      multipleSelection: null,
      params: {
        majorIds: [],
        subjectIds: []
      },
      subjectId: null,
      topicList: [],
      userlength: null,
      userInfoVos: [],
      currentKey: null,
      filterText: '',
      expandAll: false
    }
  },
  created() {
    this.topicList = dictUtils.getDictItemsFromCache('sttx')
  },
  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() {},
  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
    },
    add(val, row) {
      this.currentKey = null
      console.log(val, row)
      if (row.length > 0) {
        this.userInfoVos = row
        this.selectUser = JSON.parse(JSON.stringify(row))
        this.userlength = row.length
      }
      this.visible = true
      if (val) {
        this.treeData = val
        this.subjectId = this.treeData[0].subjectId || this.treeData[0].id
        this.currentKey = this.treeData[0].id
      }
      this.loadData()
    },
    // 获取列表
    loadData() {
      if (this.subjectId) {
        paperMenu.findBySubjectId(this.subjectId, res => {
          this.UserData = []
          res.forEach(u => {
            this.topicList.forEach(t => {
              if (u.type == t.value) {
                u.questionName = t.name
              }
            })
            this.UserData.push({
              // id: u.id,
              subjectId: this.subjectId,
              questionType: u.type,
              questionName: u.questionName,
              numuber: null,
              totalNumuber: u.count,
              score: u.count,
              subjectName: u.subjectName
            })
          })
          this.$nextTick(() => {
            this.selectUser.forEach(s => {
              this.UserData.forEach(u => {
                if (
                  s.subjectName + s.questionName ==
                  u.subjectName + u.questionName
                ) {
                  this.$refs.multipleTable.toggleRowSelection(u, true)
                }
              })
            })
          })
          this.$set(this.UserData)
        })
      }
    },
    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = null
      this.loadData(1)
    },

    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '',
          name: '课目',
          type: 0,
          children: []
        }
      ]
      this.UserData = []
      this.selectUser = []
    },
    // 树
    handleNodeClick(item) {
      console.log(item)
      this.subjectId = item.subjectId || item.id
      this.loadData(1)
    },
    dialogConfirm() {
      this.$emit('getRandom', this.selectUser)
      this.handleClose()
    },
    binRowKey(row) {
      return row.subjectName + row.questionName
    },
    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          if (
            row.subjectName + row.questionName ==
            u.subjectName + u.questionName
          ) {
            type = false
          }
        }
      })
      console.log(type)
      return type
    },
    manualSelection(selection, row) {
      //先判断当前操作flag
      //true勾选还是false取消
      let flag = selection.some(item => {
        console.log(row.subjectName + row.questionName)
        console.log(item.subjectName + item.questionName)
        if (
          row.subjectName + row.questionName ==
          item.subjectName + item.questionName
        )
          return true
      })
      console.log(flag)
      if (flag) {
        this.selectUser.push(row)
      } else {
        this.selectUser.forEach((s, si) => {
          if (
            s.subjectName + s.questionName ==
            row.subjectName + row.questionName
          ) {
            this.selectUser.splice(si, 1)
          }
        })
      }
    },
    handleSelectionChange(row) {
      this.selectUser = row
      this.multipleSelection = row
    },
    remove(val) {
      console.log(val)
      this.selectUser.forEach((u, ui) => {
        if (
          val.subjectName + val.questionName ==
          u.subjectName + u.questionName
        ) {
          this.selectUser.splice(ui, 1)
        }
      })
      this.userInfoVos = JSON.parse(JSON.stringify(this.selectUser))
      this.userlength = this.selectUser.length
      this.loadData(1)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 100% !important;
  max-height: 80%;
}

.left-aside {
  border-right: 1px solid #eee;
  padding: 10px;
}

.select-aside {
  border-left: 1px solid #eee;
  margin-left: 10px;
}

.el-card {
  height: 96% !important;
}

.el-tree {
  max-height: 400px;
  overflow-y: auto;
  border: none;
}

::v-deep .el-table__header-wrapper .el-checkbox {
  //找到表头那一行，然后把里面的复选框隐藏掉
  display: none;
}
</style>
