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

$id = $_GET['id'];
$statement = $pdo->prepare("SELECT cs.*,
COALESCE(SUM(p.scheme_weight)) as total_scheme_weight
FROM tbl_customer_scheme  cs 
LEFT JOIN tbl_payments p ON p.scheme_id=cs.id
WHERE cs.id=?");
$statement->execute(array($id));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();
if($totalData>0){
        
        $ids = $datas[0]['id'];
        $customer_id = $datas[0]['customer_id'];
        $scheme_id = $datas[0]['scheme_id'];
        $scheme_amount = $datas[0]['scheme_amount'];
        $scheme_type = $datas[0]['type'];
        
        $total_scheme_weight = $datas[0]['total_scheme_weight'];
        
        if($scheme_type==1){
            $colspan="6";
        }else{
            $colspan="8";
        }

}

$today_price = $pdo->prepare("SELECT * FROM tbl_prices  WHERE product_id=? and purity_id=?");
$today_price->execute(array(1,2));
$price_datas = $today_price->fetchAll(PDO::FETCH_ASSOC);
$price_totalData = $today_price->rowCount();

$per_gm_amount = $price_datas[0]['amount'];
$scheme_weight = ($scheme_amount/$price_datas[0]['amount']);
?>

<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 id="content-div">    
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                                          
                            
                            <input type="hidden" name="scheme_id" id="customer_scheme_id" value="<?=$ids?>" >
                            <input type="hidden" name="scheme_amount" id="scheme_amount" value="<?=$scheme_amount?>" >
                                          
                                                                 
                        <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="orginal_scheme_id">Scheme Details</label>                                   
                                    <select name="orginal_scheme_id" class="form-control select2" style="width:100%" id="orginal_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">
                                    <span class="btn btn-warning btn-xs" style="margin-top: 24px;" id="add_row"><i class="fa fa-plus"></i> Add Row</span>
                                </div>
                            </div>
                            
                            <div class="clearfix"></div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <h4><b>Scheme Amount: <i class="fa fa-inr"></i> <?=number_format($scheme_amount,2)?></b></h4>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <select name="month" class="form-control select2 month" style="width:100%" id="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">
                                    <span class="btn btn-primary btn-xs" id="month_submit" onclick="month_submit()" style="margin-top: 5px;">Submit</span>
                                </div>
                            </div>
                        
                        </div>
                        
                    
                        <div class="row">
                        <?php if($scheme_type==2){ ?>
                            <div class="col-md-12">
                                <h3><b style="color: brown;">Current Scheme Weight: <?=$total_scheme_weight?></b></h3>
                            </div>
                        <?php } ?>
                        </div>
                        
                        
                        <div id="payment_month_details">
                            
                        </div>
                        
                        
                        
                        
                        <div class="row">
                            
                            <div class="col-md-3">
                                <h4><b>Entered Amount: <i class="fa fa-inr"></i> <b id="entered_amount">0.00</b></b></h4>
                            </div>
                            <div class="col-md-12" style="text-align:center; margin-top: 5px;">
                                <b id="results_text" style="color: red;"></b>
                            </div>
                            
                        </div>
                        
                        
                        

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

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center; margin-top: 10px;">
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            <a href="customers-scheme-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                </div> 
                
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    
                    
                    <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Scheme Payments Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();" class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="customers-scheme-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


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

<style>
    th{
        padding: 5px;
    }
    td, th{
        border: 1px solid gray;
    }
</style>

<script>
$(document).ready(function() {
    
var count = 2;
var payment_amount = $("#payment_amount").val();


$("#add_row").click(function(){
    
    
    var scheme_type = '<?=$scheme_type?>';
    var scheme_id = '<?=$ids?>';
    var month = $("#month").val();
    
    $.ajax({
            url: 'ui-api/scheme-month-payment-ui-table.php',
            type: 'post',
            dataType: 'html',
            data: {type:'table_row', scheme_type:scheme_type, month:month, scheme_id:scheme_id, count:count},
            success: function (response) {
                console.log(response);
                $("#payment_tbody").append(response);
                PaymentMonth();
            }
    });
        
        
        $(".select2").select2();
        PaymentScheme(count);
        count++;
        
    
    
});

});

$(document).on('click', '.remove_row', function() {
        var dataid = $(this).data("id");
        $('.porow_'+dataid).remove();
});
</script>
<script>

$(document).ready(function() { 
    
    PaymentScheme(1);
    
        

});
        
function PaymentScheme(count){
    var customer_id = $("#customer_id").val();
    var scheme_id = $("#customer_scheme_id").val();
    var payment_month = $("#payment_month_"+count).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_span_"+count).text(response['message']).css("color", "red");
                    $("#submit").prop('disabled', true);
                }else{
                    var n = "";
                    $("#submit").prop('disabled', false);
                    $("#payment_span_"+count).text(n);
                }
            }
    });*/
    
    
    
}


function PaymentMonth(){
    
    var tot=0;
    $(".payment_amount").each(function(){
        tot+=Number($(this).val());
    });
    
    var scheme_amount = $("#scheme_amount").val();
    
    $("#entered_amount").text(tot.toFixed(2));
    console.log("Total: "+tot);
    
    if(tot>scheme_amount){
        $("#submit").prop('disabled', true);
        $("#submit").hide();
        $("#results_text").text("Payment Amount is Wrong");
    }else{
        $("#submit").show();
        $("#submit").prop('disabled', false);
        $("#results_text").text("");
    }
}



function month_submit(){
    
    var scheme_type = '<?=$scheme_type?>';
    var scheme_id = '<?=$ids?>';
    var month = $("#month").val();
    
    $("#payment_month_details").empty();

    $.ajax({
            url: 'ui-api/scheme-month-payment-ui-table.php',
            type: 'post',
            dataType: 'html',
            data: {type:'table_full_structure', scheme_type:scheme_type, month:month, scheme_id:scheme_id},
            success: function (response) {
                console.log(response);
                $("#payment_month_details").append(response);
                PaymentMonth();
                $(".select2").select2();
            }
    });

}


$(document).ready(function() {
    PaymentScheme(1);
   $('#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();*/
                                     window.open("scheme-payment-print.php?id=" + response['row_id'], '_blank');
                                     $("#submit").prop('disabled', false);
                                    //  $('#success-div').show();
                                    //  $('#loading-div').hide();
                                    setTimeout(function(){window.location.href="customers-scheme-list.php";},2000);
                                    
                                }else{
                                     toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>