<?php include("header.php");
include("side.php"); 

include("../db.php"); 
extract($_GET);
$id=$_SESSION['id'];
$rr=mysqli_query($conn,"SELECT * FROM `shops` WHERE id='$id'");
while($ro=mysqli_fetch_object($rr)){  $images=$ro->image; }
if(isset($submit)){
date_default_timezone_set("Asia/Calcutta");
$insertdate = date("Y-m-d h:i:s");
// if(mysqli_num_rows($rr) >0 ){
// 	 $alert=' <div class="alert alert-danger">Store Name <b>Already Added please enter another!!</b></div>';
// } else {

	// $sql=mysqli_query($conn,"INSERT INTO `shops`(`shop_name`, `categtory`, `delivery_charge`, `km`, `shop_address`, `disc`, `image`, `open_time`, `close_time`, `name`, `email`, `mobile`, `address`) VALUES ('$shop_name', '$categtory', '$delivery_charge', '$km', '$shop_address', '$discription', '$image', '$open_time', '$close_time', '$name', '$email', '$mobile', '$address')");

if(empty($image)){ $image=$images; }
$sql=mysqli_query($conn,"UPDATE `shops` SET `shop_name`='$shop_name', `categtory`='$categtory', `delivery_charge`='$delivery_charge', `km`='$km', `shop_address`='$shop_address', `disc`='$discription', `image`='$image', `open_time`='$open_time', `close_time`='$close_time', `name`='$name', `email`='$email', `mobile`='$mobile', `address`='$address', `status`='1', `date`='$insertdate' WHERE id='$id'");
	$alert='<div class="alert alert-success">Store successfully update</div>';

// }
 }

?>
<style>
 	.card-editor {
    border: 1px solid #ced4da !important;
}
.im{
	  height: 80px!important;
    width: 80px!important;
    margin-top: -20px;
 	}
 	.ww{
 		height: 20px;
 		width: 20px;
 		
 	}

 </style>

 <?php 
