<?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) { 




	if(isset($_POST['ilcekle'])){
		$sehir=$_POST['sehir'];

		$ilce=mb_strtoupper($_POST['ilced']);

		$ilceko      =   $dbh->prepare("SELECT * FROM ilce WHERE ilce_sehirkey=? && ilce_title=? ");
		$ilceko      ->  execute(array($sehir,$ilce));
		$ilceko      =   $ilceko      ->fetch(PDO::FETCH_OBJ);
		$rand=rand(5000,999999);
		if(!$ilceko){

			$satir = [
				'ilce_sehirkey' => $sehir,
				'ilce' => $ilce,
				'ilce_key' =>"99".$rand,
			];

			$sql = "INSERT INTO ilce SET ilce_title=:ilce , ilce_key=:ilce_key , ilce_sehirkey=:ilce_sehirkey ,aktif=1;";
			$durum = $dbh->prepare($sql)->execute($satir);
		}else{
			echo "Bu adres ekli";
		}


	}




	if(isset($_POST['mahallekle'])){
		$ilced=$_POST['ilced'];

		$mahalle=mb_strtoupper($_POST['mahalled']);

		$mahalleko      =   $dbh->prepare("SELECT * FROM mahalle WHERE mahalle_ilcekey=? && mahalle_title=? ");
		$mahalleko      ->  execute(array($ilced,$mahalle));
		$mahalleko      =   $mahalleko      ->fetch(PDO::FETCH_OBJ);
		$rand=rand(5000,9999999999);
		if(!$mahalleko){

			$satir = [
				'mahalle_ilcekey' => $ilced,
				'mahalle_title' => $mahalle,
				'mahalle_key' =>"99".$rand,
			];

			$sql = "INSERT INTO mahalle SET mahalle_title=:mahalle_title , mahalle_key=:mahalle_key , mahalle_ilcekey=:mahalle_ilcekey ,aktif=1;";
			$durum = $dbh->prepare($sql)->execute($satir);
		}else{
			echo "Bu adres ekli";
		}


	}





	$adres = $_POST['adres']; 
	$tutar = $_POST['fiyat']; 

	$satir = [
		'adres' => $adres,
		'tutar' => $tutar
	];

	$sql = "INSERT INTO adresler SET adres=:adres , tutar=:tutar;";
	$durum = $dbh->prepare($sql)->execute($satir);

	if ($durum) {
		$sonId = $dbh->lastInsertId();
		echo '<div class="col-md-6 col-lg-6"> 
		<div class="card-body collapse show" id="collapse8">
		<div class="error-notice"> 
		<div class="oaerror success">
		<strong>Eklendi..</strong> - Belirtilen Veri Başarı ile Eklendi
		</div>
		</div>
		</div> 
		</div>';

	}
}

?>





<div class="modal fade" id="ilceekle" tabindex="-1" role="dialog" aria-labelledby="ilceekle" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<form action="" method="post">
				<div class="modal-header bg-danger">
					<h6 class="modal-title m-0 text-white" id="exampleModalDanger<?=$id;?>">İlce 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-->
				<div class="modal-body">
					<div class="row"> 
						<div class="col-lg-12">
							<div class="form-group row">
								<label class=" text-left mb-1 font-semibold standart_label" for="telefon">Şehir*</label>

								<select name="sehir"  id="slct" onchange="ilcegetir(this)" class="form-control " >

									<?php 

									$sehirler = $dbh -> prepare("SELECT * FROM sehir where aktif=1 order by sehir_title");
									$sehirler-> execute(array());
									$sehirler = $sehirler->fetchAll(PDO::FETCH_OBJ);
									foreach ($sehirler as $sehir) { ?>
										<option value="<?=$sehir->sehir_key?>"><?=$sehir->sehir_title?></option>
									<?php } ?>
								</select>

								<label>İlçe</label>

								<input type="text" class="form-control" required="" name="ilced" >

							</div>


							
						</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>
					<button type="submit" name="ilcekle" class="btn btn-danger btn-sm">Ekle</button>
				</div><!--end modal-footer-->
			</form>
		</div><!--end modal-content-->
	</div><!--end modal-dialog-->
</div><!--end modal-->


