<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="字典标识" prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入字典标识" :disabled="codeJudgeAddOrEdit"/>
      </el-form-item>
      <el-form-item label="字典名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入字典名称"/>
      </el-form-item>
      <el-form-item label="排序编号" prop="sorts">
        <el-input-number v-model="ruleForm.sorts" controls-position="right"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDic, addDic, editDic } from '@/api/systemSetup/sysSetting/dataDictionary'

export default {
  name: 'AddOrEditType',
  props: {
    rowid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        code: '',
        name: '',
        sorts: 1,
        isleaf: false,
      },
      rules: {
        code: [{
          required: true,
          trigger: 'blur',
          message: '请输入字典标识'
        }],
        name: [{
          required: true,
          trigger: 'blur',
          message: '请输入字典名称'
        }]
      },
      loading: false,
      codeJudgeAddOrEdit: false, //字典标识输入状态
    }
  },
  created() {
    if (this.rowid) {
      this.ruleForm.id = this.rowid
      this.codeJudgeAddOrEdit = true
      const datas = {
        id: this.rowid, //坐席组id
        isleaf: false, //字典类别
      }
      this.getDetail(datas)
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const datas = {
            id: this.ruleForm.id,
            code: this.ruleForm.code,
            name: this.ruleForm.name,
            sort: this.ruleForm.sorts,
            isleaf: this.ruleForm.isleaf,
          }
          // 添加
          if (!this.rowid) {
            addDic(datas).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getListL() // 重新加载父级数据
                this.$message.success('恭喜你，字典类别添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editDic(datas).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getListL() // 重新加载父级数据
              this.$message.success('恭喜你，字典类别编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    // 获取详情
    getDetail(datas) {
      getDic(datas).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.code = res.code
          this.ruleForm.name = res.name
          this.ruleForm.sorts = res.sort
          this.ruleForm.isleaf = res.isleaf
        }
      })
    }
  }
}
</script>

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

</style>
