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

    require_once($_SERVER["DOCUMENT_ROOT"] . '/engine/data/config.php');
    require_once($_SERVER["DOCUMENT_ROOT"] . "/engine/data/dbconfig.php");

    $id = intval($_POST['id'] ?? -1);
    $lines = file($_SERVER["DOCUMENT_ROOT"] . "/engine/data/cron-jobs.txt", FILE_IGNORE_NEW_LINES);
    if ($id >= 0 && isset($lines[$id])) {
        unset($lines[$id]);

        // Удаляем пустые строки и пересобираем файл
        $lines = array_filter($lines, fn($line) => trim($line) !== '');

        file_put_contents($_SERVER["DOCUMENT_ROOT"] . "/engine/data/cron-jobs.txt", implode("\n", $lines) . "\n");
        shell_exec('crontab site-crontab.txt');
        echo "Удалено";
    } else {
        echo "Ошибка удаления";
    }