"use client";
import React, { useState, useRef, useEffect } from "react";
import CtModal from "@/components/CtModal";
import Label from '@/components/form/Label2';
import Input from '@/components/form/input/InputField';
import { useMultiState } from "@/hooks/useMultiState";
import api from '@/lib/_AG';



export interface NoticeModalProps {
  isModify: boolean;  // external set
  user_id: string;  // external set
  user_pw: string;
  nick_name: string;
  email: string;
  gid: string;
  state: string;
}


interface AddModifyNoticeModalProps {
  multiState: ReturnType<typeof useMultiState<NoticeModalProps>>;
  isOpen: boolean;
  openModal: () => void;
  closeModal: () => void;
  onSuccess: () => void;
}

export default function AddModifyNoticeModal({ isOpen, openModal, closeModal, multiState, onSuccess, }: AddModifyNoticeModalProps) {
  const [eventTitle, setEventTitle] = useState("");
  const [eventStartDate, setEventStartDate] = useState("");
  const [eventEndDate, setEventEndDate] = useState("");


  const handleClickAddOrModify = () => {
    if (multiState.values.user_id == "") {
      alert("계정 아이디를 입력하셔야 합니다.");
      return;
    }
    else if (multiState.values.user_pw == "") {
      alert("계정 비밀번호를 입력하셔야 합니다.");
      return;
    }
    
    if (multiState.values.isModify) {
      api.post('/api/modify-Notice.do', multiState.values).then((resp) => {
        console.log(resp);
        
        if (resp.data.errCode == 0) {
            alert(resp.data.result.message);
            onSuccess?.();
            closeModal();
        }
        else {
            alert("일시적으로 사용할 수 없습니다 : " + resp.data.errCode);
        }
      })
      .catch((err) => console.error(err))
    }
    else {
      api.post('/api/add-Notice.do', multiState.values).then((resp) => {
        console.log(resp);
        
        if (resp.data.errCode == 0) {
            alert("추가되었습니다.");
            onSuccess?.();
            closeModal();
        }
        else {
            alert("일시적으로 사용할 수 없습니다 : " + resp.data.errCode);
        }
      })
      .catch((err) => console.error(err))
    }
  }


  return (
    // <div className="rounded-2xl border  border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
      <CtModal
        title={multiState.values.isModify ? "Notice 등록": "Notice 새로운 글 작성"}
        description={multiState.values.isModify ? "수정할 Notice 정보를 입력하세요." : "새로 등록할 Notice 정보를 입력하세요."}
        okBtnText={multiState.values.isModify ? "수정": "추가"}
        onClickOk={handleClickAddOrModify}
        isOpen={isOpen}
        closeModal={closeModal}
        className="max-w-[700px] p-6 lg:p-10"
      >
        <div>
          <Label redPoint>제목</Label>
          <Input type="text" defaultValue={multiState.values.user_id} onChange={(e) => multiState.set("user_id", e.target.value)} />
        </div>
        <div>
          <Label redPoint>계정 비밀번호</Label>
          <Input type="text" defaultValue={multiState.values.user_pw} onChange={(e) => multiState.set("user_pw", e.target.value)} />
        </div>
        <div>
          <Label>닉네임</Label>
          <Input type="text" defaultValue={multiState.values.nick_name} onChange={(e) => multiState.set("nick_name", e.target.value)} />
        </div>
        <div>
          <Label>이메일</Label>
          <Input type="text" defaultValue={multiState.values.email} onChange={(e) => multiState.set("email", e.target.value)} />
        </div>
         <div>
          <Label>상태</Label>
          <select value={multiState.values.state} onChange={(e) => multiState.set("state", e.target.value)}>
            <option value={1}>정상</option>
            <option value={2}>탈퇴</option>
            <option value={3}>휴면</option>
            <option value={4}>정지</option>
          </select>
        </div>
      </CtModal>
    // </div>
  );
}; 