<?php 
ob_start();
session_start();
include ("../_init.php");

// Redirect, If user is not logged in
if (!is_loggedin()) {
    redirect(root_url() . '/index.php?redirect_to=' . url());
}

// Redirect, If User has not Read Permission
if (user_group_id() != 1 && !has_permission('access', 'read_customer')) {
    redirect(root_url() . '/'.ADMINDIRNAME.'/dashboard.php');
}

// Set Document Title
$document->setTitle('Subscription list');

$list=db()->prepare("
SELECT * FROM `customer_subscribe` WHERE `start_date` <= ? AND `end_date` >= ?");
$list->execute([time(),time()]);
//$list->execute([0,0]);
$subscribes = $list->fetchAll(PDO::FETCH_ASSOC);
$subscription_status=1;
if(isset($_GET['subscription_status'])){
    $subscription_status=intval($_GET['subscription_status']);
}
if(!empty($subscribes)){
    $customer_ids=[];
    foreach($subscribes as $s){
        $customer_ids[$s['customer_id']]=$s['customer_id'];
    }

    if($subscription_status==1){
        $list=db()->prepare("SELECT * FROM `customers` WHERE `customer_id` in(".implode(',',$customer_ids).")");
    }
    else{
        $list=db()->prepare("SELECT * FROM `customers` WHERE `customer_id` not in(".implode(',',$customer_ids).")");
    }
    $list->execute();
    $c=$list->fetchAll(PDO::FETCH_ASSOC);
    $customers=[];
    foreach($c as $d){
        $customers[$d['customer_id']]=$d;
    }
    if($subscription_status==1){
        foreach($subscribes as $k=>$s){
            $s['customer_name']     = '';
            $s['customer_mobile']   = '';
            $s['customer_code']     = '';
            $s['customer_address']     = '';
            if(isset($customers[$s['customer_id']])){
                $s['customer_name']=$customers[$s['customer_id']]['customer_name'];
                $s['customer_code']=$customers[$s['customer_id']]['customer_code'];
                $s['customer_mobile']=$customers[$s['customer_id']]['customer_mobile'];
                $s['customer_address']=$customers[$s['customer_id']]['customer_address'];
            }
            $subscribes[$k]=$s;
        }
    }
    else{
        $subscribes=$customers;
    }
}




// Include Header and Footer
include "header.php";
include "left_sidebar.php";
if(isset($_GET['bill_history'])){
    include __DIR__."/subscription_list_bill_history.php";
}
else if(isset($_GET['new_subscribe'])){
    include __DIR__."/subscription_new.php";
}
else{
?>
<div class="content-wrapper">
    <section class="content-header">
        <h1>
        Subscription list
            <small>
                <?php echo store('name'); ?>
            </small>
        </h1>
        <ol class="breadcrumb">
            <li>
                <a href="dashboard.php">
                    <i class="fa fa-dashboard"></i> 
                    <?php echo trans('text_dashboard'); ?>
                </a>
            </li>
            <li class="active">Subscription list</li>
        </ol>
    </section>
    <!-- Content Header Start -->

    <!-- Content Start -->
    <section class="content">

        <?php if(DEMO) : ?>
            <div class="box">
                <div class="box-body">
                    <div class="alert alert-info mb-0">
                        <p><span class="fa fa-fw fa-info-circle"></span> <?php echo $demo_text; ?></p>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div class="row">
            <div class="col-xs-12">
                <?php
            if(isset($_GET['new_subscribe_success'])){
                echo '<div class="alert alert-success">New subscription created successfully</div>';
            }

                ?>
                <div class="box box-success">
                    <div class="box-header">
                        <div class="row">
                            <!-- <div class="col-md-8">
                                <h3 class="box-title">
                                    Subscription list
                                </h3>
                            </div> -->
                            <div class="col-xs-12 col-md-4 ">
                                <form method="GET" class="row" action="">
                                    <div class="col-xs-8 col-md-8 ">
                                        <select class="form-select" name="subscription_status">
                                            <option value="0" <?=$subscription_status==0?'selected':''?>>UnSubscribe User</option>
                                            <option value="1" <?=$subscription_status==1?'selected':''?>>Subscribe User</option>
                                        </select>
                                    </div>
                                    <div class="col-xs-4 col-md-4 ">
                                        <button class="btn btn-primary" type="submit" name="search"><i class="fa fa-search"></i> Search</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <div>
                        </div>
                    </div>
                    <div class="box-body">
                        <div class="table-responsive">
                            <!-- Customer List Start -->
                            <table id="customer-customer-list" class="table table-bordered table-striped table-hover table-header-center" data-hide-colums="">
                                <thead>
                                    <tr class="bg-gray">
                                        <th class="w-5">SL</th>
                                        <th class="w-10">Customer ID</th>
                                        <th class="w-15">Customer Name</th>
                                        <th class="w-10">Mobile</th>
                                        <th class="w-15">Address</th>
                                        <?php
                                        if($subscription_status==1){
                                            ?>
                                            <th class="w-10">Service Start Date</th>
                                            <th class="w-10">Service Start End</th>
                                            <th class="w-10">Paid Amount</th>
                                            <?php
                                            }
                                        ?>
                                        <th class="w-10">View Bill History</th>
                                        <th class="w-10"> Payment</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $serial=1;
                                    foreach($subscribes as $s){
                                        ?>
                                        <tr>
                                        <td><?=$serial++?></td>
                                        <td><?=$s['customer_code']?></td>
                                        <td><?=$s['customer_name']?></td>
                                        <td><?=$s['customer_mobile']?></td>
                                        <td><?=$s['customer_address']?></td>
                                        <?php
                                        if($subscription_status==1){
                                            ?>
                                            <td class="text-center"><?=date('d-m-Y',$s['start_date'])?></td>
                                            <td class="text-center"><?=date('d-m-Y',$s['end_date'])?></td>
                                            <td class="text-center"><?=$s['amount']?></td>
                                            <?php
                                        }
                                            ?>
                                        <td class="text-center"><a class="btn btn-info" href="subscription_list.php?bill_history=1&customer_id=<?=$s['customer_id']?>">Bill History</a></td>
                                        <td  class="text-center"><a class="btn btn-primary" href="subscription_list.php?new_subscribe=1&customer_id=<?=$s['customer_id']?>">Add Payment</a></td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>
<style>
    .table-header-center tr th{
        text-align: center;
    }
</style>
<script>
    	$(document).ready(function() {
            $('#customer-customer-list').DataTable();
        } );
</script>
<?php
}
include "footer.php"; 
?>