<?php
// Show Movie Details
if(isset($_REQUEST['movie_slug'])) {
    require_once(dirname(__FILE__) . "/config.php");
    $slug = trim($_REQUEST['movie_slug']);
    $show_all_content = false;

    /*$optres['css'][] = "../style.css";
    $optres['js'][] = "../js/jquery-latest.js";*/
    $optres['css'][] = $module_css_path . "amodules/im_cinemas/css/im_cinemas.css";
    /*$optres['css'][] = $path . "assets/plugins/prettyphoto/prettyPhoto.css";
    $optres['js'][] = $path . "assets/plugins/prettyphoto/jquery.prettyPhoto.js";*/

    $optres['css'][] = $path . "assets/plugins/swipebox/css/swipebox.css";
    $optres['js'][] = $path . "assets/plugins/swipebox/js/jquery.swipebox.js";
    $optres['js'][] = $path . "amodules/im_cinemas/js/im_movie.js";

    require_once(dirname(__FILE__) . "/../../imCinemaAdmin/classes/movie.class.php");
    //$mc = $_req_data['content_code'];
    $t = new Movie();
    $t->get_movie_by_slug($slug);
    if($t->poster_large == "") $t->poster_large = $t->poster;
    $mc = $t->movie_code;
    $img_path = "$include_www/imCinemaAdmin/movies/" . $mc . "/";

    $poster = ($t->poster_large != "") ? $t->poster_large : $t->poster;
    $poster_file = dirname(__FILE__) . "/../../imCinemaAdmin/movies/" . $t->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);


    $webpage_title_en = $t->title . ' (' . $t->year . ')';
    $meta['description'] = '';
    if(trim($t->plot) != "" && trim($t->plot) != trim($t->storyline)) {
        $meta['description'] = trim($t->plot);
    }

    ?>
    <header>
        <h1 class="page-title"><?php echo $t->title . ' (' . $t->year . ')';?></h1>
    </header>
    <div class="article-body cyprus">
        <div class="container-fluid">
            <div class="row movie-specs">
                <div class="col-sm-3 col-xs-10 mv-poster" data-cinema-mh="group-name-<?=$mc?>">
                    <img src="<?=$poster_path;?>" class="img-responsive" rel="<?php echo $mc;?>">
                    <div class="mv-media row">

                        <?php
                        $mi = $t->media_images;
                        if($mi != "") {
                            $mi = explode("|", $mi);
                            $mi_url = $img_path . $mi[0];
                            echo '<div class="col-sm-10 col-xs-5"><a href="', $mi_url , '" data-rel="',$t->movie_code,'|',$mi[0],'" class="mmi"><i class="fa fa-photo fa-fw"></i> Media Images</a></div>';
                        }

                        $mt = $t->trailers;
                        if(trim($mt) != "") {
                            $text_trailers = "Trailer";
                            $mt = explode("|", $mt);
                            if(count($mt) > 1) $text_trailers = "Trailers";
                            echo '<div class="col-sm-10 col-xs-5"><a href="', $mt[0] , '" data-rel="',$t->movie_code,'" class="mmv"><i class="fa fa-film fa-fw"></i> Watch ',$text_trailers,'</a></div>';
                        }
                        ?>
                    </div>
                </div>
                <div class="col-sm-7 col-xs-10 mv-details" data-cinema-mh="group-name-<?=$mc?>">
                    <table class="table">
                        <thead>
                        <tr>
                            <th colspan="2" class="col-xs-10">Movie Details</th>
                        </tr>
                        </thead>
                        <tbody>

                        <?php
                        if(trim($t->title_id) != "") {
                            if(substr($t->title_id, 0, 2) == "tt") {
                                $imdb_link = "http://www.imdb.com/title/{$t->title_id}/";
                                $imdb_rating = trim($t->rating) != "" ? trim($t->rating) : 'N/A';
                                ?>
                                <tr>
                                    <td class="col-sm-2 col-xs-3"><a href="<?=$imdb_link;?>" class="imdb-link" target="_blank">IMDB:</a></td>
                                    <td class="col-sm-8 col-xs-7">Rating: <b><?=$imdb_rating;?></b></td>
                                </tr>
                                <?php
                            }
                        }

                        ?>

                        <tr>
                            <td class="col-sm-2 col-xs-3">Genres:</td>
                            <td class="col-sm-8 col-xs-7"><span class="text-danger"><?php echo $t->genres;?></span></td>
                        </tr>
                        <tr>
                            <td class="col-sm-2 col-xs-3">Runtime:</td>
                            <td class="col-sm-8 col-xs-7"><?php echo $t->runtime;?> min</td>
                        </tr>
                        <tr>
                            <td class="col-sm-2 col-xs-3">Rating:</td>
                            <td class="col-sm-8 col-xs-7">
                                <strong><span class="text-danger"><?php echo $t->cy_rating;?></span></strong>
                                <?php
                                if(isset($ratings[$t->cy_rating])) echo '<span class="text-muted"> - ' . $ratings[$t->cy_rating] . '</span>';
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="col-sm-2 col-xs-3">Director:</td>
                            <td class="col-sm-8 col-xs-7"><?php echo $t->directors;?></td>
                        </tr>
                        <tr>
                            <td class="col-sm-2 col-xs-3">Stars:</td>
                            <td class="col-sm-8 col-xs-7"><?php echo $t->stars;?></td>
                        </tr>

                        <?php
                        if(trim($t->plot) != "" && trim($t->plot) != trim($t->storyline)) {
                            ?>
                            <tr>
                                <td class="col-sm-2 col-xs-3">Plot:</td>
                                <td class="col-sm-8 col-xs-7"><?php echo $t->plot; ?></td>
                            </tr>

                            <?php
                        }
                        ?>
                        <?php
                        if(trim($t->storyline) != "") {
                            ?>
                            <tr>
                                <td class="col-sm-2 col-xs-3">Storyline:</td>
                                <td class="col-sm-8 col-xs-7"><?php echo $t->storyline;?></td>
                            </tr>
                            <?php
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <?php

            if(isset($_REQUEST['adsense_debug'])) {
                echo '<pre>';
                echo '$gads_show = ' . var_export($gads_show, true);
                echo '$def_ads = ' . var_export($def_ads, true);
                echo '$def_ad_id = ' . var_export($def_ad_id, true);
                echo '$gads = ' . var_export($gads, true);
                echo '</pre>';
            }

            if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                $adsense_container_class = "no-margin";
                echo '<div style="margin: 10px 0;">';
                include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                echo '</div>';
                unset($adsense_container_class);
            }

            $start_date_t = strtotime("previous thursday", strtotime("next thursday"));
            $end_date_t = strtotime("previous wednesday", strtotime("next wednesday"));

            if($end_date_t < $start_date_t) $end_date_t = strtotime("next wednesday");

            $start_date = date("Y-m-d", $start_date_t);
            $end_date = date("Y-m-d", $end_date_t);
            $today = date("Y-m-d", time());

            $q = "select
                  c.id as cinema_id,
                  c.name as cinema_name,
                  l.name as location_name,
                  l.cinema_phone as location_phone,
                  l.office_phone as office_phone,
                  s.* from im_theaters c, im_locations l, im_nowplaying s
                  where
                    c.id=l.theater_id
                    and l.id=s.location_id
                    and s.movie_code='$mc'
                    AND '$today' >= s.start_date
                    AND '$today' <= s.end_date
                    order by s.location ASC, l.location ASC;";

            //echo $q;

            $r = @mysql_query($q);
            if($r && mysql_num_rows($r)>0) {
                ?>
                <div class="row cinema-schedules movie-cinemas">
                    <h2>
                        <?php
                        echo '<strong>Cyprus cinemas</strong>' . ' playing <em>' . stripslashes($t->title) . '</em>';
                       /* if ($admin_view) {
                            echo '<a href="javascript:;" rel="' . $dd . '|' . $t->movie_code . '" class="pull-right hidden-xs add_cinema_sched btn btn-warning btn-xs">Add Cinema</a>';
                        }*/
                        ?>
                    </h2>
                    <?php

                    $all_cinemas = [];

                    //$dd = [];

                    while ($row = mysql_fetch_assoc($r)) {
                        //$dd[$row['id']] = $row;
                        $loc = $row['location'];
                        if(!isset($all_cinemas[$loc])) $all_cinemas[$loc] = [];
                        $all_cinemas[$loc][] = $row;
                    }

                   //echo '<pre>', var_export($all_cinemas, true), '</pre>';


                    foreach($all_cinemas as $loc => $cinemas) {
                        ?>
                        <div class="box box-default box-solid">
                            <div class="box-header with-border cinema-locations">
                                <h3><strong><?=$loc?> cinemas</strong> playing <em><?=$t->title;?></em></h3>
                            </div>
                            <div class="container-fluid">

                                <?php
                                foreach($cinemas as $row) {
                                    ?>
                                    <?php
                                    if ($sales_view) {
                                        //echo '<pre>', var_export($row, true), '</pre>';
                                    }
                                    $cin = array_map('stripslashes', $row);
                                    $cinema_title = $cin['cinema_name'];
                                    if(trim($cin['location_name']) != "") {
                                        $cinema_title .= ' - ' . trim($cin['location_name']);
                                    }

                                    $phone = 'N/A';
                                    if(trim($cin['location_phone']) != "") {
                                        $phone = trim($cin['location_phone']);
                                    }
                                    elseif(trim($cin['office_phone']) != "") {
                                        $phone = trim($cin['office_phone']);
                                    }

                                    /*else {
                                        $cinema_title .= ' - ' . $loc;
                                    }*/
                                    ?>
                                        <div class="row cinema-item">
                                            <div class="col-sm-10 cinema-title-container">
                                                <?php
                                                if ($sales_view) {
                                                    echo '<a href="javascript:;" class="edit_cinema_sched cinema-title" data-id="' . $cin['cinema_id'] . '">' . $cinema_title . '</a>';
                                                } else {
                                                    echo '<span class="cinema-title">' . $cinema_title . '</span>';
                                                }
                                                ?>
                                                <div class="pull-right">
                                                    <span><i class="fa fa-phone fa-fw"></i> <?php echo $phone; ?></span>
                                                </div>
                                            </div>
                                            <div class="col-sm-3">


                                                <div class="row cine-info">
                                                    <div class="col-sm-10 col-xs-4">
                                                        Screen: <span class="badge badge-info bg-red-active"><?php echo trim($cin['screen']) != "" ? $cin['screen'] : '*'; ?></span>
                                                    </div>

                                                    <?php
                                                    if ($cin['movie_gr'] == 1) {
                                                        ?>
                                                        <div class="col-sm-5 col-xs-3"><span class="label label-info" title="Greek Language">Greek</span></div>
                                                        <?php
                                                    }
                                                    if($cin['movie_3d'] == 1) {
                                                        ?>
                                                        <div class="col-sm-5 col-xs-3"><span class="label label-danger" title="3D Movie">3D Movie</span></div>
                                                        <?php
                                                    }

                                                    ?>
                                                </div>

                                            </div>
                                            <div class="col-sm-7">
                                                <?php
                                                display_schedule($cin['showtimes']);
                                                ?>
                                            </div>

                                        </div>
                                    <?php
                                }

                                ?>

                            </div>
                         </div>
                        <?php
                    }
                    ?>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
    <?php
    include_once(dirname(__FILE__) . "/im_related.php");

}