<?php
// 모델관리
$CURRENT_SECTION = 'item';
include '../inc/header.php';
?>
    <div class="container" style="max-width:1280px; margin-top:24px;">
        <form id="modelForm" class="needs-validation" novalidate>
            <div class="row g-4">
                <!-- LEFT: form fields -->
                <div class="col-lg-8">
                    <div class="card">
                        <div class="card-header bg-white d-flex justify-content-between align-items-center">
                            <h2 class="h6 m-0">모델 기본정보</h2>
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="isActive" checked>
                                <label class="form-check-label" for="isActive">활성화</label>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row g-3">
                                <div class="col-md-4">
                                    <label class="form-label required" for="code">모델 코드</label>
                                    <input type="text" class="form-control" id="code" placeholder="예: MDL-2025-001" required>
                                    <div class="invalid-feedback">모델 코드를 입력하세요.</div>
                                </div>
                                <div class="col-md-8">
                                    <label class="form-label required" for="name">모델명</label>
                                    <input type="text" class="form-control" id="name" placeholder="예: 컨베이어 모듈 A" required>
                                    <div class="invalid-feedback">모델명을 입력하세요.</div>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label required" for="unit">기본 단위</label>
                                    <select id="unit" class="form-select" required>
                                        <option value="">선택...</option>
                                        <option>EA</option>
                                        <option>SET</option>
                                        <option>M</option>
                                        <option>KG</option>
                                        <option>BOX</option>
                                    </select>
                                    <div class="invalid-feedback">기본 단위를 선택하세요.</div>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label required" for="categoryName">카테고리</label>
                                    <div class="input-group">
                                        <input type="text" id="categoryName" class="form-control" placeholder="카테고리를 선택하세요" readonly required>
                                        <button type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#categoryModal"><i class="bi bi-diagram-3"></i> 선택</button>
                                    </div>
                                    <input type="hidden" id="categoryId" />
                                    <div class="invalid-feedback">카테고리를 선택하세요.</div>
                                </div>
                                <div class="col-12">
                                    <label class="form-label" for="description">설명</label>
                                    <textarea id="description" rows="3" class="form-control" placeholder="모델 설명을 입력"></textarea>
                                </div>
                                <div class="col-12">
                                    <label class="form-label" for="tagsInput">태그</label>
                                    <div id="tagsInput" class="tag-input">
                                        <input id="tagText" type="text" class="form-control border-0 p-0" placeholder="Enter로 태그 추가" />
                                    </div>
                                </div>
                                <div class="col-12">
                                    <label class="form-label">대표 이미지</label>
                                    <div id="imageDrop" class="dropzone">클릭 또는 드래그하여 선택</div>
                                    <input type="file" id="imageFile" accept="image/*" class="form-control mt-2" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="mt-4 d-flex justify-content-end gap-2">
                        <button type="reset" class="btn btn-outline-secondary"><i class="bi bi-arrow-counterclockwise"></i> 초기화</button>
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2-circle"></i> 등록</button>
                    </div>
                </div>

                <!-- RIGHT: preview -->
                <div class="col-lg-4">
                    <div class="card preview-card">
                        <div class="card-header bg-white">
                            <h2 class="h6 m-0">미리보기</h2>
                        </div>
                        <div class="card-body">
                            <div class="d-flex align-items-center gap-3 mb-3">
                                <div id="previewImg" class="bg-light border rounded" style="width:72px;height:72px; display:flex; align-items:center; justify-content:center;">
                                    <i class="bi bi-image fs-3 text-muted"></i>
                                </div>
                                <div>
                                    <div class="text-muted small" id="pvCode">CODE</div>
                                    <div class="fw-bold" id="pvName">모델명 미입력</div>
                                    <div class="small text-muted" id="pvCategory">카테고리 미선택</div>
                                </div>
                            </div>
                            <div id="pvTags" class="d-flex flex-wrap gap-1 mb-3"></div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <!-- Category Modal -->
    <div class="modal fade" id="categoryModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-diagram-3 me-2"></i>카테고리 선택</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <ul class="list-unstyled" id="catTree">
                        <li><label><input type="radio" name="cat" value="101" data-name="제품/모터" class="form-check-input me-2"> 제품 > 모터</label></li>
                        <li><label><input type="radio" name="cat" value="201" data-name="제품/컨베이어" class="form-check-input me-2"> 제품 > 컨베이어</label></li>
                        <li><label><input type="radio" name="cat" value="301" data-name="반제품/프레임" class="form-check-input me-2"> 반제품 > 프레임</label></li>
                    </ul>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">취소</button>
                    <button type="button" class="btn btn-primary" id="btnPickCategory">선택</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(function(){
            function updatePreview(){
                $('#pvCode').text($('#code').val() || 'CODE');
                $('#pvName').text($('#name').val() || '모델명 미입력');
                $('#pvCategory').text($('#categoryName').val() || '카테고리 미선택');
            }
            $('#code,#name,#categoryName').on('input change', updatePreview);

            $('#tagText').on('keydown', function(e){
                if(e.key==='Enter'){
                    e.preventDefault();
                    const tag=this.value.trim();
                    if(tag){
                        const chip=$(`<span class="tag-chip" data-tag="${tag}"><i class="bi bi-hash"></i>${tag}<i class="bi bi-x-lg remove"></i></span>`);
                        $('#pvTags').append(chip);
                        this.value='';
                    }
                }
            });
            $('#pvTags').on('click','.remove',function(){ $(this).parent().remove(); });

            $('input[name="cat"]').on('change', function(){
                $('#categoryId').val(this.value);
                $('#categoryName').val($(this).data('name')).trigger('change');
            });

            $('#imageFile').on('change', function(){
                const file=this.files[0];
                if(file){
                    const reader=new FileReader();
                    reader.onload=e=>$('#previewImg').html(`<img src="${e.target.result}" class="img-fluid rounded" style="max-width:72px; max-height:72px;">`);
                    reader.readAsDataURL(file);
                }
            });

            $('#imageDrop').on('click', ()=> $('#imageFile').trigger('click'));

            $('#modelForm').on('submit', function(e){
                e.preventDefault();
                if(!this.checkValidity()){ this.classList.add('was-validated'); return; }
                const payload={
                    code:$('#code').val(),
                    name:$('#name').val(),
                    unit:$('#unit').val(),
                    is_active:$('#isActive').is(':checked')?1:0,
                    category_id:$('#categoryId').val(),
                    description:$('#description').val(),
                    tags:$('#pvTags .tag-chip').map((i,e)=>$(e).data('tag')).get()
                };
                console.log('SAVE',payload);
                alert('모델 등록 데이터 콘솔 확인');
            });
        });
    </script>

<?php include '../inc/footer.php'; ?>
