<?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.                                                      *
        *****************************************************************************
    */

    // Путь к config.php относительно текущего файла
    require_once(ENGINE_DIR . '/data/config.php');


    // Проверка, была ли отправлена форма для удаления страницы
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_page_id'])) {
        $page_id = $_POST['delete_page_id'];

        // Выполняем запрос для удаления страницы с использованием Medoo
        $db_connect->delete('movies', [
            'id' => $page_id
        ]);

        header("Location: /admin?action=modules/movies/list");
        exit();
    }

    $movies = $db_connect->select('movies', '*');

    $categoriesAltNames = [];
    $categories = $db_connect->select('movies_categories', ['name', 'alt_name']);
    foreach ($categories as $category) {
        $categoriesAltNames[$category['name']] = $category['alt_name'];
    }


    $smarty->registerPlugin('modifier', 'json_encode', function($value) {
        return json_encode($value);
    });
    require_once ENGINE_DIR . '/mods/get_module_categories.php';

    foreach ($movies as &$movie) {
        $movie[$lang["datatable"]['category']] = getCategoriesString($movie['id'], "movies") ?? "";
        $movie[$lang["datatable"]["date"]] = $movie["date"];
        $movie[$lang["datatable"]["author"]] = $movie["autor"];
        $movie[$lang["datatable"]["views"]] = $movie["views"];
        $movie[$lang["datatable"]["title"]] = "<a target='_blank' href='/movies/{$movie['alt_name']}?lang={$movie['language']}'>" . $movie["title"] . "</a>";
        $movie[$lang["datatable"]["lang"]] = "<img src='/engine/lang/{$movie['language']}/icon.png' style='height: 22px;' alt=''>";
        $movie[$lang["datatable"]["actions"]] = "<a href='/admin?action=modules/movies/edit&id={$movie['id']}' class='mx-1 top-1'><i class='fa fa-edit'></i></a>
                                            <form method='POST' action='/admin?action=modules/movies/list' style='display:inline;'>
                                                <input type='hidden' name='delete_page_id' value='{$movie['id']}'>
                                                <button type='submit' class='no-b mx-1' onclick='return confirm(`Вы уверены, что хотите удалить этот фильм?`);'>
                                                    <i class='fa fa-trash'></i>
                                                </button>
                                            </form>";
    }

    // Устанавливаем массив переменных в Smarty
    $smarty->assign('movies', $movies);
    $smarty->assign('categoriesAltNames', $categoriesAltNames);
    $yourData = array('title' => $lang['nav']['movieslist'] . ' - ' . $config['title']);
    $smarty->assign('data', $yourData);
    $smarty->assign('page_name', '
        <li class="breadcrumb-item active">' . $lang['nav']['movieslist'] . '</li>
    ');
    $smarty->assign('theme', SYSTEM_DIR);
    $smarty->assign('config', $config);