<?php include 'inc/header.php';?>
<?php include 'inc/sidebar.php';?>
<?php include 'inc/navbar.php';?>
<link href="assets/switch.css" rel="stylesheet"/>
<!-- Page Content-->

<?php
if ($_POST) {  
	$coupon_name = $_POST['coupon_name'];
	$coupon_code = $_POST['coupon_code'];
	$discount_amount = $_POST['discount_amount'];
	$minimum_amount = $_POST['minimum_amount']; 

	if ($coupon_name <> "" && $coupon_code <> "" && $discount_amount <> "" && $minimum_amount <> "" ) {

		$satir = [
			'coupon_name' => $coupon_name,
			'coupon_code' => $coupon_code,
			'discount_amount' => $discount_amount,
			'minimum_amount' => $minimum_amount,
		];

		$sql = "INSERT INTO coupon SET coupon_name=:coupon_name, coupon_code=:coupon_code, discount_amount=:discount_amount, minimum_amount=:minimum_amount;";
		$durum = $dbh->prepare($sql)->execute($satir);
		if ($durum) {
			$sonId = $dbh->lastInsertId();
			echo "<script>
			const Toast = Swal.mixin({
				toast: true,
				position: 'top-end',
				showConfirmButton: false,
				timer: 6000,
				timerProgressBar: true,
				didOpen: (toast) => {
					toast.addEventListener('mouseenter', Swal.stopTimer)
					toast.addEventListener('mouseleave', Swal.resumeTimer)
				}
				})

				Toast.fire({
					icon: 'success',
					title: 'Başarı ile kayıt edildi.'
					})
					</script>";
				}
			}else{
				echo "Tüm alanları doldurunuz";

			}
		}
		?>


		<div class="page-content">
			<div class="container-fluid">
				<!-- Page-Title -->
				<div class="row">
					<div class="col-sm-12">
						<div class="page-title-box">
							<div class="row">
								<div class="col">
									<h4 class="page-title">Kupon Listesi</h4>
									<ol class="breadcrumb">
										<li class="breadcrumb-item"><a href="anasayfa">Anasayfa</a></li> 
										<li class="breadcrumb-item active">Kupon Listesi</li>
									</ol>
								</div><!--end col--> 
								<div class="col-auto align-self-center"> 
									<button type="button" class="btn btn-outline-primary waves-effect waves-light" data-toggle="modal" data-target="#exampleModalDanger11"> <i data-feather="plus-square"></i></i> Kupon Ekle</button>   
								</div><!--end col-->  
							</div><!--end row-->                                                              
						</div><!--end page-title-box-->
					</div><!--end col-->
				</div><!--end row-->
				<!-- end page title end breadcrumb -->
				<style type="text/css">
					i {  
						font-size: 16px;
					}
				</style>
				<div class="row"> 
					<div class="col-lg-12">
						<div class="card">
							<div class="card-header">
								<h4 class="card-title">Kupon Listesi</h4>
								<p class="text-muted mb-0">Değerli kullanıcı, bu alan sayesinde indirim kuponu oluşturabilirsiniz. Örneğin 100 liralik sepet tutarı için 5 liralik indirim verebilirsiniz. Aktif - Pasif modülü sayesinde geçici olarak kuponu pasif yapabilirsiniz.
								</p>
							</div><!--end card-header-->
							<div class="card-body">
								<div class="table-responsive">
									<table class="table table-bordered mb-0 table-centered">
										<thead>
											<tr>
												<th>#</th> 
												<th>Kupon Adı</th>
												<th>Kupon Kodu</th>
												<th>İndirim Tutarı</th>
												<th>Minimum Tutarı</th> 
												<th>Tarih</th>
												<th>Durum</th>
												<th>İşlem</th>   
											</tr>
										</thead>
										<tbody>
											<?php
											$ebultensorgu = $dbh->prepare("SELECT * FROM coupon ");
											$ebultensorgu->execute();

											while ($ebultensonuc = $ebultensorgu->fetch()) {

												$id = $ebultensonuc['id'];
												$coupon_name = $ebultensonuc['coupon_name']; 
												$coupon_code = $ebultensonuc['coupon_code']; 
												$discount_amount = $ebultensonuc['discount_amount'];
												$minimum_amount = $ebultensonuc['minimum_amount'];
												$status = $ebultensonuc['status']; 
												$date = $ebultensonuc['date'];
												$phpdate = strtotime( $date );
												$mysqldate = date( 'd-m-Y ', $phpdate );
												?>
												<tr>
													<td style="width: 10px; height: 10px;">#<?=$ebultensonuc['id'];?></td> 
													<td contenteditable="true" onBlur="kuponKaydet(this,'coupon_name','<?php echo $id ?>')"
														onClick="duzenle(this);"><?=$ebultensonuc['coupon_name'];?></td> 
														<td contenteditable="true" onBlur="kuponKaydet(this,'coupon_code','<?php echo $id ?>')"
															onClick="duzenle(this);"><?=$ebultensonuc['coupon_code'];?></td>
															<td contenteditable="true" onBlur="kuponKaydet(this,'discount_amount','<?php echo $id ?>')"
																onClick="duzenle(this);"><?=$ebultensonuc['discount_amount'];?></td>
																<td contenteditable="true" onBlur="kuponKaydet(this,'minimum_amount','<?php echo $id ?>')"
																	onClick="duzenle(this);"><?=$ebultensonuc['minimum_amount'];?></td>	
																	
																	<td><?=$mysqldate?></td> 
																	<td>
																		<label class="switch"> 
																			<input type="checkbox" id='<?php echo $ebultensonuc['id'] ?>' class="kuponPasif" <?php echo $ebultensonuc['status']==1?'checked':'' ?>  />
																			<span class="slider round"></span>
																		</label>
																	</td>
																	<td> <button type="button" class="btn btn-outline-danger btn-icon-circle-sm" data-toggle="modal" data-target="#exampleModalDanger<?=$ebultensonuc['id'];?>" ><i class="ti-trash"></i></button> </td>
																</tr>


																<!-- silme başlangıçı -->

																<div class="modal fade" id="exampleModalDanger<?=$ebultensonuc['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalDanger<?=$ebultensonuc['id'];?>" aria-hidden="true">
																	<div class="modal-dialog" role="document">
																		<div class="modal-content">
																			<div class="modal-header bg-danger">
																				<h6 class="modal-title m-0 text-white" id="exampleModalDanger<?=$ebultensonuc['id'];?>"><?=$ebultensonuc['title'];?></h6>
																				<button type="button" class="close " data-dismiss="modal" aria-label="Close">
																					<span aria-hidden="true"><i class="la la-times text-white"></i></span>
																				</button>
																			</div><!--end modal-header-->
																			<div class="modal-body">
																				<div class="row"> 
																					<div class="col-lg-12">
																						<h5>Bu veriyi silmek istiyormusunuz ?</h5>
																						<span class="badge bg-soft-secondary">Eklenme Tarihi:</span>
																						<small class="text-muted ml-2"> <?=$mysqldate?></small>
																						<ul class="mt-3 mb-0">
																							<li>Bu veri silinirse geri döndürülemez.</li> 
																						</ul>
																					</div><!--end col-->
																				</div><!--end row-->                                                     
																			</div><!--end modal-body-->
																			<div class="modal-footer">
																				<button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Kapat</button>
																				<a href="islem.php?id=<?=$ebultensonuc['id']; ?>&islem=kupon-sil"><button type="button" class="btn btn-danger btn-sm">Evet, Sil</button></a>
																			</div><!--end modal-footer-->
																		</div><!--end modal-content-->
																	</div><!--end modal-dialog-->
																</div><!--end modal-->
															<?php } ?>


														</tbody>
													</table><!--end /table-->
													<h5 id="ebultensonuc"></h5>
												</div><!--end /tableresponsive-->
											</div><!--end card-body-->
										</div><!--end card-->
									</div> <!-- end col -->
								</div> <!-- end row -->


								<div class="modal fade" id="exampleModalDanger11" tabindex="-1" role="dialog" aria-labelledby="exampleModalDanger11" aria-hidden="true">
									<div class="modal-dialog" role="document">
										<div class="modal-content">
											<div class="modal-header bg-danger">
												<h6 class="modal-title m-0 text-white" id="exampleModalDanger11">Yeni Kupon Ekle</h6>
												<button type="button" class="close " data-dismiss="modal" aria-label="Close">
													<span aria-hidden="true"><i class="la la-times text-white"></i></span>
												</button>
											</div><!--end modal-header-->
											<form method="post">
												
												<div class="modal-body">
													<div class="row">
														<div class="col-xl-6"> 
															<div class="form-group">
																<label for="exampleInputEmail1">Kupon Başlığı * </label>
																<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Kupon Başlığı" name="coupon_name" required=""> 
															</div>
														</div>

														<div class="col-xl-6"> 
															<div class="form-group">
																<label for="exampleInputEmail1">Kupon Kodu * </label>
																<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Kupon Kodu" name="coupon_code" required=""> 
															</div>
														</div>

														<div class="col-xl-6"> 
															<div class="form-group">
																<label for="exampleInputEmail1">İndirim Tutarı (sepette uygulanacak TL miktarı) * </label>
																<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="İndirim Tutarı" name="discount_amount" required=""> 
															</div>
														</div>

														<div class="col-xl-6"> 
															<div class="form-group">
																<label for="exampleInputEmail1">Minimum Tutar (uygulanabilecek minumum sepet tutarı) * </label>
																<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Minimum Tutar" name="minimum_amount" required=""> 
															</div>
														</div>

													</div>  

												</div><!--end modal-body-->
												<div class="modal-footer">
													<button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Kapat</button>
													<button type="submit" class="btn btn-danger btn-sm">Oluştur</button>
												</div><!--end modal-footer-->
											</form> 
										</div><!--end modal-content-->
									</div><!--end modal-dialog-->
								</div><!--end modal-->
							</div><!-- container -->


							<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
							<script>
								function duzenle(deger) {
									$(deger).css("background", "#ffff00");
        //seçilen hücrenin rengini değiştiriyoruz
    }

    function kuponKaydet(deger, alan, id) {
    	$(deger).css("background", "#FFF url(yukleniyor.gif) no-repeat right");
        //

        $.ajax({
            url: "kuponKaydet.php", //verileri göndereceğimiz url
            type: "POST", //post ile gönderilecek
            data: 'alan=' + alan + '&deger=' + deger.innerHTML.split('+').join('{0}')+ '&id=' + id, 
            // verileri alan deger ve id olarak yolluyoruz
            //+ (artı) post edilirken boşluk ile değişiyor 
            //bunu engellemek için + değeri {0} ile değiştirdim 
            //kayıt yaparkende index.php de geri değişimini yapıyoruz 
            success: function (data) {
            	if (data == true) {
            		$(deger).css("background", "#fff");
                    // eğer veriler veri tabanına yazılmış ise hücrenin
                    //arka plan rengini beyaza geri döndürüyoruz
                }

                else {
                	$(deger).css("background", "#f00");
                	$("#ebultensonuc").text("Hata veri düzenlenmedi");

                    //Eğer hata varsa hücre rengini kırmızı ve
                    // tablo altında hata mesajı yazdırıyoruz
                }
            }
        });
    }
</script>

<script src="assets/jquery.min.js"></script>
<script src="assets/ccustom.js"></script>
<?php include 'inc/footer.php';?>