<?php
session_start();
$user_id=$_SESSION['LOGIN_USERID'];
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_REQUEST);
$date = strtoupper(date("d-M-y"));


if($operation == 'date_change'){
	$dt_change = "-";
	$dt_dot = ".";
	$input_date = $_POST['date'];
	$sel_in_dot = str_replace($dt_dot,$dt_change,$input_date);	

	$sel_in_date = str_replace("/",$dt_change,$sel_in_dot);
	$date_arr = explode($dt_change,$sel_in_date);
	if(strlen($date_arr[2])==2){
		$in_date = date('y-M-d', strtotime($sel_in_date));
	}
	else{
		$in_date = date('Y-M-d', strtotime($sel_in_date));
	}
	$in_date = date('d-m-Y', strtotime($in_date));
	
	$sel_in_date = date('d-M-y', strtotime($in_date));
	// print_r($sel_in_date);
	// exit();
	$curr_date = date('d-m-Y');
	$in_date = strtotime($in_date);
	
	$ret_msg = 0;
	if($in_date > strtotime($curr_date)){
		$ret_msg = 0;
	}
	else{
		$ret_msg = strtoupper($sel_in_date);
	}
	echo $ret_msg;
}



// if($operation=='save_multiple'){
// 	$ret_msg = "";
// 	$dt_change = "-";
// 	$date_arr = explode($dt_change,$v_date);
// 	if(strlen($date_arr[1])==2){
// 		$in_date = date('y-M-d', strtotime($v_date));
// 		$in_date = strtoupper($in_date);
// 	}
// 	else{
// 		$in_date = $v_date;
// 	}
// 	$data_arr = explode(",",$data_list);
// 	// $data_arr = explode("#",$data_list);
// 	$up_success = 0;
//     // print_r(count($data_arr));
// 	for( $i=0; $i<count($data_arr); $i++) {
// 		$ret_msg .= $data_arr[$i]."<br/>";
// 		$input_arr = explode('#', $data_arr[$i]);
// 		$form_data['VERIFIED_WL']= "'".$data_arr[1]."'";
// 		$form_data['STATUS']="'VR'";
// 		$form_data['VERIFIED_LOG_DATE']="TO_DATE('$in_date', 'DD-MON-RR')";
// 		$form_data['UPDATE_BY']="'$user_id'";
// 		//$form_data['UPDATE_DATE']= "'$date'";
// 		$form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
// 		$table_name='MDM_WELL_DATA_LOG';
// 		//$where_cond="ID=".$input_arr[0];
// 		$id_val = $data_arr[0]; 
// 		print_r($form_data);
// 		// exit();
// 		$where_cond = "ID='$id_val'";	
// 		$update = $obj->Update_Data($table_name, $form_data, $where_cond);
// 		// if($update) {
// 		// 	$up_success++;
// 		// }
// 	}
// 	echo $up_success;
// }

if($operation=='save_multiple'){
	$dt_change = "-";
	$data_arr = explode("#",$data_list);
	$in_date = strtoupper($data_arr[2]);
	$id_val = $data_arr[0];
	$form_data['VERIFIED_WL']= "'".$data_arr[1]."'";
	$form_data['STATUS']="'VR'";
	$form_data['VERIFIED_LOG_DATE']="TO_DATE('$in_date', 'DD-MON-RR')";
	$form_data['UPDATE_BY']="'$user_id'";
	$form_data['UPDATE_DATE']= "TO_DATE('$date','DD-MON-RR')";
	$table_name='BWDBFFWC.MDM_WELL_DATA_LOG';
	// print_r($form_data);
	$where_cond = "ID='$id_val'";	
	$update = $obj->Update_Data($table_name, $form_data, $where_cond);
	echo $update;
	
}

if($operation=='update_sms_content'){
	$ret_msg = "";
	$data_arr = explode(",",$data_list);
	$up_success = 0;
	for( $i=0; $i<count($data_arr); $i++) {
		//$ret_msg .= $data_arr[$i]."<br/>";
		$input_arr = explode('#', $data_arr[$i]);
		$form_data['UP_SMS_CONTENT']=$input_arr[1];
		$form_data['UPDATE_BY']=$user_id;
		$form_data['UPDATE_DATE']=$date;
		$table_name='MDM_SMS_INBOX_LOG';
		$where_cond="ID=".$input_arr[0];	
		$update = $obj->Update_Data($table_name, $form_data, $where_cond);
		if($update) {
			$up_success++;
		}
	}
	echo $up_success; 
}

