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


if(isset($_POST['save']))
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE; 
	if(!isset($_POST['total']) || trim($_POST['total'])=='')
	{   
		$flgEr=TRUE;
		array_push($alert_err,"Please enter total."); 
	}
	if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("examterm",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{   
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Attandence", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}

if(isset($_POST['save_time'])){
    // print_r($_POST);
    // die();
    $db->delete("attendance_timing", "where month='$_GET[month]' and session='".$_SESSION["admin"]["session"]."' ");
    for($i = 0; $i<$_POST['totaldays'];$i++){
        $saveary = array(
                    "date"          =>  $_POST['date'][$i],
                    "start_time"    =>  $_POST['start_time'][$i],
                    "end_time"      =>  $_POST['end_time'][$i],
                    "month"         =>  $_GET['month'],
                    "added_by"      =>  $_SESSION["admin"]["uid"],
                    "session"       =>  $_SESSION["admin"]["session"]
            );
            if($_POST['start_time'][$i]!=""){
                $db->insertAry("attendance_timing", $saveary);
            }
    }

    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Attandence", "page_id"=>$pgMod, "month"=>$_GET['month'], "add_time"=>"")));
    
}
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($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM examterm WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
<!-- 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> Terms</h3>
				</div>
				<!-- Forms: Content -->
				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label" for="class">Term</label>
							<div class="controls">
								<?php echo $aryForm["full_name"] ?>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="total">Total</label>
							<div class="controls">
								<input type="text" id="total" name="total" class="input-block-level" value="<?php if($_POST)echo $_POST["total"]; else echo $aryForm["total"] ?>">
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary" name="save"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box --> 

        </form>
	</div>
<?php   
}
else{
    	$allexam = $db->getRows("SELECT * FROM examname 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>
        <form method="get" action="">

        <input type="hidden" name="src" value="Attandence" />
        <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 Month</label>
                <div  class="controls">
                    <select name="month">
                        <option value=""> -- Select -- </option>
                        <option value="4" <?php if($_GET['month']=="4"){echo "selected";} ?>>April</option>
                        <option value="5" <?php if($_GET['month']=="5"){echo "selected";} ?>>May</option>
                        <option value="6" <?php if($_GET['month']=="6"){echo "selected";} ?>>June</option>
                        <option value="7" <?php if($_GET['month']=="7"){echo "selected";} ?>>July</option>
                        <option value="8" <?php if($_GET['month']=="8"){echo "selected";} ?>>August</option>
                        <option value="9" <?php if($_GET['month']=="9"){echo "selected";} ?>>September</option>
                        <option value="10" <?php if($_GET['month']=="10"){echo "selected";} ?>>October</option>
                        <option value="11" <?php if($_GET['month']=="11"){echo "selected";} ?>>November</option>
                        <option value="12" <?php if($_GET['month']=="12"){echo "selected";} ?>>December</option>
                        <option value="1" <?php if($_GET['month']=="1"){echo "selected";} ?>>January</option>
                        <option value="2" <?php if($_GET['month']=="2"){echo "selected";} ?>>February</option>
                    </select>
                </div>
            </div>

            <button type="submit" name="add_time" class="btn btn-primary"> Add Time</button>
        </form>
    </div>
    <div class="well no-padding">
    <?php if(isset($_GET["add_time"])){ ?>
		<form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
				<div class="well no-padding">
                	<div class="controls controls-row">

    		<input type="hidden" name="name" value="<?php echo $exam['name']; ?>" />
    		<input type="hidden" name="full_name" value="<?php echo $exam['full_name']; ?>" />
		
            <h3 style="text-align:center;"><?php echo $exam['name']; ?></h3>
            <div id="printableDiv">
            <table class="table">
			<tr>
                <th>S. No.</th>
                <th>Date</th>
                <th>Start Time</th>
                <th>End Time</th>
            </tr>
			<?php $i=0; $j = 0;
        	$month = $_GET["month"];

            $years = explode("-",$_SESSION["admin"]["session"]);

        	if($month < 4){
        	    $y = $years[1];
        	} else {
        	    $y = $years[0];
        	}

    		$mn=date('m',strtotime($y."-".$month."-1"));

    		$maxDays=date('t',strtotime($y."-".$mn."-1"));
			?>
			<input type="hidden" name="totaldays" value="<?php echo $maxDays; ?>" />
			<?php
			for($i=1; $i<=$maxDays; $i++){
                $data = $db->getRow("select * from attendance_timing where date='".$y."-".$month."-$i"."' and month='$_GET[month]' and session='".$_SESSION['admin']['session']."' ");

				?>
				<tr> 
                	<td><?php echo $i; ?></td> 
                	<td><?php echo $i."-".$month."-$y"; ?>
                    	<input type="hidden" name="date[]" value="<?php echo $y."-".$month."-$i";; ?>" />
                	</td>
            	    <td>
            	        <input type="time" name="start_time[]" value="<?php echo $data['start_time']; ?>" style="width:150px;"  />
            	    </td>
            	    <td>
            	        <input type="time" name="end_time[]" value="<?php echo $data['end_time']; ?>" style="width:150px;"  />
            	    </td>
                </tr>
				<?php $j++;
			}
			?></table>
			</div>
            <div>
				<button type="submit" name="save_time" class="btn btn-primary"> Save Time</button> 
			</div>
		</div></div></div></div>
		</form>
    <?php } ?>
	<?php /*
	$aryPgAct=array("src"=>"Attandence", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM examterm where school_id='".$_SESSION["admin"]["school_id"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Class</th>
                    <th>section</th>
                    <th>Subject</th>
                    <th>Exam Name</th>
                    <th>Percent</th>
                    <th>Total</th>
                    <!--<th>Action</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["section"] ?></td>
                    <td><?php echo $clist["subject"] ?></td>
                    <td><?php echo $clist["full_name"]." (".$clist["name"].")"; ?></td>
                    <td><?php echo $clist["percent"] ?></td>
                    <td><?php echo $clist["total"] ?></td>
                    <!--<td>-->
                    <?php //$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> -->
                    <!--</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
}
?>
<!-- / Forms -->