<template>
  <Modal :maskClosable="false" :keyboard="false" v-model:open="data.isShowModal" :footer="null" :title="$t('system.menu.editTitle')" 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.menu.name')" name="name">
            <Input v-model:value="form.name" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.notes')" name="notes">
            <Input v-model:value="form.notes" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.menu')" name="menu">
            <Input v-model:value="form.menu" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.permission')" name="permission">
            <Input v-model:value="form.permission" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.sort')" name="sort">
            <Input v-model:value="form.sort" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.type')" name="type">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.type" allowClear :placeholder="$t('common.placeholderIn')">
              <SelectOption v-for="(item ,index) in data.typeOptions" :key="index" :value="item.value" :label="item.label" :text="item.label">{{ item.label }}</SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('system.menu.status')" name="status">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.status" allowClear :placeholder="$t('common.placeholderIn')">
              <SelectOption v-for="(item ,index) in data.statusOptions" :key="index" :value="item.value" :label="item.label" :text="item.label">{{ item.label }}</SelectOption>
            </Select>
          </FormItem>
        </Col>
      </Row>
      <FormItem style="text-align: right">
        <Button @click="_close" :disabled="data.loading">{{ $t('common.cancel') }}</Button>
        <Button danger style="margin-left: 10px" @click="handleResetForm" :disabled="data.loading">{{ $t('common.reset') }}</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, Select, Row, Col, Button,message} from 'ant-design-vue';
import {menu} from '#/api';

const FormItem = Form.Item;
const SelectOption = Select.Option;

interface Emits {
  (e: 'close' ,submit: Boolean): void
}
const emits = defineEmits<Emits>()

const data = reactive({
  isShowModal: false,
  loading: false,
  statusOptions: [
    {label: $t('system.menu.statusOptions.v0') , value: 0},
    {label: $t('system.menu.statusOptions.v1') , value: 1},
  ],
  typeOptions: [
    {label: '菜单' , value: '菜单'},
    {label: '按钮' , value: '按钮'},
    {label: '链接' , value: '链接'},
  ]
})

const form = reactive({
  uid: null,
  notes: null,
  name: null,
  permission: null,
  sort: 0,
  menu: null,
  status: 1,
  type: '菜单',
})

// 选择框过滤方法
const selectFilterOption = (input: string, option: any) => {
  return option.text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
}

// 表单规则
const rules = reactive({
  name: [
    {required: true, message: $t('form.required')}
  ],
  sort: [
    {pattern: /^\d+$/, message: $t('form.number')}
  ]
})
const formRef = ref();

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

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

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

function _open(formData:Object = {}) {
  data.isShowModal = true;
  Object.assign(form, formData);
}

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

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

<style scoped>

</style>