<script lang="ts" setup>
import { addAdminApi } from '#/api/core/admin.user';
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import type { UserInfo } from '#/model/admin.user';
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: '用户名',
      },
      formItemClass: 'col-span-2',
      fieldName: 'username',
      rules: 'required',
      label: '用户名',
    },
    {
      component: 'InputPassword',
      componentProps: {},
      rules: 'required',
      fieldName: 'password',
      label: '密码',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Input',
      componentProps: {},
      rules: 'required',
      fieldName: 'phone',
      label: '手机号',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Select',
      defaultValue: 1,
      componentProps: {
        options: [
          { label: '超级管理员', value: 0 },
          { label: '管理员', value: 1 },
          { label: '普通用户', value: 2 },
          { label: '外包翻译', value: 99 },
        ],
      },
      rules: 'required',
      fieldName: 'role',
      label: '角色',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Select',
      defaultValue: 'xijing',
      componentProps: {
        options: [{ label: '蹊径', value: 'xijing' }],
      },
      rules: 'required',
      fieldName: 'group',
      label: '用户组',
      formItemClass: 'col-span-2',
    },
    {
      component: 'Textarea',
      componentProps: {},
      rules: 'required',
      fieldName: 'remark',
      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 Parms: UserInfo = {
      username: values.username,
      password: values.password,
      phone: values.phone,
      role: values.role,
      group: values.group,
      remark: values.remark,
    };
    await addAdminApi(Parms);
    modalApi.close();
  },
});
defineOptions({
  name: 'AddServerModal',
});
defineExpose({
  FormApi,
});
</script>

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