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


if($_POST)
{
    // print_r($_POST);
    // die();
    
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['photo']['tmp_name'],PATH_MEDIA."photo".DS.$lnewfile))
		{
			$_POST["photo"]=$lnewfile;
		}
	}

	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("school_data",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			sms($_POST["mobile"],"Dear Parent, You are successfully joined  ".$LinksDetails["site_name"]." , heartly congrates from ".$LinksDetails["site_name"]." campus.","faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ERP", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("school_data",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			sms($_POST["mobile"],"Dear Parent, Your detail is updated successfully in ".$LinksDetails["site_name"],"faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"ERP", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$flgUp=$db->delete("school_data","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"=>"ERP", "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($pgAct=="edit" && $_GET["id"]!="")
{
	$aryForm=$db->getRow("SELECT * FROM school_data WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}

?>
<!-- Forms --> 
<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 96.7% !important;overflow: inherit !important;} 
</style>
<form method="post" action="" id="printableDiv" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Create New Admission</h3>
                <div style="float:right; margin-top:5px">
                	<button type="button" onclick="printDiv('printableDiv')" class="btn btn-primary">Print</button>
                    <button type="submit" class="btn btn-primary"> Save</button>
                    <button type="reset" class="btn"> Cancel</button>
                </div>
            </div>
            <div class="well">

			<div class="span6"> 

				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label">School Name</label>
							<div class="controls">
								<input type="text" name="school_name" class="input-block-level" value="<?php echo $aryForm["school_name"];	?>">
							</div>
						</div>
       <!--                 <div class="control-group">-->
							<!--<label class="control-label" >School Location</label>-->
       <!--                     <div class="controls">-->
       <!--                             <input type="text" name="school_location"  class="input-block-level" value="<?php echo $aryForm["school_location"]; ?>">-->
       <!--                         </div>-->
       <!--                 </div>-->

                        <div class="control-group">
							<label class="control-label" >Principal Contact</label>
							<div class="controls">
                                <input type="text" name="principal_contact" class="input-block-level" value="<?php echo $aryForm["principal_contact"]; ?>" >
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" >Director Contact</label>
							<div class="controls">
								<input type="text" name="director_contact" class="input-block-level" value="<?php echo $aryForm["director_contact"]; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">School Email Id</label>
							<div class="controls">
								<input type="text" name="email" class="input-block-level" value="<?php echo $aryForm["email"]; ?>" >
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Status</label>
							<div class="controls">
								<select class="span12" name="status" >
								    <option <?php if("Hot"==$aryForm["status"])echo 'selected';?> value="Hot">Hot</option>
									<option <?php if("Active"==$aryForm["status"])echo 'selected';?> value="Active">Active</option>
									<option <?php if("Closed"==$aryForm["status"])echo 'selected';?> value="Closed">Closed</option>
								</select>
							</div>
						</div>

					</div>
				</div>

			<div class="span6">

				<div class="well no-padding">

                        <div class="control-group">
							<label class="control-label">Sales Person</label>
							<div class="controls">
								<select class="span12" name="sales_person">
									<option value="">--Select Person--</option>
									<?php 	$aryList=$db->getRows("select * from sales_person where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 order by name");
									        foreach($aryList as $clist){
                                    ?>
                                    <option value="<?php echo $clist['id']; ?>" <?php if($aryForm["id"] === $clist['id']){echo "selected"; } ?> ><?php echo $clist['name']; ?></option>
                                    <?php } ?>
								</select>
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" >Visited Date</label>
                            <div class="controls">
                                <input type="date" name="visit_date"  class="input-block-level" value="<?php echo $aryForm["visit_date"]; ?>">
                            </div>
                        </div>

                        <div class="control-group">
							<label class="control-label">Address</label>
							<div class="controls">
							<textarea rows="3" class="input-block-level" name="address" ><?php echo $aryForm["address"]; ?></textarea>
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Feedback</label>
							<div class="controls">
							<textarea rows="3" class="input-block-level" name="feedback" ><?php echo $aryForm["feedback"]; ?></textarea>
							</div>
						</div>

				</div>

			</div>

            </div>
        </div>
        
	</div>
</form>
<!-- / Forms -->
<?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" onclick="window.location='./?src=ERP&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"ERP", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from school_data where school_id='".$_SESSION["admin"]["school_id"]."' ");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>School Name</th>
                    <th>Principal</th>
                    <th>Director</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Feedback</th>
                    <th>Sales Person</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["school_name"] ?></td>
                    <td><?php echo $clist["principal_contact"] ?></td>
                    <td><?php echo $clist["director_contact"] ?></td>
                    <td><?php echo $clist["email"] ?></td>
                    <td><?php echo $clist["address"] ?></td>
                    <td><?php echo $clist["feedback"] ?></td>
                    <td><?php echo $db->getVal("select name from sales_person where id='$clist[sales_person]' "); ?></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>
                        <?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>
        </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
}
?>