import moment from "jalali-moment"; import { useEffect, useState } from "react"; import { AiOutlinePlus, AiFillDelete, AiOutlineCloseCircle, } from "react-icons/ai"; import { connect } from "react-redux"; import { toast } from "react-toastify"; import { userFactor,userTransaction } from "../../redux/actions"; const PaymentBox = ({ setShowPaymentBox, getFactorInfo, factorInfo, payment, query, addUserTransaction, restartTransaction, transactionSituation }) => { useEffect(() => { getFactorInfo(); }, []); console.log("factorInfo:"); console.log(factorInfo); //------------------------------------------------- const [fake, setFake] = useState(0); const [paymentList, setPaymentList] = useState([ { id: 1, amount: factorInfo?.payPrice, status: null, type: 1, cardNumber: null, paidAt: null, TracingNumber: null, }, ]); useEffect(() => { if (localStorage.getItem("paymentList") && query.status) { console.log("fnjwebfjkwbf:"); console.log(factorInfo); let newList = JSON.parse(localStorage.getItem("paymentList")); let lastPaymentIndex = localStorage.getItem("lastPaymentIndex"); let payPrice = localStorage.getItem("factorInfoPrice"); if (query.status === "OK") { newList[lastPaymentIndex].status = 1; if (newList.amount < payPrice) { newList.push({ id: newList.length + 1, amount: null, status: null, type: 1, cardNumber: null, paidAt: null, TracingNumber: null, }); } } else { newList[lastPaymentIndex].status = null; } localStorage.removeItem("paymentList"); localStorage.removeItem("lastPaymentIndex"); localStorage.removeItem("factorInfoPrice"); setPaymentList(newList); setFake(fake + 1); } }, []); //----------------------------------------- const changeType = (e, index) => { let newList = paymentList; newList[index] = { ...newList[index], type: Number(e.target.value) }; console.log("g:"); console.log(newList[index]); setPaymentList(newList); setFake(fake + 1); }; const addPaymentHandler = () => { let newList = paymentList; newList.push({ id: newList.length + 1, amount: null, status: null, type: 1, cardNumber: null, paidAt: null, TracingNumber: null, }); setPaymentList(newList); setFake(fake + 1); }; const deletePayment = (index) => { let newList = []; for (let i = 0; i < paymentList.length; i++) { if (i !== index) { newList.push(paymentList[i]); } } setPaymentList(newList); setFake(fake + 1); }; //changeAmount-------------------- const changeAmount = (e, index) => { let newList = paymentList; newList[index].amount = e.target.value; setPaymentList(newList); setFake(fake + 1); }; //paymentStatuslists---------------------- let paymentStatusColorList = ["#FBBF24", "#84CC16"]; let paymentStatusTextList = ["در انتظار تایید", "پرداخت شده"]; //cardToCardCHeck----------------------------------- console.log("transactionSituation:"); console.log(transactionSituation); useEffect(()=>{ if(transactionSituation) { alert("hooooooooo"); let newList = paymentList; newList[newList.length-1].status=0; setPaymentList(newList); setFake(fake+1) restartTransaction() } },[transactionSituation]) //paymentHandler-------- const paymentHandler = (item, index) => { console.log(item); if (item.type === 1) { localStorage.setItem("paymentList", JSON.stringify(paymentList)); localStorage.setItem("lastPaymentIndex", index); localStorage.setItem("factorInfoPrice", factorInfo.payPrice); // payment({ // price: item.amount, // redirectUrl: "https://new.andishmand.ir/services", // }); payment({ price: item.amount, redirectUrl: "http://localhost:3000/services", }); } else if (item.type === 2) { let cardNumber = document.querySelector( "#paymentBox-cardNumber-input" ).value; let dateInfo = document.querySelectorAll( ".paymentBox-payment-date-input" ); let TracingNumber = document.querySelector( "#paymentBox-TracingNumber-input" ).value; if (Number(dateInfo[0]) > 31) { toast.error("تاریخ وارد شده معتبر نیست"); } else if (Number(dateInfo[1].value) > 12) { toast.error("تاریخ وارد شده معتبر نیست"); } else if (Number(dateInfo[2].value) < 1401) { toast.error("تاریخ وارد شده معتبر نیست"); } else if (Number(dateInfo[3].value) > 59) { toast.error("ساعت وارد شده معتبر نیست"); } else if (Number(dateInfo[4].value) > 23) { toast.error("ساعت وارد شده معتبر نیست"); } else if (cardNumber.length < 16) { toast.error("کارت وارد شده نامعتبر است"); } else if (TracingNumber.length < 1) { toast.error("شماره پیگیری را وارد کنید"); } else { //create date------------------------------------------- let dateString = `${Number(dateInfo[2].value)}/${Number( dateInfo[1].value )}/${Number(dateInfo[0].value)}`; console.log(dateString); let dueDate = moment .from( `${dateString} ${Number(dateInfo[4].value)}:${Number( dateInfo[3].value )}`, "fa", `YYYY/M/D HH:mm` ) .format("YYYY-M-D HH:mm"); console.log(dueDate); //----------------------------------------- let newList = paymentList; newList[index].cardNumber = cardNumber; newList[index].paidAt = dueDate; newList[index].TracingNumber = TracingNumber; setPaymentList(newList); setFake(fake + 1); addUserTransaction({ price: item.amount, dueDate: dueDate, cardNumber, followCode: TracingNumber, type:5 }); } } }; return (