import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import {
  ContractsContractNatureEnum,
  ContractsStatusEnum,
  ContractTemplatesIncomeExpenseTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import dayjs from 'dayjs';
import { useRef, useState } from 'react';
interface DataType {
  key?: React.Key;
  id?: React.Key;
}
export default function CompanySealsSelectList(
  props: MyBetaModalFormProps & {
    onChange?: (selectedRows: DataType[]) => void;
  },
) {
  const modalRef = useRef<any>();
  // const [selectedDataRow, setSelectedDataRow] = useState<any>({});
  const [getSelectedRow, setSelectedRow] = useState<any>([]);
  const rowSelection: any = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
      console.log(selectedRows, 'selectedRows[0]');
      setSelectedRow(selectedRows);
    },
    getCheckboxProps: (record: any) => ({
      disabled: record.deleted_at,
      checked: props?.item?.some((item: any) => {
        console.log(item, record);
        return item?.id === record?.id;
      }),
    }),
    defaultSelectedRowKeys: props?.item?.map((item: any) => item?.id) || [],
  };

  return (
    <MyModal
      title={'选择合同'}
      width="1000px"
      myRef={modalRef}
      size="middle"
      onOpen={() => {
        setSelectedRow(props?.item);
        console.log(props?.item, 'props?.item?.id');
      }}
      node={
        <ProTable
          {...MyProTableProps.props}
          request={async (params, sort) =>
            MyProTableProps.request(
              {
                ...params,
                // 已通过/已签约/已归档的合同才能申请账单
                status_array: [
                  ContractsStatusEnum.Approved.value,
                  ContractsStatusEnum.Signed.value,
                  ContractsStatusEnum.Archived.value,
                ],
                // 收支类型
                income_expense_type_array: [
                  ContractTemplatesIncomeExpenseTypeEnum.Income.value,
                  ContractTemplatesIncomeExpenseTypeEnum.Expense.value,
                ],
              },
              sort,
              Apis.Contract.Contracts.List,
            )
          }
          rowSelection={{ type: 'radio', ...rowSelection }}
          options={false}
          tableAlertOptionRender={() => {
            return (
              <MyButtons.Default
                key="okSelect"
                size="middle"
                type="primary"
                onClick={() => {
                  let res: any = getSelectedRow;
                  props?.onChange?.(res);
                  modalRef.current?.close();
                }}
                title="确定选项"
              />
            );
          }}
          columns={[
            MyColumns.ID({ search: false }),
            {
              title: '合同编号',
              dataIndex: 'code',
            },
            {
              title: '合同名称',
              dataIndex: 'name',
            },

            {
              title: '签订部门',
              dataIndex: 'sign_department',
            },
            MyColumns.EnumTag({
              title: '状态',
              dataIndex: 'status',
              valueEnum: ContractsStatusEnum,
            }),
            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: '有效期',
              search: false,
              render: (_, item: any) => {
                return `${dayjs(item.start_time).format('YYYY-MM-DD')}至${dayjs(
                  item.end_time,
                ).format('YYYY-MM-DD')}`;
              },
            },
            {
              title: '开始时间',
              dataIndex: 'start_time',
              hidden: true,
            },
            {
              title: '结束时间',
              dataIndex: 'end_time',
              hidden: true,
            },
          ]}
        />
      }
    ></MyModal>
  );
}
