import React from 'react';
import { Form, FormikProps } from 'formik';
import moment from 'moment';
import Input from '../components/Input';
import Select from '../components/Select';
import hooks from '../hooks/useAPIClient';


const mapper = (item: any) => ({ key: item._id, label: item.name });


const TransferForm: React.FC<FormikProps> = ({ setFieldValue, values }) => {
  const { data: contractors } = hooks.contractors.useList();

  if (!values.date) setFieldValue('date', moment().format('YYYY-MM-DD'));
  if (!values.contractorId && contractors) setFieldValue('contractorId', contractors[0]._id);

  return (
    <Form id="form">
      <div className="grid grid-cols-2">
        <Select
          name="contractorId"
          label="Контрагент"
          options={contractors?.map(mapper)}
          required
        />
        <Input name="date" type="date" label="Дата" required />
      </div>
      <div className="grid grid-cols-2">
        <Select
          name="operation"
          label="Операция"
          options={[
            { key: 'in', label: 'Приход' },
            { key: 'out', label: 'Расход' },
          ]}
        />
        <Input name="amount" type="number" label="Сумма" required />
      </div>
    </Form>
  );
};

export default TransferForm;
