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


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

interface AddModifyResourceBoardModalProps {
  multiState: ReturnType<typeof useMultiState<ResourceBoardModalProps>>;
  onOk: (insertedId: number) => void;
  isOpen: boolean;
  closeModal: () => void;
}

export default function AddModifyResourceBoardModal({ multiState, onOk, isOpen, closeModal }: AddModifyResourceBoardModalProps) {

  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-biz-group.do', multiState.values).then((resp) => {
        console.log(resp);
        
        if (resp.data.errCode == 0) {
            alert("수정되었습니다.");
            onOk(-1);
        }
        else {
            alert("일시적으로 사용할 수 없습니다 : " + resp.data.errCode);
        }
      })
      .catch((err) => console.error(err))
    }
    else {
      api.post('/api/add-biz-group.do', multiState.values).then((resp) => {
        console.log(resp);
        
        if (resp.data.errCode == 0) {
            alert("추가되었습니다.");
            onOk(resp.data.result.biz_group_id);
        }
        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 ? "사업자정보 수정": "사업자 등록"}
        description={multiState.values.isModify ? "수정할 사업자원장 정보를 입력하세요." : "새로 등록할 사업자원장 정보를 입력하세요."}
        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" value={multiState.values.user_id} onChange={(e) => multiState.set("user_id", e.target.value)} />
        </div>
        <div>
          <Label redPoint>단말기 TID</Label>
          <Input type="text" value={multiState.values.user_pw} onChange={(e) => multiState.set("user_pw", e.target.value)} />
        </div>
        <div>
          <Label>관리자명</Label>
          <Input type="text" value={multiState.values.nick_name} onChange={(e) => multiState.set("nick_name", e.target.value)} />
        </div>
        <div>
          <Label>...</Label>
          <Input type="text" value={multiState.values.email} onChange={(e) => multiState.set("email", e.target.value)} />
        </div>
        <div>
          <Label>...</Label>
          <Input type="text" value={multiState.values.state} onChange={(e) => multiState.set("state", e.target.value)} />
        </div>
      </CtModal>
    // </div>
  );
};
