<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/functions.php'); # general functions
$advanceAmount = 0;

$catRs = mysqli_query($con, "SELECT id, name FROM tbl_product_categories WHERE parent_id = '0' ORDER BY name");

if(isset($_REQUEST['shop_id']) && $_REQUEST['shop_id']!=''){	
	$shopId = mysqli_real_escape_string($con, trim($_REQUEST['shop_id']));	
	$editCatArray = array();		
	$editRs = mysqli_query($con, "SELECT * FROM tbl_purchase_partner_categories WHERE partner_id = '".$shopId."'");
	if(mysqli_num_rows($editRs)>0){
		$editRec = mysqli_fetch_array($editRs);
		$categories = $editRec['categories'];
		$editCatArray = explode(',', $categories);
	}
	//get location of shop
	$shopRec = mysqli_fetch_array(mysqli_query($con, "SELECT place FROM tbl_purchase_partner WHERE id = '".$shopId."'"));
	$shopPlace = $shopRec['place'];
	
	//make partner of thos category on this location
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='make_partner'){
		if(isset($_REQUEST['cat_id']) && $_REQUEST['cat_id']!='' && $shopPlace!=''){
			$catId = trim($_REQUEST['cat_id']);
			$insert = mysqli_query($con, "INSERT INTO tbl_partner_category (partner_id, category_id, location) VALUES ('".$shopId."', '".$catId."', '".$shopPlace."')");
			if($insert){
				mysqli_query($con, "UPDATE tbl_purchase_partner SET featured = 'yes' WHERE id = '".$shopId."'");
				$success = 'The shop has been set as a partner at '.$shopPlace;
			}
		}		
	}
	
	//remove from partner
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='rem_partner'){
		if(isset($_REQUEST['cat_id']) && $_REQUEST['cat_id']!=''){
			$catId = trim($_REQUEST['cat_id']);
			$delete = mysqli_query($con, "DELETE FROM tbl_partner_category WHERE partner_id = '".$shopId."' AND category_id = '".$catId."'");
			if($delete){
				//check any cat exist in tbl_partner_category. if not remove featured status
				$shopCatRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_partner_category WHERE id = '".$shopId."'"));
				$shpCatCount = $shopCatRec['count'];
				if($shpCatCount>0){
					//ok
				} else {
					mysqli_query($con, "UPDATE tbl_purchase_partner SET featured = '' WHERE id = '".$shopId."'");
				}
				$success = 'The shop has been removed from partner for the category';
			}
		}		
	}
	
	
	
} else {
	?>
	        <script language="javascript" type="text/javascript">	
	        	alert('Something went wrong');		
				window.location='index.php?page=manage_purchase_partner';
			</script>
	<?php
}



if(isset($_POST['submit'])) {
	$catString = '';
		
	$catArray = array();
	if (isset($_POST['category']) && is_array($_POST['category'])) {
	    foreach($_POST['category'] as $categoryId){
	      $catArray[] = $categoryId;
	    }
	}
	if(count($catArray)>0){
		$catString = implode(',', $catArray);
	} 
		
	if(empty($error)) {	
		//check if rec exist. if so update
		$chkRs = mysqli_query($con, "SELECT id FROM tbl_purchase_partner_categories WHERE partner_id = '".$shopId."'");
		if(mysqli_num_rows($chkRs)>0){
			$execInsert = mysqli_query($con, "UPDATE tbl_purchase_partner_categories SET categories  = '".$catString."', added_by = '".$_SESSION['adminId']."', added_on = NOW() WHERE partner_id = '".$shopId."'");
		} else {
			//insert new rec
			$insertQry = "INSERT INTO tbl_purchase_partner_categories(partner_id, categories, added_by, added_on) VALUES (
			'".mysqli_real_escape_string($con, $shopId)."',
			 '".mysqli_real_escape_string($con, $catString)."',
			 '".$_SESSION['adminId']."',
			 NOW())";
			$execInsert = mysqli_query($con,$insertQry);	
		}		
					
		if($execInsert) {					
			$success = 'Categories Assigned Successfully';
			header('Location:index.php?page=shop_assign_category&shop_id='.$shopId.'&success='.$success);
			exit();												
		}								
	} 
}







?>