<?php
$color=$size=0;
 include("header.php");
 include("side.php");
 include("../db.php"); 
  // $admin_id=$_SESSION['id'];
 date_default_timezone_set("Asia/Calcutta");
$insertdate = date("h:i:s d-m-Y ");
extract($_GET);
if(isset($submit)){

	$sql=mysqli_query($conn,"INSERT INTO `product`(`product_name`, `category`, `price`, `mrp`, `detail`, `image1`, `image2`, `image3`, `status`, `date`, `color`, `size`) VALUES ('$product_name', '$category', '$price', '$mrp', '$discription', '$image1', '$image2', '$image3', '1', '$insertdate', '$color', '$size')");

	$alert='<div class="alert alert-success">Product successfully Upload</div>';
	
?>

	<script>
         setTimeout(function(){
            window.location.href = 'products.php?main=7&cat=1';
         }, 3000);
 	</script>
      <?php
}

if(isset($_GET['id'])){
	$id=$_GET['id'];
$sql=mysqli_query($conn,"SELECT * FROM `product` WHERE id='$id'");
  while ($row=mysqli_fetch_object($sql)) { 
  	$product_names=$row->product_name;
  	$categorys=$row->category;
  	$prices=$row->price;
  	$mrps=$row->mrp;
  	$details=$row->detail;
  	$image1s=$row->image1;
  	$image2s=$row->image2;
  	$image3s=$row->image3;
  
 }
}
if(isset($update)){
date_default_timezone_set("Asia/Calcutta");
$updatedate = date("Y-m-d h:i:s");

if(empty($image1)){ $image1=$image1s; }
if(empty($image2)){ $image2=$image2s; }
if(empty($image3)){ $image3=$image3s; }

	 $sql=mysqli_query($conn,"UPDATE `product` SET `product_name`='$product_name', `category`='$category', `price`='$price', `mrp`='$mrp', `detail`='$discription', `image1`='$image1', `image2`='$image2', `image3`='$image3', `updated`='$insertdate' WHERE id='$id'");
	$alert='<div class="alert alert-success">Product successfully Update</div>';
?>
		<script>
         setTimeout(function(){
            window.location.href = 'products.php?main=7&cat=1';
         }, 3000);
      </script>
      <?php
}
 ?>
 <style>
 	.card-editor {
    border: 1px solid #ced4da !important;
}

 	.ww{
 		height: 20px;
 		width: 20px;
 		
 	}
 	.col, .col-1, .col-10, .col-11, .col-12, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9, .col-auto, .col-lg, .col-lg-1, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-auto, .col-md, .col-md-1, .col-md-10, .col-md-11, .col-md-12, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-auto, .col-sm, .col-sm-1, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-auto, .col-xl, .col-xl-1, .col-xl-10, .col-xl-11, .col-xl-12, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6, .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-auto {
    position: inherit!important;
}

 </style>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="css/boot.css">

          <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid">
                        <h2 class="mt-30 page-title">Products</h2>
                        <ol class="breadcrumb mb-30">
                            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="products.php?main=7&cat=1">Products</a></li>
                            <li class="breadcrumb-item active"><?php if(isset($id)){ echo 'Edit Product'; } else { echo 'Add New Product'; }	 ?></li>
                        </ol>
                        <div class="row">
							<div class="col-lg-12 col-md-12">
								<div class="card card-static-2 mb-30">
									<div class="card-title-2">
									<?php if(isset($id)){ echo '<h4>Edit Product</h4>'; } else { echo '<h4>Add New Product</h4>'; }	 ?>
									</div>
									<div class="card-body-table">
									<?php if(!empty($alert)){ echo $alert; } ?>
										<div class="news-content-right pd-20">
											
											<form action="" method="get">
	<input type="hidden" name="image1s" value="<?php if(!empty($image1s)){ $image1s; }?>">
	<input type="hidden" name="image2s" value="<?php if(!empty($image2s)){ $image2s; }?>">
	<input type="hidden" name="image3s" value="<?php if(!empty($image3s)){ $image3s; }?>">
	<input type="hidden" name="id" value="<?php if(isset($id)){ echo $id; }?>">
											
											<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Product Name*</label>
												<input type="text" name="product_name" class="form-control" placeholder="Product Name" autocomplete="on" value=" <?php if(!empty($product_names)){ echo $product_names; } ?>" required/>
											</div>
										</div>
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Category*</label>

												<select id="category" name="category" class="form-control" required/>
													<option selected=""><?php if(!empty($categorys)){ echo $categorys; } else { echo '--Select Category--'; } ?></option>
													<?php $mysql=mysqli_query($conn,"SELECT * FROM `category` WHERE (status='0' or status='1' or status='2') ORDER BY category_name ASC");
													while($ro=mysqli_fetch_object($mysql)){ ?>
 
													<option style="background-image:url(../uploads/<?=$ro->image;?>);" class='ww'><?=$ro->category_name; ?></option>
													<?php } ?>
												</select>
											</div>
										</div>
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Price*</label>
												<input type="currency" name="price" class="form-control" placeholder="&#8377;0" value='<?php if(!empty($prices)){ echo $prices; }?>' required/>
											</div>
										</div>
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">MRP*</label>
												<input type="currency2" name="mrp" class="form-control" placeholder="&#8377;0" value='<?php if(!empty($mrps)){ echo $mrps; }?>' required/>
											</div>
										</div>
										
		

			<div class="col-lg-12 col-md-!2" >
							
			<table style="margin-top: 11px;">
				<tr>
					<td>
					<div class="form-group">
						UNIT -- 
					</div>
				</td>	<td>
					<td>
					<div class="form-group">
						<label class="control-label">
							<input type="checkbox" name="color" value="color">
							Available in colors
						</label>
					</div>
				</td><td> -- </td><td>
					<div class="form-group">
						<label class="control-label">
							<input type="checkbox" name="size" value="size">
							Available in sizes
						</label>
					</div>
				</td>
				
			</td>
		</tr>
	</table>
		</div>
	

						<div class="col-lg-12 col-md-!2" style="margin-top: 1em;">
											<div class="form-group">
												<label class="form-label">Product Detail</label>
												<div class="card card-editor">
													<div class="content-editor">
														<textarea id="froala-editor" name="discription" placeholder="Enter Product details.." required/><?php if(!empty($details)){ echo $details; }?></textarea>
														 <!-- <div id='edit' name='Description'></div> -->
													</div>
												</div>
											</div>
										</div>
										
											<div class="col-lg-4 col-md-4">
											<div class="form-group">
												<label class="form-label">Product Image*</label>
												<div class="input-group">
													<div class="custom-file">
														<input id="file1" type="file" name="image1"  class="custom-file-input " />
														
														<label class="custom-file-label" for="file">Choose Image</label>
													</div>
												</div>
												<div class="add-cate-img">
													<span id="uploaded_image1">

													<img src="<?php if(!empty($image1s)){ echo '../uploads/'.$image1s; } else{ echo 'images/product/big-1.jpg'; }?>" alt="your image" class="im" />
															<!-- Progress bar -->
														</span>
											</div>
												
											</div>
										</div>
										<div class="col-lg-4 col-md-4">
											<div class="form-group">
												<label class="form-label">More Image*</label>
												<div class="input-group">
													<div class="custom-file">
														<input id="file2" type="file" name="image2" class="custom-file-input " />
														<label class="custom-file-label" for="file2">Choose Image</label>
													</div>
												</div>
														<div class="add-cate-img-1">
															<div class="add-cate-img">
													<span id="uploaded_image2">
													<img src="<?php if(!empty($image2s)){ echo '../uploads/'.$image2s; } else{ echo 'images/product/big-1.jpg'; }?>" alt="your image" class="im" />
															<!-- Progress bar -->
														</span>
											</div>
															<!-- <img src="images/product/big-1.jpg" alt=""> -->
														</div>
												
											</div>
										</div>
										<div class="col-lg-4 col-md-4">
											<div class="form-group">
												<label class="form-label">More Image*</label>
												<div class="input-group">
													<div class="custom-file">
														<input id="file3" type="file" name="image3" class="custom-file-input "  />
														<label class="custom-file-label" for="file3">Choose Image</label>
													</div>
												</div>
														<div class="add-cate-img-1">
															<span id="uploaded_image3">

															<img src="<?php if(!empty($image3s)){ echo '../uploads/'.$image3s; } else{ echo 'images/product/big-1.jpg'; } ?>" class="im" alt="">
														</span>
														</div>
												
											</div>
										</div>
										<div class="col-lg-12 col-md-12">
										
											<?php if(!isset($id)){ ?>
											<button name='submit' class="save-btn hover-btn" type="submit" style="float: right!important;">Add New Product</button>
										<?php } else { ?>

											<button name='update' class="save-btn hover-btn" type="submit" style="float: right!important;">Update Product</button>
										<?php } ?>
										</div> 

									</div>
								</form>
							
									</div>
								</div>
							</div>
                        </div>
                    </div>
                </main>
              
