<?php  include "header.php"; ?>
<?php

$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM tbl_customer_scheme  WHERE tbl_customer_scheme.id=?");
$statement->execute(array($id));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();
if($totalData>0){
        
        $customer_id = $datas[0]['customer_id'];
        $scheme_id = $datas[0]['scheme_id'];

}        
?>

<div class="content-wrapper">
        <section class="content-header">
            <h1>Add Payment Customer Scheme</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add Payment Customer Scheme</li>            
            </ol>
        </section>


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                <div class="box-header">
                    <h3 class="box-title">Please fill in the information below</h3>
                </div>
                <div class="box-body">
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                                                                 
                        <div class="row">
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="customer_id">Customer Details</label>                                   
                                    <select name="customer_id" class="form-control select2" style="width:100%" id="customer_id" required="required">
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customers` where status=1 and id=?");
                                            $statement->execute(array($customer_id));
                                            $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($resultsss as $ress) { ?>
                                                <option value="<?=$ress['id']?>"><?=$ress['customer_name']?> - <?=$ress['mobile_no']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="scheme_id">Scheme Details</label>                                   
                                    <select name="scheme_id" class="form-control select2" style="width:100%" id="scheme_id" required="required">
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_schemes` where status=1 and id=?");
                                            $statement->execute(array($scheme_id));
                                            $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($resultsss as $ress) { ?>
                                                <option value="<?=$ress['id']?>"><?=$ress['name']?> - <?=$ress['month']?> Months</option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="payment_date">Payment Date</label>                                   
                                    <input type="date" name="payment_date" class="form-control" value="<?=date('Y-m-d')?>" style="width:100%" id="payment_date" required>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="payment_amount">Payment Amount</label>                                   
                                    <input name="payment_amount" class="form-control" style="width:100%" id="payment_amount" required>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="payment_month">Payment Month</label> 
                                    <span id="payment_customer_details" style="color: red; font-weight: 600;"></span>
                                    <select name="payment_month" class="form-control select2" style="width:100%" id="payment_month" required>
                                        <?php 
                                            for($i=1;$i<=12;$i++){
                                                if($i==date('m')){
                                                    echo "<option value='".$i."' selected>".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";
                                                }else{
                                                    echo "<option value='".$i."'>".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";   
                                                }
                                            }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="payment_type">Payment Type</label><br> 
                                    <select name="payment_type" class="form-control select2" id="payment_type" style="width: 100%;">
                                        <?php 
                                        $statement = $pdo->prepare("SELECT * FROM `tbl_payment_type` WHERE status=1");
                                        $statement->execute();
                                        $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"><?=$result['name']?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="reference">Reference No</label><br> 
                                    <input type="text" name="reference"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label><br> 
                                    <textarea name="notes" id="notes" placeholder="Notes" rows="4" class="form-control"></textarea>
                                </div>
                            </div>
                            
                            
                            
                            
                            
                           
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Add Customer Scheme" class="btn btn-success" />
                        </form>
                        </div>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <a href="scheme-add.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                            <a href="scheme-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php include "footer.php"; ?>


<script>
$(document).ready(function() { 
    
    PaymentScheme();
    
        $('#customer_id, #scheme_id, #payment_month').change(function() {
            PaymentScheme();
        });

});
        
function PaymentScheme(){
    var customer_id = $("#customer_id").val();
    var scheme_id = $("#scheme_id").val();
    var payment_month = $("#payment_month").val();
    
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'customer_scheme_payment_month',customer_id:customer_id, scheme_id:scheme_id, payment_month:payment_month},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    console.log(response);
                    $("#payment_customer_details").text(response['message']);
                    $("#submit").prop('disabled', true);
                }else{
                    var n = "";
                    $("#submit").prop('disabled', false);
                    $("#payment_customer_details").text(n);
                }
            }
        });
    
}
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'add_payment');
                    $.ajax({
                            url: 'apis/scheme.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     alert(response['message']);
                                     location.reload();
                                     $("#submit").prop('disabled', false);
                                     $('#content-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>