$rr=mysqli_query($conn,"SELECT * FROM `shops` WHERE id='".$_SESSION['id']."'");
 while($row=mysqli_fetch_object($rr)){ ?>
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid">
                        <h2 class="mt-30 page-title">Store</h2>
                        <ol class="breadcrumb mb-30">
                            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="shop_view.php?main=2&cat=1">Store</a></li>
                            <li class="breadcrumb-item active">Edit Store</li>
                        </ol>
                        <?php if(isset($alert)){ echo $alert; } ?>
                        <div class="row">
                        	
							<div class="col-lg-12">
								<div class="add-new-shop">
									<div class="card card-static-2 mb-30">
										<form action="" method="get">
										<div class="row no-gutters">
											<div class="col-lg-6 col-md-6">
												<div class="card-title-2">
													<h4>Edit Store</h4>
												</div>
												<div class="card-body-table">
													<div class="add-shop-content pd-20">
														
														<div class="form-group">
															<label class="form-label">Shop Name*</label>
															<input type="text" name="shop_name" class="form-control" placeholder="Shop Name" value="<?=$row->shop_name;?>" required/>
														</div>
														
														<div class="form-group">
															<label class="form-label">Category*</label>
															<select id="categtory" name="categtory" class="form-control" required/>
																<option selected=""><?php if(!empty($row->categtory)){ echo $row->categtory; } else{ echo '--Select Category--'; } ?></option>
																<?php 
													$sql=mysqli_query($conn,"SELECT * FROM `category` WHERE 1");
													$sn='1';
													while ($rows=mysqli_fetch_object($sql)) {
													?>	
																<option ><?=$rows->category_name;?></option>
																
															
														<?php } ?>
														</select>
														</div>
														<div class="form-group">
															<label class="form-label">Delivery Charge*</label>
															<input type="number" class="form-control" name="delivery_charge" placeholder="&#8377;0" value="<?=$row->delivery_charge;?>" required/>
														</div>
														
														<div class="form-group">
															<label class="form-label">Delivery Maximum KM*</label>
															<input type="number" name="km" class="form-control" placeholder="0" value="<?=$row->km;?>" required/>
														</div>
														
														<div class="form-group">
															<label class="form-label">Shop Address*</label>
															<div class="card card-editor">
																<div class="content-editor">
																	 <textarea class="text-control" name="shop_address"placeholder="Enter Address"  required/><?php if(!empty($row->shop_address)){ echo $row->shop_address; }  ?></textarea>
																</div>
															</div>
														</div>
														<div class="form-group">
															<label class="form-label">Description*</label>
															<div class="card card-editor">
																<div class="content-editor">
																	 <textarea class="text-control" name="discription" placeholder="Enter Description" id="froala-editor"><?php if(!empty($row->disc)){ echo $row->disc; } ?></textarea>
																</div>
															</div>
														</div>
														<div class="form-group">
														<label class="form-label">Shop Image*</label>
														<div class="input-group">
															<div class="custom-file">
																<input id="uploadImage1" type="file" onchange="PreviewImage1();" class="custom-file-input uploadimg" aria-describedby="inputGroupFileAddon04" name="image" >
																
																<label class="custom-file-label" for="inputGroupFile04">Choose Image</label>
															</div>
														</div>
														<div class="add-cate-img">
															<img id="uploadPreview1" src="<?php if(!empty($row->image)){ echo '../uploads/'.$row->image; } else{ echo 'images/product/big-1.jpg'; } ?>" alt="your image" class="im" />
															<!-- Progress bar -->
											</div>
												
											</div>
														<button class="save-btn hover-btn" name="submit" type="submit">Update store</button>
													</div> 
												</div>
											</div>
											<div class="col-lg-6 col-md-6">
												<div class="card-title-2">
													<h4>Edit Store</h4>
												</div>
												<div class="card-body-table">
													<div class="add-shop-content pd-20">
														<div class="form-group">
															<label class="form-label">Opening Time*</label>
															<input type="time" name="open_time" id="default-picker" class="form-control" value="<?php if(!empty($row->open_time)){ echo $row->open_time; } else{ echo '--Select time--'; } ?>" placeholder="Select time">
														</div>
														<div class="form-group">
															<label class="form-label">Close Time*</label>
															<input type="time" name="close_time" id="default-picker" class="form-control" value="<?php if(!empty($row->close_time)){ echo $row->close_time; } else{ echo '--Select time--'; } ?>" placeholder="Select time">
														</div>
													</div>
												</div>
												
												<div class="card-title-2">
													<h4>Shop Owner</h4>
												</div>
												<div class="card-body-table">
													<div class="add-shop-content pd-20">
														<div class="form-group">
															<label class="form-label">Full Name*</label>
															<input class="form-control" name="name" type="text" placeholder="Enter Full Name" value="<?php if(!empty($row->name)){ echo $row->name; }  ?>" required/>
														</div>
														
														<div class="form-group">
															<label class="form-label">Email Address*</label>
															<input class="form-control" name="email" type="email" placeholder="Enter Email Address" value="<?php if(!empty($row->email)){ echo $row->email; }  ?>" required/>
														</div>
														<div class="form-group">
															<label class="form-label">Phone Number*</label>
															<input class="form-control" name="mobile" type="text" placeholder="Enter Phone Number" value="<?php if(!empty($row->mobile)){ echo $row->mobile; } ?>" required/>
														</div>
														<div class="form-group">
															<label class="form-label">Owner Address*</label>
															<div class="card card-editor">
																<div class="content-editor">
																	 <textarea class="text-control" name="address" placeholder="Enter Address"><?php if(!empty($row->address)){ echo $row->address; } ?></textarea>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
										</div>
									</form>
									</div>
								</div>
							</div>
                        </div>
                    </div>
                </main>
            
              <?php }  include("footer.php"); ?>
               <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
                <script>
    function PreviewImage1() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage1").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview1").src = oFREvent.target.result;
        };
    };

      function PreviewImage2() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage2").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview2").src = oFREvent.target.result;
        };
    };

      function PreviewImage3() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage3").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview3").src = oFREvent.target.result;
        };
    };

      function PreviewImage4() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage4").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview4").src = oFREvent.target.result;
        };
    };


  </script>
			
  <script>
$(document).ready(function(){
    $('.uploadimg').on('change', function() {
        console.log("test");
    var file_data = $(this).prop('files')[0];   
    var form_data = new FormData();
    var ext = $(this).val().split('.').pop().toLowerCase();
    if ($.inArray(ext, ['png','jpg','jpeg','gif']) == -1)   {
        alert("only jpg, jpeg, gif and png images allowed");
        return;
    }  
    var picsize = (file_data.size);
    console.log(picsize); /*in byte*/
    if(picsize > 2097152) /* 2mb*/
        {
            alert("Image allowd less than 2 mb")
            return;
        }
    form_data.append('file', file_data);   
    $.ajax({
        url: 'upload.php', /*point to server-side PHP script */
        dataType: 'text',  /* what to expect back from the PHP script, if anything*/
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(res){
           console.log(res);
        }
     });
});
})

</script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<script>
  new FroalaEditor('textarea#froala-editor')
</script>