<?php
if($_POST){
    
    if(isset($_GET['duzenle_id'])){
        $islem = $db->prepare("UPDATE urun SET baslik = ?, sef = ?, description = ?, aciklama = ?, eski_fiyat = ?, fiyat = ?, stok = ?, stok_kodu = ?, marka_id = ? WHERE id = ?");
        $islem = $islem->execute(array($_POST['baslik'],'',$_POST['description'],$_POST['aciklama'],$_POST['eski_fiyat'],$_POST['fiyat'],$_POST['stok'],$_POST['stok_kodu'],$_POST['marka_id'],$_GET['duzenle_id']));


        $id = $_GET['duzenle_id'];
        
        $delete = $db->exec("DELETE FROM urun_kategori WHERE urun_id = '{$id}' ");
        $delete = $db->exec("DELETE FROM urun_renk WHERE urun_id = '{$id}' ");
        $delete = $db->exec("DELETE FROM urun_img WHERE urun_id = '{$id}' ");
        $query = $db->query("SELECT * FROM urun_secenek WHERE urun_id = '{$id}'", PDO::FETCH_ASSOC);
		if($query->rowCount()){
			foreach( $query as $row ){
				$delete = $db->exec("DELETE FROM urun_secenek_alt WHERE urun_secenek_id = '{$row['id']}'");
				$delete = $db->exec("DELETE FROM urun_secenek WHERE id = '{$row['id']}'");
			}
		}


    }else{
        $islem = $db->prepare("INSERT INTO urun SET baslik = ?, sef = ?, description = ?, aciklama = ?, eski_fiyat = ?, fiyat = ?, stok = ?, stok_kodu = ?, marka_id = ?");
        $islem = $islem->execute(array($_POST['baslik'],'',$_POST['description'],$_POST['aciklama'],$_POST['eski_fiyat'],$_POST['fiyat'],$_POST['stok'],$_POST['stok_kodu'],$_POST['marka_id']));
        $id = $db->lastInsertId();
    }

    if(isset($_POST['renk_urun_id'])){
    	foreach ($_POST['renk_urun_id'] as $renk_urun_id) {
    		$islem = $db->prepare("INSERT INTO urun_renk SET urun_id = ?, renk_urun_id = ?");
        	$islem = $islem->execute(array($id,$renk_urun_id));
    	}
    }


    if(isset($_POST['img'])){
    	foreach ($_POST['img'] as $img) {
    		$islem = $db->prepare("INSERT INTO urun_img SET urun_id = ?, img = ?");
        	$islem = $islem->execute(array($id,$img));
    	}
    }

    $i = 0;
    if(isset($_POST['secenek_adi'])){
    	foreach ($_POST['secenek_adi'] as $s) {
    		$islem = $db->prepare("INSERT INTO urun_secenek SET urun_id = ?, baslik = ?");
        	$islem = $islem->execute(array($id,$s));
        	$secenek_id = $db->lastInsertId();

        	$ii = 0;
        	if(isset($_POST['alt_secenek_adi'.$i])){
        		foreach ($_POST['alt_secenek_adi'.$i] as $as) {
        			$islem = $db->prepare("INSERT INTO urun_secenek_alt SET urun_secenek_id = ?, baslik = ?, stok = ?, fiyat = ?");
        			$islem = $islem->execute(array($secenek_id,$as,$_POST['alt_secenek_stok'.$i][$ii],$_POST['alt_secenek_fiyat'.$i][$ii],));
        			$ii++;
        		}
        	}

        	$i++;
    	}
    }


    if($_POST['kategori']){
		foreach ($_POST['kategori'] as $k) {
			$islem = $db->prepare("INSERT INTO urun_kategori SET urun_id = ?, kategori_id = ?");
        	$islem = $islem->execute(array($id,$k));
		}
	}

    if($islem){
    	$sef = sef($_POST['baslik']).'-'.$id;
    	$islem = $db->prepare("UPDATE urun SET sef = ? WHERE id = ?");
        $islem = $islem->execute(array($sef,$id));
        echo b();
    }else{
        echo h();
    }
	
}

