<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    @ok="handleOk"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="modelRef" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { buryPointAdd, } from '/@/api/statistics/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  // import { useUserStore } from '/@/store/modules/user';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok'],
    setup(_, context) {
      const modelRef = ref({});
      // const userStore = useUserStore();
      // const userinfo = computed(() => userStore.getUserInfo);
      const schemas: FormSchema[] = [
        {
          field: 'applicationName',
          component: 'Input',
          label: '应用名称',
          colProps: {
            span: 18,
          },
          helpMessage:'创建应用，将自动生成应用key。',
          componentProps: {
            maxLength: 15,
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                var reg_tel = /^[a-zA-Z\u4e00-\u9fa5]+$/;
                // var reg = /\S+@\S+\.\S+/;
                if (!value) {
                  return Promise.reject('请输入应用名称');
                }
                if (!reg_tel.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject('请输入正确的应用名称');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
        },
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
      ];
      const title = ref('新 增');
      const { createMessage } = useMessage();
      const [registerForm, { validate, resetFields }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((_) => {
        resetFields();
      });
      async function handleOk() {
        let data = await validate();
        let res = await buryPointAdd(data);
        context && context.emit('ok', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

      return {
        register,
        title,
        schemas,
        registerForm,
        modelRef,
        handleOk,
        resetFields,
      };
    },
  });
</script>
