import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} 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 ChargeStandardUpdate from './modals/ChargeStandardUpdate';
import ChargingStandard from './modals/ChargingStandard';
import CreateStandard from './modals/CreateStandard';
import Show from './modals/Show';

export default function Index({ title = '收费标准配置' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="charge-standard"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle={title}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.HouseCharge.HouseChargeStandards.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: <CreateStandard key="Create" reload={action?.reload} />,
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          {
            title: '收费名称',
            dataIndex: 'name',
            search: false,
            render: (_, item: any) => {
              return <ChargingStandard item={item} />;
            },
          },
          {
            title: '关联数',
            search: false,
            render: (_, item: any) => {
              const parts: string[] = [];
              if (item.house_charge_has_houses_count)
                parts.push(`${item.house_charge_has_houses_count}`);
              if (item.house_meter_has_standards_count)
                parts.push(`${item.house_meter_has_standards_count}`);
              return <span>{parts.join(' / ') || '-'}</span>;
            },
          },
          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.Option({
            render: (_, item: any, _index, action) => (
              <MyTableActions
                actions={{
                  update: (
                    <ChargeStandardUpdate
                      item={item}
                      reload={action?.reload}
                      title="编辑"
                    />
                  ),
                  has: (
                    <Show item={item} reload={action?.reload} title="关联" />
                  ),
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.HouseCharge.HouseChargeStandards.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
