"use client";

import AppContext from "@ctx/AppContext";
import { useRouter } from "next/navigation";
import Buttonbriz from "plugins/Buttonbriz/page";
import Input from "plugins/Input/page";
import PersianNumber from "plugins/PersianNumber";
import React, { useContext, useEffect, useRef, useState } from "react";
import { toast } from "react-toastify";
import SimpleReactValidator from "simple-react-validator";
import { useSearchParams } from "next/navigation";

const SignUp = (props) => {
  const CTX = useContext(AppContext);
  const router = useRouter();
  const query = useSearchParams();

  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
  const [complexName, setComplexName] = useState("");
  const [supportPhoneNumber, setSupportPhoneNumber] = useState(
    CTX.state.phoneNumber
  );
  const [complexAddress, setComplexAddress] = useState("");
  const [, forceUpdate] = useState();

  const validator = useRef(
    new SimpleReactValidator({
      messages: {
        required: "پر کردن این فیلد الزامی میباشد",
      },
      element: (message) => (
        <>
          <div className="text-right px-1 ">
            <small className="text-red-600 t-ig-small  ">{message}</small>
          </div>
        </>
      ),
    })
  );

  const body = {
    firstName,
    lastName,
    complexName,
    supportPhoneNumber,
    complexAddress,
  };

  useEffect(() => {
    if (!!query.get("phoneNumber")) {
      setSupportPhoneNumber(query.get("phoneNumber"));
    }
  }, []);

  const handleSingnUp = () => {
    if (validator.current.allValid()) {
      CTX.SignUpLogin(body);
    } else {
      toast.error("پرکردن همه ی فیلد ها واجب است", {
        position: "bottom-right",
        autoClose: 2000,
        hideProgressBar: false,
        closeOnClick: true,
        pauseOnHover: true,
        draggable: true,
        progress: undefined,
      });

      validator.current.showMessages();
      forceUpdate(1);
    }
  };

  return (
    <div className="px-5 mt-6">
      <p className="mb-0 text-textMain-100 mt-1 text-right text-sm ">
        برای ورود یا ثبت نام به اپلیکیشن مدیریت وظایف رستوران باید شماره تلفن
        همراه خود را وارد کنید
      </p>

      <div className="mt-8">
        <Input
          lable="نام"
          mt={5}
          theme={1}
          id="firstName-id"
          name="firstName"
          type={"text"}
          value={firstName}
          inputEvent={(e) => {
            setFirstName(e.target.value);
            validator.current.showMessageFor("firstName");
          }}
          style="text-right"
          validator={true}
          validatorData={validator.current.message(
            "firstName",
            firstName,
            "required"
          )}
        />
      </div>
      <div className="mt-8">
        <Input
          lable="نام خانوادگی"
          mt={5}
          theme={1}
          id="lastName-id"
          name="lastName"
          type={"text"}
          value={lastName}
          inputEvent={(e) => {
            setLastName(e.target.value);
            validator.current.showMessageFor("lastName");
          }}
          style="text-right"
          validator={true}
          validatorData={validator.current.message(
            "lastName",
            lastName,
            "required"
          )}
        />
      </div>

      <div className="mt-8">
        <Input
          lable="نام مجموعه"
          mt={5}
          theme={1}
          id="complexName-id"
          name="complexName"
          type={"text"}
          value={complexName}
          inputEvent={(e) => {
            setComplexName(e.target.value);
            validator.current.showMessageFor("complexName");
          }}
          style="text-right"
          validator={true}
          validatorData={validator.current.message(
            "complexName",
            complexName,
            "required"
          )}
        />
      </div>
      <div className="mt-3">
        <Input
          lable="شماره تماس مدیریت مجموعه"
          mt={5}
          theme={1}
          id="supportPhoneNumber-id"
          name="supportPhoneNumber"
          type={"number"}
          value={supportPhoneNumber}
          inputEvent={(e) => setSupportPhoneNumber(e.target.value)}
          style="text-right"
          readOnly={true}
        />
      </div>
      <div className="mt-3">
        <Input
          lable="ادرس مجموعه "
          mt={5}
          theme={1}
          id="complexAddress-id"
          name="complexAddress"
          type={"text"}
          value={complexAddress}
          inputEvent={(e) => {
            setComplexAddress(e.target.value);
            validator.current.showMessageFor("complexAddress");
          }}
          style="text-right"
          validator={true}
          validatorData={validator.current.message(
            "complexAddress",
            complexAddress,
            "required"
          )}
        />
      </div>

      <Buttonbriz
        title="تایید اطلاعات"
        color="PRIMARY"
        icon="CHECK"
        buttonEvent={() => handleSingnUp()}
      />
    </div>
  );
};

export default SignUp;
