<?php
$pgMod="exams";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

    $permission = $db->getVal("select id from tbl_privilages where roll_id='".$_SESSION["admin"]["roll"]."' and page_name='InventoryEditDelete' and a='1' ");

if($_POST)
{
    // print_r($_POST);
    // die();
	$flgEr=FALSE;

	if($flgEr!=TRUE && $pgAct=="add")
	{  

	if(is_array($_POST["examname"]) && count($_POST["examname"])>0)
	{
		for($i=0; $i<count($_POST["examname"]); $i++){
			if(trim($_POST["examname"][$i])!=""){
			 //   $name = $_POST["examname"][$i];

				$insAry = array(
				                "name"                  =>  $_POST["examname"][$i],
				                "full_name"             =>  $_POST["fullname"][$i],
            	                "app_visibilty"         =>  $_POST["app_visibilty"][$i],
            	                "co_scholastic_view"    =>  $_POST["co_scholastic_view"][$i],
            		            "added_by"              =>  $_SESSION["admin"]["uid"],
            		            "result_submission"     =>  $_POST["result_submission"][$i]
            		            );
				$db->insertAry("examname",$insAry);
			}
		}
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
	}

	}
	
	
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$insAry = array(
	                "name"                  =>  $_POST['examname'],
	                "full_name"             =>  $_POST["fullname"],
	                "app_visibilty"         =>  $_POST["app_visibilty"],
	                "co_scholastic_view"    =>  $_POST["co_scholastic_view"],
                    "added_by"              =>  $_SESSION["admin"]["uid"],
                    "result_submission"     =>  $_POST["result_submission"]
	            );
		$flgUp=$db->updateAry("examname",$insAry,"where id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}


}

elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{

	$res=$db->delete("examname","where id='".$_GET['id']."' ");

	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ExamCBSE", "page_id"=>$pgMod)));
}


if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
?>
<div class="alert alert-success">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-thumbs-up-alt"></i> <b></b><?php echo $_SESSION['msg'] ?></i>
</div>
<?php
unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
?>
<div class="alert alert-error">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-warning-sign"></i> <b></b> <?php echo $alert_err[0] ?></i>
</div>
<?php

}


if($pgAct=="edit" && $_GET["id"]!="")
{
	$aryForm=$db->getRow("SELECT * FROM examname WHERE id='".$_GET['id']."'");
}

if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='')){
?>
<input type="hidden" name="src" value="ExamCBSE" />
<input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
<div class="row-fluid">
<div class="span12">
<div class="top-bar">
<h3><i class="icon-cog"></i>Add Expense</h3>

</div>
<div class="well no-padding"  id="printDiv2">
<div class="controls controls-row">

<div style="padding:20px;">
  <form class="login-form" method="post" onsubmit="return checkbudget()">

    <!--<div class="control-group">-->
    <!--    <label class="control-label">Select Head</label>-->
    <!--    <div  class="controls">-->
    <!--        <select name="head_type" id="head_type" style="width:100%;" onchange="filterSubject()">-->
    <!--            <option value="Head" <?php if($aryForm['head_type'] == "Head")echo "selected";  ?>>Head</option>-->
    <!--            <option value="Sub Head" <?php if($aryForm['head_type'] == "Sub Head")echo "selected";  ?>>Sub Head</option>-->
    <!--        </select>-->
    <!--    </div>-->
    <!--</div>-->

    

        <?php if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''){ ?>
        <div class="control-group">
            <label class="control-label">Name </label>
            <div  class="controls">
                <input type="text" placeholder="Exam name" name="examname" value="<?php echo $aryForm['name']; ?>">
            </div>
        </div><br>
        
        <div class="control-group">
            <label class="control-label">Full Name </label>
            <div  class="controls">
                <input type="text" placeholder="Full name" name="fullname" value="<?php echo $aryForm['full_name']; ?>">
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">Result View </label>
            <div  class="controls" style="padding-left:100px;">
                Yes <input type="radio" name="app_visibilty" value="1" <?php if($aryForm['app_visibilty']==1)echo "checked"; ?> />
                No  <input type="radio" name="app_visibilty" value="0" <?php if($aryForm['app_visibilty']==0)echo "checked"; ?> />
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">Co Scholastic View </label>
            <div  class="controls" style="padding-left:100px;">
                Yes <input type="radio" name="co_scholastic_view" value="1" <?php if($aryForm['co_scholastic_view']==1)echo "checked"; ?> />
                No  <input type="radio" name="co_scholastic_view" value="0" <?php if($aryForm['co_scholastic_view']==0)echo "checked"; ?> />
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">Result Submission </label>
            <div  class="controls" style="padding-left:100px;">
                Yes <input type="radio" name="result_submission" value="0" <?php if($aryForm['result_submission']==0)echo "checked"; ?> />
                No  <input type="radio" name="result_submission" value="1" <?php if($aryForm['result_submission']==1)echo "checked"; ?> />
            </div>
        </div>

        <?php } else { ?>
          <table  class="table">
              <tfoot>
                <tr>
                  <td colspan="2"><a id="Addlink" href="javascript:;">
                    <button type="button" class="btn btn-info">Add Other</button>
                    </a>
                  </td>
                </tr>
              </tfoot>
              <thead>
                  <tr style="color:white;">
                      <th>S.No.</th>
                      <th>Exam name</th>
                      <th>Full name</th>
                      <th>Result View</th>
                      <th>Co Scholastic View </th>
                      <th>Result Submission</th>
                  </tr>
              </thead>
              <tbody id="tbodylink">
                <tr>
                  <td>(1)</td>
                  <td><input type="text" placeholder="Exam name" class="input-block-level" name="examname[]" required></td>
                  <td><input type="text" placeholder="Full name" class="input-block-level" name="fullname[]" required></td>
                  <td>
                      <!--<input type="text" placeholder="Result View" class="input-block-level" name="app_visibilty[]" required>-->
                    Yes <input type="radio" name="app_visibilty[]" value="1" />
                    No  <input type="radio" name="app_visibilty[]" value="0" />
                  </td>
                  <td>
                      <!--<input type="text" placeholder="Co-Scholastic View" class="input-block-level" name="co_scholastic_view[]" required>-->
                    Yes <input type="radio" name="co_scholastic_view[]" value="1" />
                    No  <input type="radio" name="co_scholastic_view[]" value="0" />
                  </td>
                  <td>
                    Yes <input type="radio" name="result_submission[]" value="0" />
                    No  <input type="radio" name="result_submission[]" value="1" />
                  </td>
                </tr>
              </tbody>
              </table>
        <?php } ?>      

          
        <div class="form-actions">
			<button type="submit" class="btn btn-primary"> Save</button>
			<button type="button" class="btn"> Cancel</button>
		</div>
    </form>
    
