</div><!-- /page-body -->

    <div class="page-footer">
        Design &amp; Develop by <span style="color:#e91e8c;font-weight:800;">Com</span><span style="color:#00bcd4;font-weight:800;">transe</span> Technology Pvt.Ltd.
    </div>

</div><!-- /main-wrapper -->

<!-- Print CSS -->
<style>
/* Compact SweetAlert2 styling — applies app-wide (success/error/warning + delete confirm) */
.swal-compact {
    width: 330px !important;
    border-radius: 16px !important;
    padding: 1.4em 1em 1.6em !important;
    font-family: 'Segoe UI', Arial, sans-serif !important;
}
.swal-compact .swal2-icon {
    transform: scale(0.68);
    margin: -0.6em auto -0.7em !important;
}
.swal-compact .swal2-title {
    font-size: 17px !important;
    font-weight: 700 !important;
    padding: 0 !important;
    margin: 0 0 4px !important;
}
.swal-compact .swal2-html-container {
    font-size: 12.5px !important;
    margin: 0 !important;
    line-height: 1.45 !important;
}
.swal-compact .swal2-actions {
    margin-top: 1.1em !important;
}
.swal-compact .swal2-confirm,
.swal-compact .swal2-cancel {
    padding: 0.45em 1.3em !important;
    font-size: 13.5px !important;
    border-radius: 8px !important;
    box-shadow: none !important;
}

@media print {
    .sidebar,
    #sidebar,
    #sbOverlay,
    .top-header,
    .page-footer,
    .card-box-header,
    .dataTables_length,
    .dataTables_filter,
    .dataTables_info,
    .dataTables_paginate,
    .btn,
    .btn-edit,
    .btn-delete,
    th:last-child,
    td:last-child {
        display: none !important;
    }
    .main-wrapper,
    #mainWrapper {
        margin-left: 0 !important;
        padding: 0 !important;
        width: 100% !important;
    }
    .page-body {
        padding: 0 !important;
    }
    .card-box {
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
    }
    table {
        width: 100% !important;
        font-size: 12px !important;
        border-collapse: collapse !important;
    }
    table th,
    table td {
        border: 1px solid #999 !important;
        padding: 6px 10px !important;
        color: #000 !important;
    }
    table thead tr {
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    table thead th {
        color: #000 !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    table tbody tr:nth-child(even) {
        background: #f9f9f9 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    .no-print,
    .stat-card,
    form .row {
        display: none !important;
    }
    * {
        color: #000 !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    .badge-active,
    .badge-inactive {
        color: #000 !important;
        font-size: 12px !important;
        background: none !important;
        border: none !important;
        padding: 0 !important;
        box-shadow: none !important;
    }
    .badge-active i,
    .badge-inactive i {
        display: none !important;
    }
    a {
        color: #000 !important;
        text-decoration: none !important;
    }
    .print-title {
        display: block !important;
        font-size: 16px !important;
        font-weight: bold !important;
        margin-bottom: 10px !important;
        color: #000 !important;
    }
}
</style>

<!-- JS Libraries now loaded in layout/header.php <head> (so they're available
     before each page's inline scripts run). Not reloaded here to avoid
     double-loading jQuery/Bootstrap/etc. -->

<script>
/* ════════════════════════════════════════════
   SIDEBAR TOGGLE — Desktop: mini/full | Mobile: show/hide
════════════════════════════════════════════ */
var isMobile = () => window.innerWidth < 992;

(function() {
    if (!isMobile() && localStorage.getItem('sb_mini') === '1') {
        document.body.classList.add('sb-mini');
    }
})();

function toggleSidebar() {
    if (isMobile()) {
        var sb  = document.getElementById('sidebar');
        var ov  = document.getElementById('sbOverlay');
        var isOpen = sb.classList.contains('mob-open');
        sb.classList.toggle('mob-open', !isOpen);
        ov.classList.toggle('on', !isOpen);
    } else {
        var mini = document.body.classList.toggle('sb-mini');
        localStorage.setItem('sb_mini', mini ? '1' : '0');
    }
}

function closeMobileSidebar() {
    document.getElementById('sidebar').classList.remove('mob-open');
    document.getElementById('sbOverlay').classList.remove('on');
}

window.addEventListener('resize', function() {
    if (!isMobile()) {
        document.getElementById('sidebar').classList.remove('mob-open');
        document.getElementById('sbOverlay').classList.remove('on');
    }
});

/* ════════════════════════════════════════════
   SIDEBAR SUB-MENU SMART HANDLER
   - Mini mode (desktop): icon click → navigate directly to data-mini-href
   - Full mode: icon click → expand/collapse sub-menu
   - Mobile: always expand sub-menu
════════════════════════════════════════════ */
function handleSubMenu(el) {
    var isMiniDesktop = document.body.classList.contains('sb-mini') && !isMobile();

    if (isMiniDesktop) {
        // In mini mode: navigate to the page directly
        var href = el.getAttribute('data-mini-href') || el.getAttribute('href');
        if (href && href !== '#') {
            window.location.href = href;
            return false;
        }
    }

    // Full mode: toggle sub-menu
    var li = el.closest('li');
    // Close all others
    document.querySelectorAll('.has-sub.open').forEach(function(item) {
        if (item !== li) item.classList.remove('open');
    });
    li.classList.toggle('open');
    return false; // prevent default navigation in full mode
}

/* ════════════════════════════════════════════
   AUTO-OPEN ACTIVE SUB-MENU ON LOAD
════════════════════════════════════════════ */
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.has-sub').forEach(function(li) {
        if (li.querySelector('a.active')) {
            li.classList.add('open');
        }
    });
});

