<?php
require_once(dirname(__FILE__) . "/../../imCinemaAdmin/classes/Str.php");
require_once(dirname(__FILE__) . "/../../imCinemaAdmin/classes/movie.class.php");

$optres ['css'][] = $path . "amodules/im_cinemas/css/im_cinemas.css";
$optres['js'][] = $path . "assets/plugins/isotope/isotope.pkgd.min.js";
$optres['css'][] = $path . "amodules/im_cinemas/css/im_playing.css";
$optres['js'][] = $path . "amodules/im_cinemas/js/im_playing.js";

$genres = [];
$playing = [];
$q = "SELECT DISTINCT m.* FROM im_movies m, im_showtimes s WHERE m.movie_code=s.movie_code ORDER BY m.title ASC;";
//echo $q;
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $data = $row;
        $mgenres = array_map('trim', explode(",", $row['genres']));
        $genres = array_merge($genres, $mgenres);
        $data['class_filter'] = implode(" ", array_map("strtolower", $mgenres));
        $playing [$row['movie_code']] = $data;
    }

    $genres = array_unique($genres);
}
?>


<div class="button-group filter-button-group movies-filter">
    <a href="#" class="btn-filter active" data-filter=".all">All Genres</a>
    <?php
    foreach($genres as $gen) {
        $filter_class = trim(strtolower($gen));
        ?>
        <a href="#<?=$filter_class?>" class="btn-filter" data-filter=".<?=$filter_class?>"><?=$gen;?></a>
        <?php
    }
    ?>
</div>

<div class="grid">
    <div class="grid-sizer"></div>
    <?php
    foreach($playing as $rm_code=>$rm) {
        $poster = ($rm['poster_large'] != "") ? $rm['poster_large'] : $rm['poster'];
        $poster_file = dirname(__FILE__) . "/../../imCinemaAdmin/movies/" . $rm['movie_code'] . "/" . $poster;
        $no_poster_file = dirname(__FILE__) . "/../../imCinemaAdmin/movies/no-poster.png";
        $poster_path = file_exists($poster_file) ? $poster_file : $no_poster_file;

        $poster_path = str_replace(dirname(__FILE__) . "/../../", $full_uri, $poster_path);

        $rurl = $homepage_url . "movie/" . Str::slug($rm['title'] . ' ' . $rm['year'], '-');
        ?>
        <div class="grid-item all <?=$rm['class_filter'];?>">
            <div class="now-playing-movie">
                <a href="<?= $rurl ?>">
                    <img src="<?= $poster_path; ?>" class="img-responsive"
                         rel="<?php echo $rm_code; ?>" alt="<?php echo $rm['title'] . ' (' . $rm['year'] . ')'; ?>">
                    <span class="rm-title"><?php echo $rm['title'] . ' (' . $rm['year'] . ')'; ?></span>
                </a>
            </div>
        </div>
        <?php
    }
    ?>
</div>
