<template>
  <div class="app-content" style="height:180px;overflow:auto">
    <a-spin :spinning="loading" style="width: 100%;height: 100%;">
      <a-form-model ref="form" :model="formData" :rules="rules">
        <a-form-model-item ref="name" label="角色名称" prop="name">
          <a-input v-model="formData.name"  style="width:280px;" />
        </a-form-model-item>
        <a-form-model-item ref="authority" label="角色权限" prop="authority" >
          <a-input v-model="formData.authority" style="width:280px;" />
        </a-form-model-item>
        <a-row>
          <a-col :span="24" style="text-align: center">
            <a-button type="primary" @click="submit">提交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </div>
</template>

<script>
import { isEmptyParams } from '@/views/utils/common'

export default {
  name: "roleEdit",
  props: {
    value: {
      type: String,
      default: () => {
        return null
      },
    },
  },
  data () {
    return {
      formData: { id: null, name: null, authority: null },
      rules: {
        name: { required: true, message: '请填写角色名称', trigger: 'blur' },
        authority: { required: true, message: '请填写角色权限', trigger: 'blur' },
      },
      loading: false
    };
  },
  created () {
    if (!!this.value) {
      this.getRole()
    }
  },
  computed: {

  },
  methods: {
    getRole () {
      this.loading = true
      let pars = { id: this.value }
      this.$api.systemManage.getRoleById(pars).then(({ data = {} }) => {
        if (data) {
          this.formData = data
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          if (!!!this.value) {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.insertRole(par).then(({ data = {} }) => {
              if (data) {
                this.$message.success("添加成功！")
                this.$emit('close')
              }
              this.loading = false
            }).catch(() => { this.loading = false })
          } else {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.updateRole(par).then(({ data = {} }) => {
              if (data) {
                this.$message.success("修改成功！")
                this.$emit('close')
              }
              this.loading = false
            }).catch(() => { this.loading = false })
          }
        }
      })
    }
  },
};
</script>
<style scoped lang="less">
.app-content {
  .ant-form-item {
    margin-bottom: 6px;
  }
  ::v-deep .ant-row {
    .ant-col {
      display: inline-block;
    }
    .ant-form-item-label {
      width: 90px;
    }
    .ant-form-item-control-wrapper {
      width: calc(100% - 90px);
    }
    .ant-form-explain {
      margin-left: 5px;
      display: inline-block;
    }
  }
}
</style>
