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 ChargeStandardUpdate from '../modals/ChargeStandardUpdate';
import ChargingStandard from '../modals/ChargingStandard';
import CreateSharedElectricityFee from '../pages/CreateSharedElectricityFee';

export default function Index({ title = '公摊电费标准' }) {
  const navigate = useNavigate();

  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle="公摊电费标准"
      request={async (params, sort) =>
        MyProTableProps.request(
          {
            ...params,
            charge_type: HouseBillsTypeEnum.SharedElectricityFee.value,
          },
          sort,
          Apis.HouseCharage.HouseChargeStandards.List,
        )
      }
      toolBarRender={(action) => [
        <CreateSharedElectricityFee
          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 };
            },
          },
        },
        {
          title: '收费名称',
          dataIndex: 'name',
          search: false,
          render: (_, item: any) => {
            return <ChargingStandard item={item} />;
          },
        },
        {
          title: '关联房屋数',
          dataIndex: 'house_charge_has_houses_count',
          search: false,
        },
        {
          title: '关联仪表数',
          dataIndex: 'house_meter_has_standards_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.UpdatedAt(),
        // MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <MyButtons.View
                title="查看"
                onClick={() => {
                  navigate(
                    `/charge/standard/show/${item.id}?type=Meter&usage_type=ElectricMeter&meter_type=CommonMeter`,
                  );
                }}
              />
              <ChargeStandardUpdate
                item={item}
                reload={action?.reload}
                title="编辑"
              />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.HouseCharage.HouseChargeStandards.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
