<?php
require("header.php")
?>
<style>
    .btn-group>.btn:not(:last-child):not(.dropdown-toggle) {
        background: black;
    }

    .btn-group>.btn-group:not(:first-child)>.btn {
        background: black;
    }

    .page-item.active .page-link {
        background: black;
    }

    .dt-button-collection .dropdown-menu .buttons-columnVisibility {
        background-color: black;
    }
</style>
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <!-- <h1>Pending Order</h1> -->
                </div>
                <div class="col-sm-6">
                    <!-- <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item active">Pending Order</li>
                    </ol> -->
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card card-secondary">
                        <div class="card-header" style="background-color: black;">
                            <h3 class="card-title">Pending Orders</h3>
                            <ol class="breadcrumb float-sm-right"  style="background-color: black;">
                                <li class="breadcrumb-item"><a href="dashboad.php">Home</a></li>
                                <li class="breadcrumb-item active">Pending Orders</li>
                            </ol>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">

                            <table id="example1" class="table table-bordered table-striped">
                                <!-- <table id="example1" class="table table-hover table-bordered results"> -->
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>User Id</th>
                                        <th>Order ID</th>
                                        <th>Vendor</th>
                                        <th>Tracking</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                    <!-- <tr class="warning no-result">
                                      <td colspan="4"><i class="fa fa-warning"></i> No result</td>
                                    </tr> -->
                                </thead>
                                <tbody>
                                    <?php
                                    include('connection.php');

                                    $sql = "SELECT * FROM new_order WHERE status IN ('In transit', 'Label created', 'Pending') ORDER BY date_time";
                                    $res = mysqli_query($conn, $sql);
                                    while ($row = mysqli_fetch_assoc($res)) {
                                    ?>
                                        <tr>
                                            <td>
                                                <?php echo $row['date_time']; ?>
                                            </td>
                                            <td>
                                                <a href="#" class="user-id-linking" style="color:blue ;font-weight:600;" data-toggle="modal" data-target="#modal-lg-id<?php echo $row['user_id']; ?>">
                                                    <?php echo $row['user_id']; ?>
                                                </a>
                                            </td>

                                            <td>
                                                <a href="#" class="user-id-link" style="color:blue ;font-weight:600;" class="btn btn-default" data-toggle="modal" data-target="#myModal<?php echo $row['order_id']; ?>">

                                                    <?php echo $row['order_id']; ?>
                                                </a>
                                            </td>
                                            <td>
                                                <?php echo $row['vendor']; ?>
                                            </td>
                                            <td style="display: flex;">
                                                &nbsp;&nbsp;
                                                <?php echo $row['traking']; ?>&nbsp;&nbsp;
                                                <a href="https://tools.usps.com/go/TrackConfirmAction?tRef=fullpage&tLc=2&text28777=&tLabels=<?php echo $row['traking']; ?>" target="_blank">
                                                    <small class="badge badge-warning">Status</small>
                                                    <a>

                                            </td>
                                            <td>
                                                <?php echo $row['status']; ?>
                                            </td>
                                            <td>
                                                <a href="#" class="view-order" data-toggle="modal" data-target="#exampleModalCenter" data-orderid="<?php echo $row['order_id']; ?>">
                                                    <i style="color:red;" class="nav-icon fas fa-eye"></i>
                                                </a>
                                            </td>

                                        </tr>
                                        <!-- order id demo modal -->

                                        <div class="modal fade" id="myModal<?php echo $row['order_id']; ?>" role="dialog">
                                            <div class="modal-dialog modal-lg">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h6 class="modal-title float-left" style="font-weight:600; text-transform:uppercase">Order
                                                            Id:<?php echo $row['order_id']; ?> </h6>
                                                        <button type="button" class="close" data-dismiss="modal">&times;</button>

                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="row">
                                                            <div class="col-6">
                                                                <span style="font-weight:600; text-transform:uppercase">User
                                                                    name: </span> <span style="color:green">
                                                                    <?php echo $row['user_id']; ?>
                                                                </span>
                                                            </div>
                                                            <div class="col-6">
                                                                <span style="font-weight:600; text-transform:uppercase;float:right;">Added
                                                                    by: <span style="color:green;text-transform: none;font-weight:400;">
                                                                        Sadmin
                                                                    </span></span>
                                                            </div>
                                                        </div>
                                                        <hr>
                                                        <div class="row">

                                                            <div class="col-6">
                                                                <ul style="list-style-type: none">
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Name:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['name']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Address:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['address']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>City:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['city']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Zip
                                                                        Code: <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['zipcode']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Product
                                                                        Name: <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['product_name']; ?></span>
                                                                    </li>
                                                                </ul>
                                                            </div>
                                                            <div class="col-6">
                                                                <ul style="list-style-type: none">
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Origin:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['origin']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>State:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['state']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Country:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['country']; ?></span>
                                                                    </li>
                                                                    <li style="font-weight:600; text-transform:uppercase">
                                                                        <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Quantity:
                                                                        <span style="color:blueviolet;margin-left:1%;text-transform: none;font-weight:400;"><?php echo $row['quantity']; ?></span>
                                                                    </li>
                                                                </ul>
                                                            </div>

                                                        </div>
                                                        <div class="row">

                                                            <form action="pending_order_code.php" method="post">
                                                                <div class="col-4">
                                                                    <div class="form-group">
                                                                        <label for="traking" class="col-form-label" style="font-weight:600; text-transform:uppercase">
                                                                            Tracking ID</label>
                                                                        <input type="text" class="form-control" name="traking" value="<?php echo $row['traking']; ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-4">
                                                                    <div class="form-group">
                                                                        <input type="hidden" id="id" name="id" value="<?php echo $row['id']; ?>">
                                                                        <label for="vendor" class="col-form-label" style="font-weight:600; text-transform:uppercase">Vendor</label>
                                                                        <select class="form-control" name="vendor">
                                                                            <option value="">Select Vendor</option>
                                                                            <!-- Khali option -->
                                                                            <?php
                                                                            $sql1 = "SELECT * FROM vendor ORDER BY vendor_name";
                                                                            $res1 = mysqli_query($conn, $sql1);
                                                                            while ($row1 = mysqli_fetch_assoc($res1)) {
                                                                                $selected = ($row1['vendor_name'] == $row['vendor']) ? "selected" : "";
                                                                            ?>
                                                                                <option value="<?php echo $row1['vendor_name']; ?>" <?php echo $selected; ?>>
                                                                                    <?php echo $row1['vendor_name']; ?>
                                                                                </option>
                                                                            <?php } ?>
                                                                        </select>
                                                                    </div>
                                                                </div>

                                                                <div class="col-4">
                                                                    <div class="form-group">
                                                                        <label for="status" class="col-form-label" style="font-weight:600; text-transform:uppercase">Status</label>
                                                                        <select class="form-control" name="status">
                                                                            <option value="Pending" <?php echo ($row['status'] == "Pending") ? "selected" : ""; ?>>
                                                                                Pending</option>
                                                                            <option value="Label created" <?php echo ($row['status'] == "Label created") ? "selected" : ""; ?>>
                                                                                Label created</option>
                                                                            <option value="In transit" <?php echo ($row['status'] == "In transit") ? "selected" : ""; ?>>
                                                                                In transit</option>
                                                                            <option value="Delivered" <?php echo ($row['status'] == "Delivered") ? "selected" : ""; ?>>
                                                                                Delivered</option>
                                                                            <option value="Wrong Address" <?php echo ($row['status'] == "Wrong Address") ? "selected" : ""; ?>>
                                                                                Wrong Address</option>
                                                                            <option value="Order canceled" <?php echo ($row['status'] == "Order canceled") ? "selected" : ""; ?>>
                                                                                Order canceled</option>
                                                                        </select>
                                                                    </div>
                                                                </div>

                                                                <div class="modal-footer justify-content-between">
                                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                                    <input type="submit" class="btn btn-primary" name="submit" value="Submit">
                                                                </div>
                                                            </form>

                                                        </div>
                                                    </div>

                                                </div>
                                            </div>
                                            <!-- order id demo modal -->

                                        <?php } ?>

                                </tbody>


                                <!-- User ID Modal -->
                                <?php
                                $sql = "SELECT * FROM user ORDER BY date_time";
                                $res = mysqli_query($conn, $sql);

                                while ($row = mysqli_fetch_array($res)) {
                                ?>
                                    <div class="modal fade" id="modal-lg-id<?php echo $row['user_id']; ?>" style="display: none;" aria-hidden="true">
                                        <div class="modal-dialog modal-lg">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h6 class="modal-title float-left">
                                                        <?php
                                                        echo '<span style="font-weight: 600; text-transform: uppercase">Added by : &nbsp;</span>';
                                                        $addedByChain = [];
                                                        $currentUser = $row;
                                                        while (!empty($currentUser['added_by'])) {
                                                            $addedByChain[] = $currentUser['added_by'];
                                                            $sql = "SELECT * FROM user WHERE user_id = '{$currentUser['added_by']}'";
                                                            $result = mysqli_query($conn, $sql);
                                                            $currentUser = mysqli_fetch_assoc($result);
                                                        }
                                                        $addedByChain = array_reverse($addedByChain);
                                                        foreach ($addedByChain as $addedByUser) {
                                                            echo '<span style="color: white;text-transform: none;">' . $addedByUser . '</span>';
                                                            echo '&nbsp;<i class="fa fa-chevron-right" aria-hidden="true" style="font-size: 12px;"></i>&nbsp;';
                                                        }
                                                        echo '<span style="text-transform: none;">' . $row['user_id'] . '</span>';
                                                        ?>
                                                    </h6>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">×</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="row">

                                                        <div class="col-12">
                                                            <span style="float:center;font-weight:600; text-transform:uppercase">Created On&nbsp;:
                                                                &nbsp;</span> <span style="color:green">
                                                                <?php echo $row['date_time']; ?>
                                                            </span>
                                                        </div>

                                                    </div>
                                                    <br>

                                                    <div class="row">

                                                        <div class="col-6">
                                                            <ul style="list-style-type: none">
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Name: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['user_name']; ?>
                                                                    </span></li>
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>City: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['city']; ?>
                                                                    </span></li>
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Password: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['password']; ?>
                                                                    </span></li>
                                                            </ul>
                                                        </div>
                                                        <div class="col-6">
                                                            <ul style="list-style-type: none">
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Email: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['user_email']; ?>
                                                                    </span></li>
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Country: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['country']; ?>
                                                                    </span></li>
                                                                <li style="font-weight:600; text-transform:uppercase"> <i class="fa fa-chevron-right" aria-hidden="true" style="margin-right: 2%;font-size: 11px;"></i>Security Answer: <span style="color:blueviolet;text-transform: none;margin-left:1%;font-weight:400;">
                                                                        <?php echo $row['answer']; ?>
                                                                    </span></li>
                                                            </ul>
                                                        </div>

                                                    </div>


                                                </div>

                                            </div>

                                        </div>

                                    </div>
                                <?php } ?>
                                <!-- modal end -->
                            </table>
                        </div>

                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>


