<?php
defined("BASEPATH") or exit("No direct access allowed");
?>
<div class="card-header">
					
          <nav aria-label="breadcrumb">
                      <ol class="breadcrumb bg-warning text-white-all">
                        <li class="breadcrumb-item"><a href="#"><i class="fas fa-tachometer-alt"></i> Home</a></li>
                        <li class="breadcrumb-item"><a href="#"><i class="far fa-file"></i> Prospek</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> Edit</li>
                      </ol>
                    </nav>
<?php
if (is_numeric($_GET['edit'])) {
	$id = $_GET['edit'];
	echo '';
	if (isset($_POST['nama']) && $_POST['nama'] != '' && isset($_POST['email'])) {
		$nama = special($_POST['nama']);
		$email = $_POST['email'];
		$hp = special($_POST['hp']);
		if (isset($_POST['custom']) && is_array($_POST['custom'])) {
			$addcustom = array();
			while ($custom = current($_POST['custom'])) {
				$key = key($_POST['custom']);
				$addcustom[$key] = $custom;
				next($_POST['custom']);
			}
			$dbcustom = serialize($addcustom);

		}

		$query = mysqli_query($con,"SELECT email FROM prospek WHERE email LIKE '$email' AND ID != $id");
		if (mysqli_num_rows($query) > 0) {
			$error = 'Email sudah terdaftar';
		} else {
			mysqli_query($con,"UPDATE prospek SET nama='$nama', email='$email', hp='$hp' WHERE ID=$id");
		}
		

		$query = mysqli_query($con,"SELECT grup.id AS idgrup, champ.ID AS idchampaign, champ.delay FROM grup left join champ ON champ.grup = grup.id GROUP BY grup.id");
		while ($groups = mysqli_fetch_array($query,MYSQLI_ASSOC)) {			
			if (in_array($groups['idgrup'], $_POST['grup'])) {
				$qc = mysqli_query($con,"SELECT id_grup FROM gtm WHERE id_grup=".$groups['idgrup']." AND id_member=$id");
				
				if (mysqli_num_rows($qc) == 0) {
					if (isset($groups['idchampaign'])) {
						mysqli_query($con,"INSERT INTO gtm VALUES  (NULL,'$id','".$groups['idgrup']."',".$groups['idchampaign'].",NOW() + INTERVAL ".$groups['delay']." DAY,0,1,'$nowtgl',0,0,0,'$nowtgl','',1,0)");
						
					} else {
						mysqli_query($con,"INSERT INTO gtm VALUES  (NULL,'$id','".$groups['idgrup']."',0,0,0,1,'$nowtgl',0,0,0,'$nowtgl','',0,0)");
						
					}
				} 

				if ($groups['idgrup'] == $idgrup && isset($dbcustom)) {
					mysqli_query($con,"UPDATE `gtm SET customdata='$dbcustom' WHERE id_grup=".$groups['idgrup']." AND id_member=$id");
				}

			} else {
				mysqli_query($con,"DELETE FROM gtm WHERE id_grup=".$groups['idgrup']." AND id_member=$id");
			}
		}

		echo '
                
                <script>
        	        swal("Good Job","Data Prospek Berhasil Diupdate","success");
                </script>     
                
                <script language="JavaScript">history.go(-1); </script> 
                ';
	}

	$query = mysqli_query($con,"SELECT prospek.nama,prospek.email,prospek.hp,gtm.customdata,gtm.id_grup FROM prospek,gtm WHERE gtm.id_member=$id AND gtm.id_grup=$idgrup AND prospek.ID=$id"); //
	if (mysqli_num_rows($query)) {
		$prospek = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$grup = $prospek['id_grup'];
		$customdata = unserialize($prospek['customdata']);
		echo '
		<div class="container">
		<h3>Edit Prospek</h3>
		<form class="form-horizontal" role="form" action="index.php?page=grupprospek" method="POST">
                   <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> Kembali</button>
                 </form>
		<form action="" method="post">
			<p><label>Nama</label><input type="text" name="nama" class="form-control" value="'.$prospek['nama'].'"/></p>
			<p><label>Email</label><input type="email" class="form-control" name="email" value="'.$prospek['email'].'"/></p>
			<p><label>HP</label><input type="text" name="hp" class="form-control" value="'.$prospek['hp'].'"/></p>
			';	
			
		$query = mysqli_query($con,"SELECT id_grup FROM gtm WHERE id_member=$id");
		while ($gtm = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
			$grdata[$gtm['id_grup']] = ' checked';
		}

		$listgrup = '';
		$query = mysqli_query($con,"SELECT id,nama,custom FROM grup");
		while ($gr = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
			if (isset($grdata[$gr['id']])) {
				$listgrup .= '<input type="checkbox" class="checkbox" name="grup[]" value="'.$gr['id'].'"'.$grdata[$gr['id']].'/> '.$gr['nama'].'<br/>';
			} else {
				$listgrup .= '<input type="checkbox" class="checkbox" name="grup[]" value="'.$gr['id'].'"/> '.$gr['nama'].'<br/>';
			}
			if ($gr['id'] == $idgrup) {
				$custom = unserialize($gr['custom']);
				if (is_array($custom)) {
					foreach ($custom as $custom) {
					echo '
					<p><label>'.$custom.'</label><input type="text" name="custom['.txtonly($custom).']" class="form-control" value="';
					if (isset($customdata[txtonly($custom)])) { echo $customdata[txtonly($custom)]; }
					echo '"/></p>';
					}
				}
			}
		}
		echo '
		<h4>Join Grup:</h4>
			<p>';
		echo $listgrup;
		echo '</p>
			<p><input type="submit" class="btn btn-primary" value="Edit Prospek"/></p>			
		</form></div>';
	} else {
		echo '<div class="error">Prospek tersebut sudah tidak berada di grup ini sekarang</div>';
	}
}
?>