<!-- SAM RULES: include=../inc/header.php; base=/tenant; width=1280; js=jQuery+BS5 -->
<?php
// 모달 바디로 Ajax 로드되는 "중간검사서" 전용 페이지(본문 조각)
$title = $_POST['sheet_title'] ?? '중간 검사성적서';
$rowsJson = $_POST['rows'] ?? '[]';
$rows = json_decode($rowsJson, true) ?: [];
?>
<div class="p-3">

    <!-- 문서 헤더 -->
    <div class="border rounded mb-3">
        <div class="p-2 bg-light d-flex justify-content-between align-items-center">
            <div class="fw-bold"><?= htmlspecialchars($title) ?></div>
            <div class="small text-muted">문서번호: KD-QP-01-006</div>
        </div>
        <div class="p-3 small">
            <div class="row g-2">
                <div class="col-md-3"><span class="text-muted">품명</span><br><strong>스크린</strong></div>
                <div class="col-md-3"><span class="text-muted">제품 LOT NO</span><br><strong>자동입력</strong></div>
                <div class="col-md-3"><span class="text-muted">검사일자</span><br><strong>연도-월-일</strong></div>
                <div class="col-md-3"><span class="text-muted">검사자</span><br><strong>자동입력</strong></div>
            </div>
            <div class="row g-2 mt-2">
                <div class="col-md-3"><span class="text-muted">현장명</span><br><strong>자동입력</strong></div>
                <div class="col-md-3"><span class="text-muted">규격</span><br><strong>자동입력</strong></div>
                <div class="col-md-3"><span class="text-muted">결재</span><br>
                    <div class="d-flex gap-2">
                        <span class="badge text-bg-secondary">작성</span>
                        <span class="badge text-bg-secondary">검토</span>
                        <span class="badge text-bg-secondary">승인</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 검사표 -->
    <div class="border rounded">
        <div class="p-2 border-bottom bg-light fw-semibold">중간검사 결과</div>
        <div class="table-responsive">
            <table class="table table-sm align-middle m-0">
                <thead class="table-light">
                <tr>
                    <th style="width:70px;">일련</th>
                    <th style="width:140px;">원자재 로트</th>
                    <th style="width:150px;">자재명</th>
                    <th style="width:120px;">길이(mm)</th>
                    <th style="width:120px;">높이(mm)</th>
                    <th style="width:220px;">판정</th>
                </tr>
                </thead>
                <tbody id="miTbody">
                <?php foreach ($rows as $i => $r) { ?>
                    <tr data-idx="<?= $i ?>">
                        <td><?= htmlspecialchars($r['id'] ?? '') ?></td>
                        <td><?= htmlspecialchars($r['rawl ot'] ?? $r['rawlot'] ?? '') ?></td>
                        <td><?= htmlspecialchars($r['item'] ?? '') ?></td>
                        <td><input type="number" class="form-control form-control-sm mi-len" placeholder="mm"></td>
                        <td><input type="number" class="form-control form-control-sm mi-hei" placeholder="mm"></td>
                        <td>
                            <div class="d-flex gap-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="mi_<?= $i ?>" value="합격">
                                    <label class="form-check-label">합격</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="mi_<?= $i ?>" value="불합격">
                                    <label class="form-check-label">불합격</label>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
        <div class="p-2 small text-muted">※ 측정값은 기록용(저장 대상 아님), 판정만 상위 화면에 반영됩니다.</div>
    </div>

    <!-- 하단 버튼 -->
    <div class="d-flex justify-content-end gap-2 mt-3">
        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">닫기</button>
        <button type="button" class="btn btn-primary" id="btnApplyMid">적용</button>
    </div>
</div>

<script>
    (function(){
        // 적용 → 부모에 결과 전달
        document.getElementById('btnApplyMid').addEventListener('click', function(){
            const out = [];
            document.querySelectorAll('#miTbody tr').forEach(tr=>{
                const idx = +tr.getAttribute('data-idx');
                const res = tr.querySelector('input[type=radio]:checked');
                out.push({ idx, result: res ? res.value : null });
            });
            // 부모에 커스텀 이벤트 전달
            document.dispatchEvent(new CustomEvent('mid:apply', { detail: out }));
        });
    })();
</script>
