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

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=1;
}
$heading="Manage Brands";
$record_type="Brands";
$script_name="manage_brands.php";
$table_name="brand_tbl";

if($id=="add_record") {

$file_name_array = [];
	if(count($_FILES['image']) > 0){
        if($_FILES['image']['name']!=''){
		foreach($_FILES["image"]["tmp_name"] as $key=>$tmp_name) {
		    $file_name=$_FILES["image"]["name"][$key];
		    $file_tmp=$_FILES["image"]["tmp_name"][$key];
		    $ext=pathinfo($file_name,PATHINFO_EXTENSION);   
		    $filename=basename($file_name,$ext);
		    $newFileName=$filename.$ext;
		    move_uploaded_file($file_tmp=$_FILES["image"]["tmp_name"][$key],"../uploaded-files/brands/".$newFileName);
		    array_push($file_name_array, $newFileName);
		}
		$file_name1 = implode(',', $file_name_array);
        }


	}
	if(count($_FILES['image2']) > 0){
        if($_FILES['image2']['name']!=''){
		foreach($_FILES["image2"]["tmp_name"] as $key=>$tmp_name) {
		    $file_name=$_FILES["image2"]["name"][$key];
		    $file_tmp=$_FILES["image2"]["tmp_name"][$key];
		    $ext=pathinfo($file_name,PATHINFO_EXTENSION);   
		    $filename=basename($file_name,$ext);
		    $newFileName=$filename.$ext;
		    move_uploaded_file($file_tmp=$_FILES["image2"]["tmp_name"][$key],"../uploaded-files/brands/".$newFileName);
		    array_push($file_name_array, $newFileName);
		}
		$file_name2 = implode(',', $file_name_array);
        }


	}

    $sql=db_query("insert into $table_name set brandName='$brandName', brandTitle='$brandTitle', brandKeyword='$brandKeyword',brandDesc='$brandDesc', brandHeading='$brandHeading',brandImage='$file_name1', brandImage2='$file_name2', brandDetail='$brandDetail', brandDetail2='$brandDetail2'");
    
		$_SESSION['session_message']='<div class="btn btn-success">New '.$record_type.' has been added successfully</div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;

	}
	

	if(isset($_REQUEST['id']) && $_REQUEST['id']=='delete'){

	$name = $_REQUEST['name'];
	$cat_id = $_REQUEST['cat_id'];
	$delete_sql = mysqli_query($dbconn,"SELECT * FROM $table_name WHERE product_id='$cat_id'");
	$result = mysqli_fetch_array($delete_sql);
	if(count($result) > 0){
		$image_array = explode(',',$result['brandImage']);
		if(in_array($name, $image_array)){
			$new_array = array_diff($image_array, [$name]);
			$file_name = implode(',',$new_array);
			$table = mysqli_query($dbconn,"UPDATE $table_name SET brandImage='$file_name' WHERE product_id='$cat_id'");
		}	
	}
	
}
if($id=="update_record") {
		$prod_sql = mysqli_query($dbconn,"SELECT * FROM $table_name where slno='$recordID'");
		$prod_res = mysqli_fetch_array($prod_sql);
		if(count($prod_res) > 0){
			$file_name_array = [];
$image = $_FILES['image'];
if($image['size'][0]!=0){
		foreach($_FILES["image"]["tmp_name"] as $key=>$tmp_name) {
		    $file_name=$_FILES["image"]["name"][$key];
		    $file_tmp=$_FILES["image"]["tmp_name"][$key];
		    $ext=pathinfo($file_name,PATHINFO_EXTENSION);   
		    $filename=basename($file_name,$ext);
		    $newFileName=$filename.$ext;
		    move_uploaded_file($file_tmp=$_FILES["image"]["tmp_name"][$key],"../uploaded-files/brands/".$newFileName);
		    array_push($file_name_array, $newFileName);
		}

		$file_name1 = implode(',', $file_name_array);
		$file_array = explode(',',$prod_res['brandImage']);
			$new_array = array_merge($file_array, $file_name_array);
			$file_name1 = implode(',', $new_array);	
			mysqli_query($dbconn,"UPDATE $table_name SET brandImage='$file_name1' WHERE slno='$recordID'");
	}
	
		}

		if(count($prod_res) > 0){
			$file_name_array = [];
$image = $_FILES['image2'];
if($image['size'][0]!=0){
		foreach($_FILES["image2"]["tmp_name"] as $key=>$tmp_name) {
		    $file_name=$_FILES["image2"]["name"][$key];
		    $file_tmp=$_FILES["image2"]["tmp_name"][$key];
		    $ext=pathinfo($file_name,PATHINFO_EXTENSION);   
		    $filename=basename($file_name,$ext);
		    $newFileName=$filename.$ext;
		    move_uploaded_file($file_tmp=$_FILES["image2"]["tmp_name"][$key],"../uploaded-files/brands/".$newFileName);
		    array_push($file_name_array, $newFileName);
		}

		$file_name1 = implode(',', $file_name_array);
		$file_array = explode(',',$prod_res['brandImage2']);
			$new_array = array_merge($file_array, $file_name_array);
			$file_name1 = implode(',', $new_array);	
			mysqli_query($dbconn,"UPDATE $table_name SET brandImage2='$file_name1' WHERE slno='$recordID'");
	}
	
		}
        
		updateTable($table_name, "brandName='$brandName', brandTitle='$brandTitle', brandKeyword='$brandKeyword',brandDesc='$brandDesc', brandHeading='$brandHeading', brandDetail='$brandDetail', brandDetail2='$brandDetail2' where slno='$recordID'");
		
		$_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' has been updated successfully</div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	
	else {
	
	}	
if($id=="delete_record") {
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION['session_message']='<div class="alert alert-danger" role="alert">'.$record_type.' has been deleted successfully</div>';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header();
?>
<div class="table-responsive">

<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
    <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgimg_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">--Select--</option>
	     <?php
	     foreach($_GLOBALS['pgsize_arr'] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
</div>
<hr size="2" width="99%" color="#DA251C">

<?php
$qry1= mysqli_query($dbconn,"select * from $table_name");
$reccnt = mysqli_num_rows($qry1);
$qry= mysqli_query($dbconn,"select * from $table_name order by slno desc");
// echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>
<form name="formn" method="post" action="" enctype="multipart/form-data">
<div class="table-responsive">
<table class="lightBorder table table-bordered table-hover">
       <?php
 if($reccnt>0) {
     ?>
    <thead>
	 <tr height="25" bgcolor="#EAEAEA">
    <td width="3%"></td>
	 <td width="5%" align="center"><strong style="
    color: #3331ca;">S.No.</strong></td>
	 <td width="10%" align="left"><strong style="
    color: #3331ca;">Brands Name</strong></td>
	 <td width="8%" align="left"><strong style="
    color: #3331ca;">Brands Title</strong></td>
	 <td width="19%" align="center"><strong style="
    color: #3331ca;">Brands Image</strong></td>
	 <td width="10%" align="left"><strong style="
    color: #3331ca;">Options</strong></td>
	 </tr>
  </thead>
  <tbody>
       <?php
	 $sl=$start+1;
      while($arr=mysqli_fetch_array($qry)) { 
      ?>
    <tr>
        <td><label class="container1"><input type="checkbox" name="brand_arr[]" value="<?= $arr['slno'] ?>" />  <span class="checkmark"></span></label></td>
        <td><?=$sl;?></td>
        <td><?=$arr['brandName'];?></td>
        <td><?=$arr['brandTitle'];?></td>
        <td>	
            <?php
	   if($arr['brandImage']) {
		   $file1=SITE_DIR_PATH."/uploaded_files/brands/".$arr['brandImage'];
			   $dimens=getImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+30;
				   $dimens[1]=$dimens[1]+80;
			   }
			   else{
				   $dimens[0]=200;
				   $dimens[1]=200;
			   }
			   ?>
            <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup.php?img=brands/<?=$arr['brandImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View Image</strong></a>
			   <?php
	   }

	   ?>
        </td>
        
        <td>&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['slno'];?>#bot"><i class="fa fa-edit"></i></a>&nbsp;<a href="<?=$script_name;?>?id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['slno'];?>" onClick="return confirm('Are you sure to delete this record...');"><i class="fa fa-trash"></i></a>
        </td>
    </tr> 
      <?php $sl++; }  ?>
</tbody>
     <?php } else { ?> <tr><td><h5>Not Record Found ... </h5></td></tr><?php } ?>
</table>
</div>
</form>
<br>

<form name="bannerFrm" method=post action="" enctype="multipart/form-data" onsubmit="return validate('bannerFrm') && chk_valid_date();">
   <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
    <div class="form-group">
         <h3>Add New <?=$record_type;?></h3>
    </div>
     <?php
 }
 else {
	 ?>
    <div class="form-group">
         <h3>Edit <?=$record_type;?></h3>
    </div>
        <div class="form-group text-right">
         <a href="<?=$script_name;?>" class="btn btn-primary text-white"><b>Add New <?=$record_type;?></b></a>
    </div>
<?php
 }
 $res=getResult($table_name," where slno='$_GET[recordID]'");
 ?>
    
<div class="form-group">
      <label for="cate">Brands Name <font color="red">*</font></label>
       <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
        <input type="text" name="brandName" class="form-control" value="" placeholder="Brands Name">
         <?php
 }
 else { ?>
      <input type="text" name="brandName" class="form-control" value="<?=$res['brandName']?>" placeholder="Brands Name">
    <?php } ?>
</div>   
    
<div class="form-group">
      <label for="cate">Brand Meta Title <font color="red">*</font></label>
        
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
        <input type="text" name="brandTitle" class="form-control" value="" placeholder="Brand Title">
         <?php
 }
 else { ?>
      <input type="text" name="brandTitle" class="form-control" value="<?=$res['brandTitle']?>" placeholder="Brand Title">
    <?php } ?>