<div class="modal fade" id="mahalleekle" tabindex="-1" role="dialog" aria-labelledby="mahalleekle" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<form action="" method="post">
				<div class="modal-header bg-danger">
					<h6 class="modal-title m-0 text-white" id="exampleModalDanger<?=$id;?>">Mahalle 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-->
				<div class="modal-body">
					<div class="row"> 
						<div class="col-lg-12">
							<div class="form-group row">
								<label class=" text-left mb-1 font-semibold standart_label" for="telefon">Şehir*</label>

								<select name="sehir"  id="slct" onchange="ilcegetir(this)" class="form-control " >
									<option value="">Şehir Seç</option>

									<?php 

									$sehirler = $dbh -> prepare("SELECT * FROM sehir where aktif=1 order by sehir_title");
									$sehirler-> execute(array());
									$sehirler = $sehirler->fetchAll(PDO::FETCH_OBJ);
									foreach ($sehirler as $sehir) { ?>
										<option value="<?=$sehir->sehir_key?>"><?=$sehir->sehir_title?></option>
									<?php } ?>
								</select>

								<label>İlçe</label>

								<select name="ilced"  id="slctilce"    class="form-control " >
									<option value="">Önce Şehir Seç</option>
								</select>

								<label>Mahalle İsmi</label>

								<input type="text" class="form-control" required="" name="mahalled" >

							</div>


							
						</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>
					<button type="submit" name="mahallekle" class="btn btn-danger btn-sm">Ekle</button>
				</div><!--end modal-footer-->
			</form>
		</div><!--end modal-content-->
	</div><!--end modal-dialog-->
</div><!--end modal-->




