<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 id="roleEdit-isEnable" name="radioGroup" v-model="form.isEnable" placeholder="请选择状态">
              <a-radio :value="1" id="roleEdit-isEnable1">启用</a-radio>
              <a-radio :value="0" id="roleEdit-isEnable0">禁用</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 id="roleEdit-cancel" :style="{ marginRight: '10px' }" @click="isshow=false">取消</a-button>
            <a-button id="roleEdit-save" type="primary" @click="handleSubmit()">保存</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,
      // form表单label布局
      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>
