<?php
	if(!isset($_SESSION['login_user'])){
		header('location: ../../login.php'); // Mengarahkan ke Home Page
	}

	if(isset($_SESSION['user']) AND $_SESSION['user'] <> 'TRUE')
	{
		?>
		  <div class="alert alert-danger alert-dismissible" id="succsess-alert">
	        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
	        <h4><i class="icon fa fa-ban"></i> Alert!</h4>
	        Dilarang mengakses file ini.
	      </div>
		<?php
	}

	//link buat paging
	$linkaksi = "med.php?mod=identitas";

	if(isset($_GET['act']))
	{
		$act = $_GET['act'];
		$linkaksi .= "&act=".$act;
	}
	else
	{
		$act = "";
	}

	$aksi = "mod/sistem/act_identitas.php";

	switch ($act) {
		case 'form':
			$akses_master = array();
			if(!empty($_GET['id']))
			{
				$act = "$aksi?mod=identitas&act=edit&idmx=".$_GET['idmx'];
				$query = mysqli_query($conn,"SELECT * FROM `tb_identitas` WHERE `id` = '$_GET[id]'");
				$temukan = mysqli_num_rows($query);

				if($temukan > 0)
				{
					$c = mysqli_fetch_assoc($query);


					//echo print_r($akses_master);
				}
				else
				{
					header("location:med.php?mod=identitas&idmx=".$_GET['idmx']);
				}

			}
			else
			{
				$act = "$aksi?mod=identitas&act=simpan&idmx=".$_GET['idmx'];	
			}
			?>
			<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
				<div class="breadcrumb-title pe-3">Sistem</div>
				<div class="ps-3">
					<nav aria-label="breadcrumb">
						<ol class="breadcrumb mb-0 p-0">
							<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
							</li>
							<li class="breadcrumb-item active" aria-current="page">Form <?php echo ucwords($title) ?></li>
						</ol>
					</nav>
				</div>
			</div>
			<?php
	        flash('example_message');
	        ?>
			<div class="row">
				<div class="col-lg-12">
					<div class="card ">
						<div class="card-header bg-primary text-center">
							<h4 class="m-b-0 text-white">Form <?php echo ucwords($title) ?></h4>
						</div>

						<form class="form-horizontal" role="form" method='POST' action='<?php echo $act ?>' enctype="multipart/form-data">
							<div class="card-body">
								<div class="row">
									<div class="col-md-6">				                            
										<input type="hidden" name="id" value="<?php echo isset($c['id']) ? $c['id'] : '' ;?>">				                            	
										<input type="hidden" name="img" value="<?php echo isset($c['logo']) ? $c['logo'] : '' ;?>">				                            	
										
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Perumahan</label>
											<div class="col-lg-8">
												<input type="text" name="nama" class="form-control " value="<?php echo isset($c['nama']) ? $c['nama'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
											</div>
										</div>
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Pemilik</label>
											<div class="col-lg-8">
												<input type="text" name="pemilik" class="form-control" value="<?php echo isset($c['pemilik']) ? $c['pemilik'] : '' ;?>" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?> required>
											</div>
										</div>
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Alamat</label>
											<div class="col-lg-8">
												<textarea class="form-control" name="alamat" rows="3" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>><?php echo isset($c['alamat']) ? $c['alamat'] : '' ;?></textarea>
											</div>
										</div>
										<div class="row mb-3">
											<label class="col-lg-4 col-form-label">Provinsi</label>
											<div class="col-lg-8">
												<select class="form-control select2" style="width: 100%;" name="provinsi" id="provinsi" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
													<option value=""> Pilih Provinsi</option>
													<?php
														$sql_kategori = mysqli_query($conn,"SELECT * FROM `provinsi`");
														while($k = mysqli_fetch_assoc($sql_kategori))
														{
															if(isset($c['provinsi']) && $k['id_prov'] == $c['provinsi'])
															{
																echo"<option value='$k[id_prov]' selected>$k[nama]</option>";	
															}
															else
															{
																echo"<option value='$k[id_prov]'>$k[nama]</option>";
															}
															
														}
													?>
												</select>
											</div>
										</div>
										<div class="row mb-3">
											<label class="col-lg-4 col-form-label">Kabupaten</label>
											<div class="col-lg-8">
												<select class="form-control select2" style="width: 100%;" name="kabupaten" id="kabupaten" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
													<option value=""></option>
													<?php
														$sql_kategori = mysqli_query($conn,"SELECT * FROM `kabupaten` WHERE id_prov = '$c[provinsi]'");
														while($k = mysqli_fetch_assoc($sql_kategori))
														{
															if(isset($c['kabupaten']) && $k['id_kab'] == $c['kabupaten'])
															{
																echo"<option value='$k[id_kab]' selected>$k[nama]</option>";	
															}
															else
															{
																echo"<option value='$k[id_kab]'>$k[nama]</option>";
															}
															
														}
													?>
												</select>
											</div>
										</div>
										<div class="row mb-3">
											<label class="col-lg-4 col-form-label">Kecamatan</label>
											<div class="col-lg-8">
												<select class="form-control select2" style="width: 100%;" name="kecamatan" id="kecamatan" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
													<option value=""></option>
													<?php
														$sql_kategori = mysqli_query($conn,"SELECT * FROM `kecamatan` WHERE id_kab = '$c[kabupaten]'");
														while($k = mysqli_fetch_assoc($sql_kategori))
														{
															if(isset($c['kecamatan']) && $k['id_kec'] == $c['kecamatan'])
															{
																echo"<option value='$k[id_kec]' selected>$k[nama]</option>";	
															}
															else
															{
																echo"<option value='$k[id_kec]'>$k[nama]</option>";
															}
															
														}
													?>
												</select>
											</div>
										</div>
										<div class="row mb-3">
											<label class="col-lg-4 col-form-label">Kelurahan</label>
											<div class="col-lg-8">
												<select class="form-control select2" style="width: 100%;" name="kelurahan" id="kelurahan" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
													<option value=""></option>
													<?php
														$sql_kategori = mysqli_query($conn,"SELECT * FROM `kelurahan` WHERE id_kec = '$c[kecamatan]'");
														while($k = mysqli_fetch_assoc($sql_kategori))
														{
															if(isset($c['kelurahan']) && $k['id_kel'] == $c['kelurahan'])
															{
																echo"<option value='$k[id_kel]' selected>$k[nama]</option>";	
															}
															else
															{
																echo"<option value='$k[id_kel]'>$k[nama]</option>";
															}
															
														}
													?>
												</select>
											</div>
										</div>
									</div>		
									<div class="col-md-6">
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Telepon</label>
											<div class="col-lg-8">
												<input type="text" name="telepon" class="form-control" value="<?php echo isset($c['tlp']) ? $c['tlp'] : '' ;?>" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
											</div>
										</div>
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Email</label>
											<div class="col-lg-8">
												<input type="email" name="email" class="form-control" value="<?php echo isset($c['email']) ? $c['email'] : '' ;?>" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
											</div>
										</div>
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Website</label>
											<div class="col-lg-8">
												<input type="text" name="website" class="form-control" value="<?php echo isset($c['website']) ? $c['website'] : '' ;?>" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
											</div>
										</div>
										<div class="row mb-3">
											<label for="inputEmail1" class="col-lg-4 col-form-label">Logo Perusahaan</label>
											<div class="col-lg-8">
												<div class="fileupload fileupload-new" data-provides="fileupload">
													<div class="fileupload-new thumbnail" >
														<img class="img-fluid img-thumbnail" src="<?php echo isset($c['logo']) ? 'images/identitas/'.$c['logo'] : 'images/identitas/noimage.png' ;?>" alt="" />
													</div>
													<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
													<div>
														<span class="btn btn-secondary btn-file">
															<span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
															<span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
															<input type="file" name="foto" class="default" accept="image/jpeg" <?php echo isset($c['logo']) ? 'images/identitas/'.$c['logo'] : 'images/identitas/noimage.png' ;?> <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>/>
														</span>
													</div>
												</div>
											</div>
										</div>
									</div>  
								</div>
							</div>
							<hr>
							<div class="d-flex align-items-center justify-content-between">
								<button type="submit" name="submit" value="simpan" class="btn btn-success"><i class='fa fa-save'></i> Simpan</button>
								<button type="button" name="submit" onclick="history.back()" class="btn btn-dark"><i class='fa fa-rotate-left'></i> Kembali</button>
							</div>
						</form>
					</div>
				</div>
			</div>

			<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
			<script type="text/javascript">
				$(document).ready(function(){
					var jenis = "provinsi";
					var provinsi = $("#provinsi").val();
					$.ajax({
						type: 'POST',
						url: "mod/sistem/get_daerah.php",
						data: {provinsi: provinsi,jenis:jenis},
						cache: false, 
						success: function(msg){
							$("#provinsi").html(msg);
						}
					});
				
					$("#provinsi").change(function(){
						var provinsi = $("#provinsi").val();
						var jenis = "kabupaten";
						$.ajax({
							type: 'POST',
							url: "mod/sistem/get_daerah.php",
							data: {provinsi: provinsi, jenis:jenis},
							cache: false,
							success: function(msg){
								$("#kabupaten").html(msg);
							}
						});
					});
				
					$("#kabupaten").change(function(){
						var kabupaten = $("#kabupaten").val();
						var jenis = "kecamatan";
						$.ajax({
							type: 'POST',
							url: "mod/sistem/get_daerah.php",
							data: {kabupaten: kabupaten, jenis:jenis},
							cache: false,
							success: function(msg){
								$("#kecamatan").html(msg);
							}
						});
					});
				
					$("#kecamatan").change(function(){
						var kecamatan = $("#kecamatan").val();
						var jenis = "kelurahan";
						$.ajax({
							type: 'POST',
							url: "mod/sistem/get_daerah.php",
							data: {kecamatan: kecamatan, jenis:jenis},
							cache: false,
							success: function(msg){
								$("#kelurahan").html(msg);
							}
						});
					});
				});
			</script>
			<?php
		break;
		case 'bayar':
			?>
			<div class="row">

		        <div class="clearfix">
			
		            <div class="col-sm-12">
		                <section class="panel">
		                	 <div class="panel-body">
		                	 	<div class="feed-box text-center">
				                    <section class="panel">
				                        <div class="panel-body">
				                            <div class="corner-ribon blue-ribon">
				                                <i class="fa fa-hospital-o"></i>
				                            </div>
				                            <h1><b>Pembayaran Buka Cabang Baru</b></h1>
				                            <p style="color: red">
				                            	Silahkan Lakukan Pemabayaran Ke Bank Berikut <br>
				                            	Jika Telah Melakukan Pembayaran Silahkan Upload Bukti Pembayaran di Menu Identitas Berdasarkan Toko Yang Akan di Aktifkan
				                            </p>
				                            <p></p>
				                        </div>
				                    </section>
				                </div>

		                	 </div>
		                </section>
		            </div>
		        </div>
		    </div>
			<?php
		break;
		
		default :

				$qmn = "SELECT * FROM tb_group_menu WHERE `kode_group` = '$level_check' AND id_menu='$_GET[idmx]'";
				$sqqmn = mysqli_query($conn,$qmn);	
				$mn = mysqli_fetch_assoc($sqqmn);
		?>
		<!--breadcrumb-->
		<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
			<div class="breadcrumb-title pe-3">Sistem</div>
			<div class="ps-3">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb mb-0 p-0">
						<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
						</li>
						<li class="breadcrumb-item active" aria-current="page">Data <?php echo ucwords($title) ?></li>
					</ol>
				</nav>
			</div>
			<div class="ms-auto">
				<?php
					if ($mn['insert']=="Aktif") {
					?>	
						<a href="med.php?mod=identitas&act=form&idmx=<?php echo $mn['id_menu'] ?>">
							<button class="btn btn-primary px-4"><i class="bi bi-plus-lg me-2"></i>Add Identity</button>
						</a>
				<?php }?>
			</div>
		</div>
		<!--end breadcrumb-->
		<?php
        flash('example_message');
        ?>
		<div class="card mt-4">
			<div class="card-body">
				<div class="table-responsive">
					<table id="example" class="table table-striped table-hover align-middle">
						<thead>
							<tr>
								<th class="text-center">No</th>
								<th class="text-center">Perumahan</th>
								<th class="text-center">Pemilik</th>
								<th class="text-center">Alamat</th>
								<th class="text-center">Telepon</th>
								<th class="text-center">Email</th>
								<th class="text-center">Status</th>
								<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
									<th class="text-center">Aksi</th>
								<?php }?>
							</tr>
						</thead>
						<tbody>
							<?php
								$query = "SELECT * FROM `tb_identitas` ORDER BY id DESC";
								$sql_kul = mysqli_query($conn,$query);	
								$i=1;
								while ($m = mysqli_fetch_assoc($sql_kul)) {
							?>
							<tr class="">
								<td align="center"><?php echo $i ?></td>
								<input type="hidden" name="id" value="<?php echo $m['id'] ?>">
								<td align=""><?php echo $m['nama']?></td>
								<td align=""><?php echo $m['pemilik']?></td>
								<td align=""><?php echo $m['alamat']?>, <?php echo kelurahan($m['kelurahan'])?>, Kec. <?php echo kecamatan($m['kecamatan'])?>, Kab. <?php echo kabupaten($m['kabupaten'])?>, <?php echo provinsi($m['provinsi'])?> </td>
								<td align=""><?php echo $m['tlp']?></td>
								<td align=""><?php echo $m['email']?></td>
								<td align="center">
									<?php
										if ($m['status']=="Aktif") {
										?>
										<span class="lable-table bg-success-subtle text-success rounded border border-success-subtle font-text2 fw-bold">Active<i class="bi bi-check2 ms-2"></i></span>
										<?php
										}elseif ($m['status']=="Pending") {
											$queryb = "SELECT * FROM `tb_pembayaran_buka_cabang` WHERE `id` = '$m[id]'";
											$sqlb = mysqli_query($conn,$queryb);
											$mx = mysqli_fetch_assoc($sqlb);
											$jm = 	mysqli_num_rows($sqlb);
										if ($jm == 0) {
										?>
										<a href="#myModal<?php echo $m['id'] ?>" data-toggle="modal" class="btn btn-warning btn-xs">
											Upload Bukti
										</a>
										<?php
										} else {
											if ($mx['status'] == "Pending") {		                                    					
											?>
											
											<button data-original-title="Status <?php echo $mx['status'] ?>" data-content="<?php echo $mx['keterangan'] ?>" data-placement="top" data-trigger="hover" class="btn btn-danger popovers btn-xs"><?php echo $mx['status'] ?></button>
											
											 
											<?php
												} elseif ($mx['status'] == "Terima") {
												?>
												<span class="lable-table bg-success-subtle text-success rounded border border-success-subtle font-text2 fw-bold">Active<i class="bi bi-check2 ms-2"></i></span>
												<?php
												} else{
												?>
												<button data-original-title="Status <?php echo $mx['status'] ?>" data-content="<?php echo $mx['keterangan'] ?>" data-placement="top" data-trigger="hover" class="btn btn-danger popovers btn-xs"><?php echo $mx['status'] ?></button>
												 
												<?php
												}
											
											}
											
										} else {
										?>
										<span class="lable-table bg-danger-subtle text-danger rounded border border-danger-subtle font-text2 fw-bold">Inactive<i class="bi bi-x-lg ms-2"></i></span>
										<?php
										}
										
										?>
									</td>
									<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
									<td align="center">
										<div class="btn-group" role="group" aria-label="Aksi">
											<?php if ($mn['view'] == "Aktif") { ?>					
												<a href="<?php echo $linkaksi ?>&act=form&view=1&id=<?php echo $m['id'] ?>&idmx=<?php echo $mn['id_menu'] ?>" 
													class="btn btn-outline-info btn-sm" 
													title="View Data">
													<i class="material-icons-outlined">visibility</i>
												</a>
											<?php } ?>
											<?php if ($mn['update'] == "Aktif") { ?>					
												<a href="<?php echo $linkaksi ?>&act=form&id=<?php echo $m['id'] ?>&idmx=<?php echo $mn['id_menu'] ?>" 
													class="btn btn-outline-success btn-sm" 
													title="Edit Data">
													<i class="material-icons-outlined">edit</i>
												</a>
											<?php } ?>
											<?php if ($mn['delete'] == "Aktif") { ?>		
												<button type="button" 
													class="btn btn-outline-danger btn-sm btn-hapus" 
													data-url="<?php echo $aksi; ?>?mod=identitas&act=hapus&id=<?php echo $m['id']; ?>&idmx=<?php echo $mn['id_menu']; ?>" 
													title="Hapus Data">
													<i class="material-icons-outlined">close</i>
												</button>
											<?php } ?>
										</div>
									</td>
									<?php } ?>
							</tr>
							<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal<?php echo $m['id'] ?>" class="modal fade">
								<div class="modal-dialog">
									<div class="modal-content">
										<div class="modal-header">
											<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
											<h4 class="modal-title" align="center">Silahkan Upload Bukti Pembayaran</h4>
										</div>
										<div class="modal-body">

											<form role="form" method='POST' action='<?php echo $aksi ?>?mod=identitas&act=upload&idmx=<?php echo $mn['id_menu'] ?>' enctype="multipart/form-data">
												<div class="form-group" align="center">
													<input type="hidden" name="id" value="<?php echo $m['id'] ?>">
													<h2>BUKTI PEMBAYARAN</h2>
													<div class="fileupload fileupload-new" data-provides="fileupload">
														<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
															<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" />
														</div>
														<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
														<div>
															<span class="btn btn-white btn-file">
																<span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
																<span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
																<input type="file" name="foto" class="default" />
															</span>
															<a href="#" class="btn btn-danger fileupload-exists" data-dismiss="fileupload"><i class="fa fa-trash"></i> Remove</a>
														</div>
													</div>
													<span class="label label-danger">NOTE!</span>
													<span style="color: red">
														Setelah Berhasil Upload Tunggu Sampai Status Berubah Aktif 
													</span>
												</div>
												<div class="modal-footer">
													<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
													<button type="submit" class="btn btn-primary" type="button">Upload</button>
												</div>
											</form>
										</div>
									</div>
								</div>
							</div>
							<?php
								$i++;
							}
							?>
						</tbody>
						<tfoot>
							<tr>
								<th class="text-center">No</th>
								<th class="text-center">Perumahan</th>
								<th class="text-center">Pemilik</th>
								<th class="text-center">Alamat</th>
								<th class="text-center">Telepon</th>
								<th class="text-center">Email</th>
								<th class="text-center">Status</th>
								<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
									<th class="text-center">Aksi</th>
								<?php }?>
							</tr>
						</tfoot>
					</table>
				</div>
			</div>
		</div>

		<?php
		break;
	}

	
?>
<script>
	
function myFunction() {
  var msg;
	msg= "Apakah Anda Yakin Akan Menghapus Data ? " ;
	var agree=confirm(msg);
	if (agree)
	return true ;
	else
	return false ;
}
</script>
