<?php
//Database connection
    //$con = mysqli_connect("localhost", "root", "R3M0T31", "cropmonitor");
    $con = mysqli_connect("localhost", "cropmonitor", "brvnCcaEYxlPCS3", "cropmonitor");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $empid =  mysqli_real_escape_string($con, $_REQUEST["empid"]);
    $user_id = $modx->user->get('id');

        $query  = "CREATE TEMPORARY TABLE `plant_temp`
                    SELECT id, modx_user_id, plant_type, plant_stage, n_min, n_max, P_min, P_max, K_min, K_max, S_min, S_max, Ca_min, Ca_max, Mg_min, Mg_max, Na_min, Na_max, Cu_min, Cu_max, Zn_min, Zn_max, Mn_min, Mn_max, B_min, B_max, Fe_min, Fe_max, M_min, M_max, Co_min, Co_max, se_min, se_max, cl_min, cl_max, c_min, c_max 
                    FROM `plant_specifications`
                    WHERE `modx_user_id` = '" . $user_id . "'
                    AND `plant_type` = '" . $empid . "'; ";
        
        $query .= "ALTER TABLE `plant_temp`
                    DROP `id`, 
                    DROP `modx_user_id`,
                    DROP `plant_type`
                    ; ";
        $query .= "SELECT * FROM `plant_temp`; ";
        
        $result = mysqli_multi_query($con, $query);
    
        if ($result) {
            do {
                // grab the result of the next query
                if (($result = mysqli_store_result($con)) === false && mysqli_error($con) != '') {
                    echo "Query failed: " . mysqli_error($con);
                }
            } while (mysqli_more_results($con) && mysqli_next_result($con)); // while there are more results
        } else {
            echo "First query failed..." . mysqli_error($con);
        }
        
        //get results from database
        $all_property = array();  //declare an array for saving property
        
        
        //showing property
        echo '<div class="table-responsive text-nowrap">
                <table class="table table-sm table-striped table-hover table-bordered">
                <tbody>
                    <tr>';  //initialize table tag
                        while ($property = mysqli_fetch_field($result)) {
                            $header = str_replace("_"," ",$property->name);
                            echo '<th scope="col" class="text-center text-capitalize" >' . $header . '</th>';  //get field name for header
                            array_push($all_property, $property->name);  //save those to array

                        }
                    echo '</tr>'; //end tr tag

        //showing all data
        while ($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            foreach ($all_property as $item) {
                if ($row[$item] == "" ) { $this_value = "0.0" ; } else { if (is_numeric($row[$item]) ) { $this_value = number_format((float)$row[$item], 2, '.', ''); } else { $this_value = $row[$item]; } }
                echo '<td scope="row" class="text-center" contenteditable="true" onBlur="updateDatabase(this,"'. $row[$item->id] . '","' . $row['id'] . '")" onClick="showEdit(this);" >' . $this_value . '</td>'; //get items using property value
            }
            echo '</tr>';
        }
        echo "</tbody></table></div>";
        
        
        echo "
        <div class='row'>
            <div class='col-2'>
                <div class='row '>
        ";
        
                      
        $img_query = 'SELECT * FROM `plant_images` ORDER BY `id` DESC';
        $img_result = mysqli_query($con, $img_query);  
        while($img_row = mysqli_fetch_array($img_result)) {  
            echo ' <img src="data:image/jpeg;base64,'.base64_encode($img_row['name'] ).'" class="img-thumbnail rounded float-left border-success" alt="" /> ';  
        }
                    
        echo "
                </div>
                <br>
                <div class='row'>
                    <form method='post' enctype='multipart/form-data'>
                        <div class='form-group'>  
                            <input type='file' class='form-control-file' name='image' id='image' />  
                        </div>
                        <input type='submit' name='insert' id='insert' value='Insert' class='btn btn-info btn-sm' />
                    </form>
                </div>
            </div>
            
            <div class='col-10 border border-success'>
                <div class='row'>
                    
                </div> 
            </div>
        </div>
        
        ";

        $query .= "DROP TEMPORARY TABLE `temp`; ";
        
        mysqli_query($con, $query);
        mysqli_close($con);
		?>