<?php                                                                        
    $product=$quantity=$usage=$designation="";
    
    //if item issued to staff
    if(isset($_POST['issue']) && isset($_GET['sfid'])){
        //Function to sanitize values received from the form. Prevents SQL injection
        function clean($str) {
    		$str = @trim($str);
    		if(get_magic_quotes_gpc()) {
    			$str = stripslashes($str);
    		}
    		return mysql_real_escape_string($str);
    	}
        
       $sid = clean($_GET['sfid']);
       
        
        if(!$staffID){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Failed to identify approving officer!</span>
                </div>';
        }else
        if(!$sid){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Failed to get staff to issue item to!</span>
                </div>';
        }else
        if(!isset($_POST['checkbox'])){
            echo'<div class="alert alert-warning absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-warning">No item was selected!</span>
                </div>';
        }else{
            $check = $_POST['checkbox'];
            $Date = date("Y-m-d");
            $return = date('Y-m-d', strtotime($Date. ' + 7 days'));
            $b = implode(',', $check);
            
            
            for($i=0;$i<count($check);$i++){}
            if($i > 2){
                echo'<div class="alert alert-warning absolue center text-center" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                        <span class="text-warning">No of items to issue exceeds limit. <br>Maximum is <b>2</b>. Your selection was <b>'.$i.'</b> books!</span>
                    </div>';
            }else
            {
               
                $qry1 ="SELECT b.name,b.isbn 
                        FROM item b
                        LEFT JOIN borrowed_item s
                        ON s.itemID = b.itemID
                        WHERE s.itemID IN ($b) AND s.actualReturnDate IS NULL";
            
            $result1=mysql_query($qry1); 
            $num1=mysql_num_rows($result1);
            
            if(empty($result1)) {
                echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">'. mysql_error($result1).'</span>
                </div>';
            }else
            if($num1 > 0) {
                while ($row = mysql_fetch_array($result1)) {
                        $title[] = $row['name'];
                        $isbn[] = $row['isbn'];
                }
                    $_SESSION['title']=$title;
                    //$_SESSION['isbn']=$isbn;
                    header('Location:view-item.php?sfid='.$sid);
                    echo "<script type='text/javascript'> document.location = 'view-item.php?sfid=$sid'; </script>";
                    exit();
            }else{
                //for($i=0;$i<count($check);$i++){}
                $member = array_fill(0,$i,$sid);
                $dateOfIssue = array_fill(0,$i,date("Y-m-d H:i:s"));
                $dueDate = array_fill(0,$i,$return);
                $librarian = array_fill(0,$i,$staffID);
                $issueno = array_fill(0,$i,rand(1,999999));
                $staf_No= array_fill(0,$i,$stafNo);

                $c = array_map(function ($issueno,$check,$member,$dateOfIssue,$dueDate,$librarian,$staf_No){return "'$issueno','$check','$member','$dateOfIssue','$dueDate','$librarian','$staf_No'";} , $issueno,$check,$member,$dateOfIssue,$dueDate,$librarian,$staf_No);    
                
                if(!$insert = mysql_query("INSERT INTO borrowed_item (borrowNo,itemID,staffID,dateBorrowed,returnDate,issuingLibrarian,added_by) VALUES(".implode('),(', $c).")")){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
                            <span class="text-danger">'.mysql_error().'</span>
                        </div>';
                }else{
                    $course=$yos=$sem=$period=$start=$deadline=$unit=$quiz=$time="";
                    $_SESSION['issue']='true';
                    header('Location:view-item.php');
                    echo "<script type='text/javascript'> document.location = 'view-item.php'; </script>";
                    exit();
                }
            }
        }}
    }
    ?>