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

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=0;
}

$heading="Manage Orders";
$record_type="Orders";
$script_name="manage-orders.php";
$table_name="orders_tbl";
#################### Add Record ##################
if($_REQUEST['action']=="add_record" && $catName!="") {
	$count_category=mysqli_query($dbconn,"select * from $table_name ");
	
		$sql=mysqli_query($dbconn,"select * from $table_name where catName='$catName' and parentID='$parentID'");
		$num=mysqli_num_rows($sql);
		if($num>0) {
		$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' already exist  </div>';
        
		}
		else{
			if($_FILES['image']['nam']!="") {
				$file_extension=strtolower(getExtension($_FILES['image']['name']));
				if($_FILES['image']['name']!="") {
					$img_name=$catName.date("s");
					$img=upload_file($img_name,"image","uploaded-files/category/images");
					
					$width="50";
					$height="50";
					$source_folder=SITE_DIR_PATH."/uploaded-files/category/images/";
					$destination_folder=SITE_DIR_PATH."/uploaded-files/category/images/thumbs/";
					saveResizedImage($img, $width, $height, $source_folder, $destination_folder);
				}
			}
			if($_FILES['banner']['name']!="") {
				$file_extension=strtolower(getExtension($_FILES['banner']['name']));
				if($_FILES['banner']['name']!="") {
					$banner_name=$catName."-banner".date("s");
					$banner=upload_file($banner_name,"banner","uploaded-files/category/banners");
				}
			}
			$maxRes=db_fetch_array(db_query("select max(catOrder) from $table_name where parentID='$parentID'"));
			$catOrder=$maxRes[0]+1;
			$sql=db_query("insert into $table_name set parentID='$parentID', catName='$catName', catDesc='$catDesc', catImage='$img', catOrder='$catOrder', catBanner='$banner'");
			$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
            
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
			exit;
		}
	

	
}

####################################################

