<template>
  <el-dialog :title="title" :visible.sync="visible" width="800px" append-to-body @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="form.roleName" placeholder="请输入角色名称" clearable size="small" :disabled="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="角色编码" prop="roleCode">
            <el-input v-model="form.roleCode" placeholder="请输入角色编码" clearable size="small" :disabled="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="角色描述" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入角色描述" clearable size="small" :disabled="isView" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" v-if="!isView">
      <el-button type="primary" size="small" :loading="loading" @click="submitForm">确 定</el-button>
      <el-button size="small" @click="handleClose">取 消</el-button>
    </div>
    <div slot="footer" class="dialog-footer" v-else>
      <el-button size="small" @click="handleClose">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, updateRole } from '@/api/system/role'

export default {
  name: 'RoleDialog',
  props: {
    title: {
      type: String,
      default: '添加角色'
    },
    visible: {
      type: Boolean,
      default: false
    },
    roleData: {
      type: Object,
      default: () => ({})
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    isView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        roleName: '',
        roleCode: '',
        remark: ''
      },
      rules: {
        roleCode: [{ required: true, message: '请输入角色编码', trigger: 'blur' }],
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      loading: false
    }
  },
  watch: {
    roleData: {
      handler(val) {
        if (val && Object.keys(val).length > 0) {
          this.form = { ...val }
        }
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    submitForm() {
      // 查看模式下不允许提交
      if (this.isView) {
        return
      }

      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const submitData = { ...this.form }

          // 根据是否是编辑模式调用不同的API
          const apiRequest = this.isEdit ? updateRole(submitData) : addRole(submitData)

          apiRequest
            .then(response => {
              if (response.code === '0') {
                this.$message.success(this.isEdit ? '修改成功' : '添加成功')
                this.$emit('submit', submitData)
                this.handleClose()
                this.form = {}
              } else {
                this.$message.error(response.msg || (this.isEdit ? '修改失败' : '添加失败'))
                this.form = {}
              }
            })
            .catch(error => {
              this.$message.error(this.isEdit ? '修改角色出错' : '添加角色出错')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
</style>
