<?php
/*
* date last modified 03-10-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - manage_staff.html.php
* Mysql Tables - tbl_admin, tbl_purchase_partner, tbl_admin_type
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
require_once('./includes/functions.php'); # general functions

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

if(isset($_GET['action']) && $_GET['action'] == 'delete') {
	/* Delete User */
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		$delete = mysqli_query($con,'DELETE FROM tbl_purchase_partner WHERE id='.$_GET['del_id']);
		if($delete) {
			$success = 'Deleted the shop';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'disable') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_purchase_partner SET status = 'Disabled' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Disabled the partner';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'activate') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_purchase_partner SET status = 'Active' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Activated the partner';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'make_partner') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_purchase_partner SET featured = 'yes' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Set as '.SITE_NAME.' Partner';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'rem_partner') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_purchase_partner SET featured = '' WHERE id='".$_GET['id']."'");
		//remove all partner categories as normal categories
		mysqli_query($con, "DELETE FROM tbl_partner_category WHERE partner_id = '".$_GET['id']."'");
		if($update) {
			$success = 'Removed from '.SITE_NAME.' Partner';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'mark_paid') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$curDate = date('Y-m-d');
		$insert = mysqli_query($con,"INSERT INTO tbl_shop_paid (shop_id, date, added_by) VALUES ('".$_GET['id']."', '".$curDate."', '".$_SESSION['adminId']."')");
		if($insert) {
			$success = 'Mark Paid to the shop';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}

$pageVars = '';
$showAssign = FALSE;

// Get All User details to list 
$where = ' id>0 ';

if(isset($_POST['clear'])) { ?>
<script language="javascript" type="text/javascript">			
	window.location='index.php?page=<?php echo $page;?>';
</script>
<?php }

if(isset($_REQUEST['searchvalue']) && strlen($_REQUEST['searchvalue'])>0) {
	$keyword = trim($_REQUEST['searchvalue']);
	$where.= " AND (name LIKE '%".$keyword."%' OR building_name LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR phone LIKE '%".$keyword."%' OR place LIKE '%".$keyword."%' OR mobile LIKE '%".$keyword."%') ";
	$pageVars.= '&searchvalue='.$keyword.'&';
}

if(isset($_REQUEST['status']) && strlen($_REQUEST['status'])>0) {
	$where .= " AND status='".$_REQUEST['status']."'";
	$status = $_REQUEST['status'];
	$pageVars.= '&status='.$status.'&';
}

if(isset($_REQUEST['paid']) && strlen($_REQUEST['paid'])>0) {
	$where .= " AND is_paid='".$_REQUEST['paid']."'";
	$paidStat = $_REQUEST['paid'];
	$pageVars.= '&paid='.$paidStat.'&';
}

$statRs = mysqli_query($con, "SELECT DISTINCT status FROM tbl_purchase_partner");


if(isset($_REQUEST['order_id']) && $_REQUEST['order_id']!='') {
	$orderId = trim($_REQUEST['order_id']);
	//we have to select shops near to this user
	$orderRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id FROM tbl_purchase_order WHERE id = '".$orderId."'"));
	$userId = $orderRec['user_id'];
	//get lat long of user
    $userRs = mysqli_query($con, "SELECT latitude, longitude FROM tbl_user WHERE id = '".$userId."'");
	if(mysqli_num_rows($userRs)>0){
		$userRec = mysqli_fetch_array($userRs);
		$userLat = $userRec['latitude'];
		$userLong = $userRec['longitude'];
		if($userLat!='' && $userLong!=''){		
			$select = "SELECT *,(((acos(sin((".$userLat."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$userLat."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$userLong."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance
	FROM `tbl_purchase_partner` ORDER BY distance ";
			$showAssign = TRUE;
		} else {
			$error = 'The user does not have a location (lat,long)';
		}
	}
} else {
	$select = "SELECT * FROM tbl_purchase_partner WHERE ".$where." ORDER BY id DESC";
}

//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(30);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);
?>