<?php
$customer_id=intval($_GET['customer_id']);
$start=strtotime('today');

$list=db()->prepare("SELECT * FROM `customers` WHERE `customer_id`=?");
$list->execute([$customer_id]);
$customer=$list->fetch(PDO::FETCH_ASSOC);
if($customer){
    $list=db()->prepare("SELECT * FROM `customer_subscribe` WHERE `customer_id`=? AND end_date>? ORDER by end_date desc");
    $list->execute([$customer_id,$start]);
    $subscribe=$list->fetchAll(PDO::FETCH_ASSOC);
    if(!empty($subscribe)){
        $subscribe=$subscribe[0];
        $last_date=date('d-m-Y',$subscribe['end_date']);
        $last_date=strtotime($last_date);
        $start=strtotime('+1 day',$last_date);
    }
    
}
if(isset($_POST['save'])){
    $start_date  = strtotime($_POST['start']);
    $end    = strtotime($_POST['end']);
    $amount=$_POST['amount'];
    if($start_date>=$end){
        $error='End date must be greater than start date '.date('d/m/Y',$start_date).' '.date('d/m/Y',$end);
    }
    else if($start_date<$start){
        $error='Start date must be greater than from last subscription';
    }
    else if($amount<=0){
        $error='Amount must be greater than 0';
    }
    if(!isset($error)){
        $list=db()->prepare("INSERT INTO `subscribe_request` SET `customer_id`=?,`amount`=?,`total`=?,`created_time`=?,`is_paid`=1");
        $list->execute([$customer_id,$amount,$amount,time()]);
        $request_id=db()->lastInsertId();
        $list=db()->prepare("INSERT INTO `customer_subscribe` SET request_id=?, `customer_id`=?,`start_date`=?,`end_date`=?,`created_on`=?,`amount`=?");
        $list->execute([$request_id,$customer_id,$start_date,$end,time(),$amount]);
        
        header('location:subscription_list.php?new_subscribe_success=1');
        exit;
    }
}


?>
<div class="content-wrapper">
    <section class="content-header">
        <h1>
        New subscription
            <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><a href="subscription_list.php">Subscription list</a></li>
            <li class="active">New subscription</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($error)){
                    echo '<div class="alert alert-danger">'.$error.'</div>';
                }
                ?>
                <div class="box box-success">
                    <!-- <div class="box-header">
                        <div class="row">
                            <div class="col-md-9">
                                <h3 class="box-title">
                                New subscription
                                </h3>
                            </div>
                            <div class="col-md-3">
                                
                            </div>
                        </div>
                        <div>
                        </div>
                    </div> -->
                    <form action="" method="POST">
                        <div class="box-body">
                            <div class="row justify-content-center">
                                <div class="col-md-6">
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            Customer
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" class="form-control" value="<?=$customer['customer_name']?>" disabled>
                                        </div>
                                    </div>
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            Email
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" class="form-control" value="<?=$customer['customer_email']?>" disabled>
                                        </div>
                                    </div>
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            Mobile
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" class="form-control" value="<?=$customer['customer_mobile']?>" disabled>
                                        </div>
                                    </div>
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            Start date
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" name="start" class="form-control" value="<?=date('d-m-Y',$start)?>">
                                        </div>
                                    </div>
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            End date
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" name="end" class="form-control" value="<?=date('d-m-Y',strtotime('+30 day',$start))?>">
                                        </div>
                                    </div>
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label">
                                            Amount
                                        </label>
                                        <div class="col-sm-7">
                                            <input type="text" name="amount" class="form-control" value="">
                                        </div>
                                    </div>
                                
                                    <div class="row form-group mb-3">
                                        <label class="col-sm-3 control-label"></label>
                                        <div class="col-sm-7">
                                            <button class="btn btn-primary btn-block"  type="submit" name="save">
                                            <span class="fa fa-fw fa-save"></span>
                                            <?php echo trans('button_save'); ?>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>
