<?php

include "autoload.php";


$html = new html("SAVE");

// delete table_view_for_user
$db->query("user_table_views", "DELETE FROM user_table_views WHERE user_id = {$_SESSION['user_id']} AND table_name = '{$_POST['table_name']}'");

// loop through all column_index 
for ($i = 0; $i < $_POST['column_index_count']; $i++) {

    // check if column_index exists
    if (!isset($_POST["column_$i"])) {
        continue;
    }
    if ($_POST["column_$i"] == "") {
        continue;
    }
    // echo "INSERT INTO `user_table_views`(`user_id`, `table_name`,`column_name`,`match_to_table`,`pull_column`,`function`,`display_method`,`icon`,`onclick`,`onclick_file`) VALUES ({$_SESSION['user_id']},'{$_POST['table_name']}','{$_POST["column_$i"]}','{$_POST["match_to_table_$i"]}','{$_POST["pull_column_$i"]}', '{$_POST["function_$i"]}', '{$_POST["display_method_$i"]}', '{$_POST["icon_$i"]}', '{$_POST["onclick_$i"]}', '{$_POST["onclick_file_$i"]}')";
    // create table_view_for_user
    @$table_view_id = $db->query("user_table_views", "INSERT INTO `user_table_views`(`user_id`, `table_name`,`column_name`,`match_to_table`,`pull_column`,`function`,`display_method`,`icon`,`onclick`,`onclick_file`,`send_column`,`custom_header`,`custom_text`) VALUES ({$_SESSION['user_id']},'{$_POST['table_name']}','{$_POST["column_$i"]}','{$_POST["match_to_table_$i"]}','{$_POST["pull_column_$i"]}', '{$_POST["function_$i"]}', '{$_POST["display_method_$i"]}', '{$_POST["icon_$i"]}', '{$_POST["onclick_$i"]}', '{$_POST["onclick_file_$i"]}', '{$_POST["send_column_$i"]}', '{$_POST["custom_header_$i"]}', '{$_POST["custom_text_$i"]}');");
}

// for ($i = 0; $i < $_POST['column_index_count']; $i++) {
//     // echo var_dump($_POST);

//     // check if function_index exists
//     if (isset($_POST["function_$i"])) {

//         echo "<br><br>Index: $i<br>";
//         echo "Column: " . $_POST["column_$i"] . "<br>";
//         echo "Function: " . $_POST["function_$i"] . "<br>";
//         echo "<br><br>";

//         $index = $i;
//         $function = $_POST["function_$i"];
//         $column = $_POST["column_$i"];
//         $table_name = $_POST["match_to_table_$i"];
//         $pull_column = $_POST["pull_column_$i"];
//         $column_link = $_POST["column_link_$i"];

//         // if ($function == 'SUM') {
//         //     $table_query = "SELECT SUM($column) FROM $table_name";
//         //     if ($where_data != '' || $where_data != 1) {
//         //         $table_query .= " WHERE $where_data";
//         //     } else {
//         //         $table_query .= " WHERE 1";
//         //     }
//         //     $result = $db->query($table_name, $table_query);
//         //     echo $table_query . "<br>";

//         //     $data = $result->fetch_assoc();

//         //     echo $data["SUM($column)"] . " For Row " . $index . "<br>";

//         //     echo "<input type='text' readonly value='" . $data["SUM($column)"] . "' name='function_$index' >";

//         // } elseif ($function == 'COUNT') {
//         //     $table_query = "SELECT COUNT(*) AS CNT FROM $table_name WHERE {$pull_column} = 1";
//         //     $result = $db->query($table_name, $table_query);
//         //     echo $table_query . "<br>";

//         //     $data = $result->fetch_assoc();

//         //     echo $data["CNT"] . " For Row " . $index . "<br>";

//         //     echo "<input type='text' readonly value='" . $data["CNT"] . "' name='function_$index' >";

//         // } elseif ($function == 'AVG') {

//         //     $table_query = "SELECT AVG($column) FROM $table_name";
//         //     if ($where_data != '' || $where_data != 1) {
//         //         $table_query .= " WHERE $where_data";
//         //     } else {
//         //         $table_query .= " WHERE 1";
//         //     }
//         //     $result = $db->query($table_name, $table_query);
//         //     echo $table_query . "<br>";

//         //     $data = $result->fetch_assoc();

//         //     echo $data["AVG($column)"] . " For Row " . $index . "<br>";

//         //     echo "<input type='text' readonly value='" . $data["AVG($column)"] . "' name='function_$index' >";

//         // } elseif ($function == 'MAX') {
//         //     $table_query = "SELECT MAX($column) FROM $table_name";
//         //     if ($where_data != '' || $where_data != 1) {
//         //         $table_query .= " WHERE $where_data";
//         //     } else {
//         //         $table_query .= " WHERE 1";
//         //     }
//         //     $result = $db->query($table_name, $table_query);
//         //     echo $table_query . "<br>";

//         //     $data = $result->fetch_assoc();

//         //     echo $data["MAX($column)"] . " For Row " . $index . "<br>";

//         //     echo "<input type='text' readonly value='" . $data["MAX($column)"] . "' name='function_$index' >";

//         // } elseif ($function == 'MIN') {
//         //     $table_query = "SELECT MIN($column) FROM $table_name";
//         //     if ($where_data != '' || $where_data != 1) {
//         //         $table_query .= " WHERE $where_data";
//         //     } else {
//         //         $table_query .= " WHERE 1";
//         //     }
//         //     $result = $db->query($table_name, $table_query);
//         //     echo $table_query . "<br>";

//         //     $data = $result->fetch_assoc();

//         //     echo $data["MIN($column)"] . " For Row " . $index . "<br>";

//         //     echo "<input type='text' readonly value='" . $data["MIN($column)"] . "' name='function_$index' >";

//         // } else {

//         //     echo "No function selected for Row " . $index . "<br>";

//         //     echo "<input type='text' hidden name='function_$index' value=''>";
//         // }
//     }
// }

?>

<div class="app_body_container column width_100 background_1">
    <div class="width_100  row center_auto">
        <h1>SAVED</h1>
    </div>
</div>