<?php
session_start();
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_GET);
// print_r($_GET);
// echo $well_id;
$get_depth = $obj->Details_By_Cond("HYDROLOGY.BLPUMP_TBL@HYDRO10", "WELL_ID='$well_id'");
$total_depth_meters = $get_depth['DEPTH'];
$total_depth_feet = round($total_depth_meters * 3.28084);

// Calculate number of rows needed, each row representing 10 feet
$num_rows = ceil($total_depth_feet / 10);
?>
<!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
<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;

    }

    .tbl_border {
        border: 1px solid #ddd;
    }
</style>

<div class="row-fluid">
    <!-- <input type="hidden" id="flag" value="<?php echo $flag; ?>"></input> -->
    <div class="tabbable">
        <ul class="nav nav-tabs" id="myTab">
            <li class="active">
                <a data-toggle="tab" href="#data_manual_entry" onclick="lithology_data_manual_entry()">
                    Manual Data Entry
                </a>

            </li>
            <li>
                <a data-toggle="tab" href="#data_excel_entry" onclick="lithology_data_excel_entry()">
                    Excel Data Entry
                    <span class="badge badge-danger"></span>
                </a>
            </li>
        </ul>
        <!-- <input type="hidden" value="<?php echo $building_id; ?>" id="add_building_id"> -->

        <div class="tab-content">
            <div id="data_manual_entry" class="tab-pane fade in active">
                <div class="row-fluid">
                    <h5>Lithology Bore Information for: <?php echo $well_id; ?></h5>
                    <input type="hidden" id="well_id" value="<?php echo $well_id; ?>">
                    <table id="litho_data_table" class="table table-bordered table-striped" style="width:100%;">
                        <div class="row-fluid" style="margin-bottom:5px;">
                            <div class="control-group" style="float:right;">
                                <div class="controls">

                                    <!-- <button style="margin-right: 1px; font-size: 12px;" type="button" id="btn2" class="btn btn-small btn-primary" onclick="load_new_row()"><i class="icon-plus"></i><b>Add New</b></button> -->
                                    <button id="litho_sva_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-success" onclick="litho_multiple_save()"><i class="icon-save"></i><b>Save All</b></button>
                                    <button id="delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="litho_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>
                                    <?php if ($_SESSION['delete'] == 'Y') {
                                        //echo '<button id="delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="litho_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
                                    } ?>
                                </div>
                            </div>
                        </div>
                        <thead>
                            <tr>
                                <td style="text-align: center; font-size:13px; width: 5%; background-color:#307ECC; vertical-align:middle;" class="table-header" rowspan="2">Sample No</td>
                                <td style="text-align: center; font-size:13px; width: 14%; background-color:#307ECC;" class="table-header" colspan="2">Depth (ft)</td>
                                <td style="text-align: center; font-size:13px; width: 14%; background-color:#307ECC;" class="table-header" colspan="2">Depth (m)</td>
                                <td style="text-align: center; font-size:13px; width: 7%; background-color:#307ECC; vertical-align:middle;" class="table-header" rowspan="2">Thickness (m)</td>
                                <td style="text-align: center; font-size:13px; width: 20%; background-color:#307ECC; vertical-align:middle;" class="table-header" rowspan="2">Color</td>
                                <td style="text-align: center; font-size:13px; width: 40%; background-color:#307ECC; vertical-align:middle;" class="table-header" rowspan="2">Lithology</td>
                            </tr>
                            <tr>
                                <!-- <td style="text-align: center; font-size:13px;" class="table-header"></td> -->
                                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 7%;" class="table-header">From</td>
                                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 7%;" class="table-header">To</td>
                                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 7%;" class="table-header">From</td>
                                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 7%;" class="table-header">To</td>
                                <!-- <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 22%;" class="table-header">Color</td>
                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 22%;" class="table-header">Quantity</td>
                <td style="text-align: center; font-size:13px; background-color:#307ECC; width: 22%;" class="table-header">Material</td> -->
                            </tr>
                        </thead>
                        <tbody id="litho_tbl_body">

                            <span>
                                <input type="hidden" id="well_data_purpose_id" value="<?php echo $well_data_purpose_id; ?>">
                            </span>
                            <span>
                                <input type="hidden" id="pump_test_id" value="<?php echo $pump_test_id; ?>">
                            </span>
                            <span>
                                <input type="hidden" id="well_observation_id" value="<?php echo $well_observation_id; ?>">
                            </span>

                            <?php
                            $lithology_flag = "";
                            $row_id = 0;
                            $cond = "BOREHOLL_ID = '$well_id' AND DEPTH is not null";
                            $data_count = $obj->View_column_details_By_Cond("HYDROLOGY.LITHOLOGY_TBL@HYDRO10", "COUNT(*) AS TOT_DATA", $cond);
                            if ($data_count['TOT_DATA'] > 0) {
                                $lithology_flag = 1;
                                $previous_depth_to_ft = 0;
                                $previous_depth_value_ft = null;
                                $previous_depth_to_m = 0;
                                $previous_depth_value_m = null;
                                foreach ($obj->View_colmn_By_Cond("HYDROLOGY.LITHOLOGY_TBL@HYDRO10", "*", "BOREHOLL_ID = '$well_id' ORDER BY DEPTH ASC") as $litho_data) {
                                    $row_id++;
                                    ///table info checking
                                    //// Checking for empty field and making float points
                                    $depth = "";
                                    $mate = "";
                                    $qunt = "";
                                    $col = "";
                                    if ($litho_data['DEPTH'] != "") {
                                        $depth = number_format((float)$litho_data['DEPTH'], 3, '.', '');
                                    } else {
                                        $depth = $litho_data['DEPTH'];
                                    }
                                    $depth_metters = $depth;
                                    $depth_to_ft = round($depth_metters * 3.280);
                                    $get_lithology_id = $litho_data['ID'];
                                    $mate = $litho_data['MATERIAL_CODE'];
                                    $qunt = $litho_data['QTY_CODE'];
                                    $col = $litho_data['COLOR_CODE'];
                                    $thickness = $litho_data['THICKNESS'];
                                    // echo $col;
                                    //// Checking for empty field and making float points

                                    if ($previous_depth_value_ft !== null && $depth_to_ft == $previous_depth_value_ft) {
                                        $from_ft = $previous_from_ft;
                                    } else {
                                        $from_ft = $previous_depth_to_ft;
                                    }
                                    $previous_depth_value_ft = $depth_to_ft;
                                    $previous_depth_to_ft = $depth_to_ft;
                                    $previous_from_ft = $from_ft;


                                    if ($previous_depth_value_m !== null && $depth == $previous_depth_value_m) {
                                        $from_m = $previous_from_m;
                                    } else {
                                        $from_m = $previous_depth_to_m;
                                    }
                                    $previous_depth_value_m = $depth;
                                    $previous_depth_to_m = $depth;
                                    $previous_from_m = $from_m;



                                    $thickness_id     = "thickness_$row_id";
                                    // echo  $no_of_sample_id;
                                    $color_id         = "color_$row_id";
                                    $m_q_id      = "m_q_$row_id";
                                    // $material_id      = "material_$row_id";

                                    $after_boc_0_12_id     = "";
                                    $next_row_val          =  $row_id + 1;
                                    $after_boc_0_12_id     = "thickness_$next_row_val";
                                    // echo $after_boc_0_12_id;
                                    $function_for_nxt_val  = "call_next_val_loader(event,'$thickness_id','$after_boc_0_12_id')";


                            ?>
                                    <!--table info-->
                                    <tr>
                                        <td style='text-align: center; font-size:13px;' class=''><?php echo $row_id; ?></td>
                                        <td style='text-align: center; font-size:13px;' class=''><?php echo $from_ft; ?></td>
                                        <td style='text-align: center; font-size:13px;' class=''>
                                            <span><input type="hidden" id="lithology_id" class="lithology_id" value="<?php echo $get_lithology_id; ?>"><?php echo $depth_to_ft; ?></span>
                                        </td>

                                        <td style='text-align: center; font-size:13px;' class=''>
                                            <span><input type="hidden" id="lithology_flag" value="<?php echo $lithology_flag; ?>"><?php echo $from_m; ?></span>
                                        </td>

                                        <td style='text-align: center; font-size:13px;' class=''>
                                            <span>
                                                <input type="hidden" class="depth_old_data" value="<?php echo $depth; ?>">
                                                <?php echo $depth; ?>
                                            </span>
                                            <!-- <span>
                                <input type="text" style="width: 80%;" class="depth_old_data" value="<?php echo $depth; ?>"></span> -->
                                        </td>
                                        <td style="text-align: center;">

                                            <input type="text" style="width: 85%; margin: 0px 5px 0px 5px" id="<?php echo $thickness_id; ?>" class="thickness_old_data" onkeypress="return isNumericInput(event)" onkeyup="call_next_val_loader(event,'<?php echo $thickness_id; ?>','<?php echo $color_id; ?>')" value="<?php echo $thickness; ?>" />
                                        </td>
                                        <td style="text-align: left;">
                                            <input type="text" style="width: 95%; margin: 0px 5px 0px 5px" id="<?php echo $color_id; ?>" class="color_old_data" onkeyup="call_next_val_loader(event,'<?php echo $color_id; ?>','<?php echo $m_q_id; ?>')" value="<?php
                                                                                                                                                                                                                                                                    $color_found = false;
                                        $table_name = 'HYDROLOGY.COLOR_TBL@HYDRO10';
                                                                                                                                                                                                                                                                    $columns = 'DISTINCT COLOR, COLOR_CODE';
                                                                                                                                                                                                                                                                    $where_cond = '1=1 ORDER BY COLOR ASC';
                                                                                                                                                                                                                                                                    foreach ($obj->View_colmn_By_Cond($table_name, $columns, $where_cond) as $color_list) {
                                                                                                                                                                                                                                                                        // $is_color_selected = "";
                                                                                                                                                                                                                                                                        if ($color_list['COLOR_CODE'] == $col) {
                                                                                                                                                                                                                                                                            echo $color_list['COLOR'];
                                                                                                                                                                                                                                                                            $color_found = true; // Set the flag to true
                                                                                                                                                                                                                                                                            break;
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                    if (!$color_found) {
                                                                                                                                                                                                                                                                        echo $col;
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                    ?>" />
                                        </td>

                                        <td style="text-align: left;">
                                            <input type="text" style="width: 97%; margin: 0px 5px 0px 5px" id="<?php echo $m_q_id; ?>" class="lithology_old_data" onkeyup="<?php echo $function_for_nxt_val; ?>" value="<?php
                                                                                                                                                                                                                        $description_found = false; // Flag to check if description is found

                                                                                                                                                                                                                        foreach ($obj->View_colmn_By_Cond_hydro("HYDROLOGY.LITHO_CODE_TBL@HYDRO10", "DISTINCT MATERIAL_CODE, DESCRIPTION", "1=1 ORDER BY MATERIAL_CODE ASC") as $material_list) {
                                                                                                                                                                                                                            if ($material_list['MATERIAL_CODE'] == $mate) {
                                                                                                                                                                                                                                echo $material_list['DESCRIPTION'];
                                                                                                                                                                                                                                $description_found = true; // Set the flag to true
                                                                                                                                                                                                                                break; // Exit the loop once the description is found
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        if (!$description_found) {
                                                                                                                                                                                                                            echo $mate;
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        ?>" />

                                        </td>
                                    </tr>
                                <?php
                                }
                                $total_row = $row_id;
                                ?>
                                <!-- echo $total_row; -->
                                <input type="hidden" id="total_row" value="<?php echo $total_row; ?>">
                                <?php
                            } else {
                                $lithology_flag = 2;
                                for ($i = 0; $i < $num_rows; $i++) {
                                    $from_m = number_format(($i * 3.04878), 3);
                                    $to_m = number_format(min(($i + 1) * 3.04878, $total_depth_meters), 3);

                                    // Convert from meters to feet with precision
                                    $from_ft = round($from_m * 3.280);
                                    $to_ft = round($to_m * 3.280);

                                    $thickness_id = "thickness_$i";
                                    $color_id = "color_$i";
                                    $m_q_id = "m_q_$i";
                                ?>
                                    <tr>
                                        <td style='text-align: center; font-size:13px;'><?php echo $i + 1; ?></td>
                                        <td style='text-align: center; font-size:13px;'><?php echo $from_ft; ?></td>
                                        <td style='text-align: center; font-size:13px;'><?php echo $to_ft; ?></td>

                                        <td style='text-align: center; font-size:13px;'>
                                            <span><input type="hidden" id="lithology_flag" value="<?php echo $lithology_flag; ?>"><?php echo $from_m; ?></span>
                                        </td>

                                        <td style='text-align: center; font-size:13px;' class=''>
                                            <span><input type="hidden" style="width: 80%;" class="lithology_depth" value="<?php echo $to_m; ?>"><?php echo $to_m; ?></span>
                                        </td>
                                        <td style="text-align: center;">
                                            <input type="text" style="width: 85%; margin: 0px 5px 0px 5px" id="<?php echo $thickness_id; ?>" class="lithology_thickness" onkeypress="return isNumericInput(event)" />
                                        </td>
                                        <td style="text-align: left;">
                                            <input type="text" style="width: 95%; margin: 0px 5px 0px 5px" id="<?php echo $color_id; ?>" class="lithology_color" />
                                        </td>
                                        <td style="text-align: left;">
                                            <input type="text" style="width: 97%; margin: 0px 5px 0px 5px" id="<?php echo $m_q_id; ?>" class="lithology_material_quantity" />
                                        </td>
                                    </tr>
                                <?php } ?>
                                <!-- echo $total_row; -->
                                <input type="hidden" id="total_row" value="<?php echo $total_row ?>">
                            <?php
                            }

                            ?>
                        </tbody>
                    </table>
                    <div class="row-fluid" style="margin-bottom:5px;">
                        <div class="control-group" style="float:right;">
                            <div class="controls">
                                <button id="litho_sva_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-success" onclick="litho_multiple_save()"><i class="icon-save"></i><b>Save All</b></button>
                                <button id="delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="litho_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>
                                <?php if ($_SESSION['delete'] == 'Y') {
                                    //echo '<button id="delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="litho_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
                                } ?>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

            <div id="data_excel_entry" class="tab-pane fade">
                <div class="form-group">
                    <div style="display: inline-block; vertical-align: top; width: 20%;">
                        <h5 style="margin-bottom: 30px; display: inline;">Lithology Bore Information for: <?php echo $well_id; ?></h5>
                        <span>
                            <input type="hidden" id="excel_well_purpose_id" value="<?php echo $well_data_purpose_id; ?>">
                            <input type="hidden" name="excel_upload_well_id" id="excel_upload_well_id" value="<?php echo $well_id; ?>" />
                            <input type="hidden" id="excel_pump_test_id" value="<?php echo $pump_test_id; ?>">
                            <input type="hidden" id="excel_excel_observation_id" value="<?php echo $well_observation_id; ?>">
                        </span>
                    </div>
                    <div style="display: inline-block; vertical-align: top; width: 30%;">
                        <div class="form-group" style="margin-bottom: 0;">
                            <!-- <label for="upload_excel">Select Excel File:</label> -->
                            <div class="input-group">
                                <input type="file" name="upload_excel" id="upload_excel" class="form-control input-large">
                                <span class="input-group-btn">
                                    <button onclick="lithology_excel_data_upload()" class="btn btn-minier btn-info" style="padding: 4px !important;" type="button">
                                        <i class="icon-key"></i> Upload
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                <div style="margin-top: 30px;" id="lithology_excel_data_show">
                    <!-- <label for="upload_excel">
                        <h5 style="text-align: center; background-color: #ddd; padding: 5px 0px; font-weight: 700; font-size: 20px;">Excel Data Show</h5>
                    </label> -->
                    <!-- <div style="margin-top: 20px;" id="lithology_excel_data_show"></div> -->
                </div>
            </div>
        </div>
    </div>
    </table>
</div>
<script type="text/javascript">
    //  add_ghest_house_information();
</script>

<script>
    $('.chosen-select').chosen({
        allow_single_deselect: true
    });
    //resize the chosen on window resize

    $(window)
        .off('resize.chosen')
        .on('resize.chosen', function() {
            $('.chosen-select').each(function() {
                var $this = $(this);
                $this.next().css({
                    'width': '98%'
                });
            })
        }).trigger('resize.chosen');
    //resize chosen on sidebar collapse/expand
    $(document).on('settings.ace.chosen', function(e, event_name, event_val) {
        if (event_name != 'sidebar_collapsed') return;
        $('.chosen-select').each(function() {
            var $this = $(this);
            $this.next().css({
                'width': '98%'
            });
        })
    });
</script>


<!-- Alphanumeric Checking -->
<script>
    let inputReset = false; // Track whether input should be reset

    function isNumericInput(event) {
        const key = event.keyCode || event.which;
        const keyChar = String.fromCharCode(key);

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

        // Only allow alphanumeric input
        if (!regex.test(keyChar)) {
            event.preventDefault();
            return false;
        }

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

        if (inputReset) {
            input.value = "";
            inputReset = false;
        }

        return true;
    }
</script>
<!-- Alphanumeric Checking -->


<!-- <script>
    $(document).ready(function() {
        // Event delegation to handle keypress on dynamically added rows
        $('tbody').on('keypress', 'input', function(e) {
            var flag = $('#lithology_flag').val();
            // alert(flag);
            if (flag == 2) {
                if (e.which == 13) { // Enter key pressed
                    e.preventDefault();
                    var currentInput = $(this);
                    var currentRow = currentInput.closest('tr');
                    var nextInput = currentInput.closest('td').next('td').find('input');

                    if (nextInput.length) {
                        // Focus on the next input in the same row
                        nextInput.focus();
                    } else {
                        // If there is no next input in the same row, move to the next row's Thickness column
                        var nextRow = currentRow.next('tr');
                        if (nextRow.length) {
                            var nextThickness = nextRow.find('.lithology_thickness');
                            var nextColor = nextRow.find('.lithology_color');
                            var nextMaterialQuantity = nextRow.find('.lithology_material_quantity');

                            // Transfer values to the next row
                            nextThickness.val(currentRow.find('.lithology_thickness').val());
                            nextColor.val(currentRow.find('.lithology_color').val());
                            nextMaterialQuantity.val(currentRow.find('.lithology_material_quantity').val());

                            // Focus on the Thickness input of the next row
                            nextThickness.focus();
                        }
                    }
                }
            } else if (flag == 1) {
                if (e.which == 13) { // Enter key pressed
                    e.preventDefault();
                    var currentInput = $(this);
                    var currentRow = currentInput.closest('tr');
                    var nextInput = currentInput.closest('td').next('td').find('input');

                    if (nextInput.length) {
                        // Focus on the next input in the same row
                        nextInput.focus();
                    } else {
                        // If there is no next input in the same row, move to the next row's Thickness column
                        var nextRow = currentRow.next('tr');
                        if (nextRow.length) {
                            var nextThickness = nextRow.find('.thickness_old_data');
                            var nextColor = nextRow.find('.color_old_data');
                            var nextMaterialQuantity = nextRow.find('.lithology_old_data');

                            // Transfer values to the next row
                            nextThickness.val(currentRow.find('.thickness_old_data').val());
                            nextColor.val(currentRow.find('.color_old_data').val());
                            nextMaterialQuantity.val(currentRow.find('.lithology_old_data').val());

                            // Focus on the Thickness input of the next row
                            nextThickness.focus();
                        }
                    }
                }
            }
        });
    });
</script> -->

<script>
    $(document).ready(function() {
        $('tbody').on('keypress', 'input', function(e) {
            var flag = $('#lithology_flag').val();
            // alert(flag);
            if (flag == 2) {
                if (e.which == 13) { 
                    e.preventDefault();
                    var currentInput = $(this);
                    var currentRow = currentInput.closest('tr');
                    var nextInput = currentInput.closest('td').next('td').find('input');

                    if (nextInput.length) {
                        nextInput.select();
                    } else {
                        var nextRow = currentRow.next('tr');
                        if (nextRow.length) {
                            var nextThickness = nextRow.find('.lithology_thickness');
                            var nextColor = nextRow.find('.lithology_color');
                            var nextMaterialQuantity = nextRow.find('.lithology_material_quantity');

                            if (nextThickness.val() === "") {
                                nextThickness.val(currentRow.find('.lithology_thickness').val());
                            }
                            if (nextColor.val() === "") {
                                nextColor.val(currentRow.find('.lithology_color').val());
                            }
                            if (nextMaterialQuantity.val() === "") {
                                nextMaterialQuantity.val(currentRow.find('.lithology_material_quantity').val());
                            }

                            nextThickness.select();
                        }
                    }
                }
            } else if (flag == 1) {
                if (e.which == 13) { 
                    e.preventDefault();
                    var currentInput = $(this);
                    var currentRow = currentInput.closest('tr');
                    var nextInput = currentInput.closest('td').next('td').find('input');

                    if (nextInput.length) {
                        nextInput.select();
                    } else {
                        var nextRow = currentRow.next('tr');
                        if (nextRow.length) {
                            var nextThickness = nextRow.find('.thickness_old_data');
                            var nextColor = nextRow.find('.color_old_data');
                            var nextMaterialQuantity = nextRow.find('.lithology_old_data');

                            if (nextThickness.val() === "") {
                                nextThickness.val(currentRow.find('.thickness_old_data').val());
                            }
                            if (nextColor.val() === "") {
                                nextColor.val(currentRow.find('.color_old_data').val());
                            }
                            if (nextMaterialQuantity.val() === "") {
                                nextMaterialQuantity.val(currentRow.find('.lithology_old_data').val());
                            }

                            nextThickness.select();
                        }
                    }
                }
            }
        });
    });
</script>
