<?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";

$tvs = [];
$tv_channels = [];
$q = "SELECT * FROM tv_channels WHERE is_active='1' ORDER BY name ASC;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $tvs[$row['id']] = array_map('stripslashes',$row);
    }
}

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

/*$q = "SELECT s.*
      FROM tv_schedules s
      WHERE
        s.s_channel_id=t.id
        AND t.is_active='1'
        AND s.s_day='$tv_day'
        ORDER BY t.name ASC, s.s_timestamp ASC;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $tv_channels[$row['channel_id']] = array_map('stripslashes',$row);
    }
}*/

?>
<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 ($result && mysql_num_rows($result) > 0) {
            $channel_title = $channel_data['name'];
            $channel_schedule = [];
            $now = time();
            while ($rowp = mysql_fetch_assoc($result)) {
                $channel_schedule [] = array_map('stripslashes', $rowp);
            }
            ?>
            <div class="col-md-5">

                <section class="box box-default box-solid box-compact box-tv" 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
                        }
                    }
                    ?>
                    <table class="table table-hover table-striped">
                        <tbody>
                        <?php
                        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
                            ?>
                            <tr <?php echo $current_show? 'class="success"' : '';?>>
                                <td class="" style="width: 45px;">
                                    <?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
                                    }
                                    ?>
                                </td>
                                <td class="">
                                    <?php
                                    //echo $row['s_timestamp'] , '- ' , $s_timestamp, ' | ', date("Y-m-d H:i:s", $s_timestamp_next) . ' - ', $s_timestamp_next, ' | ', date("Y-m-d H:i:s", $now) . ' - ', $now, '<br>';
                                    ?>
                                    <div class="row">
                                        <div class="col-sm-10 col-xs-6">
                                            <?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'];
                                            }
                                            ?>
                                        </div>
                                        <div class="col-sm-10 col-xs-4">
                                            <?php
                                            if(!empty($row['s_show_categ'])) {
                                                echo '<span class="label label-default">'.$row['s_show_categ']  .'</span>';
                                            }
                                            ?>
                                        </div>
                                    </div>


                                </td>
                                <td class="text-right" style="min-width: 70px;">
                                    <?php
                                    if(!empty($row['s_rating'])) echo ($row['s_rating'] != "K") ? '<span style="display: block;" class="badge bg-red-active pull-left">' . $row['s_rating'] . '</span>' : '';
                                    if(1==1 || !empty($row['s_show_desc'])) {
                                        ?>
                                        <a class="btn btn-info btn-xs pull-right" role="button" data-toggle="collapse" href="#show_info_<?=$row['id'];?>" aria-expanded="false" aria-controls="show_info_<?=$row['id'];?>">
                                            <i class="fa fa-info-circle"></i>
                                        </a>
                                        <?php
                                    }
                                    ?>
                                </td>
                            </tr>
                            <?php
                            if( !empty($row['s_show_desc'])) {
                                ?>
                                <tr>
                                    <td colspan="3" id="show_info_<?=$row['id'];?>" class="collapse">
                                        <div><?=$row['s_show_desc'];?></div>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            <?php
                        }
                        ?>
                        </tbody>
                    </table>

                </section>
            </div>

            <?php

        }
    }
    ?>
</div>