<template>
  <div>
    <a-modal
      centered
      class="roleEdit-modal"
      :footer="null"
      :maskClosable="false"
      :title="titleText"
      v-model="isshow"
      @cancel="isshow=false"
      :width="800">
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="roleEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <!-- 用户名称 -->
          <a-form-model-item label="角色名称" prop="name">
            <a-input
              id="roleEdit-name"
              v-model.trim="form.name"
              @change="filterEmpty"
              :maxLength="30"
              allowClear
              placeholder="请输入角色名称(最多30个字符)"/>
          </a-form-model-item>
          <!-- 状态 -->
          <a-form-model-item label="状态" prop="isEnable">
            <a-radio-group name="radioGroup" v-model="form.isEnable" placeholder="请选择状态">
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">禁用</a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item label="角色描述" prop="remarks">
            <a-textarea
              id="roleEdit-remarks"
              v-model.trim="form.remarks"
              :maxLength="500"
              allowClear
              placeholder="请输入描述(最多500个字符)"/>
          </a-form-model-item>
          <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
            <a-button type="primary" @click="handleSubmit()" :style="{ marginRight: '8px' }">保存</a-button>
            <a-button :style="{ marginLeft: '8px' }" @click="isshow=false">取消</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { saveRolePower } from '@/api/power-role.js'
export default {
  name: 'RoleModal',
  mixins: [commonMixin],
  components: {
    STable, VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      titleText: '添加角色',
      isshow: this.visible,
      spinning: false,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        name: '',
        isEnable: undefined,
        remarks: ''
      },
      rules: {
        name: [ { required: true, message: '请输入角色名称', trigger: 'blur' }, { pattern: '^[^<|>]{1,30}$', message: '请输入不含<或>的字符，最多30个字符' } ],
        isEnable: [ { required: true, message: '请选择状态', trigger: 'change' } ]
      }
    }
  },
  methods: {
    // 过滤空格
    filterEmpty () {
      let str = this.form.name
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.name = str
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          params.id = _this.nowData && _this.nowData.id ? _this.nowData.id : null
          _this.spinning = true
          saveRolePower(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('refresh')
              setTimeout(function () {
                _this.isshow = false
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm () {
      this.$refs.ruleForm.resetFields()
      this.form.name = ''
      this.form.isEnable = undefined
      this.form.remarks = ''
      this.titleText = '新增角色'
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      }
    },
    nowData: {
      handler (newValue, oldValue) {
        if (this.isshow && newValue) {
          if (this.nowData.id) { //  编辑
            this.titleText = '编辑角色'
            this.form = Object.assign({}, this.nowData)
            this.form.isEnable = Number(this.nowData.isEnable)
          }
        }
      },
      deep: true
    }
  }
}
</script>

<style >
  .ant-modal-footer {
    display: none;
  }
</style>
