<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="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="code">
                <el-input
                  v-model="parameter.code"
                  clearable
                  placeholder="请输入课目分类编码"
                ></el-input>
              </el-form-item>
            </el-col>
            <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-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="备注" prop="memo">
                <el-input
                  v-model="parameter.memo"
                  :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 subMenu from '@/api/examination/subMenu.js'

export default {
  name: 'SubjectClassDialog',
  data() {
    return {
      title: '新增',
      rules: {
        code: [
          {
            required: true,
            message: '请输入课目分类编码',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入课目分类名称',
            trigger: 'blur'
          }
        ]
      },
      visible: false,
      parameter: {
        code: '',
        name: '',
        parentId: '',
        memo: ''
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    add(val) {
      this.title = '新增'
      this.visible = true
      this.parameter.parentId = val.id
      console.log(val)
    },
    edit(val) {
      this.parameter = val
      this.title = '修改'
      this.visible = true
    },
    // 关闭
    handleClose() {
      this.visible = false
      this.title = '新增'
      this.parameter = {
        code: '',
        memo: '',
        name: '',
        parentId: ''
      }
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          let method
          if (this.title === '新增') {
            method = subMenu.addSubjectClass
          }
          if (this.title === '修改') {
            method = subMenu.editSubjectClass
          }
          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)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
