<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 date = dayjs();
const [Form, FormApi] = useVbenForm({
    // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },

  // 使用 tailwindcss grid布局
  // 提交函数
  // 垂直布局，label和input在不同行，值为vertical
  layout: 'horizontal',
  // 水平布局，label和input在同一行
  schema: [
    {
        component: 'Input',
        disabled: false,
        defaultValue: 0,
        componentProps: {
            placeholder: '1',
        },
        formItemClass:'col-span-2',
        fieldName: 'AppId',
        label: 'AppId：',
    },
    {
        component: 'Input',
        disabled: false,
        componentProps: {
            placeholder: '2',
            typeof: 'number',
        },
        rules: "required",
        fieldName: 'ServerId',
        label: 'ServerId：',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: 'node1',
        },
        rules: "required",
        fieldName: 'ServerName',
        label: 'ServerName：',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: 'google.bywaystudios.com',
        },
        rules: "required",
        fieldName: 'Host',
        label: 'Host',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: '3601',
        },
        rules: "required",
        fieldName: 'Port',
        label: 'Port',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: '3701',
        },
        rules: "required",
        fieldName: 'WsPort',
        label: 'WsPort',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: '1.0.50',
        },
        rules: "required",
        fieldName: 'ClientVersion',
        label: 'ClientVersion',
        formItemClass:'col-span-2',
    },
    {
        component: 'Input',
        defaultValue: '',
        componentProps: {
            placeholder: '/usr/local/game',
        },
        rules: "required",
        fieldName: 'WorkDir',
        label: 'WorkDir',
        formItemClass:'col-span-2',
    },
    {
        component: 'Select',
        defaultValue: '',
        componentProps: {
            options: [
                {
                    label: 'ECS-tencent',
                    value: 1,
                },
                {
                    label: 'ECS-aliyun-us-silicon',
                    value: 2,
                },
            ],
        },
        rules: "required",
        fieldName: 'Ecs',
        label: 'Ecs',
        formItemClass:'col-span-2',
    },
    {
      component: 'DatePicker',
      fieldName: 'datePicker',
      defaultValue: date,
      componentProps: {
        format: 'YYYY-MM-DD',
      },
      wrapperClass: 'grid-span-1',
      label: '开服日期',
    },
    {
      component: 'TimePicker',
      fieldName: 'timePicker',
      defaultValue: date,
      componentProps: {
        format: 'HH:mm:ss',
      },
      wrapperClass: 'grid-span-2 grid-start-2',
      label: '开服时间',
    },
    {
        component: 'Select',
        defaultValue: 1,
        componentProps: {
            filterOption: true,
            options: [
                {
                    label: 'Start',
                    value: 1,
                },
                {
                    label: 'Stop',
                    value: 2,
                },
                {
                    label: 'Maintain',
                    value: 3,
                },
            ],
            placeholder: '请选择',
            showSearch: true,
        },
        fieldName: 'Status',
        label: 'Status',
    },
    
  ],
  showDefaultActions: false,
  // 大屏一行显示3个，中屏一行显示2个，小屏一行显示1个
  wrapperClass: 'grid-cols-2',
});
const [Modal, modalApi] = useVbenModal({
    confirmText: '提交',
    onOpenChange:() => {
        const modalData = modalApi.getData();
        FormApi.updateSchema([
            {
                component: 'Input',
                disabled: true,
                defaultValue: modalData.AppId,
                componentProps: {
                    placeholder: modalData.AppId,
                },
                formItemClass:'col-span-2',
                fieldName: 'AppId',
                label: 'AppId：',
            },
        ])
    },
    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, values.Host, parseInt(values.Port,10), parseInt(values.WsPort,10), values.ClientVersion, values.Ecs, values.WorkDir);
        modalApi.close();
    },
});
defineOptions({
    name: 'AddServerModal',
})
defineExpose({
    FormApi
})
</script>

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