if($action=="Update") {
	while (list($key,$val)=each($_POST[])) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			$qry1=db_query("update $table_name set catOrder='$val' where catID='$key1'");
		}
	}
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}


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 = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">e-KYC 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 . "&parentID=" . $parentID . "&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 = 1 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 . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['delete'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 2 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 . "&parentID=" . $parentID . "&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 = 1 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 . "&parentID=" . $parentID . "&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 status = 1 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 . "&parentID=" . $parentID . "&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 status = 0 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 . "&parentID=" . $parentID . "&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 status = 0 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 . "&parentID=" . $parentID . "&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 = 'Paid' 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 . "&parentID=" . $parentID . "&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 delivered = 1 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 . "&parentID=" . $parentID . "&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 dongle_dispatch = 1 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 . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
########################################################
admin_header();
$qry= mysqli_query($dbconn,"select * from $table_name where status!='2' order by order_id desc");	
?>

<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">Manage Orders</li>
                </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
					<?=print_message();?>
					<form action="" method="post">
                    <div class="table-responsive">
                        <table id="order-listing" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th class="_reab">
									<div class="form-check mt-0">
										<label class="form-check-label">
										<input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">
										<i class="input-helper"></i>
										</label>
                                    </div>
									</th>
                                    <th>Order No./User Info.</th>
                                    <th>Order Details</th>
                                    <th>Dongle</th>
                                    <th>DCS Status</th>
									<!--<th>Document</th>
									<th>Kyc</th>
                                    <th>Video</th>
                                    <th>Download</th>-->
									<th>Payment</th>
									<th>Status</th>
									<th>Invoice</th>	
                                </tr>
                            </thead>
                            <tbody>
                                <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) {
	$userid = $arr['user_id'];
	$userRes = getResult("customer_tbl"," where cid='$userid'");
	?>
	 
                                <tr>
                                    <td>
                                        <div class="form-check mt-0"><label class="form-check-label"><input
                                                    type="checkbox" class="form-check-input" name="arr_ids[]"
                                                    value="<?= $arr['order_id'] ?>"><i class="input-helper"></i></label>
                                        </div>
                                    </td>
                                    <!-- <td><?=$sl?></td> -->
                                    <td>
									 <p class="ordern"><?=$arr['invoice_no'];?></p>
                                    <p class="user-dcs"><span>User:</span>  <?= ($userRes['cname']) ? $userRes['cname'] : $arr['bill_name']; ?></p>
										<p class="mobile-dcs"><span>Mobile:</span> <?=($userRes['cmobile'])?$userRes['cmobile']:$arr['bill_mobile'];;?></p>
										<p class="email-dcs"><span>Email:</span> <?=($userRes['cemail'])?$userRes['cemail']:$arr['bill_email'];?></p>
										<?php if($arr['bill_gst']){?>
											<p class="gst-dcs"><span>GST:</span> <?=$arr['bill_gst'];?></p>
											<?php }?>
										<p class="address-dcs"><span>Address:</span> <?=$arr['bill_address'];?>,<?=$arr['bill_city'];?>,<?=$arr['bill_state'];?>,Pincode-<?=$arr['bill_zip'];?>,<?=$arr['bill_country'];?></p>	
										<?php if($arr['token_address']){?>
											<p class="token-address-dcs"><span>Token Delivery Address:</span> <?=$arr['token_address'];?>,<?=$arr['token_city'];?>,<?=$arr['token_state'];?>,Pincode-<?=$arr['token_zip'];?>,<?=$arr['token_country'];?></p>
										<?php }?>
									</td>
                                    <td>
									<p class="productid-dcs"><?=get_cat_name($arr['product_id']);?></p>
									<p class="brand-dcs"><?=($arr['brand_name'])?$arr['brand_name']:''?></p>
									<p class="total-am-dcs">₹ <?=$arr['total_amount'];?></p>
                                    <p class="rec-date-dcs"><?=$arr['reciveDate'];?></p>
									</td>
                                    <td>
                                     <p class="dongle-dcs">Included Dongle: <span> <?=($arr['dongle_price']>0)?'Yes':'NO';?></span></p>
                                      <p class="dispatched-dcs">  <?php if($arr['dongle_dispatch']){ echo "<b>Dispatched Status:</b> Dispatched";} ?></p>
									</td>
                                    <td>
               <div class="kyc-status-admin">                       
  <input type="checkbox" id="Document" name="Document" value="Document">
  <label for="Document"> Document</label>
      <select class="form-select mb-2 toggle-select" aria-label="Default select example">
        <option value="0" selected="">Pending</option>
        <option value="1">Done</option>                               
                   </select></div>
                            <div class="kyc-status-admin">                     
  <input type="checkbox" id="e-Kyc" name="e-Kyc" value="e-Kyc">
  <label for="e-Kyc"> e-Kyc</label>
        <select class="form-select mb-2 toggle-select" aria-label="Default select example">
        <option value="0" selected="">Pending</option>
        <option value="1">Done</option>                               
        </select>
                                        </div>
                                        <div class="kyc-status-admin">     
  <input type="checkbox" id="Video" name="Video" value="Video">
  <label for="Video"> Video</label>
        <select class="form-select mb-2 toggle-select" aria-label="Default select example">
        <option value="0" selected="">Pending</option>
        <option value="1">Done</option>                               
        </select>
                                        </div>
                                        <div class="kyc-status-admin">     
  <input type="checkbox" id="Download" name="Download" value="Download">
  <label for="Download"> Download</label>
        <select class="form-select mb-2 toggle-select" aria-label="Default select example">
        <option value="0" selected="">Pending</option>
        <option value="1">Done</option>                               
        </select>
                                        </div>



</td>
                                   <!-- <td><?php if($arr['aadhar_file']!='' || $arr['pan_file']!=''){ ?>
											Uploaded <br>
											<a href="<?=admin_url;?>view_document.php?orderID=<?=$arr['order_id']?>">View Document</a>
									<?php } else{
										echo "Pending";
									}?>
									</td>
                                    <td><?php if($arr['ekyc']>0){ ?>
											Done <br>
									<?php } else{
										echo "Pending";
									}?></td>
									<td><?php if($arr['video']>0){ ?>
											Done <br>
									<?php } else{
										echo "Pending";
									}?></td>
									<td><?php if($arr['download']>0){ ?>
											Done <br>
									<?php } else{
										echo "Pending";
									}?></td>-->
									<td>

                                    <p class="status-dcs"><?php if($arr['order_status']=='1') { ?><label class="badge badge-success badge-pill"><?=$arr['payment'];?></label><?php }
	   									else  { ?> <label class="badge badge-danger badge-pill">Pending</label>
                                        <?php } ?></p>
                                    </td>
									<td>
									<p class="status-dcs"><?php if($arr['status']=='1') { ?><label class="badge badge-success badge-pill">Active</label><?php }
	   									else if($arr['status']=='0') { ?> <label class="badge badge-danger badge-pill">In-Active</label>
                                        <?php } ?></p>
									</td>
                                    <td><p class="download-dcs"><a href="<?=site_url?>/invoice_download.php?or_id=<?=$arr[order_id]?>" download> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-download" viewBox="0 0 16 16"> <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/> <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/> </svg> </a></p> </td>
                                </tr>

                                <?php  $sl++; }?>

                            </tbody>
                        </table>
                    </div>
					<div class="btn-grp mt-2">
						<button type="submit" name="e-kyc" onclick="return select_chk()" class="btn btn-success btn-icon-text btn-sm">
						E-kyc Done
                        </button>
						<button type="submit" name="video" onclick="return select_chk()" class="btn btn-warning btn-icon-text btn-sm">
						Video Done
                        </button>
						<button type="submit" name="download" onclick="return select_chk()" class="btn btn-primary btn-icon-text btn-sm">
						Download Done
                        </button>
						<!-- <button type="submit" name="send_email" onclick="return select_chk()" class="btn btn-info btn-icon-text btn-sm">
						Send Email
                        </button> -->
						<button type="submit" name="status_active" onclick="return select_chk()" class="btn btn-success btn-icon-text btn-sm">
						Active
                        </button>
						<button type="submit" name="status_inactive" onclick="return select_chk()" class="btn btn-success btn-icon-text btn-sm">
						Inactive
                        </button>
						<button type="submit" name="payment_paid"  onclick="return select_chk()" class="btn btn-warning btn-icon-text btn-sm">
						Payment Paid
                        </button>
						<button type="submit" name="delivered" onclick="return select_chk()" class="btn btn-primary btn-icon-text btn-sm">
						Delivered
                        </button>
						<button type="submit"  name="dongle_dispatch" onclick="return select_chk()" class="btn btn-info btn-icon-text btn-sm">
						Dongle Dispatched
                        </button>
						<button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
							Delete
                        </button>
                        </div>
				</form>
                </div>
            </div>
        </div>
    </div>
</div>
<script language="javascript">

checked=false;
function checkall(frm1){
	var aa= frm1;
	if(checked == false){
		checked = true
	}
	else{
		checked = false
	}
	for (var i =0; i < aa.elements.length; i++){
		aa.elements[i].checked = checked;
	}
}

function select_chk(){
var chks = document.getElementsByName('arr_ids[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++){
if (chks[i].checked){
  hasChecked = true;
  break;
  }
}
if (hasChecked == false){
alert("Please Select At Least One.");
return false;
}
}
</script>
<?php
admin_footer();
exit;
?>
