<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-map-o"></i> SIGNATURES <small><?php echo $this->lang->line('student_fee1'); ?></small> </h1>
			<?php 
		//	echo "<pre>"; 
		//	print_r($gradeList);			
		//	echo "</pre>"; 
			?>

<div style='color:red;'>			
<?php

//delete signature
if(isset($_POST["del_file"])) {

$file_name = $_POST["file_name"];
	if(unlink($file_name)){
		
		$msg = "Signature Deleted.";
		echo $msg . "<br />";
		echo "<script>alert('$msg');</script>";
		
	}	
}

if(isset($_POST["sub_sig"])) {

$this_class = $_POST["class_id"];
$this_section = $_POST["section_id"];

if($this_class != "0"){
	if((($this_class == "") || ($this_section == ""))){
		
		$msg = "Class or Section is not selected.";
		echo $msg . "<br />";
		echo "<script>alert('$msg');</script>";
		
		 $this->session->set_flashdata('msg', 'Class or Section is not selected');
	}
	else{
		

	
	
$target_dir = "uploads/signatures_images/";
$target_file = $target_dir . basename($_FILES["upload_sign"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["sub_sig"])) {
    $check = getimagesize($_FILES["upload_sign"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
/*
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
*/
// Check file size
if ($_FILES["upload_sign"]["size"] > 5000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["upload_sign"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["upload_sign"]["name"]). " has been uploaded.";
		
		$new_name = "sign_$this_class". "_$this_section".".jpg";
		
		rename($target_file, $target_dir.$new_name);
		
        echo "Saved as $new_name";
		
		
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}


//
	}
}
else{
/////head	
$this_section = "";
		
		
$target_dir = "uploads/signatures_images/";
$target_file = $target_dir . basename($_FILES["upload_sign"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["sub_sig"])) {
    $check = getimagesize($_FILES["upload_sign"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
/*
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
*/
// Check file size
if ($_FILES["upload_sign"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["upload_sign"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["upload_sign"]["name"]). " has been uploaded.";
		
		$new_name = "sign_$this_class". "_$this_section".".jpg";
		
		rename($target_file, $target_dir.$new_name);
		
        echo "Saved as $new_name";
		
		
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}


//








////head	
}
	


}
?> 

</div>
			
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <div >


						
						
						<form method='POST' enctype="multipart/form-data" action="<?php echo site_url('admin/signatures/create') ?>">
                        <div class="box-body">
                            <div class="row">                             
                                <?php echo $this->customlib->getCSRF(); ?>
								
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Upload Signature:</label>
										<input type='file' name='upload_sign' style='display:block;opacity: 1;' />
                                    </div>
                                </div><!-- /.col -->
								                                <div class="col-md-4" style='display:none;'>
                                    <div class="form-group" >
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('exam_name'); ?></label>

                                        <select autofocus="" id="exam_id" name="exam_id" class="form-control" >
                                            <?php
                                            foreach ($examlist as $exam) {
                                                ?>
                                                <option value="<?php echo $exam['id'] ?>" <?php
                                                if ($exam_id == $exam['id']) {
                                                    echo "selected = selected";
													$current_term = $exam['name'];
                                                }
                                                ?>><?php echo $exam['name'] ?></option>
                                                        <?php
                                                        $count++;
                                                    }
                                                    ?>		
                                        </select>
                                        <span class="text-danger"><?php echo form_error('exam_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('class'); ?></label>
                                        <select  id="class_id" name="class_id" class="form-control" >
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                            <?php
                                            foreach ($classlist as $class) {
                                                ?>
                                                <option value="<?php echo $class['id'] ?>" <?php
                                                if ($class_id == $class['id']) {
                                                    echo "selected =selected";
													$current_class = $class['class'];
                                                }
                                                ?>><?php echo $class['class'] ?></option>

                                                <?php
                                                $count++;
                                            }
                                            ?>
											<option value="0">Head of School</option>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('class_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('section'); ?></label>
                                        <select  id="section_id" name="section_id" class="form-control" >
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('section_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
								
								<div class="col-md-4">
                                    <div class="form-group">		
																	
										<input type='submit' name='sub_sig' value='UPLOAD' />
                                    </div>
                                </div><!-- /.col -->
								
                            </div><!-- /.row -->
                        </div><!-- /.box-body -->
                   
						
						</form>
						
	<?php
		//echo "<pre>";
		//	print_r($classlist);
		//	print_r($sectionlist);
		//	print_r($classsectionlist);
		//echo "</pre>";
	?>	
					
	<?php
	$t = "uploads/signatures_images/";
	?>

	<div class="gallery">
	<div class="pphoto">

	<?php
	$img_name = "sign_0_.jpg";
		if(file_exists("$t/$img_name")){
		echo "<img src='". base_url() . "$t/$img_name'>";
		$delete = "<form method=\"POST\" action=\"\">
			<input name='file_name' type='hidden' value='$t/$img_name'>
			<input type='submit' name='del_file' value='DELETE' />
			</form>";
		}
		else
		{
			echo "<span style='color:red'>No Signature upload!</span>";
			$delete = "<button>No File!</button>";
		}
	?>
		
	</div>
	<h4 class="" style="font-weight:;min-height:20px;max-height:20px;">Head of School</h4>
	<?php echo $delete; ?>	
	</div>
	
	
	
	<?php
	foreach($classsectionlist as $key=>$value){
		$class_name = "";
		foreach($classlist as $class_key=>$class_value){
			if($class_value['id'] == $value['class_id']){
				$class_name = $class_value['class'];
			}
		}
		$img_name = "sign_" . $value['class_id'] . "_" . $value['section_id'] . ".jpg";	
	?>
			<!-- separator --><div class="gallery">
	<div class="pphoto">
	<?php
		if(file_exists("$t/$img_name")){
		echo "<img src='". base_url() . "$t/$img_name'>";
		$delete = "<form method=\"POST\" action=\"\">
			<input name='file_name' type='hidden' value='$t/$img_name'>
			<input type='submit' name='del_file' value='DELETE' />
			</form>";			
		}
		else
		{
			echo "<span style='color:red'>No Signature upload!</span>";
			$delete = "<button>No File!</button>";
		}
	?>	
	</div>
	<h4 class="" style="font-weight:;min-height:20px;max-height:20px;"><?php echo $class_name . " " . $value['section']; ?></h4>
	<?php echo $delete; ?>	
	</div>
	<!-- End Separator -->
	<?php
		
		
	}
	
	?>
<div style='clear:both'></div>						

<div style="display:none;">						
 <?php
  
$t = "uploads/signatures_images/";

$v = scandir($t, 0);
  
echo "  <h2>List of files in $t</h2>";
 


sort($v, SORT_NATURAL | SORT_FLAG_CASE);
  reset($v);
array_shift($v);


while (list($key, $val) = each($v))
  { 
		//if ($key != 0){
			
		$serial = $key;	
		$dfile = realpath($t."/".$val);

		if (2>1){
				if ($key == 0){$serial = 'Parent<<';	}			
			//$root =	$_SERVER['PATH_INFO'];
			$root =	"";
			   if (is_dir("$t/$val")){
			//	echo "Directory";
			  }
			  else
			  {				  
				 echo " <small> $key </small> .. 
				 <a href='" . base_url() ."$t/$val' target='_blank' style='display:inline;' download >
				 <img src='" . base_url() ."$t/$val' style='display:inline;'  width='100px' />
				 </a>
				 <br />
				 <strong><span> $val</span></strong>";				 
				 echo "---- ";
				 //////CHECK FILE SIZE/////
				//$dfile = basename($val);
				//round(7.055,2)
				$file_size = round(filesize($dfile)/1024,2);
				echo $file_size . " KB";
				//print_r(pathinfo($t."/".$val,PATHINFO_BASENAME));
				//echo $dfile;

				////edit to resort

				/////END CHECK FILE SIZE////

			  }
			  
			$file_permission = substr(sprintf("%o",fileperms($dfile)),-4);
			
			if (!is_dir("$t/$val")){


			echo "---- |";
			//////LAST MODIFIED //////
			//echo "Last modified: ";
			//echo "----";
			date_default_timezone_set('Africa/Lagos');
			$modtime = filemtime($dfile);
			echo date("M d, Y h:i:s A",$modtime);
			echo "
			<form method=\"POST\" action=\"\">
			<input name='file_name' type='hidden' value='$t/$val'>
			<input type='submit' name='del_file' value='DELETE' />
			</form>";

			////edit to resort

			//////END LAST MODIFIED //////
			/////if dir archive can also acheive the folders add files and then download
			//////capture all details an put them to an array the sort array by type,time,name,acsending or desecending, hold post values for dir, multiple selection/ edit, swap editors, e.g php, plain, etc.

			 echo "<br />";	
			}			 
		}
		  //
  
  }



?>

</div>
						
                        </div>
                    </div>

	
                </div>
                <!-- right column -->
            </div>   <!-- /.row -->
		</div>
    </section><!-- /.content -->

<?php

?>

<style>
#dialog {
position:fixed;
top:0px;
left:0px;
width:100%;
height:100%;
background: url('<?php echo base_url()?>/backend/images/fading.png');
display:none;
z-index:100000;
}

#dialog_box {
width:50%;
height:50%;
background:white;
overflow: auto;
position:absolute;
border-radius:10px 10px;
border:4px green dotted;
z-index:100002;
}

#dialog_head{
opacity:1;
}
</style>

<div id="dialog">

<div id="dialog_box">
<h1 style="margin:0px;text-align:center;" id="dialog_head"></h1>
<div id="dialog_body">

</div>
<center><button class='btn btn-primary btn-sm' onclick="close_dialog()">CLOSE</button></center>
</div>

</div>


<script>
function dialog(head,body){
//$("#dialog").slideDown();
$("#dialog").fadeIn();
$("#dialog_head").html(head);
$("#dialog_body").html(body);
//$("#dialog_box").css("width", "200px");
//alert(document.body.getBoundingClientRect().width);
window_width = document.body.getBoundingClientRect().width;
box_width = Math.round(7/10 * window_width);
box_position_x = Math.round(window_width-box_width)/2;


window_height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

//window_height = document.body.getBoundingClientRect().height;
box_height = Math.round(9/10 * window_height);
box_position_y = Math.round(window_height-box_height)/2;


if(window_width < 400){box_width = window_width; box_position_x = 0;}
if(window_height < 300){box_height = window_height; box_position_y = 0;}

//$("#dialog").css("display", "");
$("#dialog_box").css("position", "fixed");

$("#dialog_box").css("width", box_width + "px");
$("#dialog_box").css("left", box_position_x + "px");

$("#dialog_box").css("height", box_height + "px");
$("#dialog_box").css("top", box_position_y + "px");


}

function close_dialog(){
$("#dialog").fadeOut();
}
</script>
<script type="text/javascript">
    function getSectionByClass(class_id, section_id) {
        if (class_id != "" && section_id != "") {
            $('#section_id').html("");
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        var sel = "";
                        if (section_id == obj.section_id) {
                            sel = "selected";
                        }
                        div_data += "<option value=" + obj.section_id + " " + sel + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        }
    }

    $(document).ready(function () {
        $(document).on('change', '#class_id', function (e) {
            $('#section_id').html("");
            var class_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.section_id + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        });
        var class_id = $('#class_id').val();
        var section_id = '<?php echo set_value('section_id') ?>';
        getSectionByClass(class_id, section_id);
        $(document).on('change', '#feecategory_id', function (e) {
            $('#feetype_id').html("");
            var feecategory_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "feemaster/getByFeecategory",
                data: {'feecategory_id': feecategory_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.id + ">" + obj.type + "</option>";
                    });
                    $('#feetype_id').append(div_data);
                }
            });
        });
    });
    $(document).on('change', '#section_id', function (e) {
        $("form#schedule-form").submit();
    });
</script>
<style>
.gallery{
width:200px; 
float:left; 
padding: 5px;
margin: 5px;
border:3px #eeeeee solid;
text-align:center;
box-shadow: 2px 2px 2px grey;
background: ;
}

.gallery .pname{
width:100%; 
position:relative;
text-align:left;
padding-top:5px;
height:150px;
overflow:auto;
}

.gallery .pphoto{
height:200px;
max-height:200px;
overflow:hidden;
text-align:center;
background:white;
}

.gallery img {
min-width:100%;
max-width:100%;
padding:0px;
margin:0px;
}
p {display:inline;}

</style>
