<?php
@session_start();

$assets_url = "https://www.cypruswebsites.com/imTvAdmin/assets/";
if(strpos($_SERVER['HTTP_HOST'], '.dev') !== false) {
    $assets_url = "http://{$_SERVER['HTTP_HOST']}/imTvAdmin/assets/";
}

$icons_path = dirname(__FILE__) . "/assets/";
$show_day_content = true;
$optres ['css'][] = $module_css_path . "amodules/tv_ext/css/tv_ext.css";
$optres ['js'][] = $path . "assets/js/jquery.matchHeight-min.js";

$optres['js'][] = $path . "amodules/tv_ext/js/tv_ext.js";

$tv_ext_channels = isset($module_params['tv_ext_channels']) ? $module_params['tv_ext_channels'] : null;

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

$tvs = [];
$tv_channels = [];
if(!is_array($tv_ext_channels)) {
    $q = "SELECT * FROM tv_channels WHERE is_active='1' ORDER BY name ASC;";
}
else {
    $tv_ch_ids = [];
    foreach($tv_ext_channels as $ch_id=>$ch_data) {
        if(intval($ch_data['selected']) == 1) {
            $tv_ch_ids[$ch_id] = intval($ch_data['pos']);
        }
    }

    $tv_ch_ids_str = implode(',', array_keys($tv_ch_ids));
    $q = "SELECT * FROM tv_channels WHERE is_active='1' AND id IN ($tv_ch_ids_str);";

}
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    $cnt = 1;
    while($row = mysql_fetch_assoc($r)) {
        $tvs[$row['id']] = array_map('stripslashes',$row);

        if(!is_array($tv_ext_channels)) {
            $tvs[$row['id']]['pos'] = $cnt;
        }
        else {
            $tvs[$row['id']]['pos'] = $tv_ch_ids[$row['id']];
        }

        $cnt++;
    }
}
if(count($tvs)) {
    function compare_by_pos($a, $b)
    {
        if ($a['pos'] == $b['pos']) {
            return 0;
        }
        return ($a['pos'] < $b['pos']) ? -1 : 1;
    }

    uasort($tvs, "compare_by_pos");
}


$show_tv_h1 = false;
$tv_ext_title = (isset($module_params['tv_ext_title']) && trim($module_params['tv_ext_title']) != "") ? trim($module_params['tv_ext_title']) : null;

if($tv_ext_title != null) {
    $tv_ext_title = str_replace('{date}', date("d/m/Y", strtotime($tv_day)), $tv_ext_title);
    $tv_ext_title = str_replace('{day}', date("l", strtotime($tv_day)), $tv_ext_title);
}

if(!isset($is_h1) || $is_h1 == false) {
    $show_tv_h1 = true;
}

$date_title = null;

if($show_tv_h1) {
    if($tv_ext_title != null) {
        $date_title = $tv_ext_title;
    }
    else {
        $date_title = "Cyprus TV Schedule - " . date("l d/m/Y", strtotime($tv_day));
    }
    $date_title = '<h1>'.$date_title.'</h1>';
}
else {
    if($tv_ext_title != null) {
        $date_title = $tv_ext_title;
        $date_title = '<h2>'.$date_title.'</h2>';
    }
}

