<?php
   require_once("header.php");
   require_once("menu.php");
   
   $_SESSION['error'] = '';
   $records = array();
   if(isset($_REQUEST['edit']))
   {
       $slider_id = base64_decode($_REQUEST['edit']);
       $ex = $obj->get_slider_id($slider_id);
       $row = mysqli_fetch_array($ex);
   }
   if(isset($_POST['update']))
   {
      $slider_id = $_POST['id'];
      $slider_text = $_POST['slidertext'];
		  $server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
		  $upload_dir = '../uploads/logo/';
           
      if(!empty($_FILES['logo_slider_img']['name']))
      {      
        unlink($_POST['old_img']);      
        $tempname = $_FILES['logo_slider_img']['tmp_name'];
        $upload_name = $upload_dir.$_FILES['logo_slider_img']['name'];
        move_uploaded_file($tempname,$upload_name);
        $slider_img = $server_url."/".$upload_name;
      }
      else
      {
        $slider_img = $_POST['old_img'];
      }
		   $ex = $obj->update_slider_logo($slider_id,$slider_img);
		   if($ex)
		   {
			  echo '<script>window.location="slider_logo.php"</script>';
		   }
   }
  
    
   if(isset($_POST['submit']))
   {
   	
    $server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
		$upload_dir = '../uploads/logo/';
       
		if(!empty(array_filter($_FILES['logo_slider_img']['name']))) {
			//$success = $failure = '';
           foreach ($_FILES['logo_slider_img']['tmp_name'] as $key => $value) { 
                 
               $file_tmpname = $_FILES['logo_slider_img']['tmp_name'][$key]; 
               $file_name = $_FILES['logo_slider_img']['name'][$key]; 
              
               $filepath = $upload_dir.$file_name;
               if(move_uploaded_file($file_tmpname,$filepath)){
					$ex = $obj->logo_slider_img($server_url."/".$filepath);
					if($ex)
					{
					  echo '<script>window.location="slider_logo.php"</script>';
					}
				}
   			
			}
   		}
   }
    
   ?>  
<div id="layoutSidenav_content">
<main class="mt-4">
   <div class="container-fluid">
      <div class="card mb-4">
         <div class="card-header">	
            <i class="fas fa-check-square mr-1"></i>Logo Slider
            <a href="slider_logo.php" class="add_right btn btn-primary btn-sm float-right" style="display: inline;">Back Logo Section</a>			
         </div>
         <div class="card-body">
            <p class="text-danger"><?php echo $_SESSION['error'];?></p>
            <form method="post" enctype="multipart/form-data">
               <input type="hidden" name="id" value="<?php echo $slider_id;?>">
               <div class="table-responsive">
                  <table style="width:100%" id='myTable' class="table">
                     <thead>
                        <tr>
                           <th>
						   <?php if(!isset($_REQUEST['edit']))
								{ ?> 
									<a id="insert-more" onClick="insertRow()" class="add_row123">+</a>
								<?php } ?>
							</th>
                           <th>Logo Image</th>
                        </tr>
                     </thead>
                     <tbody>
						<?php if(isset($_REQUEST['edit']))
						  { ?> 
						<tr>
                           <td>1</td>
                          
                           <td class="slider_logo">
								<img width="100" src="<?php echo $row['logo_slider_img'];?>"/>
                              <input type="file" class="form-control"  name="logo_slider_img">
                              <input type="hidden" name="old_img" value="<?php echo $row['logo_slider_img'];?>">
                           </td>
                        </tr>
						<?php }else{ ?>
                        <tr>
                           <td>1</td>
                           
                           <td>
                              <input type="file" class="form-control" required name="logo_slider_img[]" value="<?php //echo $row['slider_img'];?>">
                           </td>
                        </tr>
                        <tr>
                           <td>2</td>
                           
                           <td>
                              <input type="file" class="form-control" name="logo_slider_img[]">
                           </td>
                        </tr>
						<?php } ?>
                     </tbody>
                  </table>
               </div>
               <?php if(isset($_REQUEST['edit']))
                  { ?> 
               <button type="submit" class="btn btn-primary" name="update">Update</button>
               <?php } 
                  else
                  { ?>
               <button type="submit" class="btn btn-primary" name="submit">Submit</button>
               <?php } ?>
            </form>
			</div>
      </div>
   </div>
</main>
<script>
   var url ="slider_logo.php";
</script>
<?php require_once("footer.php");?>
<?php
   ?>
<script>
   var row = 2;
   var index = row;
   var sr =index;
       function insertRow(){
   
         var table=document.getElementById("myTable");
         var row=table.insertRow(table.rows.length);
   
         var cell0=row.insertCell(0);
         var t0=document.createElement("span");
             t0.innerHTML=++sr;
             cell0.appendChild(t0);
   
   
   
         var cell2=row.insertCell(1);
         var t2=document.createElement("input");
             t2.setAttribute("type", "file");
             t2.name = "logo_slider_img[]";
             t2.setAttribute("class","form-control");
             cell2.appendChild(t2);
   
         
         index++;
   }
   
</script>
<style type="text/css">
   .add_row123
   {
   background: #aaa;
   color: #fff;
   padding:0 6px 3px;
   cursor: pointer;
   }
</style>