<?
//*********************************************************************************
// Original Website By : Niadesain.com Di Jual melalui http://niadesain.com
// Please Call / SMS : 081384745483 (support@niadesain.com)
// Niadesain.com - Jasa Pembuatan Website Paling Murah dan Paling Lengkap
//*********************************************************************************
?>
<script type="text/javascript">
<!--
function confirmation(noid, kat, page) {
	var answer = confirm("Yakin akan " + page +  " kode aktivasi ini?")
	if (answer){
		//alert("Bye bye!")
		window.location = "?m=cardcenter&page=" + kat + "&no=" + noid;
		
	}
	
}
//-->
</script>

<h2><img src="images/icon-48-menumgr.png" width="48" height="48" align="absmiddle" /> Kode Aktivasi</h2>
<?
//-------------password recovery & pin-------------
function randString ($pass_len) 
{ 
$allchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
//$allchars = array ($a, "a", "b", "c", "5", "8");
$string = ''; 

mt_srand ((double) microtime() * 1000000); 

for ($i = 0; $i < $pass_len; $i++) { 

$string .= $allchars{mt_rand (0,strlen($allchars))}; 
} 

return $string; 
}
function randomPassword($length) {
$allow = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$i = 1;

while ($i <= $length) {

$max = strlen($allow)-1;

$num = rand(0, $max);

$temp = substr($allow, $num, 1);

$ret = $ret . $temp;

$i++;

}

return $ret;

}

