<?php
include_once "../../includes/connectConfig.php";

if(isset($_POST))
{
	if(isset($_POST['mkup_bannerstatus'])){
		$b_bn = test_input($_POST['urecm']);
		
		$chk_rc = getOne("ktbannerset"," bn_id = '$b_bn' ","bn_id");
		if(!empty($chk_rc)){
			
			$chk_gps = getOne("ktbannerset"," bn_id <> '' ORDER BY position DESC ","position");
			
			$chk_ps = getOne("ktbannerset"," bn_id = '$b_bn' ","position");
			if(empty($chk_ps) || $chk_ps == 0){
				
				$nw_ps = ($chk_gps + 1);
				$snd = $db_con->prepare("UPDATE ktbannerset SET position = '$nw_ps' WHERE bn_id = '$chk_rc' "); $snd->execute();
				
				$output["runstt"] = "grant";
				$output["errorreason"] = "Banner Status Updated";
			}
			else{
				$snd = $db_con->prepare("UPDATE ktbannerset SET position = '0' WHERE bn_id = '$chk_rc' "); $snd->execute();
				
				$output["runstt"] = "grant";
				$output["errorreason"] = "Banner Disabled";
			}
		}
		else{
			$output["runstt"] = "warant";
			$output["errorreason"] = "Can't Find Record, Try Again.";
		}
		echo json_encode($output);
		exit;
	}
	else{
		
	$valid_extensions = array('png','jpg','jpeg');

	$namea = $_FILES['pics_a']['name'];
	$sizea = $_FILES['pics_a']['size'];
	$extsa = strtolower(pathinfo($namea, PATHINFO_EXTENSION));
	
	
	$namep = $_FILES['pics_ap']['name'];
	$sizep = $_FILES['pics_ap']['size'];
	$extsp = strtolower(pathinfo($namep, PATHINFO_EXTENSION));

	if(!empty($namea) && !(in_array($extsa, $valid_extensions)) || $sizea > 5000000)
	{
		$output["runstt"] = "warant";
		$output["errorform"] = "a";
		$output["errorreason"] = "Check file type or size";
		echo json_encode($output);
		exit;
	}
	elseif(!empty($namep) && !(in_array($extsp, $valid_extensions)) || $sizep > 5000000)
	{
		$output["runstt"] = "warant";
		$output["errorform"] = "p";
		$output["errorreason"] = "Check file type or size";
		echo json_encode($output);
		exit;
	}
	else
	{
		$folder = "../../kmart-banner/";  
		
		$b_cp = (!empty($_POST['capbns'])) ? test_input($_POST['capbns']) : NULL;
		$ln_kbns = (!empty($_POST['lnkbns'])) ? test_input($_POST['lnkbns']) : NULL;
		$b_bn = test_input($_POST['valbana']);
		
		$chk_rc = getOne("ktbannerset"," bn_id = '$b_bn' ","bn_id");
		if(!empty($chk_rc)){
			$imga = getOne("ktbannerset"," bn_id = '$b_bn' ","bannername");
			if(!empty($namea)){
				$tmpa = $_FILES['pics_a']['tmp_name'];
				list($txta, $exta) = explode(".", $namea);
				$imga = strtolower(substr(md5(uniqid(rand())), 0, 7 ).'_'.rand(1000,100000).'.'.$exta);
				$filePatha = $folder.'/'.$imga;
				move_uploaded_file($tmpa, $filePatha);
			}
			
			$imgp = getOne("ktbannerset"," bn_id = '$b_bn' ","phonebanner");
			if(!empty($namep)){
				$tmpp = $_FILES['pics_ap']['tmp_name'];
				list($txtp, $extp) = explode(".", $namep);
				$imgp = strtolower(substr(md5(uniqid(rand())), 0, 7 ).'_'.rand(1000,100000).'.'.$extp);
				$filePathp = $folder.'/'.$imgp;
				move_uploaded_file($tmpp, $filePathp);
			}
			
			$snd = $db_con->prepare("UPDATE ktbannerset SET bannername =:revpost , phonebanner=:phnbnn, maincap=:cappost,  prdurl =:lkpost WHERE bn_id = '$chk_rc' ");
			$snd->bindParam(':revpost',$imga); $snd->bindParam(':phnbnn',$imgp); $snd->bindParam(':cappost',$b_cp);$snd->bindParam(':lkpost',$ln_kbns); $snd->execute();
			
			$output["runstt"] = "grant";
			$output["errorreason"] = "Data Saved";
		}
		else{
			$output["runstt"] = "warant";
			$output["errorreason"] = "Can't Find Record, Try Again.";
		}
		echo json_encode($output);
		exit;	
	}
	
	
	}
	
}	
?>
