// Calendar Status View — alternate view of the RACI matrix that shows daily status
// per sub-process across all days of the selected month.
// Each row = sub-process; columns = days of month with status colored cells.

const CalendarStatusView = ({
  subprocesses, mainProcesses, processes,
  positions, departments, divisions,
  tasks, taskHistory, setTaskHistory, setTasks, setAuditLog,
  period, tenant, currentRole, currentUser, users,
  filterFn, // (sp) => bool for filters/search/mine
  setTimelineFor, setDrawerFor, comments, setComments,
  density,
}) => {
  const toast = useToast();
  const [cellAction, setCellAction] = useState(null); // { sp, date, status, anchor }
  const myPositionIds = currentUser?.positions || [];

  // Build days of selected month
  const days = useMemo(() => {
    const { month, year } = period;
    const last = new Date(year, month, 0).getDate();
    const arr = [];
    for (let d = 1; d <= last; d++) {
      const dt = new Date(year, month-1, d);
      const ds = `${year}-${String(month).padStart(2,"0")}-${String(d).padStart(2,"0")}`;
      const dow = dt.getDay(); // 0 Sun .. 6 Sat
      arr.push({ d, ds, dow, isWeekend: dow===0||dow===6, isToday: ds === "2026-05-22", isFuture: dt > new Date("2026-05-22T23:59:59") });
    }
    return arr;
  }, [period]);

  // Filtered rows from passed-in filter
  const rows = useMemo(() => subprocesses.filter(filterFn), [subprocesses, filterFn]);

  // Cell size + left sticky widths
  const CELL_W = density === "compact" ? 26 : 32;
  const LEFT_W = { mp: 150, pr: 150, sp: 220, due: 90 };
  const LEFT_TOTAL = LEFT_W.mp + LEFT_W.pr + LEFT_W.sp + LEFT_W.due;
  const RIGHT_W = 70; // timeline+comment icons

  // Update handler — same logic as matrix, abbreviated
  const updateInstance = (sp, dateStr, patch, audit) => {
    const stamp = nowStamp();
    if (sp.frequency === "daily" || sp.frequency === "weekly" || sp.frequency === "monthly" || sp.frequency === "quarterly") {
      setTaskHistory(prev => {
        const list = (prev[sp.id] || []).slice();
        let idx = -1;
        if (sp.frequency === "monthly" || sp.frequency === "quarterly") {
          idx = list.findIndex(h => h.date.slice(0,7) === dateStr.slice(0,7));
        } else if (sp.frequency === "weekly") {
          idx = list.findIndex(h => Math.abs((new Date(h.date) - new Date(dateStr)) / 86400000) <= 6);
        } else {
          idx = list.findIndex(h => h.date === dateStr);
        }
        if (idx >= 0) list[idx] = { ...list[idx], ...patch, updated: stamp };
        else { list.push({ date: dateStr, status: "blank", ...patch, updated: stamp }); list.sort((a,b) => a.date.localeCompare(b.date)); }
        return { ...prev, [sp.id]: list };
      });
    }
    if (dateStr === "2026-05-22" || sp.frequency === "ad-hoc") {
      setTasks(prev => prev.map(t => t.sub_process_id === sp.id ? { ...t, ...patch, updated: stamp } : t));
    }
    if (audit) {
      const lbl = dateStr === "2026-05-22" ? "วันนี้" : `งวด ${dateStr}`;
      setAuditLog(prev => [{ id:"a"+Math.random().toString(36).slice(2,7), scope:"tenant", tenant_id:tenant.id, action:"status_change", reason:`${audit} · ${lbl}`, performed_by: currentUser?.full_name || (currentRole==="admin"?"ธนพร อัศวิน":"ทีมงาน"), at: stamp }, ...prev]);
    }
  };

  // Resolve a status letter + tone for any day
  const cellFor = (sp, ds) => {
    if (sp.frequency === "daily") {
      const h = (taskHistory[sp.id]||[]).find(x => x.date === ds);
      return h || { status: "blank", date: ds };
    }
    if (sp.frequency === "monthly" || sp.frequency === "quarterly") {
      // Only show on the due day of the month
      const isLast = (sp.due_day||"").includes("สุดท้าย");
      const dayNum = +ds.slice(-2);
      let dueDay = null;
      if (isLast) {
        // Compute last workday of the month
        const [yy, mm] = ds.split("-").map(Number);
        let last = new Date(yy, mm, 0); // last calendar day
        while (last.getDay() === 0 || last.getDay() === 6) last.setDate(last.getDate() - 1);
        dueDay = last.getDate();
      } else {
        const m = (sp.due_day||"").match(/\d+/);
        dueDay = m ? +m[0] : null;
      }
      if (dueDay && dayNum === dueDay) {
        const h = (taskHistory[sp.id]||[]).find(x => x.date === ds);
        return h || { status: "blank", date: ds, isMonthlyDue: true };
      }
      return null;
    }
    if (sp.frequency === "weekly") {
      const dt = new Date(ds);
      if (dt.getDay() !== 5) return null; // Friday only
      const h = (taskHistory[sp.id]||[]).find(x => Math.abs((new Date(x.date) - dt)/86400000) <= 3);
      return h || { status: "blank", date: ds };
    }
    // ad-hoc — show only on dates with instances
    const h = (taskHistory[sp.id]||[]).find(x => x.date === ds);
    return h || null;
  };

  // Aggregated per-row stats for the month
  const rowStats = (sp) => {
    let s=0,a=0,c=0,p=0,b=0,total=0;
    days.forEach(d => {
      if (d.isFuture) return;
      const cell = cellFor(sp, d.ds);
      if (!cell) return;
      if (sp.frequency === "daily" && d.isWeekend) return;
      total++;
      if (cell.status === "S") s++;
      else if (cell.status === "A") a++;
      else if (cell.status === "C") c++;
      else if (cell.status === "P") p++;
      else b++;
    });
    return { s, a, c, p, b, total, pct: total ? Math.round(s/total*100) : 0 };
  };

  return (
    <div className={`bg-white rounded-2xl border border-ink-100 overflow-hidden shadow-sm density-${density} relative`}
         style={{ height: "calc(100vh - 280px)", minHeight: 540 }}>
      <div className="w-full h-full overflow-auto thin-scroll">
        <table className="border-separate" style={{ borderSpacing: 0 }}>
          <thead>
            <tr>
              {/* Sticky left header */}
              <th colSpan={4} className="sticky top-0 left-0 z-50 bg-ink-50 matrix-head-cell text-left px-3 py-2"
                  style={{ minWidth: LEFT_TOTAL, height: 56 }}>
                <div className="text-[10.5px] uppercase tracking-[.15em] text-ink-500 font-semibold">Process Hierarchy</div>
                <div className="text-[10.5px] text-ink-400">Main · Process · Sub-process · Due</div>
              </th>
              {/* Day headers */}
              {days.map(day => (
                <th key={day.d}
                    className={`sticky top-0 z-40 matrix-head-cell text-center bg-ink-50 ${day.isWeekend?"bg-ink-100/60":""} ${day.isToday?"bg-brand-50":""}`}
                    style={{ width: CELL_W, minWidth: CELL_W, height: 56, padding: 0 }}>
                  <div className="flex flex-col items-center justify-center h-full leading-tight">
                    <span className="text-[9px] uppercase text-ink-400 font-medium">{["อา","จ","อ","พ","พฤ","ศ","ส"][day.dow]}</span>
                    <span className={`text-[12px] font-bold ${day.isToday?"text-brand-700":day.isWeekend?"text-ink-400":"text-ink-800"}`}>{day.d}</span>
                    {day.isToday && <span className="w-1 h-1 rounded-full bg-brand-600 mt-0.5"/>}
                  </div>
                </th>
              ))}
              {/* Stats summary header */}
              <th colSpan={2} className="sticky top-0 right-0 z-50 bg-ink-50 matrix-head-cell text-left px-3 py-2 sticky-shadow-l"
                  style={{ minWidth: 140 }}>
                <div className="text-[10.5px] uppercase tracking-[.15em] text-ink-500 font-semibold">Month Summary</div>
                <div className="text-[10.5px] text-ink-400">{period.month}/{period.year}</div>
              </th>
            </tr>
          </thead>
          <tbody>
            {rows.length === 0 && (
              <tr>
                <td colSpan={4 + days.length + 2} className="text-center py-16">
                  <EmptyState icon="search" title="ไม่พบงานที่ตรงกับเงื่อนไข" subtitle="ลองล้างตัวกรอง"/>
                </td>
              </tr>
            )}
            {rows.map((sp, rIdx) => {
              const mp = mainProcesses.find(m=>m.id===sp.main_process_id);
              const pr = processes.find(p=>p.id===sp.process_id);
              const prev = rows[rIdx-1];
              const newMp = !prev || prev.main_process_id !== sp.main_process_id;
              const newPr = !prev || prev.process_id !== sp.process_id;
              const stats = rowStats(sp);
              const rUser = users.find(u => u.positions.includes(sp.R));
              const aUser = users.find(u => u.positions.includes(sp.A));

              return (
                <tr key={sp.id} className={`row group ${newMp ? "border-t-2 border-ink-200" : ""}`}>
                  {/* Main process */}
                  <td className="sticky left-0 z-30 bg-white group-hover:bg-ink-50/60 matrix-cell px-3 align-top"
                      style={{ width: LEFT_W.mp, minWidth: LEFT_W.mp, maxWidth: LEFT_W.mp }}>
                    {newMp && <Tip tip={mp?.name}><div className="flex items-center gap-1.5"><span className="w-1 h-3.5 rounded-sm bg-indigo-500"/><span className="text-[12px] font-semibold text-ink-900 truncate-1">{mp?.name}</span></div></Tip>}
                  </td>
                  {/* Process */}
                  <td className="sticky z-30 bg-white group-hover:bg-ink-50/60 matrix-cell px-3 align-top"
                      style={{ left: LEFT_W.mp, width: LEFT_W.pr, minWidth: LEFT_W.pr, maxWidth: LEFT_W.pr }}>
                    {newPr && <Tip tip={pr?.name}><div className="text-[12px] text-ink-700 truncate-1">{pr?.name}</div></Tip>}
                  </td>
                  {/* Sub-process */}
                  <td className="sticky z-30 bg-white group-hover:bg-ink-50/60 matrix-cell px-3 align-top"
                      style={{ left: LEFT_W.mp + LEFT_W.pr, width: LEFT_W.sp, minWidth: LEFT_W.sp, maxWidth: LEFT_W.sp }}>
                    <Tip tip={sp.name}><div className="text-[12px] font-medium text-ink-900 truncate-1">{sp.name}</div></Tip>
                    <div className="flex items-center gap-1.5 mt-0.5 text-[10px] text-ink-500">
                      <Badge color="slate" className="!text-[10px]">{sp.frequency}</Badge>
                      <Tip tip={`R: ${rUser?.full_name || "—"} · A: ${aUser?.full_name || "—"}`}>
                        <span className="flex items-center gap-1"><RaciBadge kind="R" size="sm"/><RaciBadge kind="A" size="sm"/></span>
                      </Tip>
                    </div>
                  </td>
                  {/* Due rule */}
                  <td className="sticky z-30 bg-white group-hover:bg-ink-50/60 matrix-cell px-2 sticky-shadow-r"
                      style={{ left: LEFT_W.mp + LEFT_W.pr + LEFT_W.sp, width: LEFT_W.due, minWidth: LEFT_W.due, maxWidth: LEFT_W.due }}>
                    <div className="text-[10.5px] text-ink-600 truncate-1">{sp.due_day}</div>
                  </td>

                  {/* Day cells */}
                  {days.map(day => {
                    const cell = cellFor(sp, day.ds);
                    const isWeekendDaily = sp.frequency === "daily" && day.isWeekend;
                    if (!cell || isWeekendDaily) {
                      return (
                        <td key={day.d} className={`matrix-cell text-center ${day.isToday?"bg-brand-50/30":isWeekendDaily?"bg-ink-50/30":""}`}
                            style={{ width: CELL_W, minWidth: CELL_W, padding: 0, height: density==="compact"?28:34 }}>
                          <span className="text-ink-200 text-[10px]">·</span>
                        </td>
                      );
                    }
                    const m = STATUS_META[cell.status];
                    const isR = myPositionIds.includes(sp.R);
                    const isA = myPositionIds.includes(sp.A);
                    const trackingOff = sp.track_status === false;
                    return (
                      <td key={day.d} className={`matrix-cell text-center p-0 ${day.isToday?"bg-brand-50/30":""}`}
                          style={{ width: CELL_W, minWidth: CELL_W, height: density==="compact"?28:34 }}>
                        <Tip tip={`${day.ds} · ${m.label}${cell.updated?` · ${cell.updated}`:""}${trackingOff ? " · ดูอย่างเดียว (ไม่ติดตามสถานะ)" : !canActOnCellStatic(sp, cell.status, currentRole, myPositionIds) ? " · อ่านอย่างเดียว":""}`}>
                          <button
                            onClick={() => { if (trackingOff) return; setCellAction({ sp, date: day.ds, status: cell.status, cell, isR, isA }); }}
                            className={`w-full h-full flex items-center justify-center text-[11px] font-bold transition relative ${trackingOff?"cursor-not-allowed":"hover:scale-110 hover:z-10"}`}
                            style={{
                              background: trackingOff ? "#f8fafc" :
                                          cell.status === "S" ? "#d1fae5" :
                                          cell.status === "A" ? "#ede9fe" :
                                          cell.status === "C" ? "#fef3c7" :
                                          cell.status === "P" ? "#dbeafe" : "#f1f3f9",
                              color:      trackingOff ? "#94a3b8" :
                                          cell.status === "S" ? "#047857" :
                                          cell.status === "A" ? "#6d28d9" :
                                          cell.status === "C" ? "#b45309" :
                                          cell.status === "P" ? "#1d4ed8" : "#94a3b8",
                              opacity: trackingOff ? 0.5 : (canActOnCellStatic(sp, cell.status, currentRole, myPositionIds) ? 1 : 0.55),
                            }}>
                            {trackingOff ? "·" : cell.status === "blank" ? "·" : m.short}
                            {day.isToday && <span className="absolute -bottom-0 left-1/2 -translate-x-1/2 w-1 h-1 rounded-full bg-rose-500"/>}
                          </button>
                        </Tip>
                      </td>
                    );
                  })}

                  {/* Month summary */}
                  <td className="sticky right-[40px] bg-white group-hover:bg-ink-50/60 matrix-cell px-2"
                      style={{ width: 100, minWidth: 100 }}>
                    {stats.total > 0 ? (
                      <div className="flex flex-col items-end gap-0.5">
                        <div className={`text-[12px] font-mono font-bold ${stats.pct>=80?"text-emerald-600":stats.pct>=50?"text-amber-600":"text-rose-600"}`}>{stats.pct}%</div>
                        <div className="w-full h-1.5 bg-ink-100 rounded-full overflow-hidden flex">
                          <div className="bg-emerald-500" style={{width:`${stats.s/stats.total*100}%`}}/>
                          <div className="bg-violet-500" style={{width:`${stats.a/stats.total*100}%`}}/>
                          <div className="bg-amber-500" style={{width:`${stats.c/stats.total*100}%`}}/>
                          <div className="bg-blue-500" style={{width:`${stats.p/stats.total*100}%`}}/>
                          <div className="bg-ink-300" style={{width:`${stats.b/stats.total*100}%`}}/>
                        </div>
                        <div className="text-[9.5px] text-ink-500 font-mono">{stats.s}/{stats.total}</div>
                      </div>
                    ) : <span className="text-[10px] text-ink-400">—</span>}
                  </td>
                  {/* Timeline + Comment */}
                  <td className="sticky right-0 bg-white group-hover:bg-ink-50/60 matrix-cell px-2 text-center"
                      style={{ width: 40, minWidth: 40 }}>
                    <div className="flex flex-col items-center gap-0.5">
                      <Tip tip="Timeline ทุกงวด"><button onClick={()=>setTimelineFor(sp)} className="w-5 h-5 rounded-md hover:bg-indigo-50 text-indigo-500"><Icon name="history" size={11}/></button></Tip>
                      <Tip tip="Comment / ประวัติ"><button onClick={()=>setDrawerFor(sp)} className="relative w-5 h-5 rounded-md hover:bg-ink-100 text-ink-500">
                        <Icon name="message" size={11}/>
                        {(comments[sp.id]||[]).length > 0 && <span className="absolute -top-0.5 -right-0.5 w-1.5 h-1.5 bg-rose-500 rounded-full"/>}
                      </button></Tip>
                    </div>
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>

      {/* Cell action popover (modal-style) */}
      <CellActionModal
        action={cellAction} onClose={()=>setCellAction(null)}
        onApply={(newStatus, commentText) => {
          const { sp, date, status } = cellAction;
          const stamp = nowStamp();
          const author = currentUser?.full_name || (currentRole==="admin" ? "ธนพร อัศวิน" : "ทีมงาน");
          const addCmt = (type, text) => {
            if (!text) return;
            setComments?.(prev => ({ ...prev, [sp.id]: [...(prev[sp.id]||[]), { id:"c"+Math.random().toString(36).slice(2,8), type, author_name:author, text, at:stamp, task_date: date }] }));
          };
          if (newStatus === "P") updateInstance(sp, date, { status:"P" }, `เปลี่ยนสถานะเป็น P (รอจัดทำ) — ${sp.name}`);
          else if (newStatus === "A") {
            updateInstance(sp, date, { status:"A" }, `เปลี่ยนสถานะเป็น A (รอตรวจสอบ) — ${sp.name}`);
            if (status === "C") addCmt("response", commentText); // R → A after correction
          }
          else if (newStatus === "S") updateInstance(sp, date, { status:"S" }, `เปลี่ยนสถานะเป็น S (สำเร็จ) — ${sp.name}`);
          else if (newStatus === "C") {
            updateInstance(sp, date, { status:"C" }, `เปลี่ยนสถานะเป็น C (มี comment) — ${sp.name}`);
            if (status === "A") addCmt("correction", commentText); // A → C reject with comment
            else if (status === "S") addCmt("cancel_success", `ยกเลิกสถานะสำเร็จ: ${commentText}`); // S → C cancel
          }
          toast({ kind:"success", msg:`อัปเดตสถานะวัน ${date} เป็น ${STATUS_META[newStatus].label} แล้ว` });
          setCellAction(null);
        }}
        users={users} positions={positions} comments={comments}
        currentRole={currentRole} myPositionIds={myPositionIds}
      />
    </div>
  );
};

// Permission helper — only R/A (or admin) may act on a given status
const canActOnCellStatic = (sp, status, currentRole, myPositionIds) => {
  if (currentRole === "admin") return true;
  if (currentRole === "viewer") return false;
  const isR = myPositionIds.includes(sp.R);
  const isA = myPositionIds.includes(sp.A);
  if (!isR && !isA) return false; // C / I → no action
  if (status === "blank") return isR || isA;
  if (status === "P") return isR;
  if (status === "A") return isA;
  if (status === "C") return isR;
  if (status === "S") return isA;
  return false;
};

// Inline cell action modal — choose what to set status to for a specific day
const CellActionModal = ({ action, onClose, onApply, users, positions, currentRole, myPositionIds, comments }) => {
  const [stage, setStage] = useState("choose"); // choose | comment
  const [chosenStatus, setChosenStatus] = useState(null);
  const [text, setText] = useState("");
  useEffect(() => { if (action) { setStage("choose"); setChosenStatus(null); setText(""); } }, [action]);
  if (!action) return null;
  const { sp, date, status, cell } = action;
  const m = STATUS_META[status];
  const rUser = users.find(u => u.positions.includes(sp.R));
  const aUser = users.find(u => u.positions.includes(sp.A));
  const canAct = canActOnCellStatic(sp, status, currentRole, myPositionIds);
  const myRoleOnSp = myPositionIds.includes(sp.R) ? "R" : myPositionIds.includes(sp.A) ? "A" : sp.C.some(id=>myPositionIds.includes(id)) ? "C" : sp.I.some(id=>myPositionIds.includes(id)) ? "I" : null;

  // Latest correction comment (shown in C → A response flow)
  const latestCorrection = (comments?.[sp.id] || []).filter(c => c.type === "correction").slice(-1)[0];

  const NextOptions = () => {
    // depending on status, suggest next moves; also allow override.
    const opts = [];
    if (status === "blank") opts.push({ key:"P", label:"เริ่มจัดทำ", tone:"blue" }, { key:"S", label:"บันทึกเป็นสำเร็จ (back-fill)", tone:"emerald" });
    if (status === "P") opts.push({ key:"A", label:"ส่งให้ผู้ตรวจสอบ", tone:"violet" });
    if (status === "A") opts.push({ key:"S", label:"อนุมัติเป็นสำเร็จ", tone:"emerald" }, { key:"C", label:"ส่งกลับให้แก้ไข", tone:"amber", needComment:true });
    if (status === "C") opts.push({ key:"A", label:"ส่งกลับให้ตรวจสอบอีกครั้ง", tone:"violet", needComment:true });
    if (status === "S") opts.push({ key:"C", label:"ยกเลิกสถานะสำเร็จ", tone:"amber", needComment:true });
    return opts;
  };
  const opts = NextOptions();
  const currentOpt = opts.find(o => o.key === chosenStatus);

  const toneClasses = {
    emerald:"bg-emerald-600 hover:bg-emerald-700 text-white",
    violet:"bg-violet-600 hover:bg-violet-700 text-white",
    amber:"bg-amber-500 hover:bg-amber-600 text-white",
    blue:"bg-blue-600 hover:bg-blue-700 text-white",
  };

  const handlePick = (o) => {
    if (o.needComment) {
      setChosenStatus(o.key);
      setStage("comment");
    } else {
      onApply(o.key);
    }
  };
  const submitComment = () => {
    if (!text.trim()) return;
    onApply(chosenStatus, text.trim());
  };

  // Comment field title/placeholder based on transition
  const commentMeta = (() => {
    if (!chosenStatus) return null;
    if (status === "A" && chosenStatus === "C") return { label:"วิธีการแก้ไข / สิ่งที่ต้องแก้", placeholder:"ระบุรายละเอียดที่ต้องการให้ผู้รับผิดชอบแก้ไข", title:"ส่ง Comment ให้แก้ไข", tone:"amber" };
    if (status === "C" && chosenStatus === "A") return { label:"วิธีการแก้ไข / คำตอบกลับ", placeholder:"อธิบายสิ่งที่ได้แก้ไขให้ผู้ตรวจสอบทราบ", title:"ตอบกลับให้ตรวจสอบ", tone:"violet" };
    if (status === "S" && chosenStatus === "C") return { label:"เหตุผลในการยกเลิกสถานะสำเร็จ", placeholder:"ระบุเหตุผลที่ต้องยกเลิกสถานะสำเร็จ", title:"ยกเลิกสถานะสำเร็จ", tone:"amber" };
    return { label:"Comment", placeholder:"", title:"ใส่ Comment", tone:"violet" };
  })();

  return (
    <Modal open onClose={onClose} title={sp.name} subtitle={`อัปเดตสถานะของวัน ${formatDateThai(date)}`} width={480}>
      <div className="space-y-4">
        <div className="rounded-xl border border-ink-100 bg-ink-50/60 p-3">
          <div className="grid grid-cols-2 gap-2 text-[12px]">
            <Kv2 k="วันที่" v={<span className="font-mono">{date}</span>}/>
            <Kv2 k="สถานะปัจจุบัน" v={<StatusPill status={status}/>}/>
            <Kv2 k="ผู้รับผิดชอบ (R)" v={rUser?.full_name || "—"}/>
            <Kv2 k="ผู้ตรวจสอบ (A)" v={aUser?.full_name || "—"}/>
            {myRoleOnSp && (
              <div className="col-span-2 pt-1.5 border-t border-ink-100">
                <div className="text-[10px] text-ink-500 uppercase tracking-wider">บทบาทของคุณในงานนี้</div>
                <div className="mt-1 flex items-center gap-2"><RaciBadge kind={myRoleOnSp} size="sm"/><span className="text-[12px] text-ink-800">{RACI_META[myRoleOnSp].label}</span></div>
              </div>
            )}
          </div>
          {cell?.updated && <div className="mt-2 text-[10.5px] text-ink-500 font-mono">อัปเดตล่าสุด: {cell.updated}</div>}
        </div>

        {canAct ? (
          stage === "choose" ? (
          <>
            <div>
              <div className="text-[11.5px] uppercase tracking-wider text-ink-500 font-semibold mb-2">เลือกสถานะที่ต้องการเปลี่ยนเป็น</div>
              <div className="grid gap-2">
                {opts.map(o => (
                  <button key={o.key} onClick={()=>handlePick(o)}
                    className={`flex items-center justify-between gap-3 px-3 py-2.5 rounded-lg ${toneClasses[o.tone]} font-medium text-[13px] transition`}>
                    <span className="flex items-center gap-2"><span className="w-6 h-6 bg-white/20 rounded font-bold flex items-center justify-center">{o.key}</span>{o.label}{o.needComment && <Badge color="dark" className="!bg-white/20 !text-white !border-white/20">ต้องระบุ comment</Badge>}</span>
                    <Icon name="arrowRight" size={13}/>
                  </button>
                ))}
                {opts.length === 0 && <div className="text-[12px] text-ink-500 italic">ไม่มีสถานะถัดไป</div>}
              </div>
            </div>
            <div className="text-[11px] text-ink-500 px-2.5 py-2 bg-amber-50 border border-amber-100 rounded-lg flex items-start gap-2">
              <Icon name="info" size={12} className="text-amber-600 mt-0.5"/>
              <span>การเปลี่ยนแปลงทั้งหมดจะถูกบันทึกใน Audit Log พร้อมระบุวันที่ของงวด</span>
            </div>
          </>
          ) : (
          <>
            {/* Comment input stage */}
            <button onClick={()=>{ setStage("choose"); setText(""); }} className="text-[11.5px] text-ink-500 hover:text-ink-800 flex items-center gap-1 mb-2"><Icon name="chevronLeft" size={11}/>ย้อนกลับ</button>
            <div className={`rounded-xl border p-3 mb-3 ${commentMeta.tone === "amber" ? "bg-amber-50 border-amber-200" : "bg-violet-50 border-violet-200"}`}>
              <div className="text-[12.5px] font-semibold text-ink-900">{commentMeta.title}</div>
              <div className="text-[11.5px] text-ink-600 mt-0.5">เปลี่ยนสถานะของวัน <span className="font-mono">{date}</span> · {sp.name}</div>
            </div>
            {/* show latest correction comment if this is C→A response */}
            {status === "C" && latestCorrection && (
              <div className="rounded-lg border border-amber-200 bg-amber-50 p-3 mb-3">
                <div className="text-[10.5px] uppercase tracking-wider text-amber-700 font-semibold flex items-center gap-1"><Icon name="message" size={11}/>Comment จากผู้ตรวจสอบ · {latestCorrection.author_name}</div>
                <div className="text-[12.5px] text-amber-900 mt-1">{latestCorrection.text}</div>
                <div className="text-[10.5px] font-mono text-amber-700 mt-1">{latestCorrection.at}</div>
              </div>
            )}
            <Field label={commentMeta.label} required>
              <Textarea rows={4} value={text} onChange={e=>setText(e.target.value)} placeholder={commentMeta.placeholder} autoFocus/>
            </Field>
            <div className="mt-3 flex items-center justify-end gap-2">
              <Button variant="secondary" onClick={()=>{ setStage("choose"); setText(""); }}>ยกเลิก</Button>
              <Button disabled={!text.trim()} onClick={submitComment} icon="check"
                className={commentMeta.tone === "amber" ? "!bg-amber-500 hover:!bg-amber-600" : "!bg-violet-600 hover:!bg-violet-700"}>
                ส่ง Comment และเปลี่ยนสถานะ
              </Button>
            </div>
          </>
          )
        ) : (
          <div className="rounded-xl border border-rose-200 bg-rose-50 px-4 py-4 text-center">
            <Icon name="ban" size={20} className="mx-auto text-rose-500 mb-1.5"/>
            <div className="text-[13px] font-semibold text-rose-800">คุณไม่มีสิทธิ์อัปเดตสถานะของงานนี้</div>
            <div className="text-[11.5px] text-rose-600 mt-1">
              {myRoleOnSp === "C" && "บทบาท C (Consulted) — เป็นเพียงข้อมูลว่าผู้ R ควรขอข้อมูลจากท่าน ไม่ต้องดำเนินการในระบบ"}
              {myRoleOnSp === "I" && "บทบาท I (Informed) — เป็นเพียงข้อมูลว่าผู้ R ต้องแจ้งให้ท่านทราบหลังเสร็จ ไม่ต้องดำเนินการในระบบ"}
              {!myRoleOnSp && "ท่านไม่ได้ถูกกำหนดเป็น R / A / C / I ในงานนี้"}
            </div>
            <div className="text-[11px] text-ink-600 mt-2.5">เฉพาะ <strong>R</strong> (ผู้รับผิดชอบ), <strong>A</strong> (ผู้ตรวจสอบ) และ <strong>Admin</strong> เท่านั้นที่อัปเดตสถานะได้</div>
          </div>
        )}
      </div>
    </Modal>
  );
};

const Kv2 = ({ k, v }) => (
  <div>
    <div className="text-[10px] text-ink-500 uppercase tracking-wider">{k}</div>
    <div className="text-ink-900 mt-0.5">{v}</div>
  </div>
);

window.CalendarStatusView = CalendarStatusView;
