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

if (empty($catID)) {
	$heading="Add New Location";
}
else {
	$heading="Edit Location";
}
$res=getResult("admin_details"," where user_type='admin'");

$record_type="Location";
$script_name="addeditlocation.php";
$redirect_script_name="manage-location.php";
$table_name="location_tbl";
#################### Add Record ##################

if($_REQUEST['action']=="add_record" && $catName!="") {
	$numCnt=getCount($table_name," where locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType' ");
	$count_cat = mysqli_query($dbconn,"SELECT * FROM $table_name");
	if(mysqli_num_rows($count_cat) >= $res['location_count']){
		$_SESSION['session_message']='<div class="btn btn-warning">'.$record_type." count exceeds the allowed limit[$res[location_count]]</div>";
	}else{
		if($numCnt>0) {
		$_SESSION['session_message']='<div class="btn btn-warning">'.$record_type.' already exist</div>';
	}
	else{

		$maxRes=db_fetch_array(db_query("select max(locationOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;

		$sql=db_query("insert into $table_name set parentID='$parentID', locationName='$catName',loc_url='$loc_url', locationType='$catgType',locationOrder='$catOrder'");
        //insert data in wps_meta
$insertedcatId = mysqli_insert_id($GLOBALS['dbconn']);
$sqlmeta = mysqli_query($GLOBALS['dbconn'], "insert into `wps_meta_tags` set 
entity_id='$insertedcatId',
page_table='$table_name',
page_url='$loc_url',
entity_type='index/market',
entity_name='market',
meta_title='$catName',
meta_description='$catName',
meta_keyword='$catName'
");
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' has been added successfully</div>';
		header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
		exit;
	}
	}
	
}

################ Update Record ##################

if($_REQUEST['action']=="update_record" && $catName!="" ) {
	$numCnt=getCount($table_name," where locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType'");
	if($numCnt>0) {
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' already exist</div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
		exit;
	}
		   //update meta tags
       mysqli_query($GLOBALS['dbconn'], "UPDATE wps_meta_tags SET 
    page_table='$table_name',
page_url='$loc_url',
entity_type='index/market',
entity_name='market',
meta_title='$catName',
meta_description='$catName',
meta_keyword='$catName'
       where entity_id='$catID' AND entity_name='market' AND  page_table='$table_name'");	
	updateTable($table_name," locationName='$catName', locationType='$catgType',loc_url='$loc_url' where locationID='$catID'");
    $_SESSION['session_message']='<div class="btn btn-warning">'.$record_type.' has been updated successfully</div>';
	header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);
	exit;
}
########################################################
admin_header();
$res=getResult($table_name," where locationID='$catID'");
?>
<div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="row">
          
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form name="formn"  class="forms-sample" method="POST" action=""  enctype="multipart/form-data">
                  <?=$msg!=''?$msg:'';?>
                  <div class="form-group">
                    <label>Location Name</label>
                    <input class="typeahead" type="text" name="catName" id="catName" value="<?=$res['locationName']?>" placeholder="location Name">
                </div>
                <div class="form-group">
   
	<label for="loc_url">Url </label>
	<input type="text" name="loc_url" id="loc_url" class="form-control" readonly value="<?= $res['loc_url'] ?>"
	placeholder="Url ">

</div>   
                  <?php
        if($res['locationID']=="") {
            ?>
            <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
            <input type="hidden" name="action" value="add_record">
    <?php
          } else {
            ?>
			 <button type="submit" name="update_record" class="btn btn-primary mr-2" value="Update <?=$record_type;?>">Update</button>
			 <input type="hidden" name="action" value="update_record">
    <?php
          }
          ?>
          <button class="btn btn-light" onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>
                 
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
       
        <script>
$(document).ready(function() {

  $('#catName').on('input blur', function() {
        let input = $(this).val();
        let inputResponse = removeSpecialChars(input);

        $('#loc_url').val(inputResponse);
    


    });
});
</script>
<?php
admin_footer();
exit;
?>
