<?php

/*Import data into specific Template*/
global $wpdb;
$tablename = $wpdb->prefix.'geek_pricing_estimator_adons';
$CateAdonTablename = $wpdb->prefix.'geek_pricing_estimator_adons_category';

if(isset($_POST["addon_submit"])){

				if($_GET['eid'] !=''){
						
						// $wpdb->query($wpdb->prepare("UPDATE $table_name SET `plan_name`='$current_timestamp',  WHERE userid=$userid"));	

					$sql ="UPDATE $tablename
					    SET `name`= '".$_POST['name']."',
					    `ordering` = '".$_POST['ordering']."',					    
					    `adon_cate` = '".$_POST['adonscate']."',					    
					    `editable_qnty` = '".$_POST['editableqnty']."',					    
					    `note` = '".$_POST['note']."',					    
					    `singular` = '".$_POST['singular']."',					    
					    `plural` = '".$_POST['plural']."',					    
					    `print_description` = '".$_POST['print_description']."'					    
						WHERE  `id` = '".$_GET['eid']."'";

						if($wpdb->query($sql)){
							echo '<script>alert("'.$_POST['name'].' Add-On Updated successfully !!!!"); window.location.href="admin.php?page=gpesti-adons";</script>';
						}


				}else{

						$existServices = $wpdb->get_results("SELECT * FROM $tablename WHERE plan_name = '".$_POST['plan_name']."'");
						if($wpdb->num_rows > 0){
							
							echo '<div class="alert alert-success alert-dismissible">
								  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
								  <div><strong>Duplicate!</strong> "'.$_POST['plan_name'].'" already Exist.</div>
								</div>';
								
						}else{

							$InsertKeyword = $wpdb->insert( $tablename, array(
									'name' => $_POST['name'], 
									'ordering' => $_POST['ordering'], 
									'adon_cate' => $_POST['adonscate'], 
									'editable_qnty' => $_POST['editableqnty'], 
									'note' => $_POST['note'], 
									'singular' => $_POST['singular'], 
									'plural' => $_POST['plural'], 
									'print_description' => $_POST['print_description'], 
									'time' => time()),
									array( '%s', '%s', '%s') 
								);
							
							if($InsertKeyword){
								echo '<div class="alert alert-success alert-dismissible">
								  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
								  <div><strong>Success!</strong> "'.$_POST['name'].'" Added.</div>
								</div>';
							}


						}



				}





}
						

if($_GET['eid'] !=''){
	$EditData = $wpdb->get_results( "SELECT * FROM $tablename WHERE id='".$_GET['eid']."'" );	
}





?>
<style type="text/css">
	.chosen-container-multi{
		width:100% !important;
	}	
</style>

<div id="EstimateajaxLoder"><img src="<?php echo plugins_url(); ?>/geek-price-estimator/images/loder.gif" /></div>
<div class="container">
	<h1>Plans</h1>

	<div class="panel panel-info">
		<div class="panel-heading">Add Plans</div>
		<div class="panel-body">
			<form action="" method="post" enctype="multipart/form-data">
				<div class="row">
					<div class="form-group col-lg-6">
					  	<label >Add-on Name:</label>
						<input type="text" value="<?php echo $EditData[0]->name ; ?>" name="name" placeholder="Add-on Name" required class="form-control" />
					</div>

					<div class="form-group col-lg-6">
					  	<label >Ordering</label>
						<input type="number" min="1" step="1" value="<?php echo $EditData[0]->ordering ; ?>" name="ordering" placeholder="Enter Ordering" required class="form-control" />
					</div>
										
				</div>

				<div class="row">
					<div class="form-group col-lg-6">
					  	<label >Category:</label>
					  	<select class="form-control" name="adonscate" id="adonscate" style="max-width:100%;" >
					  		<option>--Select Category -- </option>
					  		<?php
								$AdonsCate = $wpdb->get_results("SELECT * FROM $CateAdonTablename ORDER BY id ASC");
								foreach($AdonsCate as $cate){
									echo '<option value="'.$cate->id.'">'.$cate->name.'</option>';
								}

					  		?>

					  	</select>						
					</div>

					<div class="form-group col-lg-6">
					  	<label >Editable Qnty:</label>
					  	<select class="form-control" name="editableqnty" id="editableqnty" style="max-width:100%;" >
					  		<option value="0"> No </option>
					  		<option value="1"> Yes </option>
					  		
					  	</select>						
					</div>

					
				</div>

				<div class="row">				
					<div class="form-group col-lg-12">
					  	<label >Note: <span style="color:red; font-size:12px;"><sup>*</sup>About This Add-on</span></label>						
						<?php wp_editor( $EditData[0]->note, 'note', $Editorsettings, 'title', true, 2 ) ?>

					</div>
				</div>

				<div class="row">
					<div class="form-group col-lg-6">
					  	<label >Singular:</label>
						<input type="text" value="<?php echo $EditData[0]->singular ; ?>" name="singular" placeholder="Singular" required class="form-control" />
					</div>

					<div class="form-group col-lg-6">
					  	<label >Plural</label>
						<input type="text"  value="<?php echo $EditData[0]->plural ; ?>" name="plural" placeholder="Enter Plural" required class="form-control" />
					</div>

					<div class="form-group col-lg-12">
					  	<label >Print Description: <span style="color:red; font-size:12px;"><sup>*</sup>Print Description</span></label>						
						<?php wp_editor( $EditData[0]->print_description, 'print_description', $Editorsettings, 'title', true, 2 ) ?>

					</div>
										
				</div>

					<div class="form-group col-lg-6 pull-right">
					  <input type="submit"  style="width:300px; margin-top:22px;" class="btn btn-primary pull-right" name="addon_submit" value="Submit">
					</div>
						
			</form>
		</div>
	</div>
