<?php include('site-header.php'); ?>
<!-- end of LOGO CONTAINER -->
<!-- - - - - - - - - - - - - -->
<!-- start of SIDEBAR        -->
<!-- - - - - - - - - - - - - -->
<?php include('left-nevigation.php'); ?>
<!-- - - - - - - - - - - - - -->
<!-- end of SIDEBAR          -->
<!-- - - - - - - - - - - - - -->
<main id="playground">
<!-- - - - - - - - - - - - - -->
<!-- start of TOP NAVIGATION -->
<!-- - - - - - - - - - - - - -->
<?php include('site-top-header.php'); ?>
<!-- - - - - - - - - - - - - -->
<!-- end of TOP NAVIGATION   -->
<!-- - - - - - - - - - - - - -->
<!-- PAGE TITLE -->
<?php 
   $product_id =$_GET['cat_id'];
   $image_name=$_FILES['gal_image']['name']; 
   $image_names= time()."_".$image_name;
   ?>
<?php
   if(isset($_REQUEST['Insert']))
   {
      $sqlinsrt = "INSERT INTO `tbl_category_img`(`category_parent_id`, `category_image_name`) VALUES ('$product_id','$image_names')";
      if(mysqli_query($link,$sqlinsrt))
            {
               move_uploaded_file($_FILES['gal_image']['tmp_name'],"../category-image/".$image_names);
              $msg = "<p style='color:green;'>Save Product Image Successfully!!!</p>";
            }
               
   }
   ?> 
<?php
   if(isset($_REQUEST['Updt']))
   {
      $category_image_linking = $_POST['category_image_linking'];
      $del_logo_id = $_POST['img_id'];
      $del_image = $_POST['del_image'];
      if($_FILES['gal_image']['name']!=="")
      { 
          $sqlUp ="UPDATE `tbl_category_img` SET 
                        `category_image_linking`='$category_image_linking',
                        `category_image_name`='$image_names' 
                           WHERE `img_id`='$del_logo_id'";
         if(mysqli_query($link,$sqlUp))
            {
               $msg = "<p style='color:green;'>Update category Best Successfully!!!</p>";
                 if(move_uploaded_file($_FILES['gal_image']['tmp_name'],"../category-image/".$image_names))
                       {
                        unlink("../category-image/$del_image"); 
                       }
            }
      }
      else
      {
          $sqlUp ="UPDATE `tbl_category_img` SET 
                        `category_image_linking`='$category_image_linking' 
                           WHERE `img_id`='$del_logo_id'";
         if(mysqli_query($link,$sqlUp))
            {
               $msg = "<p style='color:green;'>Update Successfully!!!</p>";
            }
      }       
   }
   ?> 
<?php 
   if($_REQUEST['del_logo_id']!="")
   {
   $del_logo_id=$_REQUEST['del_logo_id'];
   $del_image = $_REQUEST['del_image'];
   $sql="delete from tbl_category_img where img_id='$del_logo_id'";
     if($data= mysqli_query($link,$sql))
     {
      $msg = "<p style='color:green;'>Delete Product Image Successfully!!!</p>";
      unlink("../category-image/$del_image");
     }
   }
   ?>
<?php
   $sql="SELECT * FROM `tbl_category_img` WHERE `category_parent_id`=$product_id";
   $data= mysqli_query($link,$sql);
   $count=mysqli_num_rows($data);
   ?>  
<div class="container-fluid textsss">   
<section id="page-title" class="row">
   <div class="col-md-8">
      <h1>Category Best Image</h1>
      <p class="lead">Update your Category Best Image</strong>.</p>
      <p style="font-weight: bold;font-size:18px;">
                  Image for Category Best in Home Page (Size :270 X 406 pixels)
      </p>
      <?=$msg;?>
      <a href="update-category.php" class="btn btn-primary ">Back</a>
   </div>
   <div class="col-md-4">
   </div>
</section>
<!-- / PAGE TITLE -->
<div class="container-fluid">
   <div class="row">
      <?php while($result=mysqli_fetch_array($data)){ ?>
      <form enctype="multipart/form-data" method="post">
         <div class="col-md-4">
            <section class="panel tasks-widget"> 
               <img  src="../category-image/<?=$result['category_image_name']?>" class="img-responsive" style="height:280px; width:100%;"> 
            </section>
            <div class="text-center">
               <input type="file" name="gal_image">
            </div>
            <input type="text" class="form-control" placeholder="Linking" name="category_image_linking" value="<?=$result['category_image_linking']?>">
            <input type="hidden" name="del_image" value="<?php echo $result['category_image_name']; ?>">
            <input type="hidden" name="img_id" value="<?php echo $result['img_id'] ?>">
            <div class="text-center"><button class="btn btn-primary" name="Updt" style="width:100%;">Update</button> </div>
      </form>
      <form method="post">
         <input type="hidden" name="del_image" value="<?php echo $result['category_image_name']; ?>">
      <input type="hidden" name="del_logo_id" value="<?php echo $result['img_id'] ?>">
      <div class="text-center"><button class="btn btn-primary" name="del" style="width:100%;">Delete</button> </div> 
      </form>
      </div>
      <?php } ?>
   </div>
   <!-- / row -->
</div>
<?php if($count>=1){ 
   echo "";
   }else{
   ?>
<div class="container-fluid" style="margin-top:80px;">
   <div class="row">
      <div class="col-md-4">
      </div>
      <form method="post" enctype="multipart/form-data">
         <div class="col-md-4">
            <div class="text-center"><input type="file" name="gal_image" class="btn btn-primary"> </div>
            <div class="text-center"><button type="submit" name="Insert" class="btn btn-primary" style="width:100%;">Add Image </button></div>
         </div>
      </form>
      <div class="col-md-4">
      </div>
   </div>
   <!-- / row -->
</div>
</div>
<?php } ?>
<!-- / container-fluid -->
<?php include('site-footer.php'); ?>