/* eslint-disable */  //...

"use client";
import Checkbox from "@/components/form/input/Checkbox";
import Input from "@/components/form/input/InputField2";
import Label from "@/components/form/Label";
import Button from "@/components/ui/button/Button";
import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons";
import Link from "next/link";
import React, { useState } from "react";
import { useRouter } from "next/navigation";
import api, { useAuthStore } from '@/lib/_AG';


export default function SignInForm() {
  const router = useRouter();
  const tokenPayload = useAuthStore((s) => s.tokenPayload);  //Bruce, JWT

  const [user_id, setUserId] = useState("");
  const [user_pw, setUserPw] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const [isChecked, setIsChecked] = useState(false);

  const handleLogin = () => {
    //router.push("/"); //TestCode
    //return;

    if (user_id == "") {
      alert("아이디를 입력하셔야 합니다.");
      return;
    }
    else if (user_pw == "") {
      alert("암호를 입력하셔야 합니다.");
      return;
    }

    const params = {
      id: user_id,
      pw: user_pw
    }

    api.post('/api/sign-in.do', params).then((resp) => {
      console.log(resp);
      
      if (resp.data.errCode == 0) {
          useAuthStore.getState().setAccessToken(resp.data.result.sval1);
          router.push("/");
      }
      else if (resp.data.errCode == -9) {
        alert("유효하지 않은 아이디 또는 암호입니다.");
      }
      else {
        alert("로그인 에러 : " + resp.data.errCode);
      }
    })
    .catch((err) => console.error(err))
  };

  return (
    <div className="flex flex-col flex-1 lg:w-1/2 w-full">
      <div className="flex flex-col justify-center flex-1 w-full max-w-md mx-auto">
        <div>
          <div className="mb-5 sm:mb-8">
            <h1 className="mb-2 font-semibold text-gray-800 text-title-sm dark:text-white/90 sm:text-title-md">
              스마트서비스 로그인
            </h1>
            <p className="text-sm text-gray-500 dark:text-gray-400">
              Enter your ID and password to log in!
            </p>
          </div>
          <div>

            {/*<form> //... */}

              <div className="space-y-6">
                <div>
                  <Label>
                    ID <span className="text-error-500">*</span>{" "}
                  </Label>
                  <Input placeholder="아이디를 입력하세요" type="text" value={user_id} onChange={(e) => setUserId(e.target.value)} />
                </div>
                <div>
                  <Label>
                    Password <span className="text-error-500">*</span>{" "}
                  </Label>
                  <div className="relative">
                    <Input
                      type={showPassword ? "text" : "password"}
                      placeholder="암호를 입력하세요"
                      value={user_pw} onChange={(e) => setUserPw(e.target.value)}
                    />
                    <span
                      onClick={() => setShowPassword(!showPassword)}
                      className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2"
                    >
                      {showPassword ? (
                        <EyeIcon className="fill-gray-500 dark:fill-gray-400" />
                      ) : (
                        <EyeCloseIcon className="fill-gray-500 dark:fill-gray-400" />
                      )}
                    </span>
                  </div>
                </div>
                <div className="flex items-center justify-between">
                  <div className="flex items-center gap-3">
                    {/*
                    <Checkbox checked={isChecked} onChange={setIsChecked} />
                    <span className="block font-normal text-gray-700 text-theme-sm dark:text-gray-400">
                      로그인 상태 유지
                    </span>
                    */}
                  </div>
                  {/*
                  <Link
                    href="/reset-password"
                    className="text-sm text-brand-500 hover:text-brand-600 dark:text-brand-400"
                  >
                    패스워드 찾기
                  </Link>
                  */}
                </div>
                <div>
                  <Button className="w-full" size="sm" onClick={handleLogin}>
                    로그인
                  </Button>
                </div>
              </div>
            {/*</form>*/}

          </div>
        </div>
      </div>
    </div>
  );
}