</div>


<hr />

<div class="container">
	<div class="NoticeCtr"></div>
	<div class="CategoryPopupCtr">
		
	</div>
	
<!----------------Container Start ----------------------->	
	
	<select name="action" id="Estimatebulk-action-selector-top">
		<option value="">Bulk Actions</option>
		<option value="del">Delete</option>
	</select><input type="submit" id="EstimateDoAction" class="button" value="Apply">
	<table class="EstimateTable" style="text-align:center;">
		<thead>
			<tr>
				<th data-sorter="false"><input id="PPCCheckAll" class="EstimateChecBox" type="checkbox" name="" value=""></th>
				<th style="min-width:250px;" data-sorter="false">Name</th>			
				<th style="min-width:250px;" data-sorter="false">note</th>				
				<th style="max-width:250px;" data-sorter="false">Action</th>				
			</tr>			
		</thead>
		<tfoot>
		<tr>
			<th> </th>
			<th>Name </th>
			<th>note</th>
			<th>Action</th>				
		</tr>
			<td colspan="7">
				<div class="pager"> <span class="left">
					# per page:
					<a href="#">10</a> |
					<a href="#">50</a> |
					<a href="#">100</a> |
					<a href="#">500</a> |
					<a href="#">1000</a> 
				</span>
				<span class="right">
					<span class="prev">
						<img src="<?php echo plugins_url();?>/geek-price-estimator/includes/ppc-tablesorter/pager/icons/prev.png" /> Prev&nbsp;
					</span>
					<span class="pagecount"></span>
					&nbsp;<span class="next">Next
						<img src="<?php echo plugins_url();?>/geek-price-estimator/includes/ppc-tablesorter/pager/icons/next.png" />
					</span>
				</span>
				</div>
			</td>
		</tr>
	</tfoot>
		<tbody>
			<?php
				$results = $wpdb->get_results("SELECT * FROM $tablename ORDER BY id ASC");
				$StatusBtn ='';
				foreach($results as $Adondata){


					echo '<tr>
							<td><input dbID="'.$Adondata->id.'" id="EstiChecksingle" class="EstimateChecBox SingleEsticheck" type="checkbox" name="" value=""></td>
							
							<td><strong>'.$Adondata->name.'</strong></td>
							<td>'.$Adondata->note.'</td>
							<td><a href="admin.php?page=gpesti-adons&eid='.$Adondata->id.'" class="button"><span class="dashicons dashicons-edit"></span></a>
							 	<a DBName="'.$tablename.'" dbID="'.$Adondata->id.'" class="button EstimateSingledel"><span class="dashicons dashicons-trash"></span></a>
								<a href="admin.php?page=gpesti-adons&lpage=adonspricing&aid='.$Adondata->id.'&aname='.$Adondata->name.'" class="button"><span class="dashicons dashicons-plus-alt"></span> Add Pricing</a>
								<a href="admin.php?page=gpesti-adons&lpage=adonscomanagedpricing&aid='.$Adondata->id.'&aname='.$Adondata->name.'" class="button"><span class="dashicons dashicons-plus-alt"></span> Add Co Managed Pricing</a>
								 
							</td>										
						</tr>';
				}
			
			?>
			
		</tbody>
	</table>	
</div>

<?php if($_GET['eid']!=''){ ?>
<script type="text/javascript">
	jQuery(document).ready(function($){
		$('#adonscate').val('<?php echo $EditData[0]->adon_cate; ?>');
		$('#editableqnty').val('<?php echo $EditData[0]->editable_qnty; ?>');

	});
</script>
<?php } ?>