<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');


################ Paging Starts From Here ##################
@extract($_REQUEST);
$page = (isset($_REQUEST['page']) && $_REQUEST['page'] > 0)?$_REQUEST['page']:1;
$pagesize = isset($_GET['pagesize'])?$_GET['pagesize']:50;
$start = ($page-1)*$pagesize;


$heading="Edited Orders";
$record_type="Orders";
$script_name="manage-orders-edited.php";
$table_name="order_edit_history";



if (isset($_POST['e-kyc'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET ekyc = 'COMPLETED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">e-KYC has sdsbeen updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['video'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET video = 'COMPLETED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Video has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}


if (isset($_POST['download'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET download = 'COMPLETED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['status_active'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET order_status = 'COMPLETED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['status_inactive'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET order_status = 'PENDING' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['payment_paid'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET payment_status = 'SUCCESS' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['delivered'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET delivery_status = 'DELIVERED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['dongle_dispatch'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET delivery_status = 'DISPACHED' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
########################################################
admin_header();

?>


<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </h3>
        <nav aria-label="breadcrumb">
		<ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page"><?= $heading; ?></li>
                </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body p-1">

        <form action="" method="post">
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped">
                    <thead>
                        <tr>
                            <th style="width:2%;">#</th>
                            <th style="width:15%;">Date</th>
                            <th style="width:15%;">Order ID</th>
                            <th style="width:70%;">Old Order</th>
                            <th style="width:5%;">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $sl=$start+1;
                            if($_SESSION['MyAdminUserType'] == 'admin'){
                                $whr = ' WHERE id>0';
                            }else{
                                $whr = ' WHERE agentId='.$_SESSION['MyAdminUserSLNO'];
                            }
                            
                            $qry= mysqli_query($dbconn, "SELECT order_edit_history.*, (SELECT order_id FROM orders_tbl1 WHERE orders_tbl1.invoice_id=order_edit_history.invoice_id limit 1) AS order_id  FROM order_edit_history ".$whr." GROUP BY invoice_id having order_id>0 ORDER BY id DESC LIMIT ".$start.", ".$pagesize);
                            $sn = 1;
                            while($arr=mysqli_fetch_array($qry)) {
                        ?>
                        <tr>
                            <td><?= $sn++; ?></td>
                            <td><?= date('d M Y h:ia', strtotime($arr['created_at'])); ?></td>
                            <td><?= $arr['invoice_id']; ?></td>
                            <td><?= $arr['old_order']; ?></td>
                            <td><a href="<?= SITE_ADMIN_URL.'/manage-order-detail.php?orderid='.$arr['order_id']; ?>">View</a></td>
                            
                        </tr>

                        <?php  }?>

                    </tbody>
                </table>
                <div class="container">
                    <div class="row">
                        <div class="col-md-5">
                            <div class="dataTables_info" id="order-listing_info" role="status" aria-live="polite">Showing <?= $start+1; ?> to <?= $start+mysqli_num_rows($qry); ?> of <?= $row = mysqli_num_rows(mysqli_query($dbconn, "SELECT order_id FROM $table ".$whr))?> entries</div>
                        </div>
                        <div class="col-md-7">
                            <div class="dataTables_paginate paging_simple_numbers float-right">
                                <ul class="pagination">
                                    <li class="paginate_button page-item previous disabled" id="order-listing_previous">
                                        <a href="#" aria-controls="order-listing" data-dt-idx="0" tabindex="0" class="page-link">Previous</a>
                                    </li>
                                    <?php 
                                        $tpage = ceil($row/$pagesize);
                                        $prm = $_GET;
                                        unset($prm['page']);
                                        for($i=1; $i <= $tpage; $i++){
                                    ?>
                                    <li class="paginate_button page-item <?= ($i == $page)?'active':''?>">
                                        <a href="<?= $_SERVER['PHP_SELF']; ?>?page=<?= $i;?>&<?= http_build_query($prm)?>" aria-controls="order-listing" data-dt-idx="1" tabindex="0" class="page-link"><?= $i; ?></a>
                                    </li>
                                    <?php } ?>
                                    <li class="paginate_button page-item next disabled" id="order-listing_next">
                                        <a href="#" aria-controls="order-listing" data-dt-idx="2" tabindex="0" class="page-link">Next</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
            
        </form>
        </div>
    </div>
</div>


<?php admin_footer(); ?>