/* ════════════════════════════════════════════
   DATATABLES
════════════════════════════════════════════ */
$(function() {
    if ($('.dt-table').length) {
        $('.dt-table').each(function(){
            var $t = $(this);
            var emptyMsg = $t.data('empty-text') || 'No records found.';
            $t.DataTable({
                responsive: true,
                pageLength: 15,
                autoWidth: false,
                language: {
                    search: '',
                    searchPlaceholder: 'Search...',
                    emptyTable: emptyMsg,
                    paginate: { previous: '‹', next: '›' }
                },
                dom: "<'row mb-2'<'col-sm-6'l><'col-sm-6'f>>" +
                     "<'row'<'col-12'tr>>" +
                     "<'row mt-2'<'col-sm-5'i><'col-sm-7'p>>"
            });
        });
    }

    /* SELECT2 */
    if ($('.select2').length) {
        $('.select2').select2({ theme: 'bootstrap-5', width: '100%' });
    }

    /* DELETE CONFIRM */
    $(document).on('click', '.btn-delete-confirm', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        Swal.fire({
            title: 'Are you sure?',
            html: `
                <div style="font-size:15px;color:#6b7280">
                    You won't be able to revert this action.<br>
                    This will permanently delete the record.
                </div>
            `,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes, Delete!',
            cancelButtonText: 'Cancel',
            confirmButtonColor: '#ef4444',
            cancelButtonColor: '#9ca3af',
            reverseButtons: true,
            background: '#ffffff',
            customClass: {
                popup: 'swal-compact'
            },
            showClass: {
                popup: 'animate__animated animate__zoomIn'
            },
            hideClass: {
                popup: 'animate__animated animate__zoomOut'
            }
        }).then(function(r) {
            if (r.isConfirmed) {
                window.location.href = url;
            }
        });
    });

    /* FLASH MESSAGES — centered modal style (Varadvinayak CRM style) for save/update etc. */
    <?php if($this->session->flashdata('success')): ?>
    showProAlert('success', 'Success', '<?= $this->session->flashdata('success') ?>');
    <?php endif; ?>
    <?php if($this->session->flashdata('error')): ?>
    showProAlert('error', 'Error', '<?= $this->session->flashdata('error') ?>');
    <?php endif; ?>
    <?php if($this->session->flashdata('warning')): ?>
    showProAlert('warning', 'Warning', '<?= $this->session->flashdata('warning') ?>');
    <?php endif; ?>

    /* CARD-BOX entrance animation */
    $('.card-box, .stat-card').each(function(i) {
        var $el = $(this);
        $el.css({ opacity:0, transform:'translateY(16px)' });
        setTimeout(function() {
            $el.css({ transition:'opacity .35s ease, transform .35s ease', opacity:1, transform:'translateY(0)' });
        }, 60 + i * 55);
    });
});

