<?php
        /*
        *****************************************************************************
        * MEGA Digital Space - by MEGA Digital Studio                               *
        * ------------------------------------------------------------------------- *
        * CMS Website:  https://megadigital.space                                   *
        * License:  https://megadigital.space/license                               *
        * Owner:  Giorgi Basharuli                                                  *
        * Contact:  online@megadigital.studio                                       *
        * MEGA Digital Studio – Official Development Studio for MEGA Digital Space  *
        * Website:  https://megadigital.studio                                      *
        * ------------------------------------------------------------------------- *
        * Description: This file is a part of the MEGA Digital Space CMS system.    *
        * Unauthorized distribution, modification, or commercial use is strictly -  *
        * prohibited without prior written permission.                              *
        * ------------------------------------------------------------------------- *
        * Copyright (c) 2023-2025 MEGA Digital Studio.                              *
        * All rights reserved.                                                      *
        *****************************************************************************
    */

    // Подключаем дополнительные поля
    $table_name = $module_entity . '_fields'; // Динамическое имя таблицы
    $sql_fields = "SELECT * FROM {$table_name}";
    $result_fields = $db_connect->query($sql_fields);
    $fields = $db_connect->select($table_name, "*");

    foreach ($fields as &$field) {
        $field["field_id"] = $field["id"];
    }

//var_dump($fields);exit();
    $smarty->assign('fields', $fields);

    $table_name = $module_entity . '_field_values'; // Динамическое имя таблицы
    $entity_id = $module_entity . '_id'; // Динамическое имя принадлежности

    /*$sql_field_values = "SELECT * FROM {$table_name} WHERE $entity_id = {$page_id}";
    $result_field_values = $db_connect->query($sql_field_values);
    $field_values = [];
    if ($result_field_values->num_rows > 0) {
        while ($row = $result_field_values->fetch_assoc()) {
            $field_values[$row['field_id']] = $row['x_value'];
        }
    }*/

    $result_field_values = $db_connect->get($table_name, "*", [$entity_id => 17]);
    $field_values = [];

    if (!empty($result_field_values["field_id"])) {
        $field_values[$result_field_values["field_id"]] = $result_field_values["x_value"];
    } else if (!empty($result_field_values) && count($result_field_values) > 1) {
        foreach ($result_field_values as &$value) {
            $field_values[$result_field_values["field_id"]] = $value["id"];
        }
    }

    $smarty->assign('field_values', $field_values ?? []);