<?php
session_start();
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_GET);
//echo "sub_div: $subdivision, station: $station, month: $month, year: $year";  
/*$month = '03';
$year = '2016';
$river = '12';
$station ='SW26';*/
$date_array = explode("-", $date);
$month = $month;
$year = $year;
$month_day = cal_days_in_month(CAL_GREGORIAN,$month,$year);
$curr_date =  date('d-M-Y');
?>

<style>
  select, textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
  display: inline-block;
  height: 1px!important;
  padding: 8px 3px!important;
  margin-bottom: 2px!important;
  font-size: 13px!important;
  line-height: 23px!important;
  color: #555;
  vertical-align: middle;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}
.table th, .table td {
  padding: 2px!important;
  line-height: 20px;
  text-align: left;
  vertical-align: top;
  border-top: 1px solid #ddd;
}

/* #rf_data_table {
  width: 500px;
  position: relative;
  left: 280px;
} */

/* #rf_sva_btn{
  position: relative;
  left: 706px;
  bottom: 5px;
} */
.tbl_border {
  border: 1px solid #ddd;
}
</style>

<input type="hidden" style="width: 95%;" id="rf_mon_tot_day" name="rf_mon_tot_day" value="<?php echo $month_day; ?>" />
<div class="row-fluid" style="margin-bottom:10px; margin-top:-17px!important;">
<div class="span4">
	<div class="control-group" style="float:right;">
		<div class="controls">	
		
		<?php
				if($_SESSION['delete'] == 'Y'){
				echo '<button id="rainfall_delete_btn" type="button" style="margin-right: 1px; font-size: 12px; float: right;" class="btn btn-small btn-danger" onclick="rainfall_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
				}
		?>
		<button id="rf_sva_btn" style="float: right; margin-right:2px; font-size: 12px;" type="button" class="btn btn-small btn-success" onclick="save_all_de_rf_data()"><i class="icon-save"></i><b>Save All</b></button>
		</div>
	</div>

	<table id="rf_data_table" class="table table-bordered table-hover" style = "margin-top: 40px;">
		<thead>
			
			<tr style="font-family: 'Open Sans';">
				<td style="text-align: center; font-size:13px; border: 1px solid #ddd;" class="table-header">Date</td>
				<td style="text-align: center; font-size:13px; border: 1px solid #ddd;" class="table-header">Rainfall (mm)</td>
				<!-- <td style ="width: 40%;"></td> -->
			</tr> 
		</thead>

		<tbody class="tbl_border">
			<?php   
						$list=array();
						// $month = 03;
						// $year = 2016;
					   
						for($d=1; $d<=$month_day; $d++)
						{
							$time=mktime(12, 0, 0, $month, $d, $year);          
							if (date('m', $time)==$month)       
							{ 
								$show_date = date('d-M-Y', $time);
										  $show_date = strtoupper($show_date);
							   
								$rf_cond = "STATION_ID='$station' AND TO_DATE(RF_DATE, 'DD-MON-RRRR')='$show_date'";
								$rf_cols = "TO_DATE(RF_DATE, 'DD-MON-RR') AS RAIN_DATE,RAIN_FALL";
								$rf_info = $obj->View_column_details_By_Cond_hydro("RAIN_FALL_DATA ",$rf_cols,$rf_cond);

								$rf_val = number_format((float)$rf_info['RAIN_FALL'], 2, '.', '');

								$sum_total_info += $rf_val;

								$date_id = "date_$d";
										  $rf_col_id = "rf_$d";
							  
										  $next_row_val = $d + 1;
										$next_row_val = "rf_$next_row_val";

								if(strtotime($show_date) > strtotime($curr_date)){
								  $is_readonly = "readonly";
								  $call_key_up_function = "show_alert(event)";
								}
								else{
								  $is_readonly = "";
								  $call_key_up_function = "rf_call_next_val_loader(event,'$rf_col_id','$next_row_val','$d')";
								}
			?>
			<tr>
				<td style="text-align: center;">
					<input type="text" id="<?php echo $date_id; ?>" class="input-small month_date" style="text-align: center; font-weight: 700;" value="<?php echo $show_date; ?>" readonly>
				</td>
				<td style="text-align: center;">
					<input onkeypress="return isNumberKey(event)" type="text" class="input-mini rainfall" style="text-align: center; font-weight: bold;" id="<?php echo $rf_col_id;?>"
					value="<?php echo $rf_val; ?>" onkeyup="<?php echo $call_key_up_function; ?>" <?php echo $is_readonly; ?>>
				</td>
			</tr>
		<?php }} ?>    
		</tbody>
		<tfoot>
			<tr>
				<td style="text-align: center; font-size:13px; border: 1px solid #ddd;" class="table-header">Total Rainfall (mm)</td>
				<td style="text-align: center; font-size:13px; border: 1px solid #ddd;" class="table-header"><input type="text" class="input-mini" style="text-align: center;" id="total-rainfall" value="<?php echo $sum_total_info;?>" readonly></td>
			</tr>
		</tfoot>
	</table>
		
		<?php
				if($_SESSION['delete'] == 'Y'){
				echo '<button id="rainfall_delete_btn" type="button" style="margin-right: 1px; font-size: 12px; float: right;" class="btn btn-small btn-danger" onclick="rainfall_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
				}
		?>
		<button id="rf_sva_btn" style="float: right; margin-right:2px; font-size: 12px;" type="button" class="btn btn-small btn-success" onclick="save_all_de_rf_data()"><i class="icon-save"></i><b>Save All</b></button>
	</div>
	<div class="span4">
	</div>
