import { BasicColumn, FormSchema } from '@/components/Table';
import { getDataBaseCodeList } from '@/api/formdesign/index';

// 数据源table
export const dbsourcesColumns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    ifShow: false,
  },
  {
    title: '编码',
    dataIndex: 'code',
    width: 200,
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 200,
  },
  {
    title: '数据库',
    dataIndex: 'dbId',
    width: 80,
  },
  {
    title: '创建人',
    dataIndex: 'createUserName',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createDate',
    width: 80,
  },
  {
    title: '创建人id',
    dataIndex: 'reateUserId',
    ifShow: false,
  },
  {
    title: '备注',
    dataIndex: 'description',
    ifShow: false,
  },
  {
    title: 'ID',
    dataIndex: 'id',
    ifShow: false,
  },
  {
    title: 'modifyDate',
    dataIndex: 'modifyDate',
    ifShow: false,
  },
  {
    title: 'modifyUserId',
    dataIndex: 'modifyUserId',
    ifShow: false,
  },
  {
    title: 'modifyUserName',
    dataIndex: 'modifyUserName',
    ifShow: false,
  },
  {
    title: 'SQL',
    dataIndex: 'sql',
    ifShow: false,
  },
];
// 数据源table-搜索
export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '名称',
    component: 'Input',
    colProps: { span: 8 },
  },
];
// 数据源table-新增、编辑
export const formSchema: FormSchema[] = [
  {
    field: 'code',
    component: 'Input',
    label: '编号',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'name',
    component: 'Input',
    label: '名称',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'dbId',
    component: 'ApiTreeSelect',
    label: '数据库',
    required: true,
    colProps: {
      span: 24,
    },
    componentProps: {
      api: () =>
        getDataBaseCodeList({}).then((data: Recordable) => {
          return new Promise((resolve) => {
            resolve(data);
          });
        }),
      labelField: 'text',
      valueField: 'value',
      async: true,
      onChange: (e, v) => {
        console.log('ApiTreeSelect====>:', e, v);
      },
      onLoadData: ({ treeData, resolve, treeNode }) => {
        setTimeout(() => {
          const children: Recordable[] = [];
          treeData.value.forEach((item) => {
            if (item.id == treeNode.id) {
              if (item.childNodes) {
                item.childNodes.forEach((val) => {
                  children.push({
                    text: val.text,
                    value: val.value,
                  });
                });
              }
            }
          });
          children.forEach((item) => {
            item.isLeaf = false;
            item.children = [];
          });
          treeNode.dataRef.children = children;
          treeData.value = [...treeData.value];
          resolve();
          return;
        }, 300);
      },
    },
  },
  {
    field: 'sql',
    component: 'InputTextArea',
    label: 'SQL语句',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    required: true,
    componentProps: {
      placeholder: '',
    },
  },
  {
    field: 'description',
    component: 'InputTextArea',
    label: '描述',
    colProps: {
      span: 24,
    },
    defaultValue: '',
    componentProps: {
      placeholder: '请输入',
    },
  },
  {
    field: 'id',
    component: 'Input',
    label: 'id',
    ifShow: false,
  },
  {
    field: 'createUserId',
    component: 'Input',
    label: '创建人',
    ifShow: false,
  },
  {
    field: 'createUserName',
    component: 'Input',
    label: '创建人',
    ifShow: false,
  },
  {
    field: 'createDate',
    component: 'Input',
    label: '创建时间',
    ifShow: false,
  },
  {
    field: 'modifyUserId',
    component: 'Input',
    label: '修改人',
    ifShow: false,
  },
  {
    field: 'modifyUserName',
    component: 'Input',
    label: '修改人',
    ifShow: false,
  },
  {
    field: 'modifyDate',
    component: 'Input',
    label: '修改时间',
    ifShow: false,
  },
  {
    field: 'isEdit',
    component: 'Input',
    ifShow: false,
  },
  {
    field: 'tenantId',
    component: 'Input',
    ifShow: false,
  },
];
