import {Modal, ModalDate, ModalField, ModalSelect} from '@components';
import {FC} from 'react';
import {useFormState} from './hooks';
import {useDictionaryOptions, useDictionaryWidthCode} from '@hooks';

type Props = {
  visible: boolean;
  onClose: () => void;
  onFetch: () => void;
};

const AddModal: FC<Props> = function (props) {
  const [{data: materialOptions, isFetching}, onMaterialSearch] =
    useDictionaryWidthCode('物料字典');
  const [{isLoading, control}, {onSubmit}] = useFormState({
    ...props,
    onSearch: onMaterialSearch,
  });
  const {data: corporationOptions, isFetching: corporationIsFetching} =
    useDictionaryOptions('公司', false, state => state.code);
  const {data: unitOptions, isFetching: unitIsFetching} =
    useDictionaryOptions('计量单位');

  return (
    <Modal
      {...props}
      title='新增报工单'
      onSubmit={onSubmit}
      isLoading={isLoading}
      testId='product_report_modal'
    >
      <ModalField label='报工单ID' name='stockInNoticeId' control={control} />
      <ModalField label='报工单编号' name='stockInNoticeNo' control={control} />
      <ModalField label='生产订单号' name='prodOrderNo' control={control} />
      <ModalSelect
        label='物料'
        name='materialId'
        control={control}
        data={materialOptions}
        onSearch={onMaterialSearch}
        loading={isFetching}
      />
      <ModalField
        control={control}
        label='通知类型名称'
        name='notificationTypeName'
      />
      <ModalField
        control={control}
        label='通知类型编号'
        name='notificationTypeNumber'
      />
      <ModalSelect
        label='计量单位'
        name='measUnitId'
        data={unitOptions}
        control={control}
        loading={unitIsFetching}
      />
      <ModalField
        label='数量'
        type='number'
        name='orderCount'
        control={control}
      />
      <ModalSelect
        data={corporationOptions}
        control={control}
        name='companyNo'
        label='公司'
        loading={corporationIsFetching}
      />
      <ModalDate control={control} name='noticeDate' label='通知日期' />
      <ModalField label='分录号' name='entryNumber' control={control} />
      <ModalField
        label='WBS编号'
        name='WBS'
        control={control}
        required={false}
      />
    </Modal>
  );
};

export default AddModal;