</div>

<script>
    function rf_call_next_val_loader(e, rf_col_id,next_col_id,date_id){
		var sum = 0;
		$(".rainfall").each(function(){	
			//alert($(this).val()) ;
			var sel_rf_in = parseFloat($(this).val());
			if(!isNaN(sel_rf_in)){
			  sum += sel_rf_in;
			}
		});
		// alert(sum);
		sum = sum.toFixed(2);
        $('#total-rainfall').val(sum);

        if(event.keyCode==13 || event.keyCode=='tab'){
            var curr_val = $("#"+rf_col_id).val();
			//if (curr_val > Math.floor(curr_val)) {
				curr_val = parseFloat(curr_val).toFixed(2);
			//}
			$("#"+rf_col_id).val(curr_val);
           // $("#"+rf_col_id).select();
            $("#"+next_col_id).select();
        }
    }
    function show_alert(){
		alert('You cannot enter for any day after today');
	}
</script>

<script>
let inputReset = false;
function isNumberKey(evt) {
    const key = event.keyCode || event.which;
  const keyChar = String.fromCharCode(key);

  const regex = /[0-9]|\.|\-/;

  // Only allow numeric, decimal, or minus sign input
  if (!regex.test(keyChar)) {
    event.preventDefault();
    return false;
  }

  const input = event.target;
  const currentValue = input.value;

  // If the input already contains a decimal point or minus sign, disallow another one
  if ((keyChar === "." && currentValue.indexOf(".") !== -1) || 
      (keyChar === "-" && currentValue.indexOf("-") !== -1)) {
    event.preventDefault();
	event.target.value = "";
    event.target.value += keyChar;
    return false;
  }
  
  // If input should be reset, do so and allow new input
  if (inputReset) {
    input.value = '';
    inputReset = false;
  }

  // If the input would result in a value less than -99.99 or greater than 99.99, disallow it
  const newValue = parseFloat(currentValue + keyChar);
  if (newValue > 999) {
    event.preventDefault();
    event.target.value = "";
    event.target.value += keyChar;
    return false;
  }



  // If the input would result in more than 2 decimal places, set inputReset flag
  const parts = currentValue.split(".");
  if (parts.length === 2 && parts[1].length > 3 && keyChar !== "-") {
    inputReset = true;
    event.preventDefault();
    return false;
  }
  
  if (currentValue === " " && keyChar !== "." && keyChar !== "-") {
    event.preventDefault();
    return false;
  }

  return true;
}
</script>