<?php include('site-header.php'); ?>
  <!-- end of LOGO CONTAINER -->
  <!-- - - - - - - - - - - - - -->
  <!-- start of SIDEBAR        -->
  <!-- - - - - - - - - - - - - -->
<?php include('left-nevigation.php'); ?>
  <!-- - - - - - - - - - - - - -->
  <!-- end of SIDEBAR          -->
  <!-- - - - - - - - - - - - - -->
  <script src="ckeditor/ckeditor.js"></script>  
  <main id="playground">
  <!-- - - - - - - - - - - - - -->
  <!-- start of TOP NAVIGATION -->
  <!-- - - - - - - - - - - - - -->
<?php include('site-top-header.php'); ?>
    <!-- - - - - - - - - - - - - -->
    <!-- end of TOP NAVIGATION   -->
    <!-- - - - - - - - - - - - - -->
    <!-- PAGE TITLE --> 
    <?php
    extract($_POST);
    $tmp = $_FILES['att']['tmp_name'];
    $fn = $_FILES['att']['name'];
    $ext = explode(".",$fn);
    $extt = end($ext);
    $fnn = rand().".$extt";

    $tmp2 = $_FILES['att2']['tmp_name'];
    $fn2 = $_FILES['att2']['name'];
    $ext2 = explode(".",$fn2);
    $extt2 = end($ext2);
    $fnn2 = rand().".$extt2";
      
    $pro_brf=addslashes(trim($_POST['pro_brf']));
    
      if(isset($sub))
    {
      if($category_name!="select Category")
      {

          if($_FILES['att2']['name']!="" && $_FILES['att']['name']!="" )
          {
                            
            
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            $insert_Sql = "INSERT INTO `tbl_product`(`pro_category`, `pro_name`, `pro_type`, `pro_offer`, `pro_price`, `pro_Pack`, `pro_desc`,`pro_brf` ,`pro_more` ,`pro_image` ,`pro_image2`) VALUES ('$category_name','$pro_name','$pro_type','$pro_offer', '$pro_price','$pro_Pack','$pro_desc','$pro_brf','$pro_more','$fnn','$fnn2')";

            if(mysqli_query($link,$insert_Sql))
            {
              
              move_uploaded_file($tmp2, "../product-image/$fnn2");
              move_uploaded_file($tmp, "../product-image/$fnn");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
          }

          elseif($_FILES['att']['name']!="")
          {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));

            $insert_Sql = "INSERT INTO `tbl_product`(`pro_category`, `pro_name`, `pro_type`, `pro_offer`, `pro_price`, `pro_Pack`, `pro_desc`,`pro_brf` ,`pro_more` ,`pro_image`) VALUES ('$category_name','$pro_name','$pro_type','$pro_offer', '$pro_price','$pro_Pack','$pro_desc','$pro_brf','$pro_more','$fnn')";

            if(mysqli_query($link,$insert_Sql))
            {
              move_uploaded_file($tmp, "../product-image/$fnn");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
          }
          elseif($_FILES['att2']['name']!="")
          {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            $insert_Sql = "INSERT INTO `tbl_product`(`pro_category`, `pro_name`, `pro_type`, `pro_offer`, `pro_price`, `pro_Pack`, `pro_desc`,`pro_brf` ,`pro_more` ,`pro_image2`) VALUES ('$category_name','$pro_name','$pro_type','$pro_offer', '$pro_price','$pro_Pack','$pro_desc','$pro_brf','$pro_more','$fnn2')";

            if(mysqli_query($link,$insert_Sql))
            {
              move_uploaded_file($tmp2, "../product-image/$fnn2");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
          }
          

          else
          {
            $msg ="<p style='color:red;'>Choose Your Image!!!!</p>";
          }
      }
      else
      {
        $msg ="<p style='color:red;'>Choose Your Category!!!!</p>";
      }
    }
    ?>    
    <!-- Updata Product -->
    <?php
    $id = mysqli_real_escape_string($link,$_GET['edit']);
    
    if(isset($upd))
    {
        if($_FILES['att']['name']!="" && $_FILES['att2']['name']!="")
        {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            $Update_Sql = "UPDATE `tbl_product` SET 
                                `pro_category`='$category_name',
                                `pro_name`='$pro_name',
                                `pro_type`='$pro_type',
                                `pro_offer`='$pro_offer',
                                `pro_price`='$pro_price',
                                `pro_Pack`='$pro_Pack',
                                `pro_desc`='$pro_desc',
                                `pro_more`='$pro_more',
                                `pro_brf`='$pro_brf',
                                `pro_image`='$fnn',
                                `pro_image2`='$fnn2'
                                 WHERE `pro_id`=$id";
         
            if(mysqli_query($link,$Update_Sql))
            {
              unlink("../product-image/$himage");
              move_uploaded_file($tmp, "../product-image/$fnn");
              unlink("../product-image/$himage2");
              move_uploaded_file($tmp2, "../product-image/$fnn2");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
        }
        elseif($_FILES['att']['name']!="")
        {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            $Update_Sql = "UPDATE `tbl_product` SET 
                                `pro_category`='$category_name',
                                `pro_name`='$pro_name',
                                `pro_type`='$pro_type',
                                `pro_offer`='$pro_offer',
                                `pro_price`='$pro_price',
                                `pro_Pack`='$pro_Pack',
                                `pro_desc`='$pro_desc',
                                `pro_more`='$pro_more',
                                `pro_brf`='$pro_brf',
                                `pro_image`='$fnn'
                                 WHERE `pro_id`=$id";
         
            if(mysqli_query($link,$Update_Sql))
            {
              unlink("../product-image/$himage");
              move_uploaded_file($tmp, "../product-image/$fnn");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
        }
        if($_FILES['att2']['name']!="")
        {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            $Update_Sql = "UPDATE `tbl_product` SET 
                                `pro_category`='$category_name',
                                `pro_name`='$pro_name',
                                `pro_type`='$pro_type',
                                `pro_offer`='$pro_offer',
                                `pro_price`='$pro_price',
                                `pro_Pack`='$pro_Pack',
                                `pro_desc`='$pro_desc',
                                `pro_more`='$pro_more',
                                `pro_brf`='$pro_brf',
                                `pro_image2`='$fnn2'
                                 WHERE `pro_id`=$id";
         
            if(mysqli_query($link,$Update_Sql))
            {
              unlink("../product-image/$himage2");
              move_uploaded_file($tmp2, "../product-image/$fnn2");
              $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
            }
        }
        else
        {
            $pro_type = addslashes($pro_type);
            $pr9=addslashes(trim($_POST['pro_name']));
            $pro_name = ucwords(strtolower($pr9));
            
           $Update_Sql = "UPDATE `tbl_product` SET 
                                `pro_category`='$category_name',
                                `pro_name`='$pro_name',
                                `pro_type`='$pro_type',
                                `pro_offer`='$pro_offer',
                                `pro_Pack`='$pro_Pack',
                                `pro_price`='$pro_price',
                                `pro_desc`='$pro_desc',
                                `pro_more`='$pro_more',
                                `pro_brf`='$pro_brf'
                                 WHERE `pro_id`=$id";
          if(mysqli_query($link,$Update_Sql))
          {
            $msg = "<p style='color:green;'>Save Services Successfully!!!</p>";
          }
        }
    }
?>
<!-- Fetching these product -->
  <?php
    if(isset($_GET['edit']))
    {
      $sel = mysqli_query($link,"select * from tbl_product where pro_id=$id");
      $arr =  mysqli_fetch_assoc($sel);
      $cat = $arr['pro_category'];
      $sel1 = mysqli_query($link,"select * from tbl_category where cat_id=$cat");
      $arr1 = mysqli_fetch_assoc($sel1);
    }
  ?>
    <section id="page-title" class="row">
      <div class="col-md-8">
        <h1>Product</h1>
        <p class="lead"><?php if($_GET['edit']==""){echo 'Add';}else{echo 'Update';}?>your Product below</strong>.</p>
      </div>
      <div class="col-md-4">
       
      </div>
    </section>
    <!-- / PAGE TITLE -->
    <section>
      <div class="container-fluid">
      <div><label><?=@$msg;?></label></div>
        <div class="row">
          <form method="post" enctype="multipart/form-data">
            <div class="col-md-12">
              <a href="show-product.php#<?=$_GET['edit'];?>" class="btn btn-primary">Back</a>
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <label>Product Name:</label><br>
                <input class="form-control" type="text"  name="pro_name" value="<?=$arr['pro_name'];?>">
                <input type="hidden" value="<?=$arr['pro_image']?>" name="himage">
                <label>Image for Product Page (Size :600 X 600 pixels)</label>
                <?php
                if($arr['pro_image']!=='' && isset($arr['pro_image']))
                {
                  ?>
                  <img src="../product-image/<?=$arr['pro_image'];?>"  style="width:100px;height:100px;">
                  <?php
                }
                else
                {
                  ?>
                  <img src="src/img/placeholder.png" style="width:100px;height:100px;">
                  <?php
                }
                ?>                
                <input type="file" name="att" >

                <input type="hidden" value="<?=$arr['pro_image2']?>" name="himage2">
                <label> Image for over Page (Size :600 X 600 pixels)</label>
                <?php
                if($arr['pro_image2']!=='' && isset($arr['pro_image2']))
                {
                  ?>
                  <img src="../product-image/<?=$arr['pro_image2'];?>" id="blah" style="width:100px;height:100px;">
                  <?php
                }
                else
                {
                  ?>
                  <img src="src/img/placeholder.png" id="blah" style="width:100px;height:100px;">
                  <?php
                }
                ?>                
                <input type="file" name="att2" id="imgInp">
              </div>

             

             <!-- <div class="form-group">
                      <label>Product Price :</label>
                      <input type="text" class="form-control" name="pro_price" value="<?=$arr['pro_price'];?>">
              </div>   -->
             
                  
              </div>
              <div class="col-md-6">
              <div class="form-group">
                <label>Services Category:</label>
                <select class="form-control" name="category_name" required="">  
                  <?php
                  if($_GET['edit']=="")
                  {
                  ?>
                  <option>select Category</option>
                  <?php
                  }
                  ?>
                  <?php
                  $sel2 =mysqli_query($link,"select * from tbl_category");
                  while($arr2 = mysqli_fetch_assoc($sel2))
                  {
                  ?>
                  <option value="<?=$arr2['cat_id'];?>" <?php if($arr2['cat_id'] == $arr1['cat_id']){echo 'selected';}?>><?=$arr2['cate_name'];?></option>
                  <?php
                  }
                  ?>
                </select>
              </div>

            <div class="form-group">
                      <label>Product AVAILIBILITY :</label>
                      <input type="number" class="form-control" name="pro_type" value="<?=$arr['pro_type'];?>">
              </div> 
               <!-- <div class="form-group">
                <label> product Country Of Origin  </label>
                <input type="text" name="pro_Pack" class="form-control" value="<?=$arr['pro_Pack'];?>" >
              </div> -->
               <!-- <div class="form-group">
                 <label> product Size: </label>
                <input type="text" name="pro_offer" class="form-control" value="<?=$arr['pro_offer'];?>" >
              </div> -->
              <!-- <div class="form-group">
                  <label>Services Briefing : </label>
                  <textarea class="form-control"  name="pro_brf" style="width:100%; height:100px;">
                      <?=$arr['pro_brf'];?>
                  </textarea>
                </div>  -->
                
              
                 

              </div>
            </div>
             
               <div class="col-md-12">
                 <div class="form-group">
                  <label>Product Details :</label><br>
                  <textarea class="form-control" id="ckeditor1" name="pro_desc" style="width:100%"><?=$arr['pro_desc'];?></textarea>
                </div> 
            </div>
            <!-- 
           <div class="col-md-6">
                 <div class="form-group">
                  <label>Product Detail:</label><br>
                  <textarea class="form-control" id="ckeditor2" name="pro_brf" style="width:100%"><?=$arr['pro_brf'];?></textarea>
                </div> 
            </div> --> 
                       <!--  <div class="col-md-12">
                  <div class="form-group">
                    <label>Product Shipping</label><br>
                    <textarea class="form-control" id="ckeditor3" name="pro_more" style="width:100%"><?=$arr['pro_more'];?></textarea>
                  </div> 
            </div>  --> 
            

            <div class="col-md-4">
              <br><br>
              <div class="form-group">
                <?php
                if($_GET['edit']=="")
                {
                ?>
                 <input class="btn btn-primary text-center" type="submit" name="sub" value="SUBMIT">
                <?php
                }
                else
                {
                  ?>
                   <input class="btn btn-primary text-center" type="submit" name="upd" value="UPDATE">
                  <?php
                }
                ?>
              </div>
            </div>
            <div class="col-md-4">


            </div>
          </form>
        </div>
    </section>
<script type="text/javascript">
        $('document').ready(function()
          {
              $('textarea').each(function(){
                      $(this).val($(this).val().trim());
                  }
              );
          });
</script>
    <script> 
      CKEDITOR.replace(ckeditor1)
      CKEDITOR.replace(ckeditor2)
      CKEDITOR.replace(ckeditor3)
      CKEDITOR.replace(ckeditor4)

    </script>
  <script src="ckeditor/ckeditor.js">
  </script>
    <!-- / container-fluid -->
 <?php include('site-footer.php'); ?>



<script type="text/javascript">
 function readURL(input) 
 {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    
    reader.onload = function(e) {
      $('#blah').attr('src', e.target.result);
    }
    
    reader.readAsDataURL(input.files[0]); // convert to base64 string
  }
}

$("#imgInp").change(function() 
{
  readURL(this);
});


</script>