<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>角色名称：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入角色名称"
                v-model="detail.RoleName"
                maxlength="10"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>备注：</span>
          <div class="flex-item">
            <div>
              <el-input
                placeholder="请输入备注"
                type="textarea"
                :autosize="{ minRows: 3, maxRows: 5}"
                v-model="detail.Remark"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click='submit'>保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const { mapState: mapRoleState, mapActions: mapRoleActions } = createNamespacedHelpers('role')
export default {
  name: '',
  data () {
    return {}
  },
  components: {},
  computed: {
    ...mapRoleState({
      detail: x => x.roleInfo,
    }),
    ...mapState({
      caseid: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId,
    })
  },
  methods: {
    ...mapRoleActions([
      'GetRoleByID',
      'AddRole',
      'UpdateRole',
      'SetRoleNull',
    ]),
    submit () {
      if (!this.detail.RoleName || this.detail.RoleName === '') {
        this.$message({
          type: 'error',
          message: '角色名称不允许为空！'
        })
        return false
      }
      if ((this.detail.RoleId || '') === '') {
        this.detail.CaseId = this.caseid
        this.detail.OrgId = this.orgid
        this.AddRole({...this.detail, callback: this.afterSave})
      } else {
        this.UpdateRole({...this.detail, callback: this.afterSave})
      }
    },
    afterSave () {
      this.$message({
        type: 'success',
        message: '操作成功'
      })
      this.$router.push({ name: 'roleManager' })
    },
    cancel () {
      this.$router.go(-1)
    }
  },
  beforeMount () {
    const { id } = this.$route.query
    if (id && id !== '') {
      this.GetRoleByID({ roleid: id })
    } else {
      this.SetRoleNull()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
