"use client";
import { useEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from "react";
import SubPageHeader from "@/components/SubPageHeader";
import PostcodeModal from "@/components/PostcodeModal";
import type { AddrItem } from "@/app/store/sdmMng/postcode-service";
import { useDmStore } from "@/app/store";
import { fetchType, getFetch, postFetch, putFetch } from "@/components/ui/api";
import { getCookie } from "@common/components/lib";
type Option = { value: string; label: string };
type SessionUser = {
dmdInfoId?: string;
dmdCoNm?: string;
zip?: string;
daddr?: string;
telno?: string;
ctpvCd?: string;
sggCd?: string;
rgtr?: string;
[key: string]: unknown;
};
const TOKEN_ID_COOKIE = "token-id";
const FALLBACK_UUID = "c475824c-0e82-4a06-a282-2b55a20df265";
const normalizeRegionName = (value: string) => value.replace(/\s+/g, "").trim();
const isSameRegionName = (left: string, right: string) => {
const a = normalizeRegionName(left);
const b = normalizeRegionName(right);
return Boolean(a && b && (a === b || a.includes(b) || b.includes(a)));
};
const formatKoreanPhoneNumber = (value: string) => {
const digits = value.replace(/\D/g, "").slice(0, 11); // 숫자만 추출하고 최대 11자리까지만 사용
if (!digits) return ""; // 입력이 비어 있으면 빈 문자열 반환
if (/^1\d{0,7}$/.test(digits)) { // 15xx/16xx/18xx 같은 대표번호 패턴
if (digits.length <= 4) return digits; // 아직 뒷자리가 부족하면 원문 유지
return `${digits.slice(0, 4)}-${digits.slice(4, 8)}`; // xxxx-xxxx
}
if (digits.startsWith("02")) { // 서울 지역번호(02)
const rest = digits.slice(2); // 국번/가입자번호 구간
if (!rest) return "02"; // 02만 입력된 상태
if (rest.length <= 3) return `02-${rest}`; // 02-xxx
if (rest.length <= 7) return `02-${rest.slice(0, 3)}-${rest.slice(3)}`; // 02-xxx-xxxx
return `02-${rest.slice(0, 4)}-${rest.slice(4, 8)}`; // 02-xxxx-xxxx
}
if (digits.startsWith("0")) { // 02 제외 일반 지역번호/휴대폰(010 포함)
const area = digits.slice(0, 3); // 앞 3자리 식별자
const rest = digits.slice(3); // 나머지 번호
if (!rest) return area; // 식별자만 입력된 상태
if (rest.length <= 3) return `${area}-${rest}`; // 0xx-xxx
if (rest.length <= 7) return `${area}-${rest.slice(0, 3)}-${rest.slice(3)}`; // 0xx-xxx-xxxx
return `${area}-${rest.slice(0, 4)}-${rest.slice(4, 8)}`; // 0xx-xxxx-xxxx
}
if (digits.length <= 4) return digits; // 예외 입력: 앞 4자리까지
if (digits.length <= 8) return `${digits.slice(0, 4)}-${digits.slice(4)}`; // xxxx-xxxx
return `${digits.slice(0, 4)}-${digits.slice(4, 8)}-${digits.slice(8, 11)}`; // xxxx-xxxx-xxx
};
function Divider() {
return
;
}
function FormRow({
label,
required,
children,
}: {
label: ReactNode;
required?: boolean;
children: ReactNode;
}) {
return (
{label}
{required ? * : null}
{children}
);
}
function TextInput({
value,
onChange,
placeholder,
type = "text",
disabled,
inputRef,
className = "",
}: {
value: string;
onChange: (v: string) => void;
placeholder?: string;
type?: string;
disabled?: boolean;
inputRef?: RefObject;
className?: string;
}) {
return (
onChange(e.target.value)}
placeholder={placeholder}
className={[
"h-11 w-full rounded border border-neutral-300 bg-white px-3 text-sm text-neutral-800",
"placeholder:text-neutral-400 focus:border-neutral-400 focus:outline-none",
disabled ? "bg-neutral-100 text-neutral-400" : "",
className,
].join(" ")}
/>
);
}
function SelectBox({
value,
onChange,
options,
className = "",
}: {
value: string;
onChange: (v: string) => void;
options: Option[];
className?: string;
}) {
return (
);
}
export default function DemandPlaceRegisterPage() {
const { Common, initializeCommonData } = useDmStore((state) => state);
const { selectSidoData, selectSigunData } = Common;
const [editMode, setEditMode] = useState(false);
const [sessionUser, setSessionUser] = useState(null);
const [sido, setSido] = useState("");
const [sigungu, setSigungu] = useState("");
const [company, setCompany] = useState("");
const [bizNo, setBizNo] = useState("");
const [workerNo, setWorkerNo] = useState("");
const [noWorkerNo, setNoWorkerNo] = useState(false);
const [zip, setZip] = useState("");
const [addr1, setAddr1] = useState("");
const [addr2, setAddr2] = useState("");
const [isPostcodeModalOpen, setIsPostcodeModalOpen] = useState(false);
const [focusDetailAddrOnClose, setFocusDetailAddrOnClose] = useState(false);
const detailAddrRef = useRef(null);
const [tel, setTel] = useState("");
const [pw, setPw] = useState("");
const [pw2, setPw2] = useState("");
useEffect(() => {
initializeCommonData();
}, [initializeCommonData]);
useEffect(() => {
if (typeof window === "undefined") return;
const rawUser = sessionStorage.getItem("user");
if (!rawUser) {
setEditMode(false);
setSessionUser(null);
return;
}
try {
const user = JSON.parse(rawUser) as SessionUser;
setSessionUser(user);
setEditMode(true);
const fullId = String(user.dmdInfoId ?? "").trim();
const bizDigits = fullId.slice(0, 10);
const workerDigits = fullId.slice(10, 14);
setBizNo(bizDigits);
if (workerDigits && workerDigits !== "0000") {
setNoWorkerNo(false);
setWorkerNo(workerDigits);
} else {
setNoWorkerNo(true);
setWorkerNo("");
}
setCompany(String(user.dmdCoNm ?? ""));
setZip(String(user.zip ?? ""));
setAddr1(String(user.daddr ?? ""));
setAddr2("");
setTel(formatKoreanPhoneNumber(String(user.telno ?? "")));
setSido(String(user.ctpvCd ?? ""));
setSigungu(String(user.sggCd ?? ""));
} catch {
setEditMode(false);
setSessionUser(null);
}
}, []);
useEffect(() => {
if (!focusDetailAddrOnClose || isPostcodeModalOpen) return;
if (!addr1.trim()) {
setFocusDetailAddrOnClose(false);
return;
}
const timer = setTimeout(() => {
detailAddrRef.current?.focus();
setFocusDetailAddrOnClose(false);
}, 0);
return () => clearTimeout(timer);
}, [addr1, focusDetailAddrOnClose, isPostcodeModalOpen]);
const sidoOptions = useMemo