import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
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';

export default function ReceiptAccounts(props: MyBetaModalFormProps) {
  const navigate = useNavigate();

  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        headerTitle={
          <Space>
            项目已配置收费标准
            <MyButtons.View
              title="去配置"
              onClick={() => {
                navigate(`/charge/standard`);
              }}
            />
          </Space>
        }
        // tooltip="可在【目录：财务管理】中配置"
        search={false}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              asset_projects_id: props?.item?.id,
            },
            sort,
            Apis.HouseCharge.HouseChargeStandards.List,
          )
        }
        // toolBarRender={(action) => [
        //   <ChargeStandardCreate
        //     key="Select"
        //     reload={action?.reload}
        //     item={props?.item}
        //     title="添加收费标准"
        //   />,
        // ]}
        // options={false}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '收费名称',
            dataIndex: 'name',
            search: false,
          },
          MyColumns.EnumTag({
            title: '收费项',
            dataIndex: 'charge_type',
            valueEnum: HouseBillsTypeEnum,
          }),
          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`);
                  }}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharge.HouseChargeStandards.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
