<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { ElMessage } from 'element-plus';

import { useVbenForm } from '#/adapter/form';
import { cdcmDriverAPI } from '#/cdcm/api/api.js';
import req from '#/cdcm/api/req_code';
import { useStaticConfigStore } from '#/store';

const [Modal, modalApi] = useVbenModal();

const driver_types = useStaticConfigStore().support_driver_types;
const get_driver_types = () => {
  return driver_types.map((item: any) => {
    return {
      label: item,
      value: item,
    };
  });
};

const [TemplateForm] = useVbenForm({
  // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  // 提交函数
  handleSubmit: onSubmit,
  // 垂直布局，label和input在不同行，值为vertical
  // 水平布局，label和input在同一行
  layout: 'horizontal',
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '驱动名称',
      },
      fieldName: 'name',
      label: '驱动名称',
      rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: get_driver_types(),
        placeholder: '驱动类型',
        showSearch: true,
      },
      fieldName: 'driver_type',
      label: '驱动类型',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '驱动描述',
      },
      fieldName: 'description',
      label: '驱动描述',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '主机地址',
      },
      fieldName: 'host',
      label: '主机地址',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '端口号',
      },
      fieldName: 'port',
      label: '端口号',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '轮询时间',
      },
      fieldName: 'interval',
      label: '轮询时间',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '失败重启次数',
      },
      fieldName: 'retry_count',
      label: '失败重启次数',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '失败重启时间',
      },
      fieldName: 'retry_interval',
      label: '失败重启时间',
      rules: 'required',
    },
  ],
  wrapperClass: 'grid-cols-1',
});

async function onSubmit(values: Record<string, any>) {
  const param = {
    ...values,
    protocol_ext: {
      host: values.host,
      port: values.port,
      interval: values.interval,
      retry_count: values.retry_count,
      retry_interval: values.retry_interval,
    },
  };
  const ret = await cdcmDriverAPI(param, { code: req.DriverCreate.code });
  if (ret.data.code === 0) {
    ElMessage.success(ret.data.data);
    modalApi.close();
  } else {
    ElMessage.error(ret.data.data || ret.data.msg);
  }
}
</script>

<template>
  <Modal title="新增驱动" :footer="false">
    <TemplateForm />
  </Modal>
</template>
