<?php 
    $data = array();
    $data['title'] = "Zamanlanmış Görevler | ".$config->get('site_title');
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('version');
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = ['/admin/public/assets/js/pages/cron/index.js'];
    $menu_items = $menu->get(array('position'=>'footer'));
    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }

    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => "Pano",
        'href' => "/admin",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Zamanlanmış Görevler",
        'href' => "/admin/cron",
        'index' => 2
    );

    $cronApi   	    = new Cron($db, $utils);

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            $user_group         = isset($session->data['user_group']) ? $session->data['user_group'] : 0;
            $accessControl      = new AccessControl($db, $user_group);

            switch($_POST['action']) {
                case 'add':
                    if(!$accessControl->get("admin/cron/add")) {
                        header("Location: /admin/access-denied");
                        exit();
                    }else{
                        $minutes    = isset($_POST['minutes']) ? implode(',', $_POST['minutes']) : '-1';
                        $hours      = isset($_POST['hours']) ? implode(',', $_POST['hours']) : '-1';
                        $mdays      = isset($_POST['mdays']) ? implode(',', $_POST['mdays']) : '-1';
                        $months     = isset($_POST['months']) ? implode(',', $_POST['months']) : '-1';
                        $wdays      = isset($_POST['wdays']) ? implode(',', $_POST['wdays']) : '-1';
                        $url        = $_POST['url'];
                        $title      = $_POST['title'];
    
                        $request    = '{"job":{"url":"'.$url.'","title":"'.$title.'","enabled":"true","saveResponses":true,"schedule":{"timezone":"Europe/Istanbul","hours":['.$hours.'],"mdays":['.$mdays.'],"minutes":['.$minutes.'],"months":['.$months.'],"wdays":['.$wdays.']}}}';
    
                        $response = $cronApi->create($request);
                        if(!$response){
                            $session->data['messageType'] = "error"; 
                            $session->data['messageTitle'] = "Hata!"; 
                            $session->data['message'] = "Bir hata oluştu ve cron görevi kaydedilemedi."; 
                            header("Refresh: 0;");
                            exit();
                        }else{
                            $session->data['messageType'] = "success"; 
                            $session->data['messageTitle'] = "Başarılı!"; 
                            $session->data['message'] = "Cron görevi başarıyla kaydedildi. Job ID: <b>".$response['jobId']."</b>"; 
                            header("Refresh: 0;");
                            exit();
                        }
                    }
                break;
                case 'update':
                    if(!$accessControl->get("admin/cron/add")) {
                        header("Location: /admin/access-denied");
                        exit();
                    }else{
                        $job_id     = isset($_POST['job_id']) ? $_POST['job_id'] : '0';
                        $status     = isset($_POST['status']) ? $_POST['status'] : '0';
                        $minutes    = isset($_POST['minutes']) ? implode(',', $_POST['minutes']) : '-1';
                        $hours      = isset($_POST['hours']) ? implode(',', $_POST['hours']) : '-1';
                        $mdays      = isset($_POST['mdays']) ? implode(',', $_POST['mdays']) : '-1';
                        $months     = isset($_POST['months']) ? implode(',', $_POST['months']) : '-1';
                        $wdays      = isset($_POST['wdays']) ? implode(',', $_POST['wdays']) : '-1';
                        $url        = $_POST['url'];
                        $title      = $_POST['title'];
                        $status     = $_POST['status'] == 1 ? true : false;

                        $request    = '{"job":{"url":"'.$url.'","title":"'.$title.'","enabled":"'.$status.'","saveResponses":true,"schedule":{"timezone":"Europe/Istanbul","hours":['.$hours.'],"mdays":['.$mdays.'],"minutes":['.$minutes.'],"months":['.$months.'],"wdays":['.$wdays.']}}}';

                        $response = $cronApi->update($request, $job_id);
                        if(!$response){
                            $session->data['messageType'] = "error"; 
                            $session->data['messageTitle'] = "Hata!"; 
                            $session->data['message'] = "Bir hata oluştu ve cron görevi güncellenemedi."; 
                            header("Refresh: 0;");
                            exit();
                        }else{
                            $session->data['messageType'] = "success"; 
                            $session->data['messageTitle'] = "Başarılı!"; 
                            $session->data['message'] = "Cron görevi başarıyla güncellendi."; 
                            header("Refresh: 0;");
                            exit();
                        }
                    }
                break;
            }
            
        }
	}
    
    $cron_jobs          = $cronApi->getJobs();
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title">Görev Listesi</h5>
                        <button type="button" class="btn btn-primary btn-sm me-2" data-bs-toggle="modal" data-bs-target="#new-job-modal"><i class="bi bi-plus-circle-dotted"></i> Yeni Görev</button>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th>Job ID</th>
                                    <th>Durum</th>
                                    <th>Başlık</th>
                                    <th>URL</th>
                                    <th>Son İşlem</th>
                                    <th>Gelecek İşlem</th>
                                    <th style="width: 180px"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if(!empty($cron_jobs)):
                                        foreach($cron_jobs['jobs'] as $row): ?>
                                            <tr>
                                                <td><?php echo $row['jobId']; ?></td>
                                                <td><?php echo $row['enabled'] ? "Aktif" : "Pasif"; ?></td>
                                                <td><?php echo $row['title']; ?></td>
                                                <td><?php echo $row['url']; ?></td>
                                                <td><?php echo date("d M Y h:i:s", $row['lastExecution']); ?></td>
                                                <td><?php echo date("d M Y h:i:s", $row['nextExecution']); ?></td>
                                                <td>
                                                    <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#update-job-modal" data-id="<?php echo $row['jobId']; ?>"><i class="bi bi-pencil-square"></i></button>
                                                    <button class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#new-job-modal" data-id="<?php echo $row['jobId']; ?>"><i class="bi bi-clipboard"></i></button>
                                                    <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#job-history-modal" data-id="<?php echo $row['jobId']; ?>"><i class="bi bi-clock-history"></i></button>
                                                    <button class="btn btn-sm btn-danger" data-delete data-href="/admin/cron/delete?id=<?php echo $row['jobId']; ?>"><i class="bi bi-trash"></i></button>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <tr class="text-center"><td colspan="7">Hiçbir kayıt bulunamadı!</td></tr>
                                    <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </main>
        <form action="" id="new-job-form" method="POST">
            <div class="modal" tabindex="-1" id="new-job-modal" data-bs-keyboard="false">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Yeni Görev</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="minutes" class="col-form-label">Dakika</label>
                                    <select name="minutes[]" id="minutes" class="form-control form-select js-choice" required multiple>
                                        <option value="0">00</option>
                                        <option value="15">15</option>
                                        <option value="30">30</option>
                                        <option value="45">45</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="hours" class="col-form-label">Saat</label>
                                    <select name="hours[]" id="hours" class="form-control form-select js-choice" required multiple>
                                        <option value="-1">Her Saat</option>
                                        <option value="0">0</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="mdays" class="col-form-label">Gün</label>
                                    <select name="mdays[]" id="mdays" class="form-control form-select js-choice" required multiple>
                                        <option value="-1">Her Gün</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                        <option value="26">26</option>
                                        <option value="27">27</option>
                                        <option value="28">28</option>
                                        <option value="29">29</option>
                                        <option value="30">30</option>
                                        <option value="31">31</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="months" class="col-form-label">Ay</label>
                                    <select name="months[]" id="months" class="form-control form-select js-choice" required multiple>
                                        <option value="-1">Her Ay</option>
                                        <option value="1">Ocak</option>
                                        <option value="2">Şubat</option>
                                        <option value="3">Mart</option>
                                        <option value="4">Nisan</option>
                                        <option value="5">Mayıs</option>
                                        <option value="6">Haziran</option>
                                        <option value="7">Temmuz</option>
                                        <option value="8">Ağustos</option>
                                        <option value="9">Eylül</option>
                                        <option value="10">Ekim</option>
                                        <option value="11">Kasım</option>
                                        <option value="12">Aralık</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="wdays" class="col-form-label">Haftanın Günleri</label>
                                    <select name="wdays[]" id="wdays" class="form-control form-select js-choice" required multiple>
                                        <option value="-1">Her Gün</option>
                                        <option value="1">Pazartesi</option>
                                        <option value="2">Salı</option>
                                        <option value="3">Çarşamba</option>
                                        <option value="4">Perşembe</option>
                                        <option value="5">Cuma</option>
                                        <option value="6">Cumartesi</option>
                                        <option value="0">Pazar</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="url" class="col-form-label">URL</label>
                                    <input type="text" class="form-control" name="url" id="url" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="title" class="col-form-label">Başlık</label>
                                    <input type="text" class="form-control" name="title" id="title" required>
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="action" value="add">
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                            <button type="submit" class="btn btn-primary">Kaydet</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <form action="" id="update-job-form" method="POST">
            <div class="modal" tabindex="-1" id="update-job-modal" data-bs-keyboard="false">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Görev Güncelle</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-status" class="col-form-label">Durum</label>
                                    <select name="status" id="up-status" class="form-control form-select" required>
                                        <option value="1">Aktif</option>
                                        <option value="0">Pasif</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-minutes" class="col-form-label">Dakika</label>
                                    <select name="minutes[]" id="up-minutes" class="form-control form-select" required multiple>
                                        <option value="0">00</option>
                                        <option value="15">15</option>
                                        <option value="30">30</option>
                                        <option value="45">45</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-hours" class="col-form-label">Saat</label>
                                    <select name="hours[]" id="up-hours" class="form-control form-select" required multiple>
                                        <option value="-1">Her Saat</option>
                                        <option value="0">0</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-mdays" class="col-form-label">Gün</label>
                                    <select name="mdays[]" id="up-mdays" class="form-control form-select" required multiple>
                                        <option value="-1">Her Gün</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                        <option value="26">26</option>
                                        <option value="27">27</option>
                                        <option value="28">28</option>
                                        <option value="29">29</option>
                                        <option value="30">30</option>
                                        <option value="31">31</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-months" class="col-form-label">Ay</label>
                                    <select name="months[]" id="up-months" class="form-control form-select" required multiple>
                                        <option value="-1">Her Ay</option>
                                        <option value="1">Ocak</option>
                                        <option value="2">Şubat</option>
                                        <option value="3">Mart</option>
                                        <option value="4">Nisan</option>
                                        <option value="5">Mayıs</option>
                                        <option value="6">Haziran</option>
                                        <option value="7">Temmuz</option>
                                        <option value="8">Ağustos</option>
                                        <option value="9">Eylül</option>
                                        <option value="10">Ekim</option>
                                        <option value="11">Kasım</option>
                                        <option value="12">Aralık</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-wdays" class="col-form-label">Haftanın Günleri</label>
                                    <select name="wdays[]" id="up-wdays" class="form-control form-select" required multiple>
                                        <option value="-1">Her Gün</option>
                                        <option value="1">Pazartesi</option>
                                        <option value="2">Salı</option>
                                        <option value="3">Çarşamba</option>
                                        <option value="4">Perşembe</option>
                                        <option value="5">Cuma</option>
                                        <option value="6">Cumartesi</option>
                                        <option value="0">Pazar</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-url" class="col-form-label">URL</label>
                                    <input type="text" class="form-control" name="url" id="up-url" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="up-title" class="col-form-label">Başlık</label>
                                    <input type="text" class="form-control" name="title" id="up-title" required>
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="job_id" id="job_id" value="">
                        <input type="hidden" name="action" value="update">
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                            <button type="submit" class="btn btn-primary">Güncelle</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <div class="modal" tabindex="-1" id="job-history-modal" data-bs-keyboard="false">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title fw-bold">Görev Geçmişi</h5>
                    </div>
                    <div class="modal-body">
                        <div class="row mb-3">
                            <div class="col-12">
                                <div class="table-responsive">
                                    <table class="table table-condedsed table-border">
                                        <thead>
                                            <tr>
                                                <th>Durum</th>
                                                <th>İşlem Tarihi</th>
                                                <th>Planlanan Tarih</th>
                                                <th>İşlem Süresi</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Tamam</button>
                    </div>
                </div>
            </div>
        </div>
<?php include DIR_VIEWS."parts/footer.php"; ?>