<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="用户角色" prop="role_name">
        <el-input v-model="ruleForm.role_name" placeholder="请输入用户角色"/>
      </el-form-item>
      <el-form-item label="角色代码" prop="role_code">
        <el-input v-model="ruleForm.role_code" placeholder="请输入角色代码"/>
      </el-form-item>
      <el-form-item label="备注" prop="role_remark">
        <el-input v-model="ruleForm.role_remark" type="textarea" autosize placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRole, addRole, editRole } from '@/api/systemSetup/roleSetting/roleSetup'
import { validateSort } from '@/utils/validate'
import { filterContent } from '@/utils'

const validateSortRule = (rule, value, callback) => {
  if (!validateSort(value)) {
    callback(new Error('请输入有效的排序编号（正整数、负整数、0）'))
  } else {
    callback()
  }
}

export default {
  name: 'AddOrEditRole',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        role_id: '', // 角色id
        role_name: '', // 角色名称
        role_code: '', // 角色代码
        role_remark: '' // 备注
      },
      rules: {
        role_name: [{
          required: true,
          message: '请输入用户角色',
          trigger: 'blur'
        }],
        role_code: [{
          required: true,
          message: '请输入角色代码',
          trigger: 'blur'
        }],
      },
      loading: false
    }
  },
  created() {
    if (this.rowid) {
      this.ruleForm.role_id = this.rowid
      this.getDetail(this.rowid)
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.ruleForm.role_remark = filterContent.delHtmlTag(this.ruleForm.role_remark)
          // 添加
          if (!this.rowid) {
            addRole(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，角色信息添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editRole(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，角色信息编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 获取角色详情
    getDetail(rid) {
      getRole(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.role_name = res.F_RoleName // 角色名称
          this.ruleForm.role_code = res.F_RoleCode // 角色编号
          this.ruleForm.role_remark = res.F_Remark // 备注
        }
      })
    }
  }
}
</script>

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

</style>
