<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" disabled/>
      </el-form-item>
      <el-form-item label="字典项名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入字典项名称"/>
      </el-form-item>
      <el-form-item label="备注" prop="des">
        <el-input v-model="ruleForm.des" type="textarea" autosize 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'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEditInfo',
  props: {
    row_dicid: {// 添加时 字典类别的id
      type: Number,
      default: 0
    },
    row_diccode: {// 添加时 字典类别的标识
      type: String,
      default: ''
    },
    rowid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        code: '',
        name: '',
        des: '',
        sorts: 1,
        isleaf: true,
        pid: 0,
      },
      rules: {
        code: [{
          required: true,
          trigger: 'blur',
          message: '字典标识不能为空'
        }],
        name: [{
          required: true,
          trigger: 'blur',
          message: '请输入字典项名称'
        }],
      },
      loading: false
    }
  },
  created() {
    if (this.row_diccode) {
      this.ruleForm.code = this.row_diccode
      this.ruleForm.pid = this.row_dicid
    }
    if (this.rowid) {
      this.ruleForm.id = this.rowid
      const datas = {
        id: this.rowid, //坐席组id
        isleaf: true, //字典类别
      }
      this.getDetail(datas)
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const datas = {
            id: this.ruleForm.id, // 为空或null表示需要添加，有数值表示需要更新
            code: this.ruleForm.code, // 字典标识
            name: this.ruleForm.name, // 字典项名称
            describe: filterContent.delHtmlTag(this.ruleForm.des), // 备注
            sort: this.ruleForm.sorts, // 排序
            isleaf: this.ruleForm.isleaf, //字典项
            pid: this.ruleForm.pid, //字典id
          }
          // 添加
          if (!this.rowid) {
            addDic(datas).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getListR() // 重新加载父级数据
                this.$message.success('恭喜你，字典项添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editDic(datas).then(response => {
            console.log("data", datas.pid)
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getListR() // 重新加载父级数据
              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.des = res.des
          this.ruleForm.sorts = res.sort
          this.ruleForm.pid = res.pid
          this.ruleForm.isleaf = res.isleaf
        }
      })
    }
  }
}
</script>

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

</style>
