<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="this.title + '课目信息'"
      :visible="visible"
      custom-class="bbsl-location-dialog"
      width="50%"
    >
      <div>
        <el-form
          ref="formInfo"
          :model="this.parameter"
          :rules="rules"
          label-position="top"
          label-width="150px"
        >
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="课目名称" prop="name">
                <el-input
                  v-model="parameter.name"
                  clearable
                  placeholder="请输入课目名称"
                >
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="训练类别" prop="trainType">
                <el-select
                  style="width: 100%;"
                  v-model="parameter.trainType"
                  clearable
                  placeholder="请选择训练类型"
                  prop=""
                >
                  <el-option
                    v-for="item in TrainTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="专业" prop="majorId">
                <el-select
                  v-model="parameter.majorId"
                  placeholder="请选择专业"
                  style="width:100%"
                  filterable
                >
                  <el-option
                    v-for="item in majorVosList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="教练员" prop="coachId">
                <el-select
                  v-model="parameter.coachId"
                  placeholder="请选择教练员"
                  style="width:100%"
                  multiple
                >
                  <el-option
                    v-for="item in roleCodeList"
                    :key="item.fullname"
                    :label="item.fullname"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="课目层级" prop="trainLevel">
                <el-select
                  style="width: 100%;"
                  v-model="parameter.trainLevel"
                  clearable
                  placeholder="请选择课目层级"
                  prop=""
                >
                  <el-option
                    v-for="item in TrainLevelList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="训练条件">
                <el-input
                  v-model="parameter.condition"
                  clearable
                  placeholder="请输入训练条件"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="考核标准" prop="standard">
                <el-input
                  v-model="parameter.standard"
                  clearable
                  placeholder="请输入考核标准"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="训练内容">
                <el-input
                  v-model="parameter.content"
                  :rows="2"
                  clearable
                  placeholder="请输入训练内容"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSave">确认</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import equSysMenu from '@/api/examination/equSysMenu.js'
import * as api from '@/api/examination/jlyList.js'

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

export default {
  components: {},
  name: 'subMenuDialog',
  data() {
    return {
      rules: {
        name: [
          {
            required: true,
            message: '请输入课目名称',
            trigger: 'blur'
          }
        ],
        trainType: [
          {
            required: true,
            message: '请选择训练类别',
            trigger: 'blur'
          }
        ],
        majorId: [
          {
            required: true,
            message: '请选择专业',
            trigger: 'blur'
          }
        ],
        coachId: [
          {
            required: true,
            message: '请选择教练员',
            trigger: 'blur'
          }
        ]
      },
      visible: false,
      parameter: {
        content: '',
        condition: '',
        standard: '',
        trainLevel: '',
        trainType: '',
        majorId: '',
        coachId: []
      },

      TrainTypeList: [],
      TrainLevelList: [],
      majorVosList: [], //专业列表
      roleCodeList: [], //教练员列表
      title: ''
    }
  },
  created() {
    this.TrainTypeList = dictUtils.getDictItemsFromCache('xllb')
    this.TrainLevelList = dictUtils.getDictItemsFromCache('xljb')
    this.getMajorList()
    this.getRoleList()
  },
  mounted() {},
  methods: {
    async getMajorList() {
      await equSysMenu.getEqeSys({pageSize: -1}, res => {
        this.majorVosList = res.rows
      })
    },
    async getRoleList() {
      this.roleCodeList = await api.getUsersByRoleCode()
    },
    add(orgId, positionId) {
      this.title = '新增'
      this.visible = true
      this.parameter.positonVos = [
        {
          orgId: orgId,
          positionId: positionId
        }
      ]
    },
    edit(val) {
      this.title = '修改'
      this.visible = true

      subMenu.findById(val.id, res => {
        if (res.coachId) {
          res.coachId = res.coachId.split(',')
        }
        this.parameter = res
      })
    },

    // 关闭
    handleClose() {
      this.visible = false
      this.title = '新增'
      this.parameter = {
        trainLevel: '',
        trainType: ''
      }
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          let parameter = JSON.parse(JSON.stringify(this.parameter))
          let method
          if (this.title === '新增') {
            method = subMenu.addsubject
          }
          if (this.title === '修改') {
            method = subMenu.editSubject
          }
          parameter.majorName = this.majorVosList.filter(item => {
            return item.id == parameter.majorId
          })[0].name

          parameter.coachName = this.roleCodeList
            .filter(item => {
              return parameter.coachId.indexOf(item.id) > -1
            })
            .map(item => {
              return item.fullname
            })
            .join(',')
          parameter.coachId = parameter.coachId.join(',')
          method(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)
            }
          })
        }
      })
    }
  }
}
</script>

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