</div>  
<div class="form-group">
      <label for="cate">Brand Meta Keyword <font color="red">*</font></label>
        
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
        <input type="text" name="brandKeyword" class="form-control" value="" placeholder="Brand Meta Keyword">
         <?php
 }
 else { ?>
      <input type="text" name="brandKeyword" class="form-control" value="<?=$res['brandKeyword']?>" placeholder="Brand Keyword">
    <?php } ?>
</div>  
<div class="form-group">
      <label for="cate">Brand Meta Desc<font color="red">*</font></label>
        
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
        <input type="text" name="brandDesc" class="form-control" value="" placeholder="Brand Meta Desc">
         <?php
 }
 else { ?>
      <input type="text" name="brandDesc" class="form-control" value="<?=$res['brandDesc']?>" placeholder="Brand Meta Desc">
    <?php } ?>
</div>   
    <div class="form-group">
      <label for="cate">brand Heading <font color="red">*</font></label>
        
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
        <input type="text" name="brandHeading" class="form-control" value="" placeholder="brand Heading">
         <?php
 }
 else { ?>
      <input type="text" name="brandHeading" class="form-control" value="<?=$res['brandHeading']?>" placeholder="brand Heading">
    <?php } ?>
</div> 


<div class="form-group">
      <label for="cate">Brand Detail <font color="red">*</font></label>
        
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
    <textarea type="text" name="brandDetail" id="brandDetail" class="form-control" value="" placeholder="Brand Detail"></textarea>
         <?php
 }
 else { ?>
     <textarea type="text" name="brandDetail" id="brandDetail" class="form-control" value="" placeholder="Brand Detail">
         <?=$res['brandDetail'];?></textarea>
    <?php } ?>

