<?php
defined("BASEPATH") or exit("No direct access allowed");
?>
<div class="card-header">

			<ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="#">Dashboard</a>
            </li>
            <li class="breadcrumb-item active">Import Prosfek</li>
          </ol>
					
          </div>
<div class="card-body">   
<?php
if (isset($_POST['data'])) {
	$data = $_POST['data'];
	
	$query = mysqli_query($con,"SELECT ID FROM champ WHERE grup = $grup ORDER BY sort LIMIT 0,1");
	$q = mysqli_fetch_array($query,MYSQLI_ASSOC);
	
	$query = mysqli_query($con,"SELECT ID FROM prospek ORDER BY id DESC LIMIT 0,1");
	$m = mysqli_fetch_array($query,MYSQLI_NUM);
	$i = $m[0]+1;
	
	$exp = explode("\n",$data);
	$insert = $gtm = '';
	foreach ($exp as $exp) {
		$item = explode(',',$exp);
		if (trim($item[0])!= '') {
			$insert .= "(".$i.",'".trim(mysqli_real_escape_string($con,$item[0]))."','".trim(mysqli_real_escape_string($con,$item[1]))."',NOW(),'".trim(mysql_real_escape_string($item[2]))."'),";
			$gtm .= "('',".$i.",".$grup.",'".$q[0]."',NOW(),0,1,'',NOW(),0,0,0,'',1)";
			$i++;
		}
	}

	$insert = substr($insert,0,-1);
	$gtm = substr($gtm,0,-1);
	mysqli_query($con,"INSERT INTO prospek (ID,nama,email,tgl_gabung,ip) VALUES $insert");
	mysqli_query($con,"INSERT INTO gtm VALUES $gtm");
	
	echo '<div class="notif">Import Selesai!</div>
	<script>
	    swal("Good Job","Import Selesai","success");
    </script>
	';
}
?>
<h1>Import Prosfek</h1>
<p>Masukkan data prosfek dengan format:</p>
<pre>Nama,Email,IP</pre>
<form action="" method="post">
<p><textarea name="data" cols="50" rows="15"></textarea></p>
<p><label>Pilih Grup</label><select name="grup" id="exampleFormControlSelect1" class="form-control">
<?php
    $query = mysqli_query($con,"SELECT id,nama FROM grup");
    if (mysqli_num_rows($query) > 0) {
    	while ($grup = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
    		echo '<option value="'.$grup['id'].'">'.$grup['nama'].'</option>';
    	}
    }
?>
</select>
<p><input type="submit" value="Import"/></p>
</form>
</div>