<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');


$formname = "Print MRP Labels";
$action = "print_labels_fr.php";
mysqli_select_db($conn, $zdb);

include("includes/linkshead.php");
?>
<script type="text/javascript" src="MyScript/print_labels.js?a=<?= mt_rand() ?>"></script>
<style>
    /* GLOBAL */
    body {
        background: #f5f7fb;
        font-family: 'Segoe UI', Arial, sans-serif;
    }

    /* PANEL */
    .panel {
        border-radius: 10px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
        border: none;
    }

    .panel-heading {
        background: #2b3047 !important;
        color: #fff !important;
        border-radius: 10px 10px 0 0;
        padding: 12px 15px;
    }

    .panel-title {
        font-size: 16px;
        font-weight: 600;
    }

    /* INPUTS */
    .form-control {
        border-radius: 6px;
        border: 1px solid #dcdcdc;
        height: 34px;
    }

    .form-control:focus {
        border-color: #6366f1;
        box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
    }

    /* BUTTONS */
    .btn {
        border-radius: 6px;
        font-weight: 500;
        padding: 6px 14px;
    }

    /* ADD BUTTON */
    .btn-add {
        background: linear-gradient(135deg, #10b981, #059669);
        color: #fff;
        border: none;
    }

    .btn-add:hover {
        background: linear-gradient(135deg, #059669, #047857);
    }

    /* PRINT BUTTON */
    .btn-print {
        background: linear-gradient(135deg, #6366f1, #4f46e5);
        color: #fff;
        border: none;
    }

    .btn-print:hover {
        background: linear-gradient(135deg, #4f46e5, #4338ca);
    }

    /* REMOVE BUTTON */
   .btn-remove {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        background: #ffe4e4;
        color: #fb0402;
        border-radius: 8px;
        text-decoration: none;
        transition: 0.2s;
        position: relative;
        z-index: 10;
        pointer-events: auto;
        margin-left: 10px;
    }

    .btn-remove:hover {
        background: #fb0402;
        color: #fff;
        transform: scale(1.05);
    }

    /* TABLE */
    .table {
        border-radius: 8px;
        overflow: hidden;
        background: #fff;
    }

    .table th {
        color: #fff;
        font-weight: 500;
    }

    .table td {
        vertical-align: middle;
    }

    /* HEADER SECTION */
    .section-title {
        font-weight: 600;
        margin: 10px 0;
    }
</style>
</head>

<body>
    <form method='POST' action="" onsubmit="return prepareData();" id="mainForm">
        <!-- Default box -->

        <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php echo $formname; ?></h3>
                </div>

                <hr>

                <div class="row">
                    <div class="col-lg-3 text-right pad">
                        <label>Label Type</label>
                    </div>
                    
                    <div class="col-lg-3 pad">
                        <select id="layout" class="form-control select2" onchange="toggleSize()">
                            <option value="48">Small (48 Labels)</option>
                            <option value="16">Big (16 Labels)</option>
                        </select>
                    </div>
                </div>

                <div class="row">
                    <div class="col-lg-3 text-right pad">
                        <label>Product</label>
                    </div>

                    <div class="col-lg-6 pad">
                        <select id="product" class="form-control select2" onchange="fillMRP()">
                            <option value="">Select Product</option>
                            <?php
                            $prod = mysqli_query($conn, "SELECT code, name, rate FROM product ORDER BY name");
                            while ($p = mysqli_fetch_assoc($prod)) {
                                echo "<option value='{$p['code']}' data-name='{$p['name']}' data-mrp='{$p['rate']}'>{$p['name']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>
                
                <div class="row sizeRow" style="display:none;">
                    <div class="col-lg-3 text-right pad">
                        <label>Size</label>
                    </div>
                    
                    <div class="col-lg-2 pad">
                        <input type="text" id="size" class="form-control" placeholder="Size">
                    </div>
                </div>

                <div class="row">
                    <div class="col-lg-3 text-right pad">
                        <label>MRP</label>
                    </div>

                    <div class="col-lg-2 pad">
                        <input type="text" id="mrp" class="form-control" readonly>
                    </div>

                    <div class="col-lg-2 pad">
                        <input type="number" id="qty" class="form-control" value="1" min="1">
                    </div>

                    <div class="col-lg-2 pad">
                        <button type="button" class="btn btn-add" onclick="addItem()">
                            + Add
                        </button>
                    </div>
                </div>

                <hr>

                <h4>Selected Items</h4>

                <table class="table table-bordered" id="selectedTable">
                    <thead>
                        <tr class="mySearchTr">
                            <th>Product</th>
                            <th>MRP</th>
                            <th class="sizeRow" style="display: none;">Size</th>
                            <th>Qty</th>
                            <th>Remove</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>

                <!-- Hidden field to send data -->
                <input type="hidden" name="label_items" id="items_data">


                <div class="panel-footer text-center">
                    <button type="submit" class="btn btn-print" formtarget="_blank">
                        🖨 Print Labels
                    </button>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>