<template>
  <Modal :maskClosable="false" :keyboard="false" v-model:open="data.isShowModal" :footer="null" :title="$t('system.role.addTitle')" width="50%"
           @cancel="_close">
    <Form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 6 }" labelAlign="left"
           :labelWrap="true">
      <Row :gutter="24">
        <Col :span="12">
          <FormItem :label="$t('system.role.name')" name="name">
            <Input v-model:value="form.name" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.role.notes')" name="notes">
            <Input v-model:value="form.notes" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
      </Row>
      <FormItem style="text-align: right">
        <Button @click="_close" :disabled="data.loading">{{ $t('common.cancel') }}</Button>

        <Button type="primary" style="margin-left: 10px" @click="handleSubmit" :loading="data.loading"
                 :disabled="data.loading">{{ $t('common.confirm') }}
        </Button>
      </FormItem>
    </Form>
  </Modal>
</template>

<script setup lang="ts">
import {reactive, ref} from "vue";
import { $t } from '@vben/locales';
import {Modal, Form, Input, Row, Col, Button,message} from 'ant-design-vue';
import {role} from '#/api';

const FormItem = Form.Item;

interface Emits {
  (e: 'close', submit: Boolean): void
}

const emits = defineEmits<Emits>()

const data = reactive({
  isShowModal: false,
  loading: false,
})

const form = reactive({
  notes: '',
  name: '',
})

const rules = reactive({
  name: [
    {required: true, message: $t('form.required')}
  ],
})
const formRef = ref();

/**
 * 参数处理
 * @param data
 */
function wrapQueryParam(data: Object = {}) {
  return data;
}

function handleSubmit() {
  data.loading = true;
  formRef.value.validate().then(() => {
    role.addOne(wrapQueryParam(form)).then((res)=>{
      if(res.error){
        return;
      }
      message?.success($t('common.addSuccess'));
      _close(true);
    })
  }).finally(() => {
    data.loading = false;
  })
}

function handleResetForm() {
  formRef.value.resetFields();
}

function _open() {
  data.isShowModal = true;
}

function _close(submit: Boolean = false) {
  handleResetForm();
  data.isShowModal = false;
  submit && typeof submit == 'boolean' && emits('close', submit);
}

//开放方法
defineExpose({_open, _close})
</script>

<style scoped>

</style>