?>
<section class="article cyprus">
    <?php
    $tv_container_class = "";
    if($date_title != null) {
        ?>
        <header>
            <?php
            $tv_container_class = "cyprus-red";
            echo $date_title;
            if ($show_tv_h1) {
                echo '<!--[/SocialShareButtons/]-->';
            }
            ?>
        </header>
        <?php
    }
    ?>
    <div class="article-body <?=$tv_container_class;?>">
        <div class="row tv-mod">
            <?php
            foreach($tvs as $channel_id => $channel_data) {
                $sql = "SELECT s.*
                FROM tv_schedules s
                WHERE
                  s.s_channel_id=$channel_id
                  AND s.s_day='$tv_day'
                ORDER BY s.s_timestamp ASC;";
                //echo $sql;
                $result = @mysql_query($sql);
                if(1==1) {
                    $channel_title = $channel_data['name'];
                    $channel_schedule = [];
                    if ($result && mysql_num_rows($result) > 0) {


                        $now = time();
                        while ($rowp = mysql_fetch_assoc($result)) {
                            $channel_schedule [] = array_map('stripslashes', $rowp);
                        }
                    }
                    ?>
                    <div class="col-md-5">

                        <section class="box-tv effect7" data-mh="tvchannels">
                            <header class="box-header with-border">
                                <b><?=$channel_title;?></b>
                            </header>
                            <?php
                            if(!empty($channel_data['logo'])) {
                                $logo_file = dirname(__FILE__) . "/../../imTvAdmin/assets/logos/" . $channel_data['logo'];
                                if(file_exists($logo_file)) {
                                    $logo_url = $assets_url . "logos/" . $channel_data['logo'];
                                    ?>
                                    <img src="<?=$logo_url;?>" class="channel-logo img-responsive" style="width: 100%;">
                                    <?php
                                }
                            }
                            ?>
                            <div class="shows-container container-fluid">
                                <?php
                                if(count($channel_schedule) > 0) {
                                    foreach ($channel_schedule as $index => $row) {
                                        $current_show = false;
                                        $s_timestamp = strtotime($row['s_timestamp']);
                                        if (isset($channel_schedule[$index + 1])) {
                                            $s_timestamp_next = strtotime($channel_schedule[$index + 1]['s_timestamp']);

                                            if ($s_timestamp <= $now && $now < $s_timestamp_next) {
                                                $current_show = true;
                                            }
                                        }
                                        //#43404B
                                        ?>
                                        <div class="show-line row <?php echo $current_show ? 'success' : ''; ?>">
                                            <div class="col-xs-2 col-sm-2 s_time">
                                                <?php

                                                if ($current_show) {
                                                    ?>
                                                    <b><span class="badge bg-green"><?= substr($row['s_time'], 0, 5); ?></span></b>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <b><?= substr($row['s_time'], 0, 5); ?></b>
                                                    <?php
                                                }
                                                ?>
                                            </div>
                                            <div class="col-xs-8 col-sm-8 s_show">
                                                <?php
                                                /*if (!empty($row['s_show_link'])) {
                                                    echo '<a href="' . $row['s_show_link'] . '" target="_blank">' . $row['s_show'] . '</a>';
                                                } else {
                                                    echo $row['s_show'];
                                                }*/

                                                echo strtoupper($row['s_show']);

                                                ?>
                                                <div class="infos">
                                                    <?php
                                                    if (!empty($row['s_rating'])) echo ($row['s_rating'] != "K") ? '<span class="s_rating s_badge bg-red-active">' . $row['s_rating'] . '</span>' : '';
                                                    if (!empty($row['s_show_desc']) || !empty($row['s_show_categ'])) {

                                                        ?>
                                                        <span class="s_info s_badge" role="button" data-toggle="collapse"
                                                              href="#show_info_<?= $row['id']; ?>" aria-expanded="false"
                                                              aria-controls="show_info_<?= $row['id']; ?>">
                                                            <i class="fa fa-info fa-show"></i>
                                                            <i class="fa fa-times fa-hide"></i>
                                                        </span>
                                                        <?php
                                                    }
                                                    ?>
                                                </div>
                                            </div>

                                            <?php
                                            if (!empty($row['s_show_desc']) || !empty($row['s_show_categ'])) {
                                                ?>
                                                <div class="collapse s_desc" id="show_info_<?= $row['id']; ?>">
                                                    <div class="s_desc_data">
                                                        <?php
                                                        if (!empty($row['s_show_categ'])) {
                                                            echo '<span class="label label-info">' . $row['s_show_categ'] . '</span>';
                                                        }

                                                        ?>
                                                        <?php
                                                        if (!empty($row['s_show_desc'])) {
                                                            echo '<p>' . $row['s_show_desc'] . '</p>';
                                                        } ?>
                                                    </div>
                                                </div>
                                                <?php
                                            }
                                            ?>

                                        </div>
                                        <?php
                                    }
                                }
                                else {
                                    ?>
                                    <br>
                                    <div class="alert alert-warning">
                                        <p><i class="fa fa-info-circle fa-lg"></i> The program is not yet available.</p>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>

                        </section>
                    </div>

                    <?php

                }
            }
            ?>
        </div>
    </div>
</section>
