<?php  include "header.php"; 


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


if($totalData>0){
    foreach($emp_data as $data){
        
        
        $customer_id = $data['customer_id'];
        $scheme_id = $data['scheme_id'];
        $scheme_amount = $data['scheme_amount'];
        $scheme_no = $data['scheme_no'];
        $scheme_added_date = $data['scheme_added_date'];
        $scheme_completed_date = $data['scheme_completed_date'];
        $scheme_months = $data['scheme_months'];
        
        
?>


<div class="content-wrapper">
        <section class="content-header">
            <h1>Update Customer Scheme</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Update 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="ids" id="ids" value="<?=$id?>" >
                                                                 
                        <div class="row">
                            
                            <div class="col-md-4">
                                <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 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-4">
                                <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['id']==$scheme_id) ? "selected":"" ?>><?=$ress['name']?> - <?=$ress['month']?> Months</option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="scheme_amount">Scheme Amount</label>                                   
                                    <input type="text" name="scheme_amount" class="form-control" style="width:100%" id="scheme_amount" value="<?=$scheme_amount?>" >
                                </div>
                            </div>
                            
                            <input type="hidden" name="is_verified" id="is_verified" value="0">
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="completed_date">Completed Date</label>                                   
                                    <input type="date" name="completed_date" class="form-control" style="width:100%" id="completed_date" value="<?=$scheme_completed_date?>" >
                                </div>
                            </div>
                        
                           
                        </div>
                        
                        <?php if(empty($scheme_completed_date) || $scheme_completed_date == '0000-00-00'){ ?>
                        <div class="row" id="otp_sender_div">
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="otp_send_type">Type</label>                                   
                                    <select name="otp_send_type" class="form-control select2" style="width:100%" id="otp_send_type">
                                        <option value="">Select Otp Sender</option>
                                        <option value="1">Customer</option>
                                        <option value="2">User</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="sender_detail">Sender Details</label>                                   
                                    <select name="sender_detail" class="form-control select2" style="width:100%" id="sender_detail">
                                       
                                        
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group" style="margin-top:20px;">
                                    <button type="button" name="send_otp" id="send_otp" class="btn btn-primary"> Send OTP</button>
                                </div>
                            </div>
                        </div>
                         <?php } ?>  
                        <div class="row" id="otp_div" style="display:none">        
                             
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="otp">Enter OTP</label>                                   
                                    <input type="text" name="otp" class="form-control" style="width:100%" id="otp" value="" maxlength="6">
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group" style="margin-top: 20px;">
                                    <button type="button" name="submit_otp" id="submit_otp" class="btn btn-success"> Submit OTP</button>
                                </div>
                            </div>
                        </div>
                        
                        
                        

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

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <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;">Customer Scheme Updated 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"; ?>
<!--<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>-->
<style>
/*.select2-container--default .select2-selection--multiple .select2-selection__choice {
    background-color: rgb(228, 228, 228);
    box-sizing: border-box;
    display: inline-block;
    margin-left: 5px;
    margin-top: 5px;
    position: relative;
    max-width: 100%;
    text-overflow: ellipsis;
    vertical-align: bottom;
    white-space-collapse: collapse;
    text-wrap: nowrap;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(170, 170, 170);
    border-image: initial;
    border-radius: 4px;
    padding: 0px 10px 0px 20px;
    overflow: hidden;
    color: black;
    font-weight: 800;
}
.select2-container--default .select2-selection--multiple {
    background-color: white;
    border: 1px solid #aaa;
    border-radius: 4px;
    cursor: text;
    height: 100px;
    padding-bottom: 5px;
    padding-right: 5px;
    position: relative;
}*/
</style>

<script>
$(document).ready(function() {
    
    $("#otp_send_type").on("change", function(){
        
        var send_type = $(this).val();
        var row_id = $("#ids").val();
        
        if(send_type != '' && row_id != ''){
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'get_scheme_sender_details',send_type,row_id},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                       
                     $('#sender_detail').html(response['data']);  
                     $('#sender_detail').trigger('change');
                     
                    }else{
                        toastr.error(response['message']);
                    }
                }
            })
            
        }
        
    });
    
    $("#send_otp").on("click",function(){
        
        var send_type = $("#otp_send_type").val();
        var sender_id = $("#sender_detail").val();
        
        if(send_type != '' && sender_id != ''){
            
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'scheme_send_otp',send_type,sender_id},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                        toastr.success(response['message']);
                        $('#otp_div').css('display','block'); 
                        $("#otp").attr('required',false);
                        $('#send_otp').text('Resend OTP');
                     
                    }else{
                        toastr.error(response['message']);
                    }
                }
            }) 
        }
        
    });
    
    $("#submit_otp").on('click',function(){
        
        var send_type = $("#otp_send_type").val();
        var sender_id = $("#sender_detail").val();
        var otp = $("#otp").val(); 
        
         if(send_type != '' && sender_id != '' && otp != ''){
            
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'scheme_submit_otp',send_type,sender_id,otp},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                        toastr.success(response['message']);
                        $('#otp_div').css('display','none'); 
                        $('#otp_sender_div').css('display','none');
                        $("#is_verified").val(1);
                        
                    }else{
                        toastr.error(response['message']);
                    }
                }
            }) 
        }

    });
    
   $('#form_details').on('submit', function(e){
                e.preventDefault();
               
                var formData = new FormData(this);
                var is_verified = $("#is_verified").val();
                formData.append('type', 'update_customer_scheme');
                if(is_verified == '1'){
                    
                    $('#loading-div').show();
                    $('#content-div').hide();
                    $("#submit").prop('disabled', true);
                    
                    $.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']);*/
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
                }else{
                    toastr.error('Please verify otp after entering the completed date sumbit...'); 
                }
   });
});
</script>

<?php }} ?>