import { IMAGE_ROOT } from "@/shared/constants/common";
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { CalendarType, HeaderType } from '@/entities/common/model/types';
import {
  useSetHeaderTitle,
  useSetHeaderType,
  useSetFooterMode,
  useSetOnBack
} from '@/widgets/sub-layout/use-sub-layout';
import { ChangeEvent, useState } from "react";
import { useExtensionPayoutRequestMutation } from "@/entities/additional-service/api/payout/use-extension-payout-request-mutation";
import { ExtensionPayoutRequestParams, ExtensionPayoutRequestResponse } from "@/entities/additional-service/model/payout/types";
import NiceCalendar from "@/shared/ui/calendar/nice-calendar";
import { useStore } from "@/shared/model/store";
import moment from 'moment';
import { NumericFormat } from "react-number-format";
import { snackBar } from "@/shared/lib";

export const PayoutRequestPage = () => {
  const { navigate } = useNavigate();
  const userMid = useStore.getState().UserStore.mid;

  const [calendarOpen, setCalendarOpen] = useState<boolean>(false);
  const [mid, setMid] = useState<string>(userMid);
  const [submallId, setSubmallId] = useState<string>('');
  const [disbursementAmount, setDisbursementAmount] = useState<number>(0);
  const [settlementDate, setSettlementDate] = useState<string>('');

  const { mutateAsync: extensionPayoutRequest } = useExtensionPayoutRequestMutation();

  useSetHeaderTitle('지급대행 신청');
  useSetHeaderType(HeaderType.RightClose);
  useSetFooterMode(false);
  useSetOnBack(() => {
    navigate(PATHS.additionalService.payout.list);
  });

  const callExtensionPayoutRequest = () => {
    let params: ExtensionPayoutRequestParams = {
      mid: mid,
      submallId: submallId,
      disbursementAmount: disbursementAmount,
      settlementDate: settlementDate,
    };
    extensionPayoutRequest(params)
      .then((rs) => {
        if (rs.status) {
          snackBar("신청을 성공하였습니다.")
        } else {
          snackBar(`[실패] ${rs.error?.message}`)
        }
      })
      .catch((error) => {
        snackBar(`[실패] ${error?.response?.data?.message} ` || '[실패] 신청을 실패하였습니다.')
      })
      ;
  };

  const isFormValid = () => {
    return (
      submallId.trim() !== '' &&
      disbursementAmount > 0 &&
      settlementDate.trim() !== ''
    );
  };

  const setNewDate = (date: string) => {
    setSettlementDate(moment(date).format('YYYYMMDD'));
    setCalendarOpen(false);
  };


  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="ing-list">
              <div className="billing-form gap-30">
                <div className="billing-row">
                  <div className="billing-label">서브ID</div>
                  <div className="billing-field">
                    <input
                      type="text"
                      value={submallId}
                      onChange={(e: ChangeEvent<HTMLInputElement>) => setSubmallId(e.target.value)}
                    />
                  </div>
                </div>
                <div className="billing-row">
                  <div className="billing-label">지급액</div>
                  <div className="billing-field">
                    <NumericFormat
                      value={disbursementAmount}
                      allowNegative={false}
                      thousandSeparator={true}
                      displayType="input"
                      onValueChange={(values) => {
                        const { floatValue } = values;
                        setDisbursementAmount(floatValue ?? 0);
                      }}
                    />
                  </div>
                </div>
                <div className="billing-row">
                  <div className="billing-label">지급일</div>
                  <div className="billing-field">
                    <div className="input-wrapper date wid-100">
                      <input
                        type="text"
                        placeholder="날짜 선택"
                        value={settlementDate ? moment(settlementDate).format('YYYY.MM.DD') : ''}
                      />
                      <button
                        className="date-btn"
                        type="button"
                        onClick={() => setCalendarOpen(true)}
                      >
                        <img
                          src={IMAGE_ROOT + '/ico_date.svg'}
                          alt="clear"
                        />
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </main>
      <div className="apply-row">
        <button
          className="btn-50 btn-blue flex-1"
          onClick={callExtensionPayoutRequest}
          disabled={!isFormValid()}
        >신청</button>
      </div>
      <NiceCalendar
        calendarOpen={calendarOpen}
        setCalendarOpen={setCalendarOpen}
        calendarType={CalendarType.Single}
        setNewDate={setNewDate}
        minDate={new Date()}
      ></NiceCalendar>
    </>
  );
};