import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseBillsTypeEnum,
  HouseChargeStandardsCalculationMethodEnum,
  HouseChargeStandardsCalculationModeEnum,
  HouseChargeStandardsCalculationPeriodEnum,
  HouseChargeStandardsPriceAlgorithmEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import ChargeStandardHasCar from '../modals/ChargeStandardHasCar';
import ChargeStandardUpdate from '../modals/ChargeStandardUpdate';
import ChargingStandard from '../modals/ChargingStandard';
import CreateCarPortFee from '../pages/CreateCarPortFee';

export default function Index({ title = '车位管理费标准' }) {
  const navigate = useNavigate();

  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle={title}
      request={async (params, sort) =>
        MyProTableProps.request(
          {
            ...params,
            charge_type: HouseBillsTypeEnum.CarPortFee.value,
          },
          sort,
          Apis.HouseCharge.HouseChargeStandards.List,
        )
      }
      toolBarRender={(action) => [
        <CreateCarPortFee key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({ search: false }),
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          hidden: true,
        }),
        {
          title: '关联项目',
          dataIndex: ['asset_project', 'name'],
          // search: {
          //   transform: (value) => {
          //     return { project_name: value };
          //   },
          // },
          search: false,
        },
        {
          title: '收费名称',
          dataIndex: 'name',
          search: false,
          render: (_, item: any) => {
            return <ChargingStandard item={item} />;
          },
        },
        {
          title: '绑定车位',
          dataIndex: 'house_charge_has_houses_count',
          search: false,
        },
        MyColumns.EnumTag({
          title: '收费项',
          dataIndex: 'charge_type',
          valueEnum: HouseBillsTypeEnum,
          search: false,
        }),
        MyColumns.EnumTag({
          title: '计量单位',
          dataIndex: 'calculation_method',
          valueEnum: HouseChargeStandardsCalculationMethodEnum,
          search: false,
        }),
        MyColumns.EnumTag({
          title: '计费模式',
          dataIndex: 'calculation_mode',
          valueEnum: HouseChargeStandardsCalculationModeEnum,
          search: false,
        }),
        MyColumns.EnumTag({
          title: '计费算法',
          dataIndex: 'price_algorithm',
          valueEnum: HouseChargeStandardsPriceAlgorithmEnum,
          search: false,
        }),
        {
          title: '价格',
          dataIndex: 'price',
          search: false,
          render(_, record) {
            if (record?.price_algorithm === 'Fixed') {
              return record?.price;
            } else {
              return (
                <div>
                  {record?.tiered_rates?.map((rate: any, index: number) => (
                    <div key={index}>
                      {rate?.min_quantity} - {rate?.max_quantity} :{rate?.price}
                    </div>
                  ))}
                </div>
              );
            }
          },
        },
        MyColumns.EnumTag({
          title: '账单计费周期',
          dataIndex: 'calculation_period',
          valueEnum: HouseChargeStandardsCalculationPeriodEnum,
          search: false,
        }),
        {
          title: '账单自动生成日期',
          dataIndex: 'auto_date',
          search: false,
        },
        {
          title: '是否公摊',
          dataIndex: 'is_apportionment',
          search: false,
          render(_, record) {
            return `${record?.is_apportionment ? '是' : '否'} `;
          },
        },
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <MyButtons.View
                title="查看"
                onClick={() => {
                  navigate(`/charge/standard/show/${item.id}?type=Car`);
                }}
              />
              <ChargeStandardUpdate
                item={item}
                reload={action?.reload}
                title="编辑"
              />
              <ChargeStandardHasCar
                item={item}
                reload={action?.reload}
                title="绑定车位"
              />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.HouseCharge.HouseChargeStandards.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