<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Order Details</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container" id="order-details">


                </div>

                <!--<div class="modal-footer">-->
                <!--    <button type="button" class="btn btn-dark" data-dismiss="modal">Close</button>-->
                <!--    <button id="copyButton" type="button" value="copy"-->
                <!--        class="btn btn-secondary buttons-copy buttons-html5" tabindex="0" aria-controls="example1">Copy-->
                <!--        Data</button>-->

                <!--</div>-->
            </div>
        </div>
    </div>
</div>
<!-- modal end  -->




<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

<script>
    // Add your SweetAlert2 code here
    <?php if (isset($_GET['error'])) { ?>
        Swal.fire({
            title: 'Error',
            text: '<?php echo $_GET['error']; ?>',
            icon: 'error',
            confirmButtonText: 'OK',
            confirmButtonClass: 'btn btn-success btn-lg',
        });
    <?php } ?>
    <?php if (isset($_GET['fail'])) { ?>
        Swal.fire({
            title: 'Error',
            text: '<?php echo $_GET['fail']; ?>',
            icon: 'error',
            confirmButtonText: 'OK',
            confirmButtonClass: 'btn btn-success btn-lg',
        });
    <?php } ?>
    <?php if (isset($_GET['success'])) { ?>
        Swal.fire({
            title: 'Success',
            text: '<?php echo $_GET['success']; ?>',
            icon: 'success',
            confirmButtonText: 'OK',
            confirmButtonClass: 'btn btn-success btn-lg',
        });
    <?php } ?>
</script>

<script>
    $(document).ready(function() {
        $('.view-order').click(function() {
            var orderID = $(this).data('orderid');
            $.ajax({
                url: 'get_order_details.php', // Update the URL to the file that fetches order details
                type: 'POST',
                data: {
                    orderid: orderID
                },
                success: function(response) {
                    $('#order-details').html(response);
                }
            });
        });
    });
</script>

<?php
require("footer.php")
?>