<?php

include 'load.php';

?>
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $order_id = $_POST['orderid'];
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard">
    <meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard">
    <meta name="author" content="Payroll | Pos & Invetory System | Dashboard">
    <meta name="robots" content="noindex, nofollow">

    <title>Home | Payroll | Pos & Invetory System | Dashboard</title>


    <?php
    require_once 'headercss.php';
    ?>

<script>
function validateReturnQuantity(maxQuantity, inputElement) {
    var returnQty = parseInt(inputElement.value);
    if (returnQty > maxQuantity) {
        alert("Return quantity cannot exceed available quantity.");
        inputElement.value = maxQuantity; // Set the input value to the maximum quantity
    }
}
</script>

</head>

<body>
    <div class="main-wrapper">
        <div class="page-wrapper">
            <div class="content">
                <?php


                $sql = "SELECT * FROM mobileorders mo INNER JOIN invoice_other_details iod ON iod.order_id=mo.order_id WHERE mo.orid = '$order_id'  ORDER BY mo.order_id DESC";

                $result = $conn->query($sql);
                $id = '';
                $sale_id = '';
                $customer_name = '';
                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        $id =  $row["orid"];
                        $date = $row['create_at'];
                        $customer_name = $row['customer'];
                        $sale_id = $row['order_id'];
                    }
                } else {
                    echo "0 results";
                }

                ?>
                <div id="global-loader">
                    <div class="whirly-loader"> </div>
                </div>

                <div class="main-wrapper">
                    <?php
                    require_once 'header.php';
                    ?>

                    <?php
                    require_once 'navbar.php';
                    ?>
                    <div class="row">
                        <div class="card">
                            <span style="font-size: 35px;"><b> </b>Sell Return</span>
                            <span style="font-size: 20px;">Order NO : <?php echo $id ?></span>
                            <span style="font-size: 20px">Date : <?php echo date("Y-m-d", strtotime($date)); ?></span>
                            <span style="font-size: 20px;">Customer :
                                <?php
                                if ($customer_name == "0") {
                                    echo '<a class="badges bg-lightgreen text-white">Walk-in Customer</a>';
                                } else if ($customer_name == "dc") {
                                    echo '<a class="badges bg-lightgreen text-white">Delivery Customer</a>';
                                } else {
                                    $id = $customer_name;
                                    $query = $DB_con->prepare('SELECT cname FROM mobilecustomers WHERE coid=' . $id);
                                    $query->execute();
                                    $result = $query->fetch();
                                    echo $result['cname'];
                                }
                                ?>

                            </span>
                        </div>
                    </div>
                </div>

                <div class="table-responsive">
                <form action="sell_return_process.php" method="post">   
                    <input type="hidden" value="<?php echo $id ?>" name="order_id">  
                    
                    <?php
                                if ($customer_name == "0") {
                                    echo '<input type="hidden" value="Walk-in Customer" name="customer_name">';
                                    
                                } else if ($customer_name == "dc") {
                                    echo '<input type="hidden" value="Delivery Customer" name="customer_name">';

                                } else {
                                    $cusid = $customer_name;
                                    $query = $DB_con->prepare('SELECT cname FROM mobilecustomers WHERE coid=' . $cusid);
                                    $query->execute();
                                    $result = $query->fetch();
                                    echo '<input type="hidden" value="' . $result['cname'] . '" name="customer_name">';
                                }
                                ?>
                    


                    <table class="table  sellreturn">
                        <thead>
                            <tr>
                                <th>#ID</th>
                                <th>Name</th>
                                <th>Amount</th>
                                <th>Qty</th>
                                <th>Return Qty</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php

                            $sale_id = $sale_id;

                            $sql = "SELECT *  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid WHERE mobiletemporarycart.orderid='$sale_id'  GROUP BY  mobiletemporarycart.proid,mobiletemporarycart.pro_dis   ORDER BY mobiletemporarycart.tempid  DESC";

                            $result = $conn->query($sql);
                            $rowCount = 0;
                            $pamount = 0;
                            $total_discount = 0;
                            $total_amount = 0;
                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {
                                    $rowCount = $rowCount + 1;
                                    $tempid = $row["tempid"];
                                    $proid = $row["proid"];
                                    $pro_name = $row["proname"];
                                    $cover = $row["cover"];
                                    $pro_price = $row["pro_price"];
                                    $pro_dis = $row["pro_dis"];
                                    $pro_qty = $row["pro_qty"];
                                    $pamount = ($pro_price * $pro_qty) - $pro_dis;
                                    $total_discount = $total_discount + $pro_dis;
                                    $total_amount = $total_amount + $pamount;

                            ?>
                                                    <tr>
                                        <td>#
                                            <?php echo $tempid; ?>
                                        </td>

                                        <td class="productimgname">

                                            <a class="product-img">
                                                <?php if ($cover == "") {
                                                    $pro_img = "assets/img/empty.png";
                                                } else {
                                                    $pro_img = "images/products/cover/" . $cover;
                                                } ?><img src="<?php echo $pro_img; ?>" alt="img" class="me-2" style="width:40px;height:40px;">
                                                <?php echo $pro_name; ?> | <?php echo number_format((float) $pro_price, 2); ?> X <?php echo number_format((int)$pro_qty); ?>
                                            </a>

                                        </td>
                                        <td>

                                            <?php echo number_format((float) $pro_price, 2); ?>

                                        </td>
                                        <td>

                                            <?php echo number_format((int)$pro_qty); ?>
                                            <input type="hidden" class="row_qty" value="<?php echo $pro_qty ?>">


                                        </td>
                                        <td>
                                        <!-- <input type="number" id="return_qty" name="products[<?php echo $proid; ?>][quantity]"> -->
                                        <input type="number" id="return_qty_<?php echo $proid; ?>" name="products[<?php echo $proid; ?>][quantity]" onchange="validateReturnQuantity(<?php echo $pro_qty; ?>, this)">


                                        </td>
                                        <td>

                                            <?php echo number_format((float)  $pamount, 2); ?>

                                        </td>
                                
                                <?php
                                }
                            }

                                ?>
                        </tbody>
                    </table>
                    <input type="submit" value="Submit" class="btn btn-primary">

                        </form>
                </div>

            </div>
        </div>
    </div>

    <script src="assets/js/jquery-3.6.0.min.js"></script>
    <script src="assets/js/feather.min.js"></script>
    <script src="assets/js/jquery.slimscroll.min.js"></script>
    <script src="assets/js/jquery.dataTables.min.js"></script>
    <script src="assets/js/dataTables.bootstrap4.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/plugins/select2/js/select2.min.js"></script>
    <script src="assets/js/moment.min.js"></script>
    <script src="assets/js/bootstrap-datetimepicker.min.js"></script>
    <script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
    <script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
    <script src="assets/alertify/alertify.js"></script>
    <script src="assets/alertify/alertify.min.js"></script>
    <script src="assets/js/script.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

    <script>
        $(document).ready(function() {
            if (!$.fn.DataTable.isDataTable('.sellreturn')) {
                $('.sellreturn').DataTable();
            }
        });
    </script>
    <!-- check row quantity -->





</body>

</html>