<?php ob_start();session_start();?>
<?php include "../config.php"; ?>
<?php include "../function.php"; ?>

<?php
$submit_item = $_POST['submit_item'];
$jumlah = $_POST['jumlah'];
$keterangan = $_POST['keterangan'];
$verifikasi = $_POST['verifikasi'];
$module = $_POST['module'];
$divisi = $_POST['divisi'];
$next_divisi = $_POST['next_divisi'];
$id_checklist = $_POST['id_checklist'];
$checkbox=$_POST['id_item'];
?>
<?php
if(isset($submit_item))
{
    $chk="";  
    foreach($checkbox as $chk1)  
    {  
      $chk .= $chk1.",";  
    } 
   
    $pdo->query ("insert into checklist
    (
        id_user,
        id_item,
        
        divisi,
        posted
        )
        values 
            (
            
            '".$_SESSION['id_user']."',
            '".$chk."',
            
            '".$divisi."',
            '".date('Y-m-d H:i:s')."'
            )
             ")or die(print_r($pdo->errorInfo(), true));
		
	header("Location: index.php?module=checklist&divisi=");
    
}
?>


<?php
if(isset($verifikasi))
{
    if($divisi != 'potong')
    {
        
    
    if($jumlah == 0)
    {
        
        echo "<script>alert('Jumlah tidak boleh 0.');window.location.href = 'index.php?module=checklist';</script>";
        exit;
		
	    header("Location: index.php?module=".$module);
        
    }
    else
    {
        $pdo->query(
	"UPDATE checklist SET 
	    jumlah            ='$jumlah',
	    verifikasi            ='Y',
        posted           ='".date('Y-m-d H:i:s')."'
	    WHERE 
	    id='".$id_checklist."'"
		)or die(print_r($pdo->errorInfo(), true));
		
		// to next divisi
        
        if($next_divisi != '')
        {
            $sql_  = $pdo->query("SELECT * FROM users WHERE divisi = '".$next_divisi."'");
            while($data_ = $sql_->fetch(PDO::FETCH_ASSOC)){
            
            $pdo->query ("insert into checklist
        (
        id_user,
        id_item,
        divisi,
        posted
        )
        values 
            (
            
            '".$data_['id_user']."',
            '".$checkbox."',
            '".$next_divisi."',
            '".date('Y-m-d')."'
            )
             ")or die(print_r($pdo->errorInfo(), true));
            
        }
        }
        header("Location: index.php?module=".$module);
        
    }
		
	
	
    }
    else
    {
        if($jumlah == 0)
        {
            echo "<script>alert('Jumlah tidak boleh 0.');window.location.href = 'index.php?module=checklist';</script>";
            exit;
		
	        header("Location: index.php?module=".$module);
        }
        else
        {
        $pdo->query ("insert into checklist
        (
        id_user,
        id_item,
        jumlah,
        keterangan,
        divisi,
        verifikasi,
        posted
        )
        values 
            (
            
            '".$_SESSION['id_user']."',
            '".$checkbox."',
            '".$jumlah."',
            '".$keterangan."',
            '".$divisi."',
            'Y',
            '".date('Y-m-d')."'
            )
             ")or die(print_r($pdo->errorInfo(), true));
             
        // to next divisi
        if($next_divisi != '')
        {
        $sql_  = $pdo->query("SELECT * FROM users WHERE divisi = '".$next_divisi."'");
        while($data_ = $sql_->fetch(PDO::FETCH_ASSOC)){
            
            $pdo->query ("insert into checklist
        (
        id_user,
        id_item,
        divisi,
        posted
        )
        values 
            (
            
            '".$data_['id_user']."',
            '".$checkbox."',
            '".$next_divisi."',
            '".date('Y-m-d')."'
            )
             ")or die(print_r($pdo->errorInfo(), true));
            
        }
        }
        
        //================
		
	    header("Location: index.php?module=checklist");
        }
    }
    
}
?>

<!--################## start referral ############-->

<?php
$id_data_verifikasi_terbayar = "$_POST[id_data_verifikasi_terbayar]";
if(isset($id_data_verifikasi_terbayar)){
    
    $sql = $pdo->query("UPDATE users SET bonus = '0', bonusTerbayar = 'Y', bonusTglTerbayar = '".date('Y-m-d')."' WHERE id = '".$id_data_verifikasi_terbayar."'")or die(print_r($pdo->errorInfo(), true));
    
    if($sql){
        echo $success_message;
    }else{
        echo $error_message;
    }

}
?>

<!--####################/ end referral ######-->