//--------
switch ($page) {
	default :



//---pagination----------------
$limit = '50'; // How many results should be shown at a time
$scroll = '0'; // Do you want the scroll function to be on (1 = YES, 2 = NO)
$scrollnumber = '50'; // How many elements to the record bar are shown at a time when the scroll function is on
//-------------pagination--------------
if (!isset ($_GET['show'])) {

	$display = 1;
	
} else {

	$display = $_GET['show'];
	
}
$start = (($display * $limit) - $limit);
//if(!$kat) $kat=2;
if($status) {
	if($status == 2){
		$statuse = 0;
	} else {
		$statuse = 1;
	}
	$aktif = "and pasif='$statuse'";
	$pasif = "pasif='$statuse'";
}
if($kat == 1) {
	$sql0 = $db->count_records("card", "status=1 $aktif");
	$sql = $db->select("*", "card", "status=1 $aktif", "", "", "", "$start, $limit");
	//mysql_close();
} else if($kat == 2) {
	$sql0 = $db->count_records("card", "kode like '%$keywrd%' or username like '%$keywrd%'");
	$sql = $db->select("*", "card", "serial like '%$keywrd%' or idmlm like '%$keywrd%'", "id desc", "", "", "$start, $limit");
} else if($kat == "0") {
	$sql0 = $db->count_records("card", "status=0 $aktif");
	$sql = $db->select("*", "card", "status=0 $aktif", "id desc", "", "", "$start, $limit");
} else {
	$sql0 = $db->count_records("card", $pasif);
	$sql = $db->select("*", "card", $pasif, "id desc", "", "", "$start, $limit");
}					
	$numrows = $sql0;
	
?>
<p align="center"><strong>DAFTAR KODE AKTIVASI</strong></p>
<table width="1000" border="0" align="center" cellpadding="5" cellspacing="1" style="border:solid #CCCCCC 1px">
  <tr class="tbl_header">
    <td colspan="4"> Daftar Kode Aktivasi 
      <select name="select" onchange="location =  this.options[this.selectedIndex].value" class="form">
           <option value="?m=cardcenter&pilih0=selected" <? echo $pilih0; ?>>Semua</option>
            <option value="?m=cardcenter&kat=1&pilih=selected" <? echo $pilih; ?>>Sudah Digunakan</option>
            <option value="?m=cardcenter&kat=0&sel=selected" <? echo $sel; ?>>Belum Digunakan</option>
      </select> 
      Total : <?= $numrows; ?></td>
    <td colspan="6"><form id="form2" name="form2" method="post" action="?m=cardcenter&amp;kat=2" style="margin:0; padding:0">
          <label>
            Cari kode/username : 
            <input name="keywrd" type="text" id="keywrd" />
        </label>
          <label>
          <input type="submit" name="Submit" value="CARI" />
          </label>
        </form>  </td>
  </tr>
  <tr bgcolor="#FFFF33" class="tbl_header">
    <td width="3%" align="center"><strong>NO</strong></td>
    <td width="17%" align="center"><strong>
      <label>SERIAL</label>
    </strong></td>
    <td width="9%" align="center"><strong>PIN</strong></td>
    <td width="10%" align="center"><strong>STATUS</strong></td>
    <td width="9%" align="center"><strong>ID MEMBER</strong></td>
    <td width="9%" align="center"><strong>ID DEALER</strong></td>
    <td width="14%" align="center"><strong>TANGGAL GENERATE</strong></td>
    <td width="15%" align="center"><strong>TANGGAL AKTIF</strong></td>
    <td width="6%" align="center"><strong>PAKET</strong></td>
    <td width="8%" align="center"><strong>A&nbsp; E &nbsp;D</strong></td>
  </tr>
<?


$nom = 1 + $start;
while($row=$db->fetch_row($sql)) {
	$lid = $nom - 1;
	if(is_odd($nom) == 0) {
		$class = "tblrow_ganjil";
	} else {
		$class = "tblrow_genap";
	} 	
	if($row[4] > 0) {
		$status = "REGISTERED";
	} else {
		$status = "Belum Digunakan";
	}		
	if($row[6] == "0000-00-00") {
		$tglkode = "Belum digunakan";
	} else {
		$tglkode = $row[6];
	}		
	
	//$nama=namaku("nama", $row[2]);	
	if($row[9] > 0) {

		$aktif = "<a href='#' onclick=\"confirmation('".$row[0]."', 'pasifkan', 'Pasifkan')\" style='cursor:hand'><img src='images/icon-16-checkin.png' title='Kartu Aktif' border=0 /></a>";

	} else {

		$aktif = "<a href='#' onclick=\"confirmation('".$row[0]."', 'activation', 'Aktifkan')\" style='cursor:hand'><img src='images/publish_x.png' title='Kartu Pasif' border=0 /></a>";

	}

?>  
  <tr class="<?= $class; ?>">
    <td align="center"><?= $nom; ?>.</td>
    <td><?= $row[1]; ?></td>
    <td align="center"><?= $row[2]; ?></td>
    <td align="center"><?= $status; ?></td>
    <td align="center"><?= $row[3]; ?></td>
    <td align="center"><?= $row[7]; ?></td>
    <td align="center"><?= $row[5]; ?></td>
    <td align="center"><?= $row[6]; ?></td>
    <td align="center"><?= $row[10]; ?></td>
    <td align="center"><?= $aktif; ?>&nbsp;&nbsp;<a href="?m=cardcenter&amp;page=edit&noid=<?= $row[0]; ?>"><img src="images/edit_f2.png" title="Edit Card" width="17" height="22" border="0" /></a> &nbsp;<a href="#" onclick="confirmation('<?= $row[0]; ?>', 'delete', 'delete')" style='cursor:hand'><img src="images/icon-32-delete_resize.png" width="17" height="22" border="0" title="Delete this Code" /></a></td>
  </tr>
<?
	$nom++;
}
?>	  
</table>
<br>
<p><em>Ket:</em></p>
<p><strong>A : </strong>Kartu Aktif/Pasif<strong>, E : </strong>Edit Kartu,<strong> D : </strong>Hapus Kartu</p>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td align="center">
     <?php

//}
//

$paging = ceil ($numrows / $limit);

// Display the navigation
if ($display > 1) {
	
	$previous = $display - 1;
	
?>
  <a href="?m=cardcenter&kat=<?= $kat; ?>&show=1" style="font-size:10px; color:#0000CC"><< First </a> | <a href="?m=cardcenter&kat=<?= $kat; ?>&show=<?= $previous; ?>" style="font-size:10px; color:#0000CC">< Previous </a> |
  <?php

}

if ($numrows != $limit) {
	
	if ($scroll == 1) {
	
		if ($paging > $scrollnumber) {
			
			$first = $display;
			
			$last = ($scrollnumber - 1) + $display;
			
		}
	
	} else {
	
		$first = 1;
			
		$last = $paging;
			
	}
	
	if ($last > $paging ) {
			
		$first = $paging - ($scrollnumber - 1);
			
		$last = $paging;
			
	}
	
	for ($i = $first;$i <= $last;$i++){
		
		if ($display == $i) {
			
?>
[ <b>
<?= $i ?>
</b> ]
<?php
			
		} else {
			
?>
[ <a href="?m=cardcenter&kat=<?= $kat; ?>&show=<?= $i; ?>" style="font-size:10px; color:#0000CC">
<?= $i; ?>
</a> ]
<?php
		
		}
	
	}

}

if ($display < $paging) {

	$next = $display + 1;
	
?>
| <a href="?m=cardcenter&kat=<?= $kat; ?>&show=<?= $next; ?>" style="font-size:10px; color:#0000CC">Next ></a> | <a href="?m=cardcenter&kat=<?= $kat; ?>&show=<?= $paging; ?>" style="font-size:10px; color:#0000CC">Last >></a>
<?php

}
//
?>
    </td>
  </tr>
</table>
<p align="center"><a href="printkode.php?kat=<?= $kat; ?>" target="_blank"><img src="../images/fileprint.png" alt="Cetak Kode Aktivasi" width="22" height="22" border="0" /></a></p>
<?
	break;
	
	case 'generate' ;
	if($harga == 10) {
		$pakej="SILVER";
				} elseif ($harga == 1) {
					$pakej="GOLD";
				} elseif ($harga == 3) {
					$pakej="PLATINUM";
				} elseif ($harga == 5) {
					$pakej="Gold";
		        } elseif ($harga == 7) {
					$pakej="Diamond";
				} else {
					$pakej="0";
				}
	if(isset($_POST['submit'])){	
	//---
	$sql=mysql_query("select idmlm from card order by id desc");
	if(mysql_num_rows($sql) > 0) {
		$mbr = mysql_fetch_row($sql);
		$last_id = substr($mbr[0], -4);
	} else {
		$last_id = 0;
	}		

	if(!$jumlahkode && !$idmbr) {
		echo "<p align=center><b>Masukan jumlah kode aktivasi atau 3 digit ID member yang akan digenerate!</b></p>";
	} else {
		
			//$rand_kode = randString ($pass_len = 6);
			for($i=0;$i<$jumlahkode;$i++) {
				if ($unit == 3) {
					$serial = rand(10000000,99999999);	
					$x = "03$serial";
				} else if($unit == 7) {
					$serial = rand(10000000,99999999);	
					$x = "07$serial";
				} else if($unit == 15) {
					$serial = rand(10000000,99999999);	
					$x = "15$serial";	
				} else {
					$serial = rand(1000000000,9999999999);	
					$x = $serial;	
				}	
				$new_no = $last_id + $i + 1;
				if ($new_no > 999 and $new_no < 10000)
					{
						$noid="$new_no";
					//} else if ($new_no > 999 and $new_no < 10000)
						//{
							//$noid = "0000$new_no";
						} else if ($new_no > 99 and $new_no < 1000)
							{
								$noid = "0$new_no";
							} else if ($new_no > 9 and $new_no < 100)
							{
								$noid = "00$new_no";
								} else if($new_no > 0 and $new_no < 10) {
							$noid = "000$new_no";
							} else {
							$noid = "0001";
					}
				//echo randString ($u)."<br>";
				$db->insert("card", "", "'', '$x', '".randomPassword(10)."', '$idmbr$noid', '0', '$clientdate', '$tgl_aktif', '$dealer', '$dealer', '$unit', '$pakej', '0', '1'");
			//mysql_close();

			
			}
					
			

?>
<div id="notice"><img src="images/notice-info.png" width="29" height="29" align="absmiddle" />Kode Aktivasi telah berhasil dimasukkan ! </div>
<? 
 echo"<meta http-equiv=\"refresh\" content=\"2; url=./?m=cardcenter\">";
} 
}
?>
<form name="form1" method="post" action="">
  <table width="80%" border="0" align="center" cellpadding="5" cellspacing="1" style="border:solid #CCCCCC 1px">
    <tr bgcolor="#FFFF33" class="tbl_header">
      <td colspan="2" align="center"><strong>INPUT PIN </strong></td>
    </tr>
    <tr>
      <td width="48%" align="right">3 digit ID Member :</td>
      <td width="52%"><label>
        <input name="idmbr" type="text" id="idmbr" size="4" maxlength="3">
        <input name="unit" type="hidden" id="unit" value="1" size="4" maxlength="3" />
      </label></td>
    </tr>
    <tr>
      <td align="right">Jumlah kode yang akan digenerate :</td>
      <td><label>
        <input name="jumlahkode" type="text" id="jumlahkode" size="10" />
      </label></td>
    </tr>
    <tr>
      <td align="right">Username Stockist :</td>
      <td><label>
        <input name="dealer" type="text" id="dealer" size="15" />
      </label></td>
    </tr>
     
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="submit" id="submit" value="BUAT PIN SERIAL">
      </label></td>
    </tr>
  </table>
</form>
<script language="JavaScript" type="text/javascript">
 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("jumlahkode","req","Masukkan jumlah Kode Aktivasi yang akan digenerate!");
 frmvalidator.addValidation("idmbr","req","Masukkan 3 digit ID Member!"); 
 frmvalidator.addValidation("dealer","req","Masukkan ID Dealer!");


</script>
<?
	break;
	case delete :
		//echo "delete no $no";
		//myquery("delete from member where username='$mid'");
		
		$db->delete("card", "id='$no'");
		//$up = dataupline("upline0", $mid);
		//$pos = dataupline("posisi", $mid);
		//update("upline", "$pos=''", "username='$up'");
		//myquery("delete from upline where username='$mid'");
		//mysql_close();
		echo "<meta http-equiv='refresh' content='0;URL=?m=cardcenter'>";
	break;	
	
	case edit :
	
if( isset($_POST['submit'])) {
	$db->update("card", "serial='$nomorseri', pin='$pin', idmlm='$idmlm', dealer='$dealer', dealer2='$dealer', status='$status', tgl_generate='$tglgenerate', tgl_aktivasi='$tglaktif', pasif='$pasif'", "id='$noid'");
	echo "<p align=center><b>Kartu berhasil diupdate!</b></p>";
}

	$db->select("*", "card", "id='$noid'");	
?>
<form id="form2" name="form1" method="post" action="">
  <table width="80%" border="0" align="center" cellpadding="4" cellspacing="0">
    <tr>
      <td width="44%" align="right"> No. seri kartu :</td>
      <td width="56%"><b>
        <input name="nomorseri" type="text" id="nomorseri5" value="<?= $db->result(0, "serial"); ?>" />
      </b></td>
    </tr>
    <tr>
      <td align="right">PIN :</td>
      <td><input name="pin" type="text" id="nomorseri2" value="<?= $db->result(0, "pin");  ?>" />
      <input name="noid" type="hidden" id="nomorseri4" value="<?= $db->result(0, "id");  ?>" /></td>
    </tr>
    <tr>
      <td align="right">ID Member :</td>
      <td><input name="idmlm" type="text" id="idmlm" value="<?= $db->result(0, "idmlm");  ?>" /></td>
    </tr>
    <tr>
      <td align="right">ID Dealer :</td>
      <td><input name="dealer" type="text" id="dealer" value="<?= $db->result(0, "dealer");  ?>" /></td>
    </tr>
    <tr>
      <td align="right">Status : </td>
      <td><input name="status" type="text" id="status" value="<?= $db->result(0, "status");  ?>" size="5" /> 
        <span style="font-size:10px">(0= belum digunakan, 1= sudah digunakan)</span></td>
    </tr>
    <tr>
      <td align="right">Status Aktif/Pasif : </td>
      <td><input name="pasif" type="text" id="pasif" value="<?= $db->result(0, "pasif");  ?>" size="5" />
        <span style="font-size:10px">(0= belum aktif, 1= kartu pasif)</span></td>
    </tr>
    <tr>
      <td align="right">Tanggal Aktivasi :</td>
      <td><input name="tglaktif" type="text" id="tglaktif" value="<?= $db->result(0, "tgl_aktivasi");  ?>" /> 
      <span style="font-size:10px">(format YYYY-MM-DD, cth. 2009-09-09)</span></td>
    </tr>
    <tr>
      <td align="right">Tanggal Generate :</td>
      <td><label>
        <input name="tglgenerate" type="text" id="nomorseri3" value="<?= $db->result(0, "tgl_generate");  ?>" />
      </label><span style="font-size:10px">(format YYYY-MM-DD, cth. 2009-09-09)</span></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><label>
        <input type="submit" name="submit" id="submit" value="UPDATE  KARTU" />
      </label></td>
    </tr>
  </table>
</form>
<?
	break;
	case activation :
	$db->update("card", "pasif=1", "id='$no'");
	echo "<meta http-equiv='refresh' content='0;URL=?m=cardcenter'>";
	break;
	
	case pasifkan :
	$db->update("card", "pasif=0", "id='$no'");
	echo "<meta http-equiv='refresh' content='0;URL=?m=cardcenter'>";
	break;
}
?>	