<?php 
    $data = array();
    $data['title'] = "Ürün Yorumları | ".$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/product/reviews.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' => "Ürünler",
        'href' => "/admin/product/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Ürün Yorumları",
        'href' => "/admin/product/reviews",
        'index' => 3
    );

    /* Status update */
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['id']) && !empty($_POST['id'])) {
            $review_id      = $_POST['id'];
            $status         = $_POST['status'];
            if(!$accessControl->get("admin/product/reviews/edit")) {
                header("Location: admin/access-denied");
                exit();
            }else{
                $stmt = $db->query("UPDATE {$db->prefix}product_reviews SET status=:status WHERE id=:id", array(':status'=>$status, ':id'=>$review_id));
                if($stmt->error) {
                    $utils->errorLog("error", "Cannot update product review status. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
                    $session->data['messageType'] = "error"; 
                    $session->data['messageTitle'] = "Hata!"; 
                    $session->data['message'] = "Bir hata oluştu ve yorum güncellenemedi."; 
                    header("Refresh: 0;");
                    exit();
                }
            }
        }
    }
    /* Status update */

    $search_keywords    = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : "";
    $status             = isset($_GET["status"]) ? $_GET["status"] : "";
    $sort               = isset($_GET["sort"]) ? $_GET["sort"] : "id";
    $sort_order         = isset($_GET["order"]) ? $_GET["order"] : "DESC";

    $page               = !isset($_GET["page"]) || $_GET["page"] < 1 ? 1 : (int)$_GET["page"];
    $limit	            = isset($_GET["limit"]) ? (int)$_GET["limit"] : 25;
    $offset             = ($page-1)*$limit;
    $filter_data = array(
        'search_keywords'    => $search_keywords,
        'status'             => $status,
        'sort'               => $sort,
        'order'              => $sort_order,
        'offset'             => $offset,
        'limit'              => $limit
    );
    
    $filter_bar         = !empty($search_keywords) || $status != "" ? true : false;
    
    $products           = $product->getProductReviews($filter_data);
    $total_products 	= $product->getTotalProductReviews($filter_data);
    
    $pagination->total  = $total_products;
    $pagination->page   = $page;
    $pagination->limit  = $limit;
    $pagination->url    = '#';
?>
<?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">Ürün Yorumları</h5>
                        <button type="button" class="btn btn-light btn-sm <?php echo $filter_bar ? '' : 'collapsed'; ?>" data-bs-toggle="collapse" data-bs-target="#filter-bar" aria-expanded="true" aria-controls="filter-bar">
                            <i class="bi bi-funnel"></i> Filtre
                        </button>
                    </div>
                    <div class="collapse show" id="filter-bar">
                        <div class="card p-3 mb-0 border-0">
                            <form action="" method="GET" id="filter-form">
                                <div class="row">
                                    <div class="col-md-4 text-center text-md-start">
                                        <label for="q" class="col-form-label">Anahtar Kelime</label>
                                        <input type="search" name="q" id="q" class="form-control" value="<?php echo $search_keywords; ?>">
                                    </div>
                                    <div class="col-md-2 text-center text-md-start">
                                        <label for="status" class="col-form-label">Durum</label>
                                        <select name="status" id="status" class="form-control form-select">
                                            <option value="" <?php echo $status == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                            <option value="1" <?php echo $status == "1" ? 'selected' : ''; ?>>Satışta</option>
                                            <option value="0" <?php echo $status == "0" ? 'selected' : ''; ?>>Satışa Kapalı</option>
                                        </select>
                                    </div>
                                    <div class="col-md-2 text-center text-md-start">
                                        <label for="skin" class="col-form-label">&nbsp;</label>
                                        <button type="submit" class="btn btn-primary w-100">Filtrele</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th style="width:60px;" class="sorting" data-column="id" data-order="">ID</th>
                                    <th style="width:100px;" class="sorting" data-column="status" data-order="">Durum</th>
                                    <th>Ürün Adı</th>
                                    <th class="sorting" data-column="title" data-order="">Yourm Başlığı</th>
                                    <th>Puan</th>
                                    <th style="width:85px;"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_products>0) {
                                        foreach($products as $row) {
                                            echo "<tr>
                                                        <td class=\"text-center\">".$row['id']."</td>
                                                        <td style=\"width: 100px;\" class=\"text-center\">".$row['status']."</td>
                                                        <td>".$row['product_title']."</td>
                                                        <td>".$row['title']."</td>
                                                        <td class=\"text-warning\">".$utils->createRatingStars($row['rating'])."</td>
                                                        <td style=\"width: 180px;vertical-align: middle;\" class=\"text-center\">
                                                            <a class=\"btn btn-sm btn-primary me-1\"  data-bs-toggle=\"modal\" data-bs-target=\"#review-modal\" data-id=\"".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                            <a class=\"btn btn-sm btn-info me-1 text-white\" href=\"/".$row['url']."\" target=\"_blank\"><i class=\"bi bi-send\"></i></a>
                                                            <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/product/reviews/delete?id=".$row['id']."&redirect=". urlencode($_SERVER['REQUEST_URI']) ."\"><i class=\"bi bi-trash\"></i></button>
                                                        </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"6\">Hiçbir kayıt bulunamadı!</td></tr>";
                                    }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex table-responsive align-items-center justify-content-center mt-3">
                        <div class="table-responsive">
                            <nav aria-label="Page navigation">
                                <ul class="pagination" id="pagination">
                                    <?php echo $pagination->render(); ?>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <form action="" id="review-form" method="POST">
            <div class="modal" tabindex="-1" id="review-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">Yorum Detayları</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="title" class="col-form-label fw-bold">Yorum Başlığı</label>
                                    <input type="text" class="form-control" name="title" id="title" readonly>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="text" class="col-form-label fw-bold">Yorum</label>
                                    <textarea name="text" id="text" cols="30" rows="3" class="form-control" readonly></textarea>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="rating" class="col-form-label fw-bold">Puan</label>
                                    <input type="number" class="form-control" name="rating" id="rating" readonly>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="review_status" class="col-form-label fw-bold">Durum</label>
                                    <select name="status" id="review_status" class="form-control form-select">
                                        <option value="0">Pasif</option>
                                        <option value="1">Aktif</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="product_title" class="col-form-label fw-bold">Ürün</label>
                                    <input type="text" class="form-control" name="product_title" id="product_title" readonly>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="user_name" class="col-form-label fw-bold">Yorum Sahibi</label>
                                    <input type="text" class="form-control" name="user_name" id="user_name" readonly>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="date_added" class="col-form-label fw-bold">Tarih</label>
                                    <input type="datetime-local" class="form-control" name="date_added" id="date_added" readonly>
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="id" id="id" value="">
                        <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>
<?php include DIR_VIEWS."parts/footer.php"; ?>