<script lang="ts" setup>    

import { addNode, type NodeData } from '#/api/core/server';
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
const [Form, FormApi] = useVbenForm({
    // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },

  // 使用 tailwindcss grid布局
  // 提交函数
  // 垂直布局，label和input在不同行，值为vertical
  layout: 'horizontal',
  // 水平布局，label和input在同一行
  schema: [
    {
        component: 'Input',
        componentProps: {

        },
        rules: "required",
        fieldName: 'NodeName',
        label: '节点名称',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
        },
        rules: "required",
        fieldName: 'NodeIp',
        label: '外网Ip',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodeInternalIp',
        label: '内网Ip',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodeHardware',
        label: '硬件信息',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodeArea',
        label: '区域',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodeOperator',
        label: '运营商',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
          placeholder: '/usr/local/app',
        },
        rules: "required",
        fieldName: 'NodeUser',
        label: '用户名',
        formItemClass:'col-span-2',
    },
    {
        component: 'InputPassword',
        defaultValue: '',
        componentProps: {
          typeof: 'number',
        },
        rules: "required",
        fieldName: 'NodePassword',
        label: '密码',
        formItemClass:'col-span-2',
    },
    
  ],
  showDefaultActions: false,
  // 大屏一行显示3个，中屏一行显示2个，小屏一行显示1个
  wrapperClass: 'grid-cols-2',
});
const [Modal, modalApi] = useVbenModal({
    confirmText: '提交',
    onOpenChange:() => {
        
    },
    onConfirm: async () => {
        // 提交表单
        const values: Record<string, any> = await FormApi.getValues();
        const node:NodeData = {
          NodeName: values.NodeName,
          NodeIp: values.NodeIp,
          NodeInternalIp: values.NodeInternalIp,
          NodeHardware: values.NodeHardware,
          NodeArea: values.NodeArea,
          NodeOperator: values.NodeOperator,
          NodeUser: values.NodeUser,
          NodePassword: values.NodePassword,
        };
        await addNode(node);
        modalApi.close();
    },
});
defineOptions({
    name: 'AddServerModal',
})
defineExpose({
    FormApi
})
</script>

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