import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseBillsTypeEnum,
  HouseChargeStandardsCalculationMethodEnum,
  HouseChargeStandardsCalculationModeEnum,
  HouseChargeStandardsCalculationPeriodEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import ChargeStandardHasHouse from '../asset/list/components/modals/ChargeStandardHasHouse';
import ChargeStandardUpdate from '../asset/list/components/modals/ChargeStandardUpdate';
import ChargeCreate from './modals/ChargeCreate';

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

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'house-charge',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="banner-spaces"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.HouseCharage.HouseChargeStandards.List,
          )
        }
        toolBarRender={(action) => [
          <ChargeCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            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,
          }),
          {
            title: '单价',
            dataIndex: 'price',
            search: false,
          },
          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(`/house_charge_standard/${item.id}`);
                  }}
                />
                <ChargeStandardUpdate
                  item={item}
                  reload={action?.reload}
                  title="编辑"
                />
                <ChargeStandardHasHouse
                  item={item}
                  reload={action?.reload}
                  title={'绑房'}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharage.HouseChargeStandards.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
