import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractsContractNatureEnum,
  ContractsStatusEnum,
  ContractTemplatesIncomeExpenseTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import dayjs from 'dayjs';

export default function Index({ ...rest }) {
  const navigate = useNavigate();
  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              company_suppliers_id: rest.item?.company_suppliers_id,
            },
            sort,
            Apis.Contract.Contracts.List,
          )
        }
        search={false}
        columns={[
          MyColumns.ID({ search: false }),
          MyColumns.EnumTag({
            title: '合同状态',
            dataIndex: 'status',
            valueEnum: ContractsStatusEnum,
          }),
          {
            title: '合同编号',
            dataIndex: 'code',
            render: (_, item: any) => (
              <a
                onClick={() => {
                  navigate(`/contract/contracts/show/${item.id}`);
                }}
              >
                {item?.code || ''}
              </a>
            ),
          },
          {
            title: '合同名称',
            dataIndex: 'name',
          },
          {
            title: '合同类型',
            dataIndex: 'contract_type_name',
          },

          MyColumns.EnumTag({
            title: '收支类型',
            dataIndex: 'income_expense_type',
            valueEnum: ContractTemplatesIncomeExpenseTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '合同性质',
            dataIndex: 'contract_nature',
            valueEnum: ContractsContractNatureEnum,
          }),
          {
            title: '合同总价',
            search: false,
            render: (_, item: any) => {
              return `¥${item.total_amount}`;
            },
          },
          {
            title: '账单金额',
            render: (_, item: any) => {
              return `¥${item.total_bill_amount}`;
            },
          },
          {
            title: '交易金额',
            render: (_, item: any) => {
              return item.income_expense_type === 'Income'
                ? `¥${item.total_income_amount}`
                : item.income_expense_type === 'Expense'
                ? `¥${item.total_expense_amount}`
                : '-';
            },
            search: false,
          },
          {
            title: '有效期',
            search: false,
            render: (_, item: any) => {
              return `${dayjs(item.start_time).format('YYYY-MM-DD')}至${dayjs(
                item.end_time,
              ).format('YYYY-MM-DD')}`;
            },
          },
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { full_name: value };
              },
            },
          },
          {
            title: '签订部门',
            dataIndex: 'sign_department',
            search: false,
          },
          {
            title: '用印日期',
            dataIndex: 'sign_time',
            valueType: 'date',
            render: (_, item: any) => {
              return item.sign_time
                ? dayjs(item.sign_time).format('YYYY-MM-DD')
                : '-';
            },
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index) => (
              <Space key={index}>
                <MyButtons.View
                  title="查看"
                  onClick={() => {
                    navigate(`/contract/contracts/show/${item.id}`);
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
