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


<div class="content-wrapper">
        <section class="content-header">
            <h1>Add Customer Scheme</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add 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">
    
    <?php if($session_id==1 || $session_user_type==1 || $allPermissions['customer_scheme']['create']==1){ ?>
    <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-body">
                    
                    
                <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                                                                 
                        <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">
                                        <option value="">Select Customer</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customers` where status=1");
                                            $statement->execute();
                                            $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-1" style="text-align: center;">
                                    <a href="modal/customers-create.php?scheme=<?php echo base64_encode('yes') ?>" data-toggle="ajax-modal" title="Add Customers" class="tip btn btn-success btn-xs" id="" style="margin-top: 23px; padding: 5px;"><i class="fa fa-plus-circle" aria-hidden="true" style="font-size:22px;"></i></a>
                            </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">
                                        <option value="">Select Scheme</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_schemes` where status=1");
                                            $statement->execute();
                                            $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="scheme_amount">Amount</label>                                   
                                    <input type="text" name="scheme_amount" class="form-control" style="width:100%" id="scheme_amount" required="required" onkeypress="return isNumberKey(this, event);">
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="sales_man_id">Sales Man</label>                                   
                                    <select name="sales_man_id" class="form-control select2" style="width:100%" id="sales_man_id" required="required">
                                        <option value="">Select Employees</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_users` where status=1 and id!=0");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?php if($session_id==$result['id']){echo "selected";}?>><?=$result['name']?> - <?=$result['emp_code']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="location_id">Locations</label>                                   
                                    <select name="location_id" class="form-control select2" style="width:100%" id="location_id" required="required">
                                        <?php 
                                            $statement = $pdo->prepare("SELECT bb.* FROM `tbl_business_branch` bb WHERE bb.status=1 $branch_conditions");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"><?=$result['location_name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                           
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Add 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 Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span class="btn btn-info" onclick="location.reload();"><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>
                            <!-- &nbsp;&nbsp;-->
                            <!--<a href="customers-scheme-print.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Print</button></a>-->
                        </center>
                    </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
    
<?php }else{ ?>

<?=$page_not_access?>

<?php } ?>
</section>


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


<script>
$(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', 'create_customer_scheme');
                    $.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){
                                    $('#form_details').trigger("reset");
                                    toastr.success(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#success-div').show();
                                    $('#loading-div').hide();
                                    setTimeout(function(){window.open("customers-scheme-print.php?id=" + response['scheme_id'], '_blank');},1500);
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>