<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">Adres Listesi</h4>
							<ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="anasayfa">Anasayfa</a></li> 
								<li class="breadcrumb-item active">Adres 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="#ilceekle"> <i data-feather="plus-square"></i></i> İlce Ekle</button>   
							<button type="button" class="btn btn-outline-primary waves-effect waves-light" data-toggle="modal" data-target="#mahalleekle"> <i data-feather="plus-square"></i></i> Mahalle 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">

					<form action="adresislem.php?islem=adres-guncelleme" method="post">

						<div class="card-header">
							<h4 class="card-title">Hizmet Verilen İller</h4>

						</div><!--end card-header-->
						<div class="card-body row">

							<div class="col-xl-6">  
								<div class="form-group">
									<label for="exampleInputEmail1">Hizmet Verilecek İller *</label>
									<select class="form-control" id="iller" required="" onchange="ilcelergetir(this)" multiple="multiple" style="    height: 150px;">
										<?php

										$iller = $dbh -> prepare("SELECT * FROM sehir order by sehir_title");
										$iller-> execute(array());
										$iller = $iller->fetchAll(PDO::FETCH_OBJ);
										foreach ($iller as $il) { ?>
											<option value="<?=$il->sehir_key?>" <?php if($il->aktif==1){echo "selected";} ?>><?=$il->sehir_title?></option>
										<?php } ?>


									</select>
								</div>
							</div>


							<div class="col-xl-6">  
								<div class="form-group">
									<label for="exampleInputEmail1">Hizmet Verilecek İlçeler *</label>
									<select class="form-control" name="" required="" multiple="multiple" onchange="ilcesec(this)" id="ilceler" style="    height: 150px;">
										<?php

										$ilceler = $dbh -> prepare("SELECT * FROM ilce where aktif=1 order by ilce_title");
										$ilceler-> execute(array());
										$ilceler = $ilceler->fetchAll(PDO::FETCH_OBJ);
										foreach ($ilceler as $ilce) { ?>
											<option value="<?=$ilce->ilce_key?>" <?php if($ilce->aktif==1){echo "selected";} ?>><?=$ilce->ilce_title?></option>
										<?php } ?>

									</select>
								</div>
							</div>

							<input type="hidden" name="iller" id="ilsecilen" value="">
							<input type="hidden" name="ilceler" id="ilcesecilen" value="">

							<button type="submit" class="btn btn-success btn-block">Düzenle</button>
						</div><!--end card-body-->
					</form>
				</div><!--end card-->



				<div class="card">
					<div class="card-header">
						<h4 class="card-title">Adres Listesi</h4>
						<p class="text-muted mb-0">Değerli kullanıcı, bu alan sayesinde masa siparişleri için her masaya özel QR kod üretebilirsiniz. QR kodu görüntülemek için mavi ikona basarak önünüze çıkan QR koda sağ tıklayıp resmi farklı kaydet diyerek indirebilirsiniz.
						</p>
					</div><!--end card-header-->
					<div class="card-body">
						<div class="table-responsive">
							<table id="datatable-buttonss" class="table table-bordered mb-0 table-centered">
								<thead>
									<tr>
										<th>#</th>  
										<th>Aktif-Pasif</th>  
										<th>Mahalle</th> 
										<th>Min Sipariş Tutarı</th> 
										<th>İşlemler</th>  
									</tr>
								</thead>
								<tbody>
									<?php

									$ilceler = $dbh -> prepare("SELECT * FROM ilce where aktif=1 ");
									$ilceler-> execute(array());
									$ilceler = $ilceler->fetchAll(PDO::FETCH_OBJ);
									foreach ($ilceler as $ilce ) {
										$ilceliste.=$ilce->ilce_key.",";
									}
									$ilceliste.=0;


									$mahalleler = $dbh -> prepare("SELECT * FROM mahalle where  mahalle_ilcekey in ($ilceliste) ");
									$mahalleler-> execute(array());
									$mahalleler = $mahalleler->fetchAll(PDO::FETCH_ASSOC);
									foreach ($mahalleler as $adres ) {


										$ilcekey = $dbh -> prepare("SELECT * FROM ilce where ilce_key=? ");
										$ilcekey-> execute(array($adres['mahalle_ilcekey']));
										$ilcekey = $ilcekey->fetch(PDO::FETCH_OBJ);

										$id = $adres['mahalle_key'];
										$aktif = $adres['aktif'];
										$adress = $adres['mahalle_title'];
										$tutar = $adres['tutar'];  
										$mahalle_ilcekey = $adres['mahalle_ilcekey']; 
										?>
										<tr>
											<td style="width: 10px; height: 10px;">#<?=$id;?></td>  
											<td style="width: 10px; height: 10px;"><?php if($aktif==0){echo "<font style='color:red;'>Pasif</font>";}else{echo "<font style='color:green;'>Aktif</font>";}?></td>  
											<td><?=$ilcekey->ilce_title;?> / <?=$adress;?>	 </td> 
											<td><?=$tutar;?> TL	 </td> 
											<td>
												<button type="button" data-toggle="modal" data-target="#duzenle<?=$id;?>" class="btn btn-outline-primary btn-icon-circle-sm" data-toggle="tooltip" data-placement="top"><i class="ti-settings"></i></button>
												<button type="button" class="btn btn-outline-danger btn-icon-circle-sm" data-toggle="modal" data-target="#sil<?=$id;?>" ><i class="ti-trash"></i></button> 
											</td>
										</tr>




										<div class="modal fade" id="duzenle<?=$id;?>" tabindex="-1" role="dialog" aria-labelledby="duzenle<?=$id;?>" aria-hidden="true">
											<div class="modal-dialog" role="document">
												<div class="modal-content">
													<form action="islem.php?islem=adres-duzenle&id=<?=$id?>" method="post">
														<div class="modal-header bg-danger">
															<h6 class="modal-title m-0 text-white" id="exampleModalDanger<?=$id;?>"><?=$adress;?></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">
																	<label>Mahalle</label>

																	<input type="text" class="form-control" name="adres" value="<?=$adress?>">
																	<label>Minimum Tutar</label>
																	<input type="number" class="form-control" name="fiyat" value="<?=$tutar?>">

																	<label>Aktif - Pasif</label>
																	
																	<select name="durum" required="" class="form-control">
																		<option value="1" <?php if($aktif==1){echo "selected";} ?>>Aktif</option>
																		<option value="0" <?php if($aktif==0){echo "selected";} ?>>Pasif</option>
																	</select>

																</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>
															<button type="submit" class="btn btn-danger btn-sm">Düzenle</button>
														</div><!--end modal-footer-->
													</form>
												</div><!--end modal-content-->
											</div><!--end modal-dialog-->
										</div><!--end modal-->


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

										<div class="modal fade" id="sil<?=$id;?>" tabindex="-1" role="dialog" aria-labelledby="sil<?=$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<?=$id;?>"><?=$adress;?></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>
																<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=<?=$id; ?>&islem=mahalle-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><!-- 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 veriKaydet(deger, alan, id) {
    	$(deger).css("background", "#FFF url(yukleniyor.gif) no-repeat right");
        //

        $.ajax({
																url: "sosyalKaydet.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");
                	$("#sonuc").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/custom.js"></script>
<?php include 'inc/footer.php';?>


<script type="text/javascript">
	yaz();
	function yaz(){
		var foo = $('#iller').val(); 
		$('#ilsecilen').val(foo); 
		var foo = $('#ilceler').val(); 
		$('#ilcesecilen').val(foo); 
	}

	function ilcelergetir(selectObject) {

		var foo = $('#iller').val(); 
		$('#ilsecilen').val(foo); 



		$.ajax({
			url:"adresislem.php",
			data    :'id='+foo,
			type    : 'POST',
			success : function(sonuc){
				$('#ilceler').html(sonuc); 

				var ilcs = $('#ilceler').val(); 
				$('#ilcesecilen').val(ilcs); 



			}
		});

	}
	function ilcesec(select){
		var foo = $('#ilceler').val(); 
		$('#ilcesecilen').val(foo); 
	}
</script>
<script type="text/javascript">
	$('#datatable-buttonss').DataTable({

		responsive: true,

		language: {

			searchPlaceholder: 'Ara...',

			sSearch: ''

		},

		"order": [[ 2, "asc" ]]

	});



	function ilcegetir(selectObject) {
		var value = selectObject.value;

		$.ajax({
			url:"../post/adresislem.php",
			data    :'tur=ilcegetir&id='+value,
			type    : 'POST',
			success : function(sonuc){
				$('#slctilce').html(sonuc); 

			}
		});

	}

</script>