<?php
$CURRENT_SECTION = 'process';
include '../inc/header.php';
?>
    <div class="container" style="max-width:1280px; margin-top:24px;">
        <div class="d-flex align-items-center justify-content-between mb-1">
            <h2 class="m-0">공정: <strong>스크린</strong> 작업 지시 설정</h2>
            <div class="d-flex gap-2">
                <a href="/tenant/process/processes.php" class="btn btn-outline-secondary">공정 목록</a>
                <button class="btn btn-primary">저장</button>
            </div>
        </div>
        <p class="text-muted small">※ 이 화면은 실제 작업 지시 생성 시 노출될 <b>동적 필드</b>와 <b>투입 자재</b> 템플릿을 정의합니다.</p>

        <ul class="nav nav-tabs" id="setTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#fields" type="button" role="tab">작업 지시용 필드 설정</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="tab" data-bs-target="#items" type="button" role="tab">투입 품목/자재 설정</button>
            </li>
        </ul>

        <div class="tab-content pt-3">
            <!-- 필드 설정 -->
            <div class="tab-pane fade show active" id="fields" role="tabpanel">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <strong>필드 목록</strong>
                        <button id="btn-add-field" type="button" class="btn btn-sm btn-outline-secondary">+ 필드 추가</button>
                    </div>
                    <div class="table-responsive">
                        <table id="fields-table" class="table table-bordered table-striped m-0">
                            <thead class="table-light">
                            <tr>
                                <th style="width:16%;">필드명</th>
                                <th style="width:12%;">유형</th>
                                <th style="width:10%;">필수</th>
                                <th>기본값/계산식</th>
                                <th style="width:14%;">유효성</th>
                                <th>설명</th>
                                <th style="width:8%;">삭제</th>
                            </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                    <div class="card-footer text-muted small">
                        유형 예: 텍스트, 숫자, 숫자(mm), 날짜, 선택(셀렉트), 체크박스 등<br>
                        유효성 예: text, number, integer, decimal, regex 등
                    </div>
                </div>
            </div>

            <!-- 품목/자재 설정 -->
            <div class="tab-pane fade" id="items" role="tabpanel">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <strong>투입 품목/자재</strong>
                        <button id="btn-add-item" type="button" class="btn btn-sm btn-outline-secondary">+ 품목 추가</button>
                    </div>
                    <div class="table-responsive">
                        <table id="items-table" class="table table-bordered table-striped m-0">
                            <thead class="table-light">
                            <tr>
                                <th style="width:20%;">품목명</th>
                                <th style="width:14%;">자재 유형</th>
                                <th style="width:16%;">서브 자재 포함</th>
                                <th>설명</th>
                                <th style="width:8%;">삭제</th>
                            </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                    <div class="card-footer text-muted small">
                        자재 유형 예: 주 자재, 서브 자재<br>
                        서브 자재 포함 여부가 O이면, 자식 자재를 후속 단계에서 매핑(계층화)합니다.
                    </div>
                </div>
            </div>
        </div>
    </div>

    <template id="field-row-tpl">
        <tr>
            <td><input type="text" class="form-control form-control-sm" placeholder="필드명"></td>
            <td>
                <select class="form-select form-select-sm">
                    <option>텍스트</option><option>숫자</option><option>숫자(mm)</option>
                    <option>날짜</option><option>선택(셀렉트)</option><option>체크박스</option>
                </select>
            </td>
            <td class="text-center">
                <input class="form-check-input required-chk" type="checkbox">
            </td>
            <td><input type="text" class="form-control form-control-sm" placeholder="예: qty * set_count 또는 고정값"></td>
            <td>
                <select class="form-select form-select-sm">
                    <option>text</option><option>number</option><option>integer</option>
                    <option>decimal</option><option>regex</option>
                </select>
            </td>
            <td><input type="text" class="form-control form-control-sm" placeholder="참고 설명"></td>
            <td class="text-center"><button type="button" class="btn btn-sm btn-danger btn-remove-field">삭제</button></td>
        </tr>
    </template>

    <template id="item-row-tpl">
        <tr>
            <td><input type="text" class="form-control form-control-sm" placeholder="품목명"></td>
            <td>
                <select class="form-select form-select-sm">
                    <option>주 자재</option><option>서브 자재</option>
                </select>
            </td>
            <td class="text-center"><input class="form-check-input has-sub" type="checkbox"></td>
            <td><input type="text" class="form-control form-control-sm" placeholder="설명"></td>
            <td class="text-center"><button type="button" class="btn btn-sm btn-danger btn-remove-item">삭제</button></td>
        </tr>
    </template>

    <script>
        (function(){
            const seedFields = [
                {name:'일련번호', type:'텍스트', required:true, def:'', valid:'text', desc:'작업 식별번호'},
                {name:'층', type:'숫자', required:false, def:'', valid:'integer', desc:'작업 층수'},
                {name:'호수', type:'숫자', required:false, def:'', valid:'integer', desc:'작업 호수'},
                {name:'제작 가로사이즈', type:'숫자(mm)', required:true, def:'', valid:'decimal', desc:'가로 길이(mm)'},
                {name:'제작 세로사이즈', type:'숫자(mm)', required:true, def:'', valid:'decimal', desc:'세로 길이(mm)'},
                {name:'매수', type:'숫자', required:true, def:'자동 계산식 있음', valid:'integer', desc:'주문 매수'}
            ];
            const seedItems = [
                {name:'절곡판', type:'주 자재', hasSub:true, desc:'주요 작업 재료'},
                {name:'조인트바', type:'서브 자재', hasSub:false, desc:'절곡판에 부속되는 자재'}
            ];

            function addFieldRow(p){
                const tpl = document.getElementById('field-row-tpl').content;
                const node = document.importNode(tpl,true);
                const tds = node.querySelectorAll('td');
                tds[0].querySelector('input').value = p?.name || '';
                tds[1].querySelector('select').value = p?.type || '텍스트';
                tds[2].querySelector('.required-chk').checked = p?.required || false;
                tds[3].querySelector('input').value = p?.def || '';
                tds[4].querySelector('select').value = p?.valid || 'text';
                tds[5].querySelector('input').value = p?.desc || '';
                node.querySelector('.btn-remove-field').addEventListener('click', e=> e.target.closest('tr').remove());
                document.querySelector('#fields-table tbody').appendChild(node);
            }
            function addItemRow(p){
                const tpl = document.getElementById('item-row-tpl').content;
                const node = document.importNode(tpl,true);
                const tds = node.querySelectorAll('td');
                tds[0].querySelector('input').value = p?.name || '';
                tds[1].querySelector('select').value = p?.type || '주 자재';
                tds[2].querySelector('.has-sub').checked = p?.hasSub || false;
                tds[3].querySelector('input').value = p?.desc || '';
                node.querySelector('.btn-remove-item').addEventListener('click', e=> e.target.closest('tr').remove());
                document.querySelector('#items-table tbody').appendChild(node);
            }

            seedFields.forEach(addFieldRow);
            seedItems.forEach(addItemRow);
            document.getElementById('btn-add-field').addEventListener('click', ()=>addFieldRow());
            document.getElementById('btn-add-item').addEventListener('click', ()=>addItemRow());
        })();
    </script>
<?php include '../inc/footer.php'; ?>
