import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { LeftOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate, useSearchParams } from '@umijs/max';
import { Space } from 'antd';
import Create from '../modals/JinGangQuCreate';
import Update from '../modals/JinGangQuUpdate';

export default function Index({ title = '金刚区配置' }) {
  const [searchParams] = useSearchParams();
  const navigate = useNavigate();
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey={`company-set-gan-info-${searchParams.get('id')}`}
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, companies_id: searchParams.get('id') },
            sort,
            Apis.Company.CompanyQuickActions.List,
          )
        }
        bordered
        headerTitle={
          <Space>
            <MyButtons.Default
              title=""
              icon={<LeftOutlined />}
              onClick={() => navigate(-1)}
            />
            {title}
          </Space>
        }
        toolBarRender={(action) => [
          <Create
            key="Create"
            reload={action?.reload}
            item={{ id: searchParams.get('id') }}
            title={title}
          />,
        ]}
        search={false}
        columns={[
          MyColumns.ID(),
          {
            title: '标题',
            dataIndex: 'name',
          },
          {
            title: '跳转地址',
            dataIndex: 'skip_url',
          },
          MyColumns.Boolean({
            title: '是否显示',
            dataIndex: 'is_show',
          }),
          MyColumns.Boolean({
            title: '是否要绑房',
            dataIndex: 'is_bind_house',
          }),
          {
            title: '图标',
            dataIndex: 'icon',
            search: false,
            render: (_, item) => (
              <img
                src={item?.icon?.[0]?.url || ''}
                alt="图标"
                style={{ width: 40, height: 40 }}
              />
            ),
          },
          {
            title: '排序',
            dataIndex: 'sort',
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space>
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.CompanyQuickActions.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
