// approvalDetail.jsp 의 btnTargetLink 맵 대응 — 결재 대상 문서 상세 팝업 열기
// 결재 상세에서 "해당문서" 아이콘 클릭 시 각 모듈의 상세 팝업으로 분기

interface PopupConfig {
  url: (objId: string) => string;
  width: number;
  height: number;
}

export const TARGET_LINK_MAP: Record<string, PopupConfig> = {
  AS_MNG: { url: (id) => `/cs/manage/form?objId=${encodeURIComponent(id)}`, width: 1400, height: 950 },
  CSM: { url: (id) => `/cs/manage/form?objId=${encodeURIComponent(id)}`, width: 1400, height: 950 },
  EXPENSE_APPLY: { url: (id) => `/fund/expense-form/form?objId=${encodeURIComponent(id)}`, width: 1200, height: 850 },
  PURCHASE_ORDER: { url: (id) => `/purchase-order/form?objId=${encodeURIComponent(id)}`, width: 1220, height: 960 },
  INVOICE: { url: (id) => `/fund/invoice/form?objId=${encodeURIComponent(id)}`, width: 1000, height: 880 },
  DEFECT_ACTION: { url: (id) => `/scm/defect/form?objId=${encodeURIComponent(id)}`, width: 920, height: 820 },
  SALES_REQUEST: { url: (id) => `/sales/request/form?objId=${encodeURIComponent(id)}`, width: 1100, height: 600 },
  EO_MNG: { url: (id) => `/eo/form?objId=${encodeURIComponent(id)}`, width: 1000, height: 800 },
  ECR_MNG: { url: (id) => `/ecr/form?objId=${encodeURIComponent(id)}`, width: 1000, height: 800 },
  ISSUE_RELEASE: { url: (id) => `/production/issue/form?objId=${encodeURIComponent(id)}`, width: 1350, height: 630 },
  MATERIAL_APPLY: { url: (id) => `/inventory/request/form?objId=${encodeURIComponent(id)}`, width: 1500, height: 680 },
};

export const TARGET_NAME_MAP: Record<string, string> = {
  PURCHASE_ORDER: "발주",
  EXPENSE_APPLY: "경비",
  MATERIAL_APPLY: "자재",
  AS_MNG: "CS/AS",
  CSM: "고객서비스",
  ISSUE_RELEASE: "이슈",
  EO_MNG: "설변(EO)",
  ECR_MNG: "변경요청(ECR)",
  SALES_REQUEST: "영업요청",
  INVOICE: "세금계산서",
  DEFECT_ACTION: "불량조치",
};

export function openTargetDocument(targetType: string, targetObjId: string) {
  const cfg = TARGET_LINK_MAP[targetType];
  if (!cfg || !targetObjId) return false;
  const w = cfg.width, h = cfg.height;
  const left = Math.max(0, (window.screen.width - w) / 2);
  const top = Math.max(0, (window.screen.height - h) / 2);
  window.open(cfg.url(targetObjId), `target_${targetType}_${targetObjId}`,
    `width=${w},height=${h},left=${left},top=${top}`);
  return true;
}
