<?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');
$from=strtotime('today');
$to=strtotime('today');


if(isset($_GET['fromDate'])){
    $fromDate=strtotime($_GET['fromDate']);
    if($fromDate){
        $from=$fromDate;
    }
}
if(isset($_GET['toDate'])){
    $toDate=strtotime($_GET['toDate']);
    if($toDate){
        $to=$toDate;

    }
}
$to=strtotime('+1 day',$to);
$to=strtotime('-1 second',$to);
$list=db()->prepare("SELECT * FROM `customer_subscribe` WHERE `created_on` between ? AND ?");
$list->execute([$from,$to]);
//$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 Report
            <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 Report</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
            
            
                ?>
                <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-6 ">
                                <form method="GET" class="row" action="">
                                    <div class="col-xs-6 col-md-4">
                                        <div class="row">
                                            <label for="" class="col-md-3 mt-2">From</label>
                                            <div class="col-md-9">
                                                <input type="date" name="fromDate" class="form-control" value="<?=date('Y-m-d',$from)?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-xs-6 col-md-4">
                                        <div class="row">
                                            <label for="" class="col-md-3">To</label>
                                            <div class="col-md-9">
                                            <input type="date" name="toDate" class="form-control" value="<?=date('Y-m-d',$to)?>">
                                            </div>
                                        </div>
                                    </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 class="table table-bordered table-striped table-hover table-header-center">
                                <thead>
                                    <tr class="bg-gray">
                                        <th>#</th>
                                        <th>Payment Date</th>
                                        <th>Customer ID</th>
                                        <th>Customer Name</th>
                                        <th>Mobile</th>
                                        <th>Address</th>
                                        <th>Service Start Date</th>
                                        <th>Service End Date</th>
                                        <th class="w-10">Paid Amount</th>
                                        <th class="w-10">Paid By</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $serial=1;
                                    $total_amount=0;
                                    foreach($subscribes as $s){
                                        $total_amount+=$s['amount'];
                                        ?>
                                        <tr>
                                        <td><?=$serial++?></td>
                                        <td><?=date('d-m-Y h:i:s',$s['created_on'])?></td>
                                        <td class="text-center"><?=$s['customer_code']?></td>
                                        <td><?=$s['customer_name']?></td>
                                        <td><?=$s['customer_mobile']?></td>
                                        <td><?=$s['customer_address']?></td>
                                        <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>
                                        <td class="text-center">SSL</td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                                <tfoot>
                                    <tr style="background: #eaefef; font-weight: 600">
                                        <td colspan="8" class="text-right">Total</td>
                                        <td class="text-center"><strong><?=$total_amount?></strong></td>
                                        <td></td>
                                    </tr>
                            </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"; 
?>