/* ════════════════════════════════════════════
   PROFESSIONAL ALERT — app-wide, use everywhere
   instead of plain Swal.fire('Title','text','icon')
   so every popup in the system looks the same.
════════════════════════════════════════════ */
function showProAlert(icon, title, text) {
    var btnColor = (
        icon === 'success' ? '#22c55e' :
        icon === 'warning' ? '#f59e0b' :
        icon === 'info'    ? '#3b82f6' : '#ef4444'
    );
    return Swal.fire({
        icon: icon,
        title: title,
        text: text,
        confirmButtonText: icon === 'success' ? 'Great!' : 'OK',
        confirmButtonColor: btnColor,
        background: '#ffffff',
        color: '#1f2937',
        customClass: {
            popup: 'swal-compact'
        },
        showClass: {
            popup: 'animate__animated animate__zoomIn'
        },
        hideClass: {
            popup: 'animate__animated animate__zoomOut'
        },
        timer: icon === 'success' ? 2500 : undefined,
        timerProgressBar: icon === 'success',
        showConfirmButton: true
    });
}
</script>
<script>
var base_path = "<?php echo site_url(); ?>/";
</script>

<script>
/* ════════════════════════════════════════════
   ARROW KEY NAVIGATION — Items Table
   (Purchase Entry, Sales Billing, Quotation, Tailoring Order)
════════════════════════════════════════════ */
$(document).on('keydown', '#itemsTable input, #itemsTable select', function(e){
    var $this    = $(this);
    var $row     = $this.closest('tr.item-row');
    var $allRows = $('#itemsBody tr.item-row');
    var rowIdx   = $allRows.index($row);

    // Column order: product-sel(0), qty-inp(1), rate-inp(2), gst-inp(3)
    var focusCls = ['.product-sel', '.qty-inp', '.rate-inp', '.gst-inp'];
    var curCol   = -1;
    if($this.hasClass('product-sel')) curCol = 0;
    if($this.hasClass('qty-inp'))     curCol = 1;
    if($this.hasClass('rate-inp'))    curCol = 2;
    if($this.hasClass('gst-inp'))     curCol = 3;
    if(curCol === -1) return; // other inputs — skip

    function jumpTo(rIdx, cIdx){
        var $targetRow = $allRows.eq(rIdx);
        if(!$targetRow.length) return;
        var $el = $targetRow.find(focusCls[cIdx]);
        if(!$el.length) return;
        if($el.hasClass('select2-hidden-accessible')){
            $el.select2('focus');
        } else {
            $el.focus().select();
        }
    }

    switch(e.key){
        case 'ArrowRight':
            e.preventDefault();
            if(curCol < 3)             jumpTo(rowIdx, curCol + 1);
            else if(rowIdx < $allRows.length - 1) jumpTo(rowIdx + 1, 0);
            break;

        case 'ArrowLeft':
            e.preventDefault();
            if(curCol > 0)  jumpTo(rowIdx, curCol - 1);
            else if(rowIdx > 0) jumpTo(rowIdx - 1, 3);
            break;

        case 'ArrowDown':
            e.preventDefault();
            if(rowIdx < $allRows.length - 1) jumpTo(rowIdx + 1, curCol);
            break;

        case 'ArrowUp':
            e.preventDefault();
            if(rowIdx > 0) jumpTo(rowIdx - 1, curCol);
            break;

        case 'Enter':
            // Tab सारखे — पुढच्या cell ला जा; last row वर नवीन row add करा
            e.preventDefault();
            if(curCol < 3){
                jumpTo(rowIdx, curCol + 1);
            } else if(rowIdx < $allRows.length - 1){
                jumpTo(rowIdx + 1, 0);
            } else {
                // Last cell, last row — नवीन row add करा
                $('#addRow').trigger('click');
                setTimeout(function(){
                    var $newRows = $('#itemsBody tr.item-row');
                    var $newRow  = $newRows.last();
                    var $el      = $newRow.find(focusCls[0]);
                    if($el.hasClass('select2-hidden-accessible')){
                        $el.select2('focus');
                    } else {
                        $el.focus();
                    }
                }, 120);
            }
            break;

        case 'Tab':
            if(!e.shiftKey){
                e.preventDefault();
                if(curCol < 3) jumpTo(rowIdx, curCol + 1);
                else if(rowIdx < $allRows.length - 1) jumpTo(rowIdx + 1, 0);
            } else {
                e.preventDefault();
                if(curCol > 0) jumpTo(rowIdx, curCol - 1);
                else if(rowIdx > 0) jumpTo(rowIdx - 1, 3);
            }
            break;
    }
});
</script>


<?php if(isset($page_js)): ?><script src="<?php echo base_url(); ?>assets/createJs/<?php echo $page_js; ?>"></script><?php endif; ?>

</body>
</html>