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

include("../db.php"); 
extract($_GET);
if(isset($submit)){
date_default_timezone_set("Asia/Calcutta");
$insertdate = date("Y-m-d h:i:s");
	$sql=mysqli_query($conn,"INSERT INTO `offer`(`offer_category`, `title`, `offer`, `expiry`, `image`, `disc`, `status`, `date`) VALUES ('$category', '$title', '$offer', '$expiry', '$image', '$discription', '1', '$insertdate')");
	$alert='<div class="alert alert-success">Offer successfully Add</div>';

?>

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

<link rel="stylesheet" type="text/css" href="css/boot.css">
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid">
                        <h2 class="mt-30 page-title">Offers</h2>
                        <ol class="breadcrumb mb-30">
                            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="offers.php?main=8&cat=1">Offers</a></li>
                            <li class="breadcrumb-item active">Add Offer</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">
										<h4>Add New Offer</h4>
									</div>
								<div class="card-body-table">
									<div class="news-content-right pd-20">
										<?php if(isset($alert)){ echo $alert; } ?>
									
										<form action="" method="get">
                                            <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="">--Select Category--</option>
    <?php $mysql=mysqli_query($conn,"SELECT * FROM `category` WHERE (status='0' or status='1' or status='2')");
                                                    while($ro=mysqli_fetch_object($mysql)){ ?>
 
                                                    <option><?=$ro->category_name; ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Title*</label>
												<input type="text" name="title" class="form-control" placeholder="Offer Title" required/>
											</div>
										</div>
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Discount MRP*</label>
												<input type="text" name='offer' class="form-control" placeholder="Up to 25% off on Grocery & Staples & much more" required/>
											</div>
										</div>
											<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Offer Expiry Date*</label>
												<input type="date" name="expiry" class="form-control" placeholder="Expiry date" required/>
											</div>
										</div>
										
										<div class="col-lg-6 col-md-6">
											<div class="form-group">
												<label class="form-label">Product Image*</label>
												<div class="input-group">
													<div class="custom-file">
														<input id="uploadImage1" type="file" name="image" 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>
                                        </div>
                                        <div class="col-lg-6 col-md-6">
                                            <div class="form-group">
												<div class="add-cate-img">
													<img id="uploadPreview1" src="images/product/big-1.jpg" alt="your image" class="im" />
															<!-- Progress bar -->
											</div>
												
											</div>
										</div>
										<div class="col-lg-12 col-md-!2">
											<div class="form-group">
												<label class="form-label">Offer Detail</label>
												<div class="card card-editor">
													<div class="content-editor">
														<textarea id="froala-editor" name="discription" placeholder="Enter offer details.." required/></textarea>
														 <!-- <div id='edit' name='Description'></div> -->
													</div>
												</div>
											</div>
										</div>
                                        <div class="col-lg-12 col-md-!2">
											<button class="save-btn hover-btn" name="submit" type="submit">Add New Offer</button>
										</div>
                                    </div>
										</form> 
									</div>
								</div>
							</div>
                        </div>
                    </div>
                </main>
                <?php include("footer.php"); ?>
                     <script>
			tail.select(".select");
		</script>
 <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>