<!-- DUES MODAL STARTS -->
<style>

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 10000; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  margin-left:0px;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.7); /* Black w/ opacity */
}



/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
}

/* The Close Button */
.modal-header .close1 {
  color: white !important;
  float: right;
  font-size: 28px;
  font-weight: bold;
  z-index:10;
  margin-top:5px;
  background:crimson;
  width:25px;
  height:25px;
  border-radius:50%;
  padding:8px 0px 0px 8px;
}

.close1:hover,
.close1:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}
.modal-header {
  padding: 0px 16px;
  background-color: #3B5999;
  color: white;
  margin:-20px;
  margin-top:-30px;
}
.modal-header h4 {
    color:white;
}
.modal-body {
    padding: 2px;
    /*height:400px;*/
    width:100%;
    /*overflow:hidden;*/
}
</style>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">

    <div class="modal-header">
          <span class="close1">&times;</span>
          <h4>Urgent Payment Reminder</h4>
    </div>
    <br>
    <div class="modal-body">
        <!--<p>Some text in the Modal..</p>-->
        <?php
            $school = $_SERVER["HTTP_HOST"];
            $session = $_SESSION["admin"]["session"];
            $url = "https://bps.savarnacloud.com/assignmentApi/get_dues.php?school=$school&session=$session";
        
            $ch = curl_init();
        
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        
            $aryList=curl_exec($ch);
        
            curl_close($ch);
            $aryList = json_decode($aryList,true);
            // print_r($aryList);
            $totaldue = 0;

	?>
    	<br>
	    <label>Dear Sir/Madam,</label>
	    <label>Following invoices are Due / <span style="color:crimson;">Over-due</span> for payment.</label>
	    <br>
	    <table class="table" border="1">
	        <thead>
	            <tr>
	                <th>S.No.</th>
	                <th>Month</th>
	                <th>Amount</th>
	            </tr>
	        </thead>
	        <tbody>
	            <?php foreach($aryList as $list){ $i++; ?>
	               <tr>
	                   <td><?php echo $i; ?></td>
                        <?php $ij = $list["month"];
						  if($ij<=9){
							$newDate=date("Y")."-".($ij+3)."-1";
							$month1= date("F",strtotime($newDate));
						  }
						  else{
							$newDate=date("Y")."-".($ij-9)."-1";
							$month1=date("F",strtotime($newDate));
						  }
					    ?>
					    <td><?php echo $month1; ?></td>
	                   <td><?php echo $list["amount"]; $totaldue+=$list["amount"]; ?></td>
	               </tr>
	            <?php } ?>
	        </tbody>
	        <tfoot>
	            <tr>
	                <td colspan="2">Total</td>
	                <td><?php echo $totaldue; ?></td>
	            </tr>
	        </tfoot>
	    </table>
	    <br>
	    <label>Kindly make immediate payment for continued and better services.</label>
	    <label>Kindly call us at <span style="color:crimson;">9685094177</span> for any billing related issues.</label>
        <label>Thank You</label>	    
    </div>

  </div>

</div>

<script>
// alert(document.referrer);
// Get the modal
var modal = document.getElementById("myModal");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close1")[0];

function showmodal(){
      modal.style.display = "block";
// When the user clicks on <span> (x), close the modal
}

span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

$(document).ready(function(){
    
    var str = document.referrer; 

    var show = str.search("adminlogin.php");

    var due = '<?php echo $totaldue; ?>';
    if((due>0) && (show>0)){
        showmodal();
    }
});
</script>

<!-- DUES MODAL ENDING -->

<?php
$pgMod="school_dues";
$pgAct="view";

if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
			unset($_SESSION['form']);
