<?php
    if(!isset($_GET["channel_id"])){
        $_GET["channel_id"] = 1;
    }
    $channel_id = $_GET["channel_id"];

    $channels_list = $db->select("stores", "*", array( "store_id" => $store_hash));
    if($channels_list["id"]){
        $channels_list = array($channels_list);
    }
    foreach ($channels_list as $key => $channel_data) {
        if($channel_data["channel_id"] == $_GET["channel_id"]){
            $channel_data = $db->select("subscription", "*", array( "store_id" => $store_hash, "channel_id"=> $_GET["channel_id"], "status"=> "active"));
            if($channel_data){

                $subscription_data = new DateTime($channel_data["updated_at"]);
                $subscription_data = $subscription_data->format('Y-m-d H:i:s');
                $remaining_days = date_diff(date_create($subscription_data), date_create(date("Y-m-d h:i:s")));
                
                if($remaining_days->days > 31) {
                    $store_data["status"] = "deactive";
                    $store_settings = $db->update( "subscription",
                        $store_data, array("id" => $channel_data["id"])
                    );
                    ?>
                        <script> window.location.reload(); </script>
                    <?php
                    exit();
                }
            }
        }
    }


?>
<link rel="stylesheet" href="<?echo BASE_PATH; ?>subscription/styles/subscription.css">

<div class="trial" >
    <h4 class="app-name" >
        <span><a style="color: white; text-decoration: none;" href="<? echo BASE_PATH; ?>load.php?store_hash=<? echo $store_hash; ?>&channel_id=<? echo $channel_id; ?>&menu=home"><i class="fa fa-home" aria-hidden="true"></i> <?php echo APP_HEADING;?> </a></span>
    </h4>
    <div style="display: flex">
        <div class="align-items-c form-group p-1">
            <div class="">
                <label for="channel" >Channel:</label>
                <select 
                    id="channel"  
                    name="channel" 
                    style="width: 200px; display: inline;"
                    required class="form-control form-select"
                >
                    <?php foreach ($channels_list as $key => $channel) {
                        ?>
                            <option <? if($channel["channel_id"] == $_GET["channel_id"]){ echo "selected"; }?> value='<? echo $channel["channel_id"]; ?>'><? echo $channel["store_name"]; ?></option>
                        <?php
                    } ?>
                </select>
            </div>
        </div>
        <div class="align-items-c form-group p-1" style="margin-left: 25px;">
            <div class="">
                <select 
                    id="menu" name="menu"
                    style="width: 140px; display: inline;"
                    required class="form-control form-select"
                >
                    <option <? if($_GET["menu"] == "home") echo "selected"; ?> value='home'>Home</option>
                    <option <? if($_GET["menu"] == "subscription") echo "selected"; ?> value='subscription'>Subscription</option>
                    <option <? if($_GET["menu"] == "channel") echo "selected"; ?> value='channel'>Channel</option>
                </select>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).on("change", "#channel", function(){
        var channel_id = $("#channel :selected").val();
        var menu_item = $("#menu :selected").val();
        window.location.href = "?store_hash=<?echo $store_hash;?>&channel_id="+channel_id+"&menu="+menu_item;
    });
    $(document).on("change", "#menu", function(){
        var channel_id = $("#channel :selected").val();
        var menu_item = $("#menu :selected").val();
        if(menu_item == "home"){
            window.location.href = "<?php echo BASE_PATH ?>load.php?store_hash=<?echo $store_hash;?>&channel_id="+channel_id+"&menu=home";
        }
        else if(menu_item == "channel"){
            window.location.href = "<?php echo BASE_PATH ?>available_channels.php?store_hash=<?echo $store_hash;?>&channel_id="+channel_id+"&menu=channel";
        }
        else if(menu_item == "subscription"){
            window.location.href = "<?php echo BASE_PATH ?>subscription/index.php?store_hash=<?echo $store_hash;?>&channel_id="+channel_id+"&menu=subscription";
        }
    });
</script>