<?php
require './partials/head.php';
?>
<body>
    <?php require './partials/sidebar.php'; ?>
    <main class="dashboard-main fade-in">
        <?php require './partials/header.php'; ?>
        <style>
            .card {
                margin-top: 20px;
            }

            .card-img-top {
                padding-left: 10px;
            }

            .cardd {
                padding-left: 15px;
                padding-right: 15px;
                background-color: white;
            }

            .fade-in {
                opacity: 0;
                animation: fadeIn 2s 1s forwards;
            }

            @keyframes fadeIn {
                to {
                    opacity: 1;
                }
            }
        </style>
        <div class="dashboard-main-body">
            <?php
            if (isset($_GET['request_Id'])) {
                $request_Id = $_GET['request_Id'];

                $query = "SELECT * FROM ship WHERE request_Id = ?";
                $stmt = $conn->prepare($query);
                $stmt->bind_param("s", $request_Id);
                $stmt->execute();
                $result = $stmt->get_result();
                $user = $result->fetch_assoc();

                if (!$user) {
                    echo "<div class='alert alert-danger' role='alert'>detail details not found.</div>";
                    exit;
                }

                $request_Id = $user['request_Id'];
            } else {
                echo "<div class='alert alert-danger' role='alert'>No shipping request specified.</div>";
                exit;
            }
            ?>

            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <div class="row">
                                <div class="col-md-12">
                                    <button type="button" class="btn btn-danger me-2 mb-2"
                                        onclick="confirmDeleteUserRequest('<?= $request_Id; ?>')">
                                        <i class="fas fa-trash"></i> Delete
                                    </button>
                                </div>
                            </div>
                        </div>

                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>Details</th>
                                            <th>Information</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <th>Date:</th>
                                            <td><?= $user['date']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Sender Name:</th>
                                            <td><?= $user['dname']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Sender Number:</th>
                                            <td><?= $user['dphone']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Sender Email:</th>
                                            <td><?= $user['demail']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Depature:</th>
                                            <td><?= $user['depature']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Destination:</th>
                                            <td><?= $user['destination']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Weight:</th>
                                            <td><?= $user['dweight']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Type:</th>
                                            <td><?= $user['type']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Message:</th>
                                            <td><?= $user['dmessage']; ?></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php require './partials/footer.php'; ?>
    </main>
    <script>
        function confirmDeleteUserRequest(request_Id) {
            Swal.fire({
                title: 'Are you sure?',
                text: 'This will delete the shipping Request record permanently.',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: 'No, cancel',
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        type: 'post',
                        url: '../controller/deleteship', 
                        data: {
                            request_Id: request_Id
                        },
                        dataType: 'json',
                        success: function(response) {
                            if (response.status === 'success') {
                                Swal.fire('Deleted!', 'The shipping Request record has been deleted.', 'success').then(() => {
                                    location.href = 'manage-request';
                                });
                            } else {
                                Swal.fire('Error!', response.message || 'Failed to delete record.', 'error');
                            }
                        },
                        error: function() {
                            Swal.fire('Error!', 'An error occurred. Please try again.', 'error');
                        },
                    });
                }
            });
        }
    </script>
    <style>
    .swal2-popup {
        width: 300px !important;
        padding: 20px !important;
    }

    .swal2-title {
        font-size: 18px !important;
    }

    .swal2-html-container {
        font-size: 14px !important;
    }

    .swal2-confirm {
        font-size: 14px !important;
    }

    .swal2-cancel {
        font-size: 14px !important;
    }
</style>
    <?php require './partials/scripts.php'; ?>
</body>

</html>