if($_POST) 
{
    print_r($_POST);
    // die();
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;

    if(!isset($_POST['school_url']) || trim($_POST['school_url'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter school URL."); 
	}

	$url =  $_POST['school_url'];
    $db->delete("all_school_dues","where school_url='$url' " );
//     if($flgEr!=TRUE && $pgAct=="add")
// 	{
    // 	$checkexists = $db->getRows("SELECT * from all_school_dues where school_url='".$url."' and session='".$_SESSION['admin']['session']."' ");
    // 	if(count($checkexists)>0){
    //     	    $flgEr=TRUE;
    //     	    array_push($alert_err, "School URL already exists.");
    // 	}
    //     else {
    
            $i = 0;
    	    foreach($_POST["month"] as $x => $val){
    	        echo $x;
    	        $j = $x+1;
        		$aryData=array(
        					'school_url'    =>	$_POST['school_url'],
        					'view'          =>  $_POST['view'],
        					'month'         =>  $val,
        					'amount'        =>  $_POST['amount'][$x],
        					'status'        =>  $_POST["status_$j"],
        					'session'       =>  $_POST['session'][$x]
            			);
         	    $flgIn=$db->insertAry("all_school_dues",$aryData);	
         	  $i++;
            }

        // }
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
		}
		else
		{
		array_push($alert_err,$db->getErMsg());
		}

// 	}
// 	elseif($pgAct=="edit" && isset($_GET['school']) && trim($_GET['school'])!='')
// 	{
//         $i = 0;
// 	    foreach($_POST["month"] as $month){
// 	        $j = $i+1;
//     		$aryData=array(
//     					'view'          =>  $_POST['view'],
//     					'month'         =>  $month,
//     					'amount'        =>  $_POST['amount'][$i],
//     					'status'        =>  $_POST["status_$j"],
//     					'session'       =>  $_POST['session'][$i]
// 					);
//             $flgUp=$db->updateAry("all_school_dues",$aryData,"where school_url='".$_GET['school']."' and month='$month' ");
//             $i++;
//         }

// 		if(!is_null($flgUp))
// 		{
// 			$_SESSION['msg']='update Successfully';
// 			unset($_SESSION['form']);
// 			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
// 		}
// 		else
// 		{
// 			array_push($alert_err,$db->getErMsg());
// 		}
// 	}

}		
elseif($pgAct=="delete" && isset($_GET['school']) && trim($_GET['school'])!='')
{
	$res=$db->delete("all_school_dues","where school_url = '".$_GET['school']."' ");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
    redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
}
elseif($pgAct=="delete_record" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("all_school_dues","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"=>"Promote", "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>complain</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>Student Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["school"]!="")
{
// 	$aryForm=$db->getRows("SELECT * FROM all_school_dues WHERE  school_url='".$_GET['school']."' order by cast(month as signed)");
	$aryForm=$db->getRows("SELECT * FROM all_school_dues WHERE  school_url='".$_GET['school']."' order by id ");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['school']) && trim($_GET['school']) !=''))
{
 ?>

	<div class="row-fluid">
        <form method="post" enctype="multipart/form-data" action="">
			<div class="span12">

				<div class="top-bar">
					<h3><i class="icon-cog"></i> School Dues Records</h3>
				</div>

				<div class="well no-padding">

                    <div class="control-group">
						<label class="control-label" for="detail">School URL</label>
						<div class="controls">
							<input type="text" name="school_url" class="input-block-level" placeholder="bps.savarnacloud.com" value="<?php echo $aryForm[0]['school_url']; ?>" required>
						</div>
					</div>
					
					 <div class="control-group">
						<label class="control-label" for="detail">Viw to School</label>
						<div class="controls">
			                <input type="radio" name="view" value="1" <?php if($aryForm[0]['view']=="1"){echo "checked";} ?> checked /> Yes &emsp;&emsp;
			                <input type="radio" name="view" value="0" <?php if($aryForm[0]['view']=="0")echo "checked"; ?>  /> No
						</div>
					</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Dues Data</label>
							<div class="controls">
								<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>Month</th>
								            <th>Session</th>
								            <th>Dues Amount</th>
								            <th>Status</th>
								        </tr>
								    </thead>
								    <tbody id="tbodylink">
								        <?php $j=1; 
								        $aryPgAct=array("src"=>"Promote", "page_id"=>$pgMod);
								        if(is_array($aryForm) && count($aryForm))
                                          {
                                            foreach($aryForm as $record)
                                            {   ?>
								        <tr>
								            <td><?php echo $j; ?>.</td>
								            <td>
								                <select name="month[]">
								                    <option value="" >--Select Month--</option>
								                    <?php for($i=1;$i<=12;$i++){
								                        $ij=$i;
                                						  if($ij<=9){
                                							$newDate=date("Y")."-".($ij+3)."-1";
                                							$month1= date("F",strtotime($newDate));
                                						  }
                                						  else{
                                							$newDate=date("Y")."-".($ij-9)."-1";
                                							$month1=date("F",strtotime($newDate));
                                						  } ?>
                                						  <option value="<?php echo $month1; ?>" <?php if($record['month']==$month1)echo "selected"; ?> ><?php echo $month1; ?></option>
                                						  <?php } ?>
								                </select>
								            </td>
								            <td>
								                <select name="session[]">
								                    <option value="" >--Select Session--</option>
								                    <?php  $allsession = $db->getRows("SELECT session FROM `session` ORDER BY session DESc");
								                        $sessionary = array();
    								                    foreach($allsession as $session){ $sessionary[]=$session['session']; ?>
                                						  <option value="<?php echo $session['session']; ?>" <?php if($record['session']==$session['session'])echo "selected"; ?> ><?php echo $session['session']; ?></option>
                                						  <?php } ?>
								                </select>
								            </td>
                						    <td><input type="text" name="amount[]" value="<?php echo $record['amount'] ;?>" /></td>
								            <td>
								                <input type="radio" name="status_<?php echo $j; ?>" value="0" <?php if($record['status']=="0"){echo "checked";} ?> checked /> Due &emsp;
								                <input type="radio" name="status_<?php echo $j; ?>" value="1" <?php if($record['status']=="1")echo "checked"; ?>  /> Paid 
							                </td>
		                                    <td>
		                                        <!--<a class="delete" href="javascript:;" style="font-size:15px;">-->
                                          <!--      <button type="button" class="btn btn-info">Remove</button>-->
                                          <!--      </a>-->
                                          <?php $aryPgAct["id"] = $record['id']; $aryPgAct['action']="delete_record"; ?>
                                        <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 $j++;
                                            } } else {
                                            ?>
								        <tr>
								            <td>1.</td>
								            <td>
								                <select name="month[]">
								                    <option value="" >--Select Month--</option>
								                    <?php for($i=1;$i<=12;$i++){
								                        $ij=$i;
                                						  if($ij<=9){
                                							$newDate=date("Y")."-".($ij+3)."-1";
                                							$month1= date("F",strtotime($newDate));
                                						  }
                                						  else{
                                							$newDate=date("Y")."-".($ij-9)."-1";
                                							$month1=date("F",strtotime($newDate));
                                						  } ?>
                                						  <option value="<?php echo $month1; ?>"><?php echo $month1; ?></option>
                                						  <?php } ?>
								                </select>
								            </td>
								            <td>
								                <select name="session[]">
								                    <option value="" >--Select Session--</option>
								                    <?php  $allsession = $db->getRows("SELECT session FROM `session` ORDER BY session DESc");
								                        $sessionary = array();
    								                    foreach($allsession as $session){ $sessionary[]=$session['session']; ?>
                                						  <option value="<?php echo $session['session']; ?>"><?php echo $session['session']; ?></option>
                                						  <?php } ?>
								                </select>
								            </td>
                						    <td><input type="text" name="amount[]" value="<?php echo $aryForm[$i-1]['amount'] ;?>" /></td>
								            <td>
								                <input type="radio" name="status_<?php echo $j; ?>" value="0" <?php if($aryForm[$i-1]['status']=="0"){echo "checked";} ?> checked /> Due &emsp;
								                <input type="radio" name="status_<?php echo $j; ?>" value="1" <?php if($aryForm[$i-1]['status']=="1")echo "checked"; ?>  /> Paid 
							                </td>
								        </tr>
								         <?php } ?>
								        <?php
								        // for($i=1;$i<=2;$i++){
                							?>
                        <!--                    <tr>-->
                    				<!--		    <td><?php echo $month1; ?>-->
                        <!--						    <input type="hidden" name="month[]" value="<?php echo $i; ?>" />-->
                    				<!--		    </td>-->
                    				<!--		    <td><input type="text" name="amount[]" value="<?php echo $aryForm[$i-1]['amount'] ;?>" /></td>-->
    								            <!--<td>-->
    								            <!--    <input type="radio" name="status_<?php echo $i; ?>" value="0" <?php if($aryForm[$i-1]['status']=="0"){echo "checked";} ?> checked /> Due &emsp;&emsp;&emsp;-->
    								            <!--    <input type="radio" name="status_<?php echo $i; ?>" value="1" <?php if($aryForm[$i-1]['status']=="1")echo "checked"; ?>  /> Paid -->
    							             <!--   </td>-->
                    						    <?php
                    						  //echo "</tr>";
                    						  //} ?>

								    </tbody>
								</table>
							</div>
						</div>

						<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{
?>
	<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=Promote&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"=>"Promote", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM all_school_dues group by school_url  "); $i=0;

	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>School Url</th>
                    <th>Total Dues Amount</th>
                    <th>View School</th>
                    <th>Action</th>
	            </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++;
                     $aryPgAct["school"]=$clist['school_url'];
             ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i ?></td>
              		<td><?php echo $clist['school_url']; ?> </td>
              		<td><?php echo $db->getVal("select sum(amount) from all_school_dues where school_url='$clist[school_url]' and status=0 "); ?></td>
              		<td>
              		    <?php if($clist['view']=='1'){echo "Yes";} else echo "No"; ?>
              		</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>complain</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}
?>

<script>
	$('tbody td a.delete').click(function() { 
		$(this).parent().parent().remove(); 
	});
    var months = [ "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December",
                    "January",
                    "February",
                    "March"
                    ];

    let text = "<td><select name='month[]'>";
    for (let i = 0; i < months.length; i++) {
      text += "<option>" + months[i] + "</option>";
    }
    text += "</select></td>";
        
    let allsession = '<?php echo json_encode($sessionary, TRUE); ?>';
    var sessions = JSON.parse(allsession);
    let ses = "<td><select name='session[]'>";
    for (let i = 0; i < sessions.length; i++) {
      ses += "<option>" + sessions[i] + "</option>";
    }
    ses += "</select></td>";

	$("#Addlink").click(function(){
		var rowCount = ($('#tbodylink tr').length)+1;

		var add_tr_skill='<tr><td>'+rowCount+'</td>';
		    add_tr_skill+=text;
		    add_tr_skill+=ses;
		    add_tr_skill+='<td><input class="input-block-level" type="text" name="amount[]"></td><td><input type="radio" name="status_'+rowCount+'" value="0" checked /> Due &emsp;   <input type="radio" name="status_'+rowCount+'" value="1" /> Paid</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>




