<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-container>
          <el-header class="middle-header">
            <div style="padding: 10px 0">
              <el-date-picker
                v-model="parameter.year"
                placeholder="选择年份"
                type="year"
                value-format="yyyy"
              >
              </el-date-picker>

              <el-input
                v-model="querys[1].value"
                clearable
                placeholder="请输入名称"
                prefix-icon="el-icon-search"
                style="margin-left: 10px;width: 200px;"
                @keyup.enter.native="searchEnterFun"
              >
              </el-input>
              <el-button
                icon="el-icon-search"
                size="small"
                style="margin-left: 20px"
                type="primary"
                @click="searchEnterFun()"
                >查询
              </el-button>
              <!-- <el-button size="small" @click="reset()" icon="el-icon-refresh">重置</el-button> -->
            </div>
          </el-header>
          <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
                :reserve-selection="true"
                align="center"
                type="selection"
                width="45"
              >
              </el-table-column>
              <el-table-column label="课目信息" prop="name"></el-table-column>
              <el-table-column
                label="训练类别"
                prop="trainTypeName"
              ></el-table-column>
            </el-table>
          </el-main>
          <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
            layout="prev, pager, next,total, sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </el-container>
        <el-aside class="select-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-tree
                ref="selectTree"
                :data="selectUser"
                :props="selectUserProps"
                :render-content="renderContent"
                node-key="id"
              >
              </el-tree>
            </div>
          </el-card>
        </el-aside>
      </el-container>
      <span slot="footer" class="dialog-footer" style="text-align: center;">
        <!-- <el-button size="small" type="primary" @click="handleSave">确认</el-button> -->
        <el-button size="small" @click="handleClose">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
import ndpdMenu from '@/api/examination/ndpdMenu.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import questionListMenu from '@/api/examination/questionListMenu.js'

export default {
  components: {},
  name: 'ndpdMenuDialog',
  data() {
    return {
      visible: false,
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'po.position_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: ''
        }
      ],
      parameter: {
        year: moment().format('YYYY'),
        positionId: null,
        subjectInfoList: []
      },
      //已选
      selectUser: [],
      selectUserProps: {
        label: 'name'
      },
      UserData: [],
      queryParam: {
        pageBean: {
          pageSize: 20,
          page: 1
        }
      },
      total: null
    }
  },
  created() {},
  mounted() {},
  methods: {
    add(val, parentId) {
      this.title = '新增'
      this.querys[0].value = parentId
      this.parameter.positionId = parentId
      this.trainTypeList = dictUtils.getDictItemsFromCache('xllb')
      this.visible = true
      this.loadData(1)
      this.getSelectUser()
    },
    edit(val) {
      this.title = '修改'
      this.visible = true
    },

    // 关闭
    handleClose() {
      this.visible = false
      this.$emit('reload')
    },
    getSelectUser() {
      this.selectUser = []
      let parma = {
        pageBean: {
          pageSize: -1,
          page: 1
        },
        querys: [
          {
            group: 'main',
            operation: 'LIKE',
            parentGroup: '',
            property: 'po.position_id_',
            relation: 'AND',
            value: this.querys[0].value
          },
          {
            group: 'main',
            operation: 'LIKE',
            parentGroup: '',
            property: 'year',
            relation: 'AND',
            value: this.parameter.year
          }
        ]
      }
      ndpdMenu.queryAssess(parma, res => {
        this.total = res.total
        this.selectUser = res.rows
        this.$refs.multipleTable.clearSelection()
        res.rows.forEach(s => {
          this.UserData.forEach(u => {
            if (s.name == u.name) {
              this.$refs.multipleTable.toggleRowSelection(u, true)
            }
          })
        })
      })
    },
    // 获取列表
    loadData(page) {
      this.$nextTick(() => {
        this.$refs.multipleTable.clearSelection()
      })
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.UserData = []
      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])
      }
      questionListMenu.findByMajorIds(this.queryParam, res => {
        this.total = res.total
        this.UserData = res.rows
        this.UserData.forEach(u => {
          this.trainTypeList.forEach(e => {
            if (u.trainType == e.value) {
              u.trainTypeName = e.name
            }
          })
        })
        this.selectUser.forEach(s => {
          this.UserData.forEach(u => {
            console.log(s.name == u.name)
            if (s.name == u.name) {
              console.log(s.name)
              this.$refs.multipleTable.toggleRowSelection(u, true)
            }
          })
        })
      })
      this.$set(this.UserData)
    },
    // 保存
    handleSave() {
      // console.log(this.parameter);
      // this.parameter.subjectIds = this.subject.toString()

      this.parameter.subjectInfoList = this.selectUser

      let method
      if (this.title === '新增') {
        method = ndpdMenu.addAssess
      }
      if (this.title === '修改') {
        method = ndpdMenu.editAssess
      }
      method(this.parameter, response => {
        if (response.state) {
          this.$notify({
            type: 'success',
            message: this.title + '成功',
            duration: 2000
          })
          this.handleClose()
          this.$emit('reload')
        } else {
          this.$message.warning(response.message)
        }
      })
    },
    binRowKey(row) {
      return row.id
    },
    //搜索
    searchEnterFun() {},
    handleCurrentChange() {},
    handleSizeChange() {},
    manualSelection(selection, row) {
      let flag = selection.some(item => {
        if (item.id === row.id) return true
      })
      if (flag) {
        this.selectUser.push(row)
        let params = {
          year: this.parameter.year,
          positionId: this.parameter.positionId,
          subjectInfoList: [row]
        }
        ndpdMenu.addAssess(params, res => {
          if (res.state) {
            this.getSelectUser()

            this.$notify({
              type: 'success',
              message: this.title + '成功',
              duration: 2000
            })
          }
        })
      } else {
        this.selectUser.forEach((s, si) => {
          if (s.name == row.name) {
            let params = {
              year: this.parameter.year ? this.parameter.year : '',
              subjectIds: row.id ? row.id : '',
              positionId: this.parameter.positionId
                ? this.parameter.positionId
                : ''
            }
            this.$http
              .post(
                window.context.portal + '/exam/year/assessmentSub/deleteSub',
                params
              )
              .then(res => {
                if (res.data.state) {
                  this.getSelectUser()
                  // this.selectUser.splice(si, 1)
                  this.$notify({
                    type: 'success',
                    message: '删除成功',
                    duration: 2000
                  })
                }
              })
          }
        })
      }
    },
    remove(val) {
      ndpdMenu.remove(val.id, res => {
        // this.selectUser.splice(si, 1)
        if (res.state) {
          this.getSelectUser()
          this.$notify({
            type: 'success',
            message: '删除成功',
            duration: 2000
          })
        }
      })
      // this.$http.delete(window.context.portal +"/exam/year/assessmentSub/"+val.id, ).then(res => {
      // 	if (res.data.state) {
      // 		this.selectUser.splice(si, 1)
      // 		this.$notify({
      // 			type: 'success',
      // 			message: '删除成功',
      // 			duration: 2000
      // 		})
      // 	}
      // })
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.name}</span>
          <span>
            <el-button
              style="width:100%;border:0;margin:0;"
              size="mini"
              // icon="el-icon-delete"
              type="text"
              on-click={() => this.remove(data)}
            >
              删除
            </el-button>
          </span>
        </span>
      )
    }
  }
}
</script>

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

::v-deep .el-tree {
  overflow: auto !important;
}
</style>
