<?php 
session_start();
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_REQUEST);
//print_r($_REQUEST);
//echo $details_id;

$user_id = $_SESSION['LOGIN_USERID'];
$date = date('d-M-y H:i:s');
$date = strtoupper($date);
$rand_num = mt_rand(111,999);
$newName = date('y').date('m').date('d').$rand_num;

//hole info save start
if ($operation == "save_hole_info") {
	$change = "''";
	$e_hole_no = str_replace("'",$change,$e_hole_no);
	$e_at = str_replace("'",$change,$e_at);
	$e_hours = str_replace("'",$change,$e_hours);
	$e_lat = str_replace("'",$change,$e_lat);
	$e_lon = str_replace("'",$change,$e_lon);
	$e_total_sample = str_replace("'",$change,$e_total_sample);
	$e_total_depth = str_replace("'",$change,$e_total_depth);
    $e_group_level = str_replace("'",$change,rawurldecode($e_group_level));
    $e_rl = str_replace("'",$change,rawurldecode($e_rl));
    $e_water_level = str_replace("'",$change,rawurldecode($e_water_level));
    $e_hole_location = str_replace("'",$change,rawurldecode($e_hole_location));
	
	$new_e_date_started    = date("d-M-y", strtotime($e_date_started));
	$new_e_date_completed  = date("d-M-y", strtotime($e_date_completed));

	$new_e_date_started = strtoupper($new_e_date_started);
	$new_e_date_completed = strtoupper($new_e_date_completed);

	$form_data = array(
		'PROJECT'       => "'$project_id'",
		'HOLE_NO'       => "'$e_hole_no'",
		'HOLE_LOCATION' => "'$e_hole_location'",
		'GROUND_LEVEL'  => "'$e_group_level'",
		'WATER_LEVEL'   => "'$e_water_level'",
		'TIME'          => "'$e_at'",
		'DURATION'      => "'$e_hours'",
		'START_DATE'    => "TO_DATE('$new_e_date_started','DD-MON-YY')",
		'END_DATE'      => "TO_DATE('$new_e_date_completed','DD-MON-YY')",
		'RL_VAL'        => "'$e_rl'",
		'LATITUDE'      => "'$e_lat'",
		'LONGITUDE'     => "'$e_lon'",
		'UNIQ_ID'       => "'$newName'",
		'TOTAL_SAMPLE'  => "'$e_total_sample'",
		'TOTAL_DEPTH'   => "'$e_total_depth'",
	);
//        print_r($form_data);

	$table_name = "HYDROLOGY.LITHO_FIELD_BORE_LOG_MAS@HYDRO10";
	echo $insert = $obj->Insert_Data($table_name, $form_data);
}
//hole info save end
if ($operation == "insert") {
	$change = "''";
	$new_date_started = date("d-M-y", strtotime($date_started));
	$new_date_completed = date("d-M-y", strtotime($date_completed));

	$date_started = strtoupper($new_date_started);
	$date_completed = strtoupper($new_date_completed);

	$group_level = str_replace("'",$change,rawurldecode($group_level));
	$reduce_level = str_replace("'",$change,rawurldecode($reduce_level));
	$water_level = str_replace("'",$change,rawurldecode($water_level));
	$location = str_replace("'",$change,rawurldecode($location));
	$at = str_replace("'",$change,$at);
	$hours = str_replace("'",$change,$hours);
	$total_depth = str_replace("'",$change,$total_depth);
	$latitude = str_replace("'",$change,$latitude);
	$longitude = str_replace("'",$change,$longitude);
	$total_sample = str_replace("'",$change,$total_sample);
      
	$form_data = array(
        'PROJECT' => "'$project_name'",
        'HOLE_LOCATION' => "'$location'",
        'GROUND_LEVEL' => "'$group_level'",
        'WATER_LEVEL' => "'$water_level'",
        'TIME' => "'$at'",
        'DURATION' => "'$hours'",
        'START_DATE' => "TO_DATE('$date_started','DD-MON-YY')",
        'END_DATE' => "TO_DATE('$date_completed','DD-MON-YY')",
        'DRILLED_BY' => "'$drilled_by'",
        'UNIT' => "'$unit'",
        'TOTAL_DEPTH' => "'$total_depth'",
        'LATITUDE' => "'$latitude'",
        'LONGITUDE' => "'$longitude'",
        'RL_VAL' => "'$reduce_level'",
        'TOTAL_SAMPLE'  => "'$total_sample'"
    );
       // print_r($form_data);
       
    $table_name = "HYDROLOGY.LITHO_FIELD_BORE_LOG_MAS@HYDRO10";
    $where_cond = "PROJECT='$project_name' AND UNIQ_ID='$is_hole_id'";
       
	$insert = $obj->Update_Or_Insert_Data_By_Check_Duplicate($table_name,$form_data,$where_cond);

    if ($insert) {
        $details_id_list_arr = explode(",", $details_id);
        $nmber_of_sample_list_arr = explode(",", $nmber_of_sample_list);
        $type_of_smple_list_arr = explode(",", $type_of_smple_list);
        $type_of_sample_meter_arr = explode(",", $type_of_sample_meter);
        $scale_list_arr = explode(",", $scale_list);
        $lithological_description_list_arr = explode(",", $lithological_description_list);
        $density_list_arr = explode(",", $density_list);
        $date_list_arr = explode(",", $date_list);
        $color_list_arr = explode(",", $color_list);
        $moisture_list_arr = explode(",", $moisture_list);
        $dilatancy_list_arr = explode(",", $dilatancy_list);
        $bos_0_6_list_arr = explode(",", $bos_0_6_list);
        $bos_6_12_list_arr = explode(",", $bos_6_12_list);
        $bos_12_18_list_arr = explode(",", $bos_12_18_list);
        $bos_18_24_list_arr = explode(",", $bos_18_24_list);
              
        for($i=0; $i<count($nmber_of_sample_list_arr); $i++) {
			$details_id_list_arr_value = explode("#",  $details_id_list_arr[$i]);
			$nmbr_sample_list_value = explode("#", $nmber_of_sample_list_arr[$i]);
			$type_sample_list_value = explode("#", $type_of_smple_list_arr[$i]);
			$type_of_sample_meter_value = explode("#", $type_of_sample_meter_arr[$i]);
			$scale_list_value = explode("#", $scale_list_arr[$i]);

			$lithological_list_value = explode("#", $lithological_description_list_arr[$i]);
			$density_list_value = explode("#", $density_list_arr[$i]);
			$color_list_value = explode("#", $color_list_arr[$i]);
			$moisture_list_value = explode("#", $moisture_list_arr[$i]);
			$dilatancy_list_value = explode("#", $dilatancy_list_arr[$i]);
			$bos_0_6_list_value = explode("#", $bos_0_6_list_arr[$i]);
			$bos_6_12_list_value = explode("#", $bos_6_12_list_arr [$i]);
			$bos_12_18_list_value = explode("#", $bos_12_18_list_arr[$i]);
			$bos_18_24_list_value = explode("#", $bos_18_24_list_arr[$i]);
                     
			$new_details_id_list_arr_value = $details_id_list_arr_value[1];
			$new_mnbr_sample_list_value = $nmbr_sample_list_value[1];
			$new_typ_sample_list_value = $type_sample_list_value[1];
			$type_of_sample_meter_value = $type_of_sample_meter_value[1];
			$new_scale_list_value = $scale_list_value[1];
			$new_litho_list_value = $lithological_list_value[1];
			$new_density_list_value = $density_list_value[1];
			$new_color_list_value = $color_list_value[1];
			$new_moisture_list_value = $moisture_list_value[1];
			$new_dilatncy_list_value = $dilatancy_list_value[1];
			$new_bos_0_6_list_value =  $bos_0_6_list_value[1];
			$new_bos_6_12_list_value = $bos_6_12_list_value[1];
			$new_bos_12_18_list_value = $bos_12_18_list_value[1];
			$new_bos_18_24_list_value = $bos_18_24_list_value[1];
                  
            $form_data_update['NO_OF_SAMPLE']        = "'$new_mnbr_sample_list_value'";
			$form_data_update['MAS_UID']             = "'$is_hole_id'";
			$form_data_update['LITHOLOGICAL_DESC']   = "'$new_litho_list_value'";
            $form_data_update['DENSITY']             = "'$new_density_list_value'";
			$form_data_update['COLOR']               = "'$new_color_list_value'";
			$form_data_update['MOISTURE']            = "'$new_moisture_list_value '";
			$form_data_update['DILATANCY']           = "'$new_dilatncy_list_value'";
            $form_data_update['BOS_0_6']             = "'$new_bos_0_6_list_value'";
			$form_data_update['BOS_6_12']            = "'$new_bos_6_12_list_value'";
			$form_data_update['BOS_12_18']           = "'$new_bos_12_18_list_value'";
			$form_data_update['BOS_18_24']           = "'$new_bos_18_24_list_value'";
			//Update logic if the row already exists
			$where_cond = "MAS_UID='$is_hole_id' AND ID='$new_details_id_list_arr_value'";
			//echo $where_cond;
			$update = $obj->Update_Or_Insert_Data_By_Check_Duplicate("HYDROLOGY.LITHO_FIELD_BORE_LOG_DET@HYDRO10", $form_data_update, $where_cond);
			if ($update) {
				$ret_msg = 1; // Update was successful
			}
		}
	}
	// Output success message
	echo $ret_msg;
}
        

?>