import { DisplayPosition } from '../position/display-position';
import CnTableAlc from '../../../lowcode/cn-table-alc';
import columnsPrototype from '../../../lowcode/cn-table-alc/prototype/columns';
import React from 'react';

const Table = {
  position: [DisplayPosition.formDialog, DisplayPosition.form],
  title: '展示表格',
  componentName: 'Table',
  formComponent: (props) => {
    const { value, pageSetting = {}, ...rest } = props;
    let dataSource = [];
    if (Array.isArray(value) && value.length > 0) {
      dataSource = value;
    }
    return <CnTableAlc {...rest} {...pageSetting} dataSource={dataSource} />;
  },
  getDefaultProps: () => {
    return {
      columns: [
        {
          title: '名称',
          dataIndex: 'name',
          lock: false,
          sortable: false,
          filter: false,
        },
      ],
    };
  },
  getPrototypeList: () => {
    return [
      {
        name: '_context',
        title: '上下文',
        defaultValue: {
          type: 'JSExpression',
          value: 'this',
        },
        condition() {
          return false;
        },
      },
      {
        name: 'isFormItemTable',
        title: 'isFormItemTable',
        defaultValue: true,
        condition() {
          return false;
        },
      },
      {
        name: 'primaryKey',
        title: '数据主键',
        display: 'inline',
        defaultValue: 'id',
        tip: '',
        setter: 'StringSetter',
        // supportVariable: true,
      },
      columnsPrototype(),
      {
        name: 'pageSetting',
        display: 'accordion',
        title: '分页设置',
        extraProps: {
          defaultCollapsed: true,
        },
        setter: {
          componentName: 'ObjectSetter',
          props: {
            config: {
              items: [
                {
                  name: 'paging',
                  title: '是否开启',
                  display: 'inline',
                  defaultValue: false,
                  setter: 'BoolSetter',
                },
                {
                  name: 'pageSizeList',
                  title: '每页显示选择器可选值',
                  display: 'inline',
                  defaultValue: [10, 20, 50],
                  condition(prop) {
                    return prop?.parent?.getPropValue?.('paging');
                  },
                  setter: 'JsonSetter',
                },
              ],
            },
          },
        },
      },
    ];
  },
};
export default Table;
