<?php include "../config/config.php"; 

if(isset($_GET['scheme'])){
    
    $scheme = base64_decode($_GET['scheme']);
    
}else{
    $scheme = '';
}

?>

<style>
.modal-open .modal {
    overflow-x: hidden;
    overflow-y: scroll;
}
</style>

<div class="modal-dialog modal-lg" role="document" id="add_modal" style="overflow-x: hidden; overflow-y: hidden; width: 70%;">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title mt-0" id="myLargeModalLabel"><b>Create Customers Details</b></h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="margin-top: -25px;"><i class="fa fa-times"></i></button>
            </div>
            
            <form id="form_detailss">
            <div id="content-divv">
            
            
            <div class="modal-body" style="padding: 20px 40px 20px 40px;">
                
                    <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="customer_name">Customer Name</label>                                    
                                        <input type="text" name="customer_name" value="" placeholder="Enter the Customer Name" class="form-control" id="customer_name"  required="required" />
                                    </div>
                                </div>
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="mobile_no">Mobile Number</label> <span id="error_messages" style="color: red; font-weight: 600;"></span>                                     
                                        <input type="text" name="mobile_no" value="" placeholder="Enter the Mobile Number" class="form-control" id="mobile_no"  required="required"  onkeypress="if(this.value.length==10) return false; return isNumber(event);" />
                                    </div>
                                </div>
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="required" for="city_town">City/Town</label>
                                        <input type="text" name="city_town" value="" class="form-control" id="city_town" placeholder="City/Town">
                                    </div>
                                </div>
                           
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="gender">Gender</label>
                                        <select name="gender" id="gender" class="form-control select2">
                                            <option value="Male">Male</option>
                                            <option value="Female">Female</option>
                                        </select>
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="optional" for="alter_mobile_no">Alter Mobile Number</label>                                    
                                        <input type="text" name="alter_mobile_no" value="" placeholder="Alter Mobile Number" class="form-control" id="alter_mobile_no"   />
                                    </div>
                                </div>
                                 
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="dob">Date of Birth</label>
                                        <input type="date" name="dob" class="form-control" id="dob">
                                    </div>
                                </div>
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="wedding_date">Wedding Date</label>
                                        <input type="date" name="wedding_date" class="form-control" id="wedding_date">
                                    </div>
                                </div>
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="religion">Religion </label>
                                        <select name="religion" id="religion" class="form-control select2">
                                            <option value="">Select Religion</option>
                                            <option value="Hindu">Hindu</option>
                                            <option value="Muslim">Muslim</option>
                                            <option value="Christian">Christian</option>
                                        </select>
                                    </div>
                                </div>
                                
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="pan_no">Pan Number</label>
                                        <input type="text" name="pan_no" class="form-control" id="pan_no" placeholder="Pan Number">
                                    </div>
                                </div>
                                
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="aadhaar_no">Aadhaar Number</label>
                                        <input type="text" name="aadhaar_no" class="form-control" id="aadhaar_no" placeholder="Aadhaar Number" onkeypress="if(this.value.length==12) return false;return isNumber(event);">
                                    </div>
                                </div>
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="gst_no">GST Number</label>
                                        <input type="text" name="gst_no" class="form-control" id="gst_no" value="" placeholder="GST Number">
                                    </div>
                                </div>
                                
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="optional" for="address">Address</label>
                                        <textarea type="text" name="address" rows="2" class="form-control" id="address" placeholder="Address"></textarea>
                                    </div>
                                </div>
                                
                                
                                <div class="col-lg-3">
                                    <div class="form-group mb-4">
                                        <label class="required" for="customer_state">State</label>
                                        <select name="customer_state" class="form-control select2" style="width:100%" id="customer_state">
                                            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_state` WHERE status=1 order by state asc");
                                                $statement->execute();
                                                $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                foreach ($resultsss as $ress) { ?>
                                                    <option value="<?=$ress['state']?>" <?= ($ress['state']=="Tamil Nadu") ? "selected":"" ?>><?=$ress['state']?></option>
                                            <?php  }  ?>
                                        </select>
                                    </div>
                                </div>
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="pincode">Pincode</label>                                    
                                        <input type="text" name="pincode" value="" placeholder="Enter the Pincode" class="form-control" id="pincode"  required="required"  onkeypress="if(this.value.length==6) return false;return isNumber(event);"/>
                                    </div>
                                </div>
                                
                                
                          
                        
                    </div>
                    
                    <div class="row filter" style="background: darkcyan; color: white; padding: 10px; border-radius: 5px; margin: 5px; cursor: pointer; margin-bottom: 29px;">
                        <b>Nominee Details (Show/Hide) </b> <span style="float: right; padding-right: 10px;"><i class="fa fa-caret-down" style="font-size: 18px;"></i></span>
                    </div>
            
                    <div class="row" id="filter">
                        <div class="col-lg-3">
                            <div class="form-group mb-4">
                                <label class="optional" for="nominee_name">Nominee Name</label>
                                <input type="text" name="nominee_name" class="form-control" id="nominee_name" value="" placeholder="Nominee Name">
                            </div>
                        </div>
                        
                        <div class="col-lg-3">
                            <div class="form-group mb-4">
                                <label class="optional" for="nominee_mobile_no">Nominee Mobile No</label>
                                <input type="text" name="nominee_mobile_no" class="form-control" id="nominee_mobile_no" value="" placeholder="Nominee Mobile No" onkeypress="if(this.value.length==10) return false;return isNumber(event);">
                            </div>
                        </div>
                        
                        <div class="col-lg-3">
                            <div class="form-group mb-4">
                                <label class="optional" for="nominee_address">Nominee Address</label>&nbsp;&nbsp;<span class="btn btn-warning btn-xs" onclick="SameAs()"><i class="fa fa-eye"></i> Same As</span>
                                <textarea type="text" name="nominee_address" rows="5" class="form-control" id="nominee_address" placeholder="Nominee Address"></textarea>
                            </div>
                        </div>
                        
                        
                        <div class="col-lg-3">
                            <div class="form-group mb-4">
                                <label class="optional" for="nominee_type">Nominee Type</label>
                                <select name="nominee_type" class="form-control select2" style="width:100%" id="nominee_type">
                                    <option value="">Nominee Type</option>
                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_nominee_types` WHERE status=1");
                                        $statement->execute();
                                        $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                        foreach ($resultsss as $ress) { ?>
                                            <option value="<?=$ress['name']?>"><?=$ress['name']?></option>
                                    <?php  }  ?>
                                </select>
                            </div>
                        </div>
                        
                    </div>
                    
                    <?php if(!empty($scheme) && $scheme == 'yes'){  ?>
                
                        <div class="row bank_details" style="background: #3c8dbc; color: white; padding: 10px; border-radius: 5px; margin: 5px; cursor: pointer; margin-bottom: 29px;">
                            <b>Bank Details (Show/Hide) </b> <span style="float: right; padding-right: 10px;"><i class="fa fa-caret-down" style="font-size: 18px;"></i></span>
                        </div>
                        
                        <div class="row" id="bank_details">
                                        <div class="col-lg-3">
                                            <div class="form-group mb-4">
                                                <label class="optional" for="bank_name">Bank Name</label>
                                                <input type="text" name="bank_name" class="form-control" id="bank_name" value="" placeholder="Bank Name">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group mb-4">
                                                <label class="optional" for="bank_account_holder_name">Bank A/C Holder Name</label>
                                                <input type="text" name="bank_account_holder_name" class="form-control" id="bank_account_holder_name" value="" placeholder="Bank Account Holder Name">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group mb-4">
                                                <label class="optional" for="account_no">Account Number</label>
                                                <input type="text" name="account_no" class="form-control" id="account_no" value="" placeholder="Account Number">
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group mb-4">
                                                <label class="optional" for="ifsc_code">IFSC Code</label>
                                                <input type="text" name="ifsc_code" class="form-control" id="ifsc_code" value="" placeholder="IFSC Code">
                                            </div>
                                        </div>
                                        
                                         <div class="col-lg-3">
                                            <div class="form-group mb-4">
                                                <label class="optional" for="branch">Branch</label>
                                                <input type="text" name="branch" class="form-control" id="branch" value="" placeholder="Branch">
                                            </div>
                                        </div>
                                        
                                    </div>
                            
                     <?php } ?>       
               

            </div>
            
            <div class="modal-footer">
                <button type="submit" id="customer_submit" class="btn btn-primary">Create</button>
            </div>
            
            
            </div>
            </form>
            
            
            
            <div id="loading-divv" style="display: none; padding-bottom: 80px;">
                    <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
            </div>
            
            
            
        </div>
    </div>
    
