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


/////////-- Single Save --//////////
if ($operation == "save_all_data") {
    $flag = 0;
    if ($well_data_purpose_id == 4) {
        $well_data_purpose = 'Lithology';
    } elseif ($well_data_purpose_id == 5) {
        $well_data_purpose = 'Pump_Test';
    }

    if ($lithology_flag == 1) {
        $old_depths = explode("#", $depth_old_data);
        $old_thicknesses = explode("#", $thickness_old_data);
        $old_colors = explode("#", $color_old_data);
        $old_litho_datas = explode("#", $lithology_old_data);
        $lithology_ids = explode("#", $lithology_id);

        for ($i = 0; $i < count($old_depths); $i++) {
            $old_depth = $old_depths[$i];
            $old_thickness = !empty($old_thicknesses[$i]) ? $old_thicknesses[$i] : '';
			
		//	$old_color = htmlspecialchars(rawurldecode($old_colors[$i]), ENT_QUOTES, 'UTF-8');
			$old_color = $old_colors[$i];
           // $old_color = !empty($temp_old_clr) ? $temp_old_clr : '';
			
			//$old_litho_data = htmlspecialchars(rawurldecode($old_litho_datas[$i]), ENT_QUOTES, 'UTF-8');
			$old_litho_data = $old_litho_datas[$i];
            //$old_litho_data = !empty($temp_old_litho) ? $temp_old_litho : '';
		
           // $old_color_trim = trim($old_color);
           // $old_litho_data_trim = trim($old_litho_data);
            
            $lithology_id_update = !empty($lithology_ids[$i]) ? $lithology_ids[$i] : '';
            
            /*$form_data_update1['PURPOSE'] = "'$well_data_purpose'";
            $form_data_update1['DEPTH'] = "'$old_depth'";
            $form_data_update1['THICKNESS'] = "'$old_thickness'";
            $form_data_update1['MATERIAL_CODE'] = "'$old_litho_data_trim'";
            $form_data_update1['COLOR_CODE'] = "'$old_color_trim'";
            $form_data_update1['UPDT_BY'] = "'$user_id'";
            $form_data_update1['UPDT_DTM'] = "TO_DATE('$date','DD-MON-YY  HH24:MI:SS')";*/
			$form_data_update1 = array(
				'PURPOSE' => "'$well_data_purpose'",
				'DEPTH' => "'$old_depth'",
				'THICKNESS' => "'$old_thickness'",
				'MATERIAL_CODE' => "'$old_litho_data'",
				'COLOR_CODE' => "'$old_color'",
				'UPDT_BY' => "'$user_id'",
				'UPDT_DTM' => "TO_DATE('$date','DD-MON-YY  HH24:MI:SS')"
			);
		//	print_r($form_data_update1);
           $update1 = $obj->Update_Data("HYDROLOGY.LITHOLOGY_TBL@HYDRO10", $form_data_update1, "BOREHOLL_ID='$well_id' AND ID='$lithology_id_update'");
        }
        if ($well_data_purpose_id == 5) {
            $form_data_porpose_update['PUMP_TEST_ID'] = "'$pump_test_id'";
            $form_data_porpose_update['OBS_WELL_ID'] = "'$well_observation_id'";
            $update_purpose = $obj->Update_Data("HYDROLOGY.BLPUMP_TBL@HYDRO10", $form_data_porpose_update, "WELL_ID='$well_id'");
        }
        if ($update1) {
            if ($update_purpose) {
                $flag = 2;
                echo $flag;
                // exit();
            } else {
                $flag = 1;
                echo $flag;
                // exit();
            }
        }
    }
    if ($lithology_flag == 2) {
        $depths = explode("#", $lithology_depth);
        $thicknesses = explode("#", $lithology_thickness);
        $colors = explode("#", $lithology_color);
        $material_quantitys = explode("#", $lithology_material_quantity);

        for ($i = 0; $i < count($depths); $i++) {
            $depth = $depths[$i];
            $thickness = !empty($thicknesses[$i]) ? $thicknesses[$i] : '';
			//$temp_clr = htmlspecialchars(rawurldecode($colors[$i]), ENT_QUOTES, 'UTF-8');
           // $color = !empty($temp_clr) ? $temp_clr : '';
            $color = $colors[$i];
			
			//$temp_mat_quan = htmlspecialchars(rawurldecode($material_quantitys[$i]), ENT_QUOTES, 'UTF-8');
           // $material_quantity = !empty($temp_mat_quan) ? $temp_mat_quan : '';
            $material_quantity = $material_quantitys[$i];

           // $color_trim = trim($color);
           // $material_quantity_trim = trim($material_quantity);

            $form_data_update['BOREHOLL_ID'] = "'$well_id'";
            $form_data_update['PURPOSE'] = "'$well_data_purpose'";
            $form_data_update['DEPTH'] = "'$depth'";
            $form_data_update['THICKNESS'] = "'$thickness'";
            $form_data_update['MATERIAL_CODE'] = "'$material_quantity'";
            $form_data_update['COLOR_CODE'] = "'$color'";
            $form_data_update['INSERT_BY'] = "'$user_id'";
            $form_data_update['INSERT_DTM'] = "TO_DATE('$date','DD-MON-YY  HH24:MI:SS')";
        
            $insert = $obj->Insert_Data('HYDROLOGY.LITHOLOGY_TBL@HYDRO10', $form_data_update);
        }
        if ($well_data_purpose_id == 5) {
            $form_data_porpose['PUMP_TEST_ID'] = "'$pump_test_id'";
            $form_data_porpose['OBS_WELL_ID'] = "'$well_observation_id'";
            $insert_purpose = $obj->Update_Data('HYDROLOGY.BLPUMP_TBL@HYDRO10', $form_data_porpose, "WELL_ID='$well_id'");
        }
        if ($insert) {
            if ($insert_purpose) {
                $flag = 4;
                echo $flag;
            } else {
                $flag = 3;
                echo $flag;
            }
        }
    }
}
/////////-- Single Save --//////////

/////////-- Single Delete --//////////	
if ($operation == "delete") {
    // $update_cond = "ID='$id'";
    // echo $update2 = $obj->Delete_Data_hydro('LITHOLOGY_TBL_2023',$update_cond);	
}
/////////-- Single Delete --//////////

/////////-- Muulti Delete --//////////
if ($operation == "delete_all") {

    // $update_cond = "BOREHOLL_ID='$well_id'";
    echo $update2 = $obj->Delete_Data("HYDROLOGY.LITHOLOGY_TBL@HYDRO10", "BOREHOLL_ID='$well_id'");
}
/////////-- Muulti Delete --//////////

