<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.params"
          :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="params.name"
                  clearable
                  placeholder="请输入类别名称"
                  @input="getCode()"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="类别编码" prop="code">
                <el-input
                  v-model="params.code"
                  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="params.description"
                  :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="goInfo">确认</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

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

export default {
  name: 'warning',
  data() {
    return {
      title: '',
      roomList: [],
      typeList: [],
      visible: false,
      params: {
        code: '',
        name: '',
        description: ''
      },
      rules: {
        code: [
          {
            required: true,
            message: '请输入职务编码',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入职务名称',
            trigger: 'blur'
          }
        ]
      },
      queryParam: {
        pageBean: {
          page: -1,
          pageSize: 10,
          showTotal: true
        }
      }
    }
  },
  created() {
    this.typeList = dictUtils.getDictItemsFromCache('sblx')
  },
  mounted() {},
  methods: {
    add(val) {
      this.visible = true
      this.title = '新增'
    },
    edit(val) {
      this.params = val
      this.title = '修改'
      this.visible = true
    },
    //保存
    goInfo() {
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          if (this.title === '新增') {
            userTypeMenu.addJob(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '保存成功',
                  duration: 2000
                })
                this.handleClose()
                this.$parent.loadData(1)
              } else {
              }
            })
          } else if (this.title === '修改') {
            userTypeMenu.updateJob(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '修改成功',
                  duration: 2000
                })
                this.handleClose()
                this.$parent.loadData(1)
              } else {
              }
            })
          }
        } else {
          return false
        }
      })
    },
    getCode() {
      userTypeMenu.getPinyin(this.params.name, res => {
        if (res.state) {
          this.params.code = res.value
        }
      })
    },
    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.params = {
        code: '',
        name: '',
        description: ''
      }
    }
  }
}
</script>

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