<script>

function SameAs(){
    var customer_address = $("#address").val();
    if(customer_address != ''){
      $("#nominee_address").val(customer_address);  
    }else{
      toastr.error("Please Enter Customer Address. "); 
    } 
}


$(document).ready(function(){
   $('#filter').hide();
    $('.filter').click(function(){
        $("#filter").slideToggle();
        return false;
    });
    
    $('#bank_details').hide();
    $('.bank_details').click(function(){
        $("#bank_details").slideToggle();
        return false;
    });
});


$('#form_detailss').on('submit', function(e){
                e.preventDefault();
                // $('#loading-divv').show();
                $('#content-divv').hide();
                $("#customer_submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_customers');
                    $.ajax({
                            url: 'apis/customers.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']);*/
                                     toastr.success(response['message']);
                                     $("#add_modal .close").click();
                                     $('#form_details').trigger("reset");
                                     $('#loading-divv').hide();
                                     $('#content-divv').show();
                                     $('#customer_id').append('<option value='+response['customer_id']+'>'+response['customer_name']+' - '+response['mobile_no']+'</option>');
                                     $('#customer_id').val(response['customer_id']).trigger('change');
                                     
                                     
                                }else{
                                     toastr.error(response['message']);
                                    $("#customer_submit").prop('disabled', false);
                                    $('#loading-divv').hide();
                                    $('#content-divv').show();
                                }
                            }
                        });
   });


$(document).on('keyup', '#mobile_no', function() {
        MobileNoValidation();
});

function MobileNoValidation(){
    var mobile_no = $("#mobile_no").val();
    if(mobile_no.length !== 10){
        $("#error_messages").text("(Mobile number must be 10 digits long.)");
        $("#customer_submit").prop('disabled', true);
    } else {
        $("#error_messages").text("");
        $("#customer_submit").prop('disabled', false);
    }
}
</script>
