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

export default function Index({ title = '维修基金标准' }) {
  const navigate = useNavigate();

  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle="维修基金标准"
      request={async (params, sort) =>
        MyProTableProps.request(
          {
            ...params,
            charge_type: HouseBillsTypeEnum.MaintenanceFund.value,
          },
          sort,
          Apis.HouseCharge.HouseChargeStandards.List,
        )
      }
      toolBarRender={(action) => [
        <CreateMaintenanceFund
          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,
        },
        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=House`);
                }}
              />
              <ChargeStandardUpdate
                item={item}
                reload={action?.reload}
                title="编辑"
              />
              {(item.charge_type === HouseBillsTypeEnum.PropertyFee.value ||
                item.charge_type ===
                  HouseBillsTypeEnum.MaintenanceFund.value) && (
                <ChargeStandardHasHouse
                  item={item}
                  reload={action?.reload}
                  title={'绑房'}
                />
              )}
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.HouseCharge.HouseChargeStandards.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