</div>

        </div>
        <br>
</div>
<!-- / Forms: Content -->
</div>
    </div>

<?php
}
else {
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <!--<button style="float:right;margin-top:5px;margin-left:10px;" onClick="window.location='./?page_id=<?php echo $pgMod; ?>&action=addimages'" class="btn" type="button">Add Subject Images</button>-->

    <?php //if($permission){ ?>
            <button style="float:right; margin-top:5px" onClick="window.location='./?src=ExamCBSE&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    <?php //} else {?>
            <!--<button style="float:right; margin-top:5px" onclick="showalert('Add new Inventory.')" class="btn" type="button">Add <?php echo $pgMod; ?></button>-->
    <?php //}    ?>

    </div>
    <div class="well no-padding">


	<?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM examname order by id"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table  id="example" class="table table-striped table-bordered"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Exam Name</th>
                    <th>Full Name</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["full_name"] ?></td>

                    <td>
                        
                    <?php 
                    // if($permission){
        // 			$aryPgAct["action"] = "edit";
        // 			echo '<a href=\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\'><button type=\'button\' class=\'btn btn-info\'>Edit</button></a>';
        // 			$aryPgAct['action']="delete";
        // 			echo '<a href=\'javascript:;\' onclick="del(\''.URL_ADMIN_HOME.getQueryString($aryPgAct).'\')" class=\'confirmation\'><button type=\'button\' class=\'btn btn-danger\'>Delete</button></a>';
                    // } else {
                    //     echo '<a href=\'#\' onclick="showalert(\'Edit Inventory.\')"><button type=\'button\' class=\'btn btn-info\'>Edit</button></a>';
                    //     echo '<a href=\'#\' onclick="showalert(\'Delete Inventory.\')"><button type=\'button\' class=\'btn btn-danger\'>Delete</button></a>';
                    // }
                    
                      $aryPgAct['action']="edit"; ?>
                        <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                    <?php $aryPgAct['action']="delete"; ?>
                        <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>
                    </td>

                </tr>
            <?php } ?>
            </tbody>
            <!--<tfoot>-->
            <!--    <tr>-->
            <!--    	<th></th>-->
            <!--        <th colspan="3"></th>-->
            <!--    </tr>-->
            <!--</tfoot>-->
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div>
	</div></div></div>
	<?php

}
?>
    
    <!--</form>-->


<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}

@media screen {
   #myPrntbtn1 {
        display: none;
    }
}
</style>

<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});

    function showalert(msg){
        alert('You are not allowed to '+msg);
    }
	
	</script>
	
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>




<script>

function filterSubject(){

    var x = document.getElementById("head_type").value;
    if(x=='Sub Head'){
      document.getElementById("newsubjectdiv").style.display = "block";
    }
    else {
         document.getElementById("newsubjectdiv").style.display = "none";
    }

}



function getBudget(name){

    $.post( "Budget/ajax.php", { type: "get_budget", name: name } , function( data ) {
        document.getElementById("max_budget").innerHTML = data;
	});

}

	
function checkbudget(){

    var sum = 0;
    var arr = $('input[name="budget[]"]').map(function () {
        sum = parseInt(sum) + parseInt(this.value);
        return sum;
    }).get();

    var rem = $("#rembudget").val();
    if(rem){
        if(sum <= rem){
            return true;
        } else {
            alert('Your total sum should not be excceded than: '+ rem);
            return false; 
        }
    }

    return true;
}



	$("#Addlink").click(function(){
		var rowCount = ($('#tbodylink tr').length)+1;
		var add_tr_skill='<tr><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Exam name" name="examname[]"></td><td><input class="input-block-level" type="text" placeholder="Full name" name="fullname[]"></td><td> Yes <input type="radio" name="app_visibilty[]" value="1" /> No  <input type="radio" name="app_visibilty[]" value="0" /></td><td> Yes <input type="radio" name="co_scholastic_view[]" value="1" /> No  <input type="radio" name="co_scholastic_view[]" value="0" /></td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		$("#tbodylink").append($(add_tr_skill));
		$('#tbodylink td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});

</script>

<style>
a {
    color: blue;
    text-decoration: none;
}
</style>
