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


if($_POST) 
{

    $class = $_POST['class'];
    
	$db->delete("criteria","where class='$class' and session='".$_SESSION["admin"]["session"]."' " );
    
    $i = 0;
   foreach($_POST['type'] as $type){ $i++;

       foreach($_POST["name$i"] as $name){

           $insAry = array(
                    "type"      =>  $type,
                    "name"      =>  $name,
                    "class"     =>  $class,
                    "session"   =>  $_SESSION["admin"]["session"],
                    "school_id" =>  $_SESSION["admin"]["school_id"],
					"added_by"  =>  $_SESSION["admin"]["uid"]
               );
               
               if($type != "" && $name!=""){
    			$db->insertAry("criteria",$insAry);
               }

       }
       
   } 

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>Notice</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>Notice</b> <?php echo $alert_err[0] ?></i> </div>
<?php
}

if(isset($_GET["add_criteria"])){
    
    $record = $db->getRows("select type from criteria where class='$_GET[class]' and session='".$_SESSION["admin"]["session"]."' group by type ");

?>
<!-- Forms -->
<div class="row-fluid">
  <form method="post" action="">
    <!-- Forms: Box -->
    <div class="span12"> 
      
      <!-- Forms: Top Bar -->
      <div class="top-bar">
        <h3><i class="icon-cog"></i> Create a Criteria</h3>
      </div>
      <!-- / Forms: Top Bar -->
      <input type="hidden" name="session" value="<?php echo $_SESSION["admin"]["session"] ?>">
      <input type="hidden" name="class" value="<?php echo $_GET['class']; ?>" />

      <!-- Forms: Content -->
      <div class="well no-padding">

        <h4 style="text-align:center;">Class: <?php echo $_GET['class']; ?></h4>

        <table class="table">
            <tfoot>
                <tr>
                    <td colspan="2">
                        <a id="Add2-A" href="javascript:;">
                            <button type="button" class="btn btn-info" style="font-size:15px;padding:8px 12px;">Add More</button>
                        </a>
                    </td>
                </tr>
            </tfoot>
              <tbody id="tbody2-A">
                  <?php $i=1;
                  if(is_array($record) && count($record))
                      {
                        foreach($record as $skill)
                        {
                            $names = $db->getRows("select * from criteria where type='$skill[type]' and class='$_GET[class]' and session='".$_SESSION["admin"]["session"]."' ");
                            ?>
                <tr class="bigrow">
                    <td>(<?php echo $i; ?>)</td>
                    <td>
                        <input type="text" placeholder="Enter 2-A" class="input-block-level" value="<?php echo $skill["type"]; ?>" name="type[]" >
                    </td>
                    <td>
                      <table> 
                        <tfoot>
                            <tr>
                              <td colspan="2">
                                <button type="button" class="btn btn-info" onclick="createrow('<?php echo $i; ?>')">Add Other</button>
                                </td>
                            </tr>
                        </tfoot>
                        <tbody id="tbody2-B-<?php echo $i; ?>">
                            <?php $j = 0; foreach($names as $name){ $j++; ?>
                                <tr class="inner_row">
                                    <td>(<?php echo $j; ?>)</td>
                                    <td colspan="2">
                                    <input type="text" placeholder="Enter name" class="input-block-level" value="<?php echo $name['name']; ?>"  name="name<?php echo $i; ?>[]"></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                      </table>
                    </td>
                    <td><a class="delete" href="javascript:;" style="font-size:15px;">
                        <button type="button" class="btn btn-info">Remove</button>
                        </a>
                    </td>
                </tr>
                <?php $i++;
                        } } else {
                            ?>
                <tr class="bigrow">
                  <td>(1)</td>
                  <td><input type="text" placeholder="Heading" class="input-block-level"  name="type[]" ></td>
                  <td>
                      <table>
                        <tfoot>
                            <tr>
                              <td colspan="2"><a id="Add2-B-1" href="javascript:;">
                                <button type="button" class="btn btn-info">Add Other</button>
                                </a></td>
                            </tr>
                        </tfoot>
                        <tbody id="tbody2-B-<?php echo $i; ?>">
                            <tr>
                                <td>(1)</td>
                                <td colspan="2">
                                <input type="text" placeholder="Enter name" class="input-block-level"  name="name1[]"></td>
                            </tr>
                        </tbody>
                        
                      </table>
                      
                  <td></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>

        <br><br>

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

    </div>

  </form>
</div>

<?php } else { 
    $allclass = $db->getRows("SELECT * FROM class order by id");
?>

	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    </div>
    <div style="background-color:;padding-left:10px;">
        <form method="get" action="">

        <input type="hidden" name="src" value="ExamCBSE" />
        <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />

            <div class="control-group" style="display:inline-block;">
                <label class="control-label" style="color:white;">Select Class</label>
                <div  class="controls">
                    <select name="class">
                        <option value=""> -- Select -- </option>
                        <?php foreach($allclass as $exam){ ?>
                            <option value="<?php echo $exam['class']; ?>"><?php echo $exam['class']; ?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>

            <button type="submit" name="add_criteria" class="btn btn-primary"> Add Criteria</button>
        </form>
    </div>
        <div class="well no-padding">
            
            <?php
	$aryPgAct=array("src"=>"ExamCBSE", "page_id"=>$pgMod);
	$aryList=$db->getRows("SELECT * FROM criteria where session='".$_SESSION["admin"]["session"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Class</th>
                    <th>Type</th>
                    <th>Skill Name</th>
                </tr> 
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["type"] ?></td>
                    <td><?php echo $clist["name"] ?></td>
                </tr>
            <?php } ?>
            </tbody>

        </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>

<?php } ?>

<script>
$(document).ready(function(){
	$('tbody td a.delete').click(function() { 
		$(this).parent().parent().remove(); 
	});	
	$("#Add2-A").click(function(){
		var rowCount = ($('#tbody2-A .bigrow').length)+1;
		var add_tr_skill='<tr class="bigrow"><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Heading" name="type[]"></td>'+
		'<td>'+
		    '<table>'+
                // '<tfoot><tr><td colspan="2"><a id="Add2-B" href="javascript:;"><button type="button" class="btn btn-info">Add Other</button><a></td></tr></tfoot>'+
            '<tfoot><tr><td colspan="2"><button type="button" class="btn btn-info" onclick="createrow('+rowCount+')">Add Other</button></td></tr></tfoot>'+
                    '<tbody id="tbody2-B-'+rowCount+'">'+
                        '<tr class="inner_row"><td>(1)</td><td colspan="2>'+
                            '<input class="input-block-level" type="hidden" value="2-A" name="type[]">'+
                            '<input type="text" placeholder="Enter name" class="input-block-level"  name="name'+rowCount+'[]"></td>'+
                        '</tr>'+
                    '</tbody>'+
            '</table>'+
		'</td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		$("#tbody2-A").append($(add_tr_skill));
		$('#tbody2-A td a.delete').click(function() { 
			$(this).parent().parent().remove(); 
		});	
	});

	$("#Add2-B-1").click(function(){ 
		var rowCount = ($('#tbody2-B-1 tr').length)+1;
// 		var add_tr_skill='<tr><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Enter name" name="name1[]"></td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		var add_tr_skill='<tr><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Enter name" name="name1[]"></td></tr>';
		$("#tbody2-B-1").append($(add_tr_skill));
		$('#tbody2-B-1 td a.delete').click(function() { 
			$(this).parent().parent().remove();
		});	
	});

}); 

	function createrow(rowid){
		var rowCount = ($('#tbody2-B-'+rowid+' .inner_row').length)+1;
// 		var add_tr_skill='<tr class="inner_row"><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Enter name" name="name'+rowid+'[]"></td><td><a class="delete" href="javascript:;"><button type="button" class="btn btn-info">Remove</button></a></td></tr>';
		var add_tr_skill='<tr class="inner_row"><td>('+rowCount+')</td><td><input class="input-block-level" type="text" placeholder="Enter name" name="name'+rowid+'[]"></td></tr>';
		$("#tbody2-B-"+rowid).append($(add_tr_skill));
	}

</script> 
