<?php
$page_title = 'View Duplicate Details';
require_once('includes/load.php');
page_require_level(3);

// Function to get details of the duplicate based on resi, part_number, and quantity
function get_duplicate_details($resi, $part_number, $quantity) {
    global $db;
    $resi = $db->escape($resi);
    $part_number = $db->escape($part_number);
    $quantity = $db->escape($quantity);

    $sql = "SELECT * FROM awb 
            WHERE resi = '$resi' 
            AND part_number = '$part_number' 
            AND quantity = '$quantity'";
    return find_by_sql($sql);
}

// Handle delete request
if (isset($_GET['delete'])) {
    $resi = $db->escape($_GET['resi']);
    $part_number = $db->escape($_GET['part_number']);
    $quantity = $db->escape($_GET['quantity']);

    // Delete the duplicate records
    $sql = "DELETE FROM awb 
            WHERE resi = '$resi' 
            AND part_number = '$part_number' 
            AND quantity = '$quantity'";
    $db->query($sql);

    $session->msg('s', "Duplicates deleted successfully.");
    redirect('check_double.php', false);
}

$resi = isset($_GET['resi']) ? $_GET['resi'] : '';
$part_number = isset($_GET['part_number']) ? $_GET['part_number'] : '';
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : '';

$details = get_duplicate_details($resi, $part_number, $quantity);

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <h3 class="panel-title">Duplicate Details</h3>
            </div>
            <div class="panel-body">
                <?php if (!empty($details)): ?>
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Resi</th>
                            <th>Part Number</th>
                            <th>Product Name</th>
                            <th>Date Upload</th>
                            <th>Quantity</th>
                            <th>Scan In</th>
                            <th>Scan Status</th>
                            <th>Packer No</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($details as $detail): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($detail['resi']); ?></td>
                                <td><?php echo htmlspecialchars($detail['part_number']); ?></td>
                                <td><?php echo htmlspecialchars($detail['product_name']); ?></td>
                                <td><?php echo htmlspecialchars($detail['date_scan']); ?></td>
                                <td><?php echo htmlspecialchars($detail['quantity']); ?></td>
                                <td><?php echo isset($detail['scan_in']) ? htmlspecialchars($detail['scan_in']) : 'N/A'; ?></td>
                                <td><?php echo isset($detail['scan_status']) ? htmlspecialchars($detail['scan_status']) : 'N/A'; ?></td>
                                <td><?php echo htmlspecialchars($detail['packer_no']); ?></td>
                                <td>
                                    <a href="view_details.php?delete=true&resi=<?php echo urlencode($detail['resi']); ?>&part_number=<?php echo urlencode($detail['part_number']); ?>&quantity=<?php echo urlencode($detail['quantity']); ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete these duplicates?');">
                                        Delete
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>No details available.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>

<!-- Add Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