</div>  

<div class="form-group">
      <label for="cate">Brand Detail 2 <font color="red">*</font></label>
           <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
    <textarea type="text" name="brandDetail2" id="brandDetail2" class="form-control" value="" placeholder="Brand Detail 2"></textarea>
         <?php
 }
 else { ?>
     <textarea type="text" name="brandDetail2" id="brandDetail2" class="form-control" value="" placeholder="Brand Detail 2">
         <?=$res['brandDetail2'];?></textarea>
    <?php } ?>

</div>  
  
        <div class="form-group">
      <label for="multimage">Image 1<font color="red">*</font></label>
        <input type="file" name="image[]" multiple class="form-control" size="60" value="" id="NOBLANK~Please enter Person Name.~DM~" />
    </div>
    <?php
if($res['brandImage']!=""){
	$image_array = explode(',', $res['brandImage']);?>
	
	<div class="display-img">
	<ul>
	<?php foreach($image_array as $image){?>
				<li>
					
						
<img src="<?=site_url;?>/uploaded-files/brands/<?= $image ?>">
					
						<div class="card-footer">
							<a href="?id=delete&name=<?= $image ?>&cat_id=<?= $res['slno'] ?>" onclick="confirm('Do you want to delete the image?')">Delete</a>
						</div>
				
				</li>
			<?php } ?>
	</ul>
</div>
<?php } ?>
<div class="form-group">
      <label for="multimage">Image 2<font color="red">*</font></label>
        <input type="file" name="image2[]"  multiple class="form-control" size="60" value="" id="NOBLANK~Please enter Person Name.~DM~" />
    </div>
    <?php
if($res['brandImage2']!=""){
	$image_array = explode(',', $res['brandImage2']);?>
	
	<div class="display-img">
	<ul>
	<?php foreach($image_array as $image){?>
				<li>
					
						
<img src="<?=site_url;?>/uploaded-files/brands/<?= $image ?>">
					
						<div class="card-footer">
							<a href="?id=delete&name=<?= $image ?>&cat_id=<?= $res['slno'] ?>" onclick="confirm('Do you want to delete the image?')">Delete</a>
						</div>
				
				</li>
			<?php } ?>
	</ul>
</div>
<?php } ?>
    <div class="form-group">
           <?php
   if($recordID !="") {
	   ?>
	   <input type="submit" class="btn btn-primary" name="submit" value="Update <?=$record_type;?>" class="button">
	   <input type="hidden" name="recordID" value="<?=$res['slno'];?>">
	   <input type="hidden" name="id" value="update_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" class="btn btn-primary" name="submit" value="Add New <?=$record_type;?>" class="button">
	   <input type="hidden" name="id" value="add_record">
	   <?php
   }
   ?>
   <input type="hidden" name="start" value="<?=$start;?>">
   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
    </div>
</form>
<link rel='stylesheet' href='calendar.css' title='calendar'>
<script language="javascript" src="calendar.js"></script>
<script src="jquery.min.js"></script>

<iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
</iframe>
<?php
admin_footer();
exit;
?>