import { useState } from 'react';
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { CashReceiptHandWrittenIssuanceStep1 } from '@/entities/transaction/ui/cash-receipt-hand-written-issuance-step1';
import { CashReceiptHandWrittenIssuanceStep2 } from '@/entities/transaction/ui/cash-receipt-hand-written-issuance-step2';
import { CashReceiptPurposeType, ProcessStep } from '@/entities/transaction/model/types';
import { HeaderType} from '@/entities/common/model/types';
import { useSetFooterMode, useSetHeaderTitle, useSetHeaderType } from '@/widgets/sub-layout/use-sub-layout';
import { useCashReceiptManualIssueMutation } from '@/entities/transaction/api/use-cash-receipt-manual-issue-mutation';

export const CashReceitHandWrittenIssuancePage = () => {
  const { navigate } = useNavigate();

  // 1 or 2
  const [processStep, setProcessStep] = useState<ProcessStep>(ProcessStep.One);
  const [businessNumber, setBusinessNumber] = useState<string>('5459577852');
  const [purposeType, setPurposeType] = useState<CashReceiptPurposeType>(CashReceiptPurposeType.INCOME_DEDUCTION);
  const [productName, setProductName] = useState<string>('상품명');
  const [buyerName, setBuyerName] = useState<string>('구매자명');
  const [issueNumber, setIssueNumber] = useState<string>('01012341234');
  const [email, setEmail] = useState<string>('test123@nicepay.com');
  const [phoneNumber, setPhoneNumber] = useState<string>('01012341234');
  const [supplyAmount, setSupplyAmount] = useState<number>(9091);
  const [vatAmount, setVatAmount] = useState<number>(909);
  const [taxFreeAmount, setTaxFreeAmount] = useState<number>(0);
  const [serviceCharge, setServiceCharge] = useState<number>(0);

  useSetHeaderTitle('수기 발행');
  useSetHeaderType(HeaderType.RightClose);
  useSetFooterMode(false);

  const { mutateAsync: cashReceiptManualIssue } = useCashReceiptManualIssueMutation();
  
  const callManualIssue = () => {
    let params = {
      businessNumber: businessNumber,
      purpose: purposeType,
      productName: productName,
      buyerName: buyerName,
      issueNumber: issueNumber,
      email: email,
      phoneNumber: phoneNumber,
      supplyAmount: 9091,
      vatAmount: 909,
      taxFreeAmount: 0,
      serviceCharge: 0
    };
    cashReceiptManualIssue(params).then((rs) => {
      console.log(rs);
      alert('완료');
      navigate(PATHS.transaction.cashReceipt.list);
    });
  };

  const onClickToChangeTab = () => {
    if(processStep === ProcessStep.One){
      setProcessStep(ProcessStep.Two);
    }
    else if(processStep === ProcessStep.Two){
      callManualIssue();
      // 완료시?
      //alert('완료');
      // navigate(PATHS.transaction.cashReceipt.list);
    }
  };

  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="option-list">
              <div className="issue-progress">
                <div className="bar">
                  {(processStep === ProcessStep.One) &&
                    <div
                      className="fill"
                      style={{ width: '50%' }}
                    ></div>
                  }
                  {(processStep === ProcessStep.Two) &&
                    <div
                      className="fill"
                      style={{ width: '100%' }}
                    ></div>
                  }
                </div>
              </div>
              { (processStep === ProcessStep.One) &&
                <CashReceiptHandWrittenIssuanceStep1
                  businessNumber={ businessNumber }
                  purposeType={ purposeType }
                  productName={ productName }
                  buyerName={ buyerName }
                  issueNumber={ issueNumber }
                  email={ email }
                  phoneNumber={ phoneNumber }
                  setBusinessNumber={ setBusinessNumber }
                  setPurposeType={ setPurposeType }
                  setProductName={ setProductName }
                  setBuyerName={ setBuyerName }
                  setIssueNumber={ setIssueNumber }
                  setEmail={ setEmail }
                  setPhoneNumber={ setPhoneNumber }
                ></CashReceiptHandWrittenIssuanceStep1>
              }
              { (processStep === ProcessStep.Two) &&
                <CashReceiptHandWrittenIssuanceStep2
                  setProcessStep={ setProcessStep }
                  supplyAmount={ supplyAmount }
                  vatAmount={ vatAmount }
                  taxFreeAmount={ taxFreeAmount }
                  serviceCharge={ serviceCharge }
                  setSupplyAmount={ setSupplyAmount }
                  setVatAmount={ setVatAmount }
                  setTaxFreeAmount={ setTaxFreeAmount }
                  setServiceCharge={ setServiceCharge }
                ></CashReceiptHandWrittenIssuanceStep2>
              }
            </div>
            <div className="apply-row">
              <button 
                className="btn-50 btn-blue flex-1"
                onClick={ () => onClickToChangeTab() }
              >
                { (processStep === ProcessStep.One) && '다음' }
                { (processStep === ProcessStep.Two) && '발행' }
              </button>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};