<?php include("footer.php"); ?>
 
 <script>
$(document).ready(function(){
 $(document).on('change', '#file1', function(){
  var name = document.getElementById("file1").files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("file1").files[0]);
  var f = document.getElementById("file1").files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 200000)
  {
   alert("Image File Size is very big");
  }
  else
  {
   form_data.append("file", document.getElementById('file1').files[0]);
   $.ajax({
    url:"upload.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image1').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
     $('#uploaded_image1').html(data);
     // alert("successfully upload Click ( OK ) to submit");
     
    }
   });
  }
 });
});



$(document).ready(function(){
 $(document).on('change', '#file2', function(){
  var name = document.getElementById("file2").files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("file2").files[0]);
  var f = document.getElementById("file2").files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 2000000)
  {
   alert("Image File Size is very big");
  }
  else
  {
   form_data.append("file", document.getElementById('file2').files[0]);
   $.ajax({
    url:"upload.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image2').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
     $('#uploaded_image2').html(data);
     // alert("successfully upload Click ( OK ) to submit");
     
    }
   });
  }
 });
});



$(document).ready(function(){
 $(document).on('change', '#file3', function(){
  var name = document.getElementById("file3").files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("file3").files[0]);
  var f = document.getElementById("file3").files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 2000000)
  {
   alert("Image File Size is very big");
  }
  else
  {
   form_data.append("file", document.getElementById('file3').files[0]);
   $.ajax({
    url:"upload.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image3').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
     $('#uploaded_image3').html(data);
     // alert("successfully upload Click ( OK ) to submit");
     
    }
   });
  }
 });
});
</script>


