<?php
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Database connection constants
define('DB_HOST', 'localhost');
define('DB_USER', 'wiselie_nanda');
define('DB_PASS', 'Clemira123');
define('DB_NAME', 'wiselie_inventory');

// Establish database connection
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Check connection
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

// Include Picqer Barcode library
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;

// Initialize search variables
$searchPartNumber = '';
$searchName = '';

// Check if search form is submitted
if (isset($_GET['search'])) {
    $searchPartNumber = trim($_GET['part_number']);
    $searchName = trim($_GET['name']);
}

// Fetch all data from the generate_barcode table with search functionality
$query = "SELECT id, part_number, barcode, name, uom FROM generate_barcode WHERE 1=1"; // Tidak include quantity
$params = [];
$types = '';

// Add search conditions
if (!empty($searchPartNumber)) {
    $query .= " AND part_number LIKE ?";
    $params[] = '%' . $searchPartNumber . '%';
    $types .= 's';
}
if (!empty($searchName)) {
    $query .= " AND name LIKE ?";
    $params[] = '%' . $searchName . '%';
    $types .= 's';
}

// Prepare and execute the query
$stmt = $db->prepare($query);
if ($types) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();

$allData = [];
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $allData[] = $row;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage ITF-14 Carton (Offline Mode)</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        .reminder-box {
            background-color: #fff3cd;
            border-left: 5px solid #ffc107;
            padding: 15px;
            margin-bottom: 20px;
            font-weight: bold;
            font-size: 1.1em;
        }
        .quantity-input {
            max-width: 80px;
        }
        .print-btn {
            min-width: 80px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2 class="mt-4">Manage ITF-14 Carton (Offline Mode)</h2>
    
    <div class="reminder-box">
        ⚠️ PASTIKAN QUANTITY LABEL DAN ACTUAL DI KARDUS SUDAH SESUAI<br>
        ⚠️ QUANTITY TIDAK DISIMPAN KE DATABASE
    </div>

    <h3>Search Barcodes</h3>
    <form method="GET" class="mb-4">
        <input type="hidden" name="offline" value="1">
        <div class="form-row">
            <div class="form-group col-md-5">
                <label for="search_part_number">Part Number</label>
                <input type="text" name="part_number" id="search_part_number" class="form-control" value="<?php echo htmlspecialchars($searchPartNumber); ?>">
            </div>
            <div class="form-group col-md-5">
                <label for="search_name">Product Name</label>
                <input type="text" name="name" id="search_name" class="form-control" value="<?php echo htmlspecialchars($searchName); ?>">
            </div>
            <div class="form-group col-md-2">
                <label>&nbsp;</label>
                <button type="submit" name="search" class="btn btn-primary btn-block">Search</button>
            </div>
        </div>
    </form>

    <h3>All Generated ITF-14 Barcodes</h3>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Part Number</th>
                <th>ITF-14 Barcode</th>
                <th>Name</th>
                <th>Quantity</th>
                <th>UOM</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($allData)): ?>
                <?php foreach ($allData as $data): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($data['id']); ?></td>
                        <td><?php echo htmlspecialchars($data['part_number']); ?></td>
                        <td><?php echo htmlspecialchars($data['barcode']); ?></td>
                        <td><?php echo htmlspecialchars($data['name']); ?></td>
                        <td>
                            <input type="number" class="form-control quantity-input" 
                                   id="quantity_<?php echo $data['id']; ?>" 
                                   value="1" min="1" step="1">
                        </td>
                        <td><?php echo htmlspecialchars($data['uom']); ?></td>
                        <td>
                            <a href="print_itf14_offline.php?id=<?php echo $data['id']; ?>&quantity=1" 
                               class="btn btn-sm btn-secondary print-btn" 
                               target="_blank" 
                               data-id="<?php echo $data['id']; ?>">Print</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="7" class="text-center">No data available</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
    // Update print link with quantity when button is clicked
    $('.print-btn').click(function(e) {
        var id = $(this).data('id');
        var quantity = $('#quantity_' + id).val();
        $(this).attr('href', 'print_itf14_offline.php?id=' + id + '&quantity=' + quantity);
    });
});
</script>
</body>
</html>