import {
  MyButtons,
  MyColumns,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Apis } from '@/gen/Apis';
import {
  ContractBillObjectsAuditStatusEnum,
  ContractBillsAuditStatusEnum,
  ContractBillsCostTypeEnum,
  HouseOrdersOrderStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import { useState } from 'react';
import PayBills from '../modals/PayBills';
import CompleteApply from './modals/CompleteApply';

export default function Index() {
  const navigate = useNavigate();
  const [getParams, setParams] = useState({});

  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) => {
        setParams(params);
        return MyProTableProps.request(
          {
            ...params,
            is_valid: 1,
            cost_type: ContractBillsCostTypeEnum.Expense.value,
          },
          sort,
          Apis.Contract.ContractBills.List,
        );
      }}
      toolBarRender={(action: any) => [
        <MyToolBarActions
          key="toolbar"
          actions={{
            export: (
              <MyExport
                key="export"
                title="账单导出"
                item={{
                  ...getParams,
                  cost_type: ContractBillsCostTypeEnum.Expense.value,
                }}
              />
            ),
          }}
        />,
      ]}
      headerTitle="合同应付账单"
      columns={[
        MyColumns.ID({ search: false }),
        MyColumns.EnumTag({
          title: '费用类型',
          dataIndex: 'cost_type',
          valueEnum: ContractBillsCostTypeEnum,
        }),
        {
          title: '合同名称',
          dataIndex: ['contract', 'name'],
          search: false,
        },
        {
          title: '合同编号',
          dataIndex: 'contracts_id',
          render: (_, item: any) => {
            return (
              <a
                onClick={() => {
                  navigate(`/contract/contracts/show/${item.contract?.id}`);
                }}
              >
                {item?.contract?.code || ''}
              </a>
            );
          },
        },
        {
          title: '年月',
          search: false,
          render: (_, item: any) => {
            return `${item?.year || 0}-${
              item?.month < 10 ? `0${item?.month}` : item?.month
            }`;
          },
        },
        {
          title: '期数',
          dataIndex: 'period_num',
          search: false,
        },

        {
          title: '账单金额',
          search: false,
          render: (_, item: any) => {
            return `¥${item?.amount || 0}`;
          },
        },
        MyColumns.EnumTag({
          title: '交易状态',
          dataIndex: 'payment_status',
          valueEnum: HouseOrdersOrderStatusEnum,
        }),
        MyColumns.EnumTag({
          title: '付款审核',
          dataIndex: 'audit_status',
          valueEnum: ContractBillsAuditStatusEnum,
        }),
        {
          title: '交易方',
          render: (_, item: any) => {
            const payeeText = item?.payee?.includes('：')
              ? item?.payee?.split('：')?.[1]
              : item?.payee || '';
            const payerText = item?.payer?.includes('：')
              ? item?.payer?.split('：')?.[1]
              : item?.payer || '';
            return (
              <Space>
                <div>{payeeText}</div>||
                <div>{payerText}</div>
              </Space>
            );
          },
        },

        MyColumns.UpdatedAt(),
        MyColumns.Option({
          render: (_, item: any, _index, action) => (
            <MyTableActions
              actions={{
                apply: (
                  <CompleteApply
                    item={item}
                    reload={action?.reload}
                    title="申请付款"
                  />
                ),
                pay: (
                  <PayBills item={item} reload={action?.reload} title="登记" />
                ),
                update: (
                  <MyButtons.Default
                    key="Create"
                    size="small"
                    type="primary"
                    disabled={
                      item?.payment_status === 'Paid' ||
                      item?.audit_status ===
                        ContractBillObjectsAuditStatusEnum.Pending.value ||
                      item?.audit_status ===
                        ContractBillObjectsAuditStatusEnum.Approved.value
                    }
                    onClick={() => {
                      navigate(
                        `/contract/contract_bills/pages/update?id=${item.id}`,
                      );
                    }}
                    title="编辑"
                  />
                ),
                delete: (
                  <MyButtons.Delete
                    disabled={item?.payment_status === 'Paid'}
                    onConfirm={() =>
                      Apis.Contract.ContractBills.Delete({ id: item.id }).then(
                        () => action?.reload(),
                      )
                    }
                  />
                ),
              }}
            />
          ),
        }),
      ]}
    />
  );
}