if($operation=='approve'){
	//print_r($data_list);
	$ret_msg = "";
	if(strpos($data_list,",")!=""){
		$data_arr = explode(",",$data_list);
	}
	else{
		$data_arr = $data_list;
	}
	//print_r($data_arr);
	$up_success = 0;
	
	for( $i=0; $i<count($data_arr); $i++) {
		if(count($data_arr) == 1){
			$input_arr = explode('#', $data_arr);
		}else{
			$input_arr = explode('#', $data_arr[$i]);
		}
		//print_r($input_arr);
		$in_date = strtoupper($input_arr[2]);
		$sel_id = trim($input_arr[0]);
		$form_data['VERIFIED_WL'] = "'".$input_arr[1]."'";
		$form_data['WELL_ID'] = "'".$input_arr[3]."'";
		// $form_data['VERIFIED_LOG_DATE'] = "TO_DATE('$in_date', 'DD-MON-RR')";
		$form_data['STATUS'] =  "'A'";
		$form_data['TRANSFER_STATUS'] =  "'P'";
		$form_data['UPDATE_BY'] = "'$user_id'";
		$form_data['UPDATE_DATE'] = "TO_DATE('$date','DD-MON-RR')";
		//$form_data['UPDATE_DATE'] = "'"$date"'";
		// print_r($form_data);
		// exit();
		$table_name = 'MDM_WELL_DATA_LOG';
		$where_cond = "ID='$sel_id'";	
		$approve = $obj->Update_Data($table_name, $form_data, $where_cond);
		if($approve) {
			$well_data_cols = "ID,WELL_ID,LOG_DATE,VERIFIED_WL";
			$well_data_tbl = "MDM_WELL_DATA_LOG";
			$well_data_cond = "ID='$sel_id'";
			$well_data_info = $obj->View_column_details_By_Cond($well_data_tbl,$well_data_cols,$well_data_cond);
			//var_dump($well_data_info);
			$single_well_data = array(
	 			"station_id" => trim($well_data_info['WELL_ID']),
	 			"read_date" => $well_data_info['LOG_DATE'],
				"wl_data" => $well_data_info['VERIFIED_WL']
			);
			$single_well_data_json = json_encode($single_well_data);
			//var_dump($single_well_data_json);
			//$ret_msg = call_curl($single_well_data_json);
			$newDate = $well_data_info['LOG_DATE'];
			$newDate = strtoupper($newDate);
			$date_time = "";
				$date_time = "$newDate";
				$form_data_update['WELL_ID'] = "'".trim($well_data_info['WELL_ID'])."'";
				$form_data_update['DT'] = "TO_DATE('$newDate', 'DD-MON-RR')";
				$form_data_update['WATER_LVL'] = "'".$well_data_info['VERIFIED_WL']."'";
				$form_data_update['UPDT_BY'] = "'$user_id'";
				$form_data_update['UPDT_DTM'] = "TO_DATE('$date','DD-MON-YY  HH24:MI:SS')";
			 //  print_r($form_data_update);
			$update_cond = "WELL_ID='".trim($well_data_info['WELL_ID'])."' AND TO_CHAR(DT, 'DD-MON-YY')='$date_time'";
			  $update2 = $obj->Update_Or_Insert_Data_By_Check_Duplicate_hydro('GWT_DATA_TBL', $form_data_update, $update_cond);
			if($update2){
				$up_transfer_stat_data['TRANSFER_STATUS'] = 'T';
				$up_transfer_stat_data['UPDATE_BY'] = $user_id;
				$up_transfer_stat_data['UPDATE_DATE'] = $date;
				$table_name = 'MDM_WELL_DATA_LOG';
				$where_cond = "ID='$sel_id'";
				$update = $obj->Update_Data($table_name, $up_transfer_stat_data, $where_cond);
			}
			if($update) {
				$up_success++;
			}
		}
	}
	echo $up_success;
	
}
?>