if(isset($_GET['duzenle_id'])){
    $duzenle = $db->query("SELECT * FROM urun WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    ?>
		<script type="text/javascript">
			$(function(){
				$('select[name="marka_id"] option[value="<?php echo $duzenle['marka_id']; ?>"]').attr('selected','select');
				<?php
				$cek = $db->query("SELECT * FROM urun_kategori WHERE urun_id = '{$_GET['duzenle_id']}' ", PDO::FETCH_ASSOC);
				if($cek->rowCount()){
					foreach( $cek as $c ){
						?>
							$('input[name="kategori[]"][value="<?php echo $c['kategori_id']; ?>"]').attr('checked','checked');
						<?php
					}
				}
				?>
			});
		</script>
	<?php
}
?>

<div class="page-header">
	<div class="page-leftheader">
		<h4 class="page-title">Ürün Yönetimi</h4>
	</div>
	<div class="page-rightheader ml-auto d-lg-flex d-none">
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="index.php">Anasayfa</a></li>
			<li class="breadcrumb-item active" aria-current="page">Ürün Yönetimi</li>
		</ol>
	</div>
</div>
						
<div class="row">
	
	<div class="col-md-12">
		<div class="card">
			<div class="card-body">
				<div class="row">
					<div class="col-md-12">
						
						<form class="form-horizontal" method="post" action="">

							<div class="form-group row">
							  <label class="col-sm-3 form-label">Kategori</label>
							  <div class="col-sm-9">

							  	<div class="transfer-double-list-content" style="border:1px solid #ddd;">
							  		<div class="transfer-double-list-main">
							  			<ul class="transfer-double-group-list-ul">
							  				<?php
											$cek = $db->query("SELECT * FROM kategori", PDO::FETCH_ASSOC);
											if($cek->rowCount()){
												foreach( $cek as $c ){
									  				echo '<li class="transfer-double-group-list-li">
									  					<div class="checkbox-group">
									  						<input type="checkbox" class="checkbox-normal" name="kategori[]" value="'.$c['id'].'" id="kategori'.$c['id'].'">
									  						<label for="kategori'.$c['id'].'">#'.$c['id'].' '.$c['baslik'].'</label>
									  					</div>
									  				</li>';
									  			}
									  		}
									  		?>
							  			</ul>
							  		</div>
							  	</div>

							  </div>
							</div>

							<div class="form-group row">
							  <label class="col-sm-3 form-label">Marka</label>
							  <div class="col-sm-9">
								<select class="form-control" name="marka_id">
									<option value="">Marka Seçiniz</option>
									<?php
										$cek = $db->query("SELECT * FROM marka", PDO::FETCH_ASSOC);
										if($cek->rowCount()){
											foreach( $cek as $c ){
												echo '<option value="'.$c['id'].'">#'.$c['id'].' '.$c['baslik'].'</option>';
											}
										}
									?>
								</select>
							  </div>
							</div>

							<div class="form-group row">
								<label class="col-md-3 form-label">Ürün Adı</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="baslik" value="<?php echo @$duzenle['baslik']; ?>" required="">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-md-3 form-label">Kısa Açıklama (description)</label>
								<div class="col-md-9">
									<textarea class="form-control" rows="3" name="description"><?php echo @$duzenle['description']; ?></textarea>
								</div>
							</div>
							<div class="form-group row">
								<label class="col-md-3 form-label">Stok Kodu</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="stok_kodu" value="<?php echo @$duzenle['stok_kodu']; ?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-md-3 form-label">Stok Sayısı</label>
								<div class="col-md-9">
									<input type="number" class="form-control" name="stok" value="<?php echo @$duzenle['stok']; ?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-md-3 form-label">Eski Fiyat</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="eski_fiyat" value="<?php echo @$duzenle['eski_fiyat']; ?>">
								</div>
							</div>

							<div class="form-group row">
								<label class="col-md-3 form-label">Fiyat</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="fiyat" value="<?php echo @$duzenle['fiyat']; ?>">
								</div>
							</div>

							<div class="form-group row">
								<label class="col-md-3 form-label">Varyant Ekle</label>
								<div class="col-md-9">
									<div class="row" id="secenekler">
										<?php
											$i = 0;
											if(isset($_GET['duzenle_id'])){
												$cek = $db->query("SELECT * FROM urun_secenek WHERE urun_id = '{$_GET['duzenle_id']}' ", PDO::FETCH_ASSOC);
												if($cek->rowCount()){
													foreach( $cek as $c ){
														echo '<div class="col-md-12" data-secenek="'.$i.'">
																<div class="row form-group">
																	<div class="col-md-8"><input type="text" class="form-control" name="secenek_adi[]" value="'.$c['baslik'].'" placeholder="Varyant Adı"></div>
																	<div class="col-md-1"><button type="button" data-secenek-sil="'.$i.'" class="btn btn-danger">Sil</button></div>
																	<div class="col-md-3"><button type="button" data-alt-secenek-ekle="'.$i.'" class="btn btn-success">Alt Seçenek Ekle</button></div>
																</div>
																<div class="row form-group alt_senecekler" data-alt-secenek="'.$i.'">
																';

																$ii = 0;
																$cek1 = $db->query("SELECT * FROM urun_secenek_alt WHERE urun_secenek_id = '{$c['id']}' ", PDO::FETCH_ASSOC);
																if($cek1->rowCount()){
																	foreach( $cek1 as $c1 ){
																		echo '<div class="col-md-12" data-alt-secenek-dis="'.$ii.'">
																				<div class="row form-group">
																					<div class="col-md-3">
																						<input type="text" class="form-control" placeholder="Alt Seçenek Adı" value="'.$c1['baslik'].'" name="alt_secenek_adi'.$i.'[]">
																					</div>
																					<div class="col-md-3">
																						<input type="text" class="form-control" placeholder="Stok Sayısı" value="'.$c1['stok'].'" name="alt_secenek_stok'.$i.'[]">
																					</div>
																					<div class="col-md-3">
																						<input type="text" class="form-control" placeholder="+fiyat" value="'.$c1['fiyat'].'" name="alt_secenek_fiyat'.$i.'[]">
																					</div>
																					<div class="col-md-3">
																						<button type="button" data-alt-secenek-sil="'.$ii.'" class="btn btn-danger">Sil</button>
																					</div>
																				</div>
																			</div>';
																		$ii++;
																	}
																}


														echo 	'</div>
															</div>';
											            $i++;
													}
													?>
													<script type="text/javascript">
														$(document).ready(function(){
															$('#ekle').hide(1000);
														});
													</script>
													<?php
												}
											}
										?>

									</div>
									<button type="button" id="ekle" class="btn btn-success">Varyant Ekle</button>
								</div>
							</div>
							<script type="text/javascript">
								$(document).ready(function(){



									$('#ekle').click(function(){
										var say = $('[data-secenek]').length;
										$('#secenekler').append('\
											<div class="col-md-12" data-secenek="'+say+'">\
												<div class="row form-group">\
													<div class="col-md-8"><input type="text" class="form-control" name="secenek_adi[]" placeholder="Seçenek Adı"></div>\
													<div class="col-md-1"><button type="button" data-secenek-sil="'+say+'" class="btn btn-danger">Sil</button></div>\
													<div class="col-md-3"><button type="button" data-alt-secenek-ekle="'+say+'" class="btn btn-success">Alt Seçenek Ekle</button></div>\
												</div>\
												<div class="row form-group alt_senecekler" data-alt-secenek="'+say+'"></div>\
											</div>\
										');
										$('#ekle').hide(1000);
									});

									$(document).on('click','[data-secenek-sil]', function(){
										$('[data-secenek="'+$(this).attr('data-secenek-sil')+'"]').remove();
										$('#ekle').fadeIn(1000);
									});

									$(document).on('click','[data-alt-secenek-ekle]', function(){
										var say = $('[data-alt-secenek-dis]').length;
										$('[data-alt-secenek="'+$(this).attr('data-alt-secenek-ekle')+'"]').append('\
											<div class="col-md-12" data-alt-secenek-dis="'+say+'">\
												<div class="row form-group">\
													<div class="col-md-3">\
														<input type="text" class="form-control" placeholder="Alt Seçenek Adı" name="alt_secenek_adi'+$(this).attr('data-alt-secenek-ekle')+'[]">\
													</div>\
													<div class="col-md-3">\
														<input type="text" class="form-control" placeholder="Stok Sayısı" name="alt_secenek_stok'+$(this).attr('data-alt-secenek-ekle')+'[]">\
													</div>\
													<div class="col-md-3">\
														<input type="text" class="form-control" placeholder="Fiyat" name="alt_secenek_fiyat'+$(this).attr('data-alt-secenek-ekle')+'[]">\
													</div>\
													<div class="col-md-3">\
														<button type="button" data-alt-secenek-sil="'+say+'" class="btn btn-danger">Sil</button>\
													</div>\
												</div>\
											</div>\
										');
									});

									$(document).on('click','[data-alt-secenek-sil]', function(){
										$('[data-alt-secenek-dis="'+$(this).attr('data-alt-secenek-sil')+'"]').remove();
									});

								});
							</script>


							<div class="form-group row">
								<label class="col-md-3 form-label">Renk Ekle</label>
								<div class="col-md-9">
									<div class="row" id="renksecenekler">
										
										<?php
											$i = 0;
											if(isset($_GET['duzenle_id'])){
												$cek = $db->query("SELECT * FROM urun_renk WHERE urun_id = '{$_GET['duzenle_id']}' ", PDO::FETCH_ASSOC);
												if($cek->rowCount()){
													foreach( $cek as $c ){
														?>
														<div class="col-md-12" data-renksecenekdis="<?php echo $i; ?>">
															<div class="row form-group">
																<div class="col-md-8">
																<select class="form-control" name="renk_urun_id[]" required="">
																	<option value="">Ürün Seçiniz</option>
																	<?php
																		$cek1 = $db->query("SELECT * FROM urun", PDO::FETCH_ASSOC);
																		if($cek1->rowCount()){
																			foreach( $cek1 as $c1 ){
																				$aktif = '';
																				if($c['renk_urun_id'] == $c1['id']){
																					$aktif = 'selected';
																				}
																				echo '<option value="'.$c1['id'].'" '.$aktif.'>'.$c1['baslik'].'</option>';
																			}
																		}
																	?>
																</select>
																</div>
																<div class="col-md-4"><button type="button" data-renksecenek-sil="<?php echo $i; ?>" class="btn btn-danger">Sil</button></div>
															</div>
														</div>
														<?php
														$i++;
													}
												}
											}
										?>
									</div>
									<button type="button" id="renkekle" class="btn btn-success">Renk Ekle</button>
								</div>
							</div>
							<script type="text/javascript">
								$(document).ready(function(){

									$('#renkekle').click(function(){
										var say = $('[data-renksecenekdis]').length;
										$('#renksecenekler').append('\
											<div class="col-md-12" data-renksecenekdis="'+say+'">\
												<div class="row form-group">\
													<div class="col-md-8">\
													<select class="form-control" name="renk_urun_id[]" required="">\
														<option value="">Ürün Seçiniz</option>\
														<?php
															$cek = $db->query("SELECT * FROM urun", PDO::FETCH_ASSOC);
															if($cek->rowCount()){
																foreach( $cek as $c ){
																	echo '<option value="'.$c['id'].'">'.addslashes($c['baslik']).'</option>';
																}
															}
														?>
													</select>\
													</div>\
													<div class="col-md-4"><button type="button" data-renksecenek-sil="'+say+'" class="btn btn-danger">Sil</button></div>\
												</div>\
											</div>\
										');
									});

									$(document).on('click','[data-renksecenek-sil]', function(){
										$('[data-renksecenekdis="'+$(this).attr('data-renksecenek-sil')+'"]').remove();
									});

								});
							</script>

							


							<div class="form-group row" id="resimler">
								<?php
									$i = 0;
									if(isset($_GET['duzenle_id'])){
										$cek = $db->query("SELECT * FROM urun_img WHERE urun_id = '{$_GET['duzenle_id']}' ", PDO::FETCH_ASSOC);
										if($cek->rowCount()){
											foreach( $cek as $c ){
												echo '<div class="col-md-3" data-resim-dis-id="'.$i.'">
									                    <div class="uploaddis pasif" style="float:left;">
									        			  <div class="yuklendi">
									        				  <img src="../upload/'.$c['img'].'">
									        				  <div class="icon" data-resim-sil-id="'.$i.'"><span class="fa fa-trash"></span></div>
									        				  <input type="hidden" name="img[]" value="'.$c['img'].'" required="">
									        			  </div>
									        			</div>
									                </div>';
									            $i++;
											}
										}
									}
								?>
							</div>

							<div class="form-group row">
								<label class="col-md-3 form-label">&nbsp;</label>
				                <div class="col-md-3">
				                    <div class="uploaddis aktif" data-id="1" style="float:left;">
				        			  <div class="upload1">
				        				  <span class="metin" style="width: 100%;float: left;">Ürün Resimi Yükle</span>
				        				  <div class="icon"><span class="fa fa-upload" data-id="1"></span></div>
				        			  </div>
				        			</div>
				                </div>
				            </div>

							<div class="form-group row">
								<label class="col-md-12 form-label">Ürün Açıklaması</label>
								<div class="col-md-12">
									<textarea class="form-control" id="editor" rows="3" name="aciklama"><?php echo @$duzenle['aciklama']; ?></textarea>
								</div>
							</div>

							<div class="form-group row">
				                <div class="col-md-3">
				                    <div class="uploaddis aktif" data-id="1" style="float:left;">
				        			  <div class="yuklendi">
				        				  <img src="">
				        				  <div class="icon" data-id="1"><span class="fa fa-trash"></span></div>
				        				  <input type="hidden" name="img1" value="" required="">
				        			  </div>
				        			  <div class="upload">
				        				  <span class="metin" style="width: 100%;float: left;">Resim Yükle</span>
				        				  <div class="icon"><span class="fa fa-upload" data-id="1"></span></div>
				        			  </div>
				        			</div>
				                </div>
								<label class="col-md-9 form-label">
									<input type="text" class="form-control" id="url" name="" value="" placeholder="Resim Url Adresi">
								</label>
				            </div>


							<div class="form-group row">
								<div class="col-md-12"><center><button type="submit" class="btn btn-info">Kaydet</button></center></div>
							</div>


							<div id="queue"></div>

						</form>

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

<script src="assets/ckeditor/ckeditor.js"></script>
<script src="assets/ckeditor/samples/js/sample.js"></script>
<script>
	initSample();
</script>