<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <a-form-model ref="form" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="validatorRules" :model="model">
      <a-form-model-item label="职务名称" prop="roleName">
        <a-input v-model="model.roleName" />
      </a-form-model-item>
      <a-form-model-item label="数据权限" prop="dataScope">
        <j-dict-select-tag
          type="radio"
          v-model="model.dataScope"
          dict-code="data_scope"
        />
      </a-form-model-item>
      <a-form-model-item label="职务描述">
        <a-textarea
          v-model="model.remark"
          placeholder="请输入"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item label="排序">
        <a-input-number style="width: 100%" v-model="model.roleSort" :min="0" :max="100000"/>
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { httpAction } from '@/api/request'
import JModal from '@/components/JModal'
export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        roleName: [{ required: true, message: '请输入' }]
      },
      url: {
        add: '/system/role',
        edit: '/system/role'
      }
    }
  },
  created() {
  },
  methods: {
    add() {
      this.model = {}
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close(isSubmit) {
      if (isSubmit) {
        this.checkData()
      } else {
        this.visible = false
      }
    },
    checkData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveData()
        } else {
          return false
        }
      })
    },
    saveData() {
      let url, type
      if (!this.model.roleId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      httpAction(url, this.model, type).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$emit('ok')
          this.visible = false
        } else {
          console.log(res)
        }
      })
    }
  }
}
</script>
