<script lang="ts" setup>    

import { addServer } from '#/api/core/server';
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import dayjs from 'dayjs';

const [Form, FormApi] = useVbenForm({
    // 所有表单项共用，可单独在表单内覆盖s
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },

  // 使用 tailwindcss grid布局
  // 提交函数
  // 垂直布局，label和input在不同行，值为vertical
  layout: 'horizontal',
  // 水平布局，label和input在同一行
  schema: [
    {
        component: 'Input',
        componentProps: {
            placeholder: '1',
        },
        formItemClass:'col-span-2',
        fieldName: 'NodeId',
        rules: "required|integer",
        label: 'NodeId',
    },
    {
        component: 'Input',
        componentProps: {

        },
        rules: "required",
        fieldName: 'AppName',
        label: 'App名称',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
        },
        rules: "required",
        fieldName: 'WsHost',
        label: '域名',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'WsPort',
        label: 'ws端口',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodeName',
        label: '节点名称',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'MysqlName',
        label: 'Mysql名称',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'Topic',
        label: 'kafka主题',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'Database',
        label: '数据库',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          placeholder: '/usr/local/app',
        },
        rules: "required",
        fieldName: 'Path',
        label: '根目录路径',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'Tz',
        label: '时区',
        formItemClass:'col-span-2',
    },
    
  ],
  showDefaultActions: false,
  // 大屏一行显示3个，中屏一行显示2个，小屏一行显示1个
  wrapperClass: 'grid-cols-2',
});
const [Modal, modalApi] = useVbenModal({
    confirmText: '提交',
    onOpenChange:() => {
        
    },
    onConfirm: async () => {
        // 提交表单
        const values = await FormApi.getValues();
        const date = dayjs(values.datePicker).format('YYYY-MM-DD');
        const time = dayjs(values.timePicker).format('HH:mm:ss');
        const dateTime = dayjs(`${date} ${time}`).valueOf()/1000;
        const modalData = modalApi.getData();
        const ServerId = parseInt(values.ServerId, 10);
        await addServer(modalData.AppId, ServerId, values.ServerName, values.Status, dateTime);
        modalApi.close();
    },
});
defineOptions({
    name: 'AddServerModal',
})
defineExpose({
    FormApi
})
</script>

<template>
    <Modal title="添加服务器" :width="800">
        <Form />
    </Modal>
</template>
