<?php if(isset($_SESSION['iduser'])){?>
<div class="block-header">
     <h2>
          <ol class="breadcrumb breadcrumb-bg-teal">
                   <li><a href="index.php?home"><i class="material-icons">home</i> Home</a></li>
                   <li><a href="index.php?anggota"><i class="material-icons">person</i> Member</a></li>
                   <li class="active">Member Add - Upload</li>
          </ol>
     </h2>
</div>			
<?php
$uploadedStatus = 0;
if ( isset($_POST["submit"]) ) {
    
if ( isset($_FILES["file"])) {
   
//if there was an error uploading the file
	if ($_FILES["file"]["error"] > 0) 
	{$img='';}
	else {
		$fileType = $_FILES['file']['type'];
		$target_dir = "uploads/";
		$pembeda=rand(1111111,9999999);
		$pembeda1=rand(11111,99999);
		$pembeda2=rand(1111,9999);
		$allowed = array('application/vnd-ms-excel','application/vnd.ms-excel');
		$img='';
		if (in_array($fileType, $allowed)) 
		{
			$img=$pembeda.$pembeda1.$pembeda2.basename($_FILES["file"]["name"]);
			$target_file = $target_dir.$pembeda.$pembeda1.$pembeda2.basename($_FILES["file"]["name"]);
			move_uploaded_file($_FILES["file"]["tmp_name"],  $target_file);
		}
	}
}

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
 
// This is the file path to be uploaded.
$inputFileName = $target_file;
try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
 
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet

for($i=1;$i<=$arrayCount;$i++){
			$idmember=noninjection($koneksi,$allDataInSheet[$i]["B"]);

			$nik=noninjection($koneksi,$allDataInSheet[$i]["C"]);
			$nama=noninjection($koneksi,$allDataInSheet[$i]["D"]);
		//	$alamat=noninjection($koneksi,$allDataInSheet[$i]["E"]);
			$alamat='-';
			$phone=noninjection($koneksi,$allDataInSheet[$i]["F"]);
			$status='OPEN';
			$email=noninjection($koneksi,$allDataInSheet[$i]["G"]);
			$password=noninjection($koneksi,$allDataInSheet[$i]["H"]);
			$kelompok=noninjection($koneksi,$allDataInSheet[$i]["I"]);
			//$password='Santri';
		//	$pin=noninjection($koneksi,$allDataInSheet[$i]["J"]);
			$pin='123456';
			$task="34567fghjkl;FGHJK09865fghjs12345@#$@#$!@#";
			$password=md5($password);
			$password=md5($password.$email.$task);
			$pin=md5($pin);
			$pin=md5($pin.$task);

$qry_cari=mysqli_query($koneksi,"SELECT * FROM `tb_anggota` WHERE idanggota='".$idmember."'");
 if(mysqli_num_rows($qry_cari)==0)
 {
//data user login
$res=mysqli_query($koneksi,"INSERT INTO `tb_anggota` (`idanggota`, `nik`, `nama`, `alamat`, `phone`, `kategori`, `kelompok`, `email`, `password`, `pin`,`rfid`,`rfid_new`,`img`, `status`,iduser, `tgl`) VALUES ('".$idmember."', '".$nik."', '".$nama."', '".$alamat."', '".$phone."', 'Siswa', '".$kelompok."', '".$email."', '".$password."', '".$pin."',NULL,'0', '-', '".$status."','".$_SESSION['iduser']."', current_timestamp());");
//echo "INSERT INTO `tb_anggota` (`idanggota`, `nik`, `nama`, `alamat`, `phone`, `kategori`, `kelompok`, `email`, `password`, `pin`,`rfid`,`rfid_new`,`img`, `status`,iduser, `tgl`) VALUES ('".$idmember."', '".$nik."', '".$nama."', '".$alamat."', '".$phone."', 'Siswa', '".$kelompok."', '".$email."', '".$password."', '".$pin."',NULL,'0', '-', '".$status."','".$_SESSION['iduser']."', current_timestamp());<br>";
 //input biodata
 } else {
		$res=mysqli_query($koneksi,"UPDATE tb_anggota SET `nik`='".$nik."', `nama`='".$nama."', `alamat`='".$alamat."', `phone`='".$phone."', `kategori`='".$kategori."', `kelompok`='".$kelompok."', `email`='".$email."', `password`='".$password."', `pin`='".$pin."', `status`='".$status."' WHERE idanggota='".$idmember."'");
//update biodata
 }
}//end for
if($res){echo '<div class="alert bg-green alert-dismissible" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <strong>Well done!</strong> Import Data Sukses.
              </div>';
							}else {
							echo '<div class="alert bg-pink alert-dismissible" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                               <strong>Warning!</strong> Import Data Gagal.
                            </div>';
							}
}
?>
		 <div class="row clearfix">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="card">
                        <div class="header">
                            <h2>MEMBER UPLOAD</h2>
                           
                        </div>
                        <div class="body">
						<form action="index.php?anggota&mn=anggota_upload" method="post" enctype="multipart/form-data" name="form1" id="form1">
						  <div class="form-group form-float">
                                    <div class="form-line">
                                        <input name="file" type="file" class="form-control" />
                                    </div>
                                    <div class="help-info">Upload File excel format file *.xls</b></div>
                                </div>
							 <button class="btn btn-primary waves-effect" name="submit" type="submit" data-toggle="tooltip" data-placement="right" title="Simpan Data">SUBMIT</button>	
						</form>
						 <div align="left"><br>
                    <b>Perhatian !</b>
				
					    
					    	<ul>
						   PIN: <b>12345678</b>  <br />   
					    </ul>
                    <ul>
                      <li>File yang diupload harus berformat atau ekstensi .xls</li>
                      <li>Format xls-nya silahkan download, klik [ <a href="files/format-anggota2.xls">disini</a> ]</li>
                      <li>Hapus baris 1 (judul field) jika akan diupload !</li>
                      <li>Import bersifat menimpa, jika ada nomor induk sama, maka akan ditimpa dengan yang baru diimport.</li>
                      <li>Ukuran file .xls maksimal 1 MB</li>
                    </ul>
                  </div>
						 </div>
					</div>
				</div>
		</div>
		<?php } ?>