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

<div class="content-wrapper">
        <section class="content-header">
            <h1>Add Expenses</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add Expenses</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">
                                                                     
                            <div class="row">
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="date">Date</label>                                    
                                        <input type="date" name="date"  class="form-control" id="date"  value="<?=date('Y-m-d')?>" required="required" />
                                    </div>
                                </div>
                                
                                <div class="col-md-2">
                                    <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']?>" <?php if(isset($datas) && $datas[0]['location_id']==$result['id']){ echo "selected";}?> ><?=$result['location_name']?></option>
                                            <?php  }  ?>
                                        </select>
                                    </div>
                                </div>
                                
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <span class="btn btn-primary btn-xs" style="margin-top: 20px;" id="add_new_row"><i class="fa fa-plus"></i> Add Row</span>
                                    </div>
                                </div>
                                
                                
                                
                                
                               
                                
                                
                                
                            </div>
                            
                            
                            <div class="row porow" style="margin-top: 20px;">
                            
                            
                            <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="expenses_name">Expenses Name</label>                                    
                                        <input type="text" name="expenses_name[]" value="" placeholder="Enter the Expenses Name" class="form-control" id="expenses_name"  required="required" />
                                    </div>
                            </div>
                            
                            <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="date">Date</label>                                    
                                        <input type="date" name="date[]"  class="form-control" id="date"  value="<?=date('Y-m-d')?>" required="required" />
                                    </div>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                     <label class="required" for="exp_category">Category</label>
                                    <select name="exp_category[]" class="form-control select2" id="exp_category" style="width: 100%;" required="required" >
                                        <option value="">Select Category</option>
                                        <?php 
                                        $statement = $pdo->prepare("SELECT * FROM `tbl_exp_category` 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="required" for="paid_amount">Amount</label>     
                                    <input type="text" name="paid_amount[]"  placeholder="Enter the Amount" class="form-control paid_amount" required="required" id="paid_amount" onkeypress="return isNumberKey(this, event);" />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                     <label class="required" for="payment_type">Payment Type</label>
                                    <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</label>                                   
                                    <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label>                                   
                                    <textarea name="notes[]" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                </div>
                            </div>
                            
                            
                           
                        </div>
                        
                        <div id="append_sections"></div>
                            
                            
                            
    
                            <div class="form-group" style="text-align:center; margin-top: 20px;">
                                <input type="submit"  id="submit" value="Add Expenses" class="btn btn-success" />
                            </div>
                            
                        </form>
                        
                        
                        <div class="form-group" style="text-align:center; margin-top: 10px;">
                            <a href="expenses_add.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                            <a href="expenses_list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                         </div>
    
                        </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;">Expenses Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="expenses_add.php"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="expenses_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"; ?>

<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_expenses');
                    $.ajax({
                            url: 'apis/expenses.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){
                                    toastr.success(response['message']);
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                    //  $('#success-div').show();
                                     $('#loading-div').hide();
                                     setTimeout(function(){ window.location.href="expenses_list.php"; },1500);
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>


<style>
    .porow{
        margin-top: 20px;
        background: #e7e1e1;
        padding: 10px 0px 10px 0px;
        box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;
    }
</style>

<script>
$(document).ready(function() {
    
var count = 1;
$("#add_new_row").click(function(){
    
    var date = $("#date").val();
    
    var row=`<div class="row porow" style="margin-top: 20px;">
                            
                            
                            <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="expenses_name">Expenses Name</label>                                    
                                        <input type="text" name="expenses_name[]" value="" placeholder="Enter the Expenses Name" class="form-control" id="expenses_name"  required="required" />
                                    </div>
                            </div>
                            
                            <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="required" for="date">Date</label>                                    
                                        <input type="date" name="date[]"  class="form-control" id="date"  value="`+date+`" required="required" />
                                    </div>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                     <label class="required" for="exp_category">Category</label>
                                    <select name="exp_category[]" class="form-control select2" id="exp_category" style="width: 100%;" required="required" >
                                        <option value="">Select Category</option>
                                        <?php 
                                        $statement = $pdo->prepare("SELECT * FROM `tbl_exp_category` 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="required" for="paid_amount">Amount</label> 
                                    <input type="text" name="paid_amount[]"  placeholder="Enter the Amount" class="form-control paid_amount" required="required" id="paid_amount" onkeypress="return isNumberKey(this, event);" />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                     <label class="required" for="payment_type">Payment Type</label>
                                    <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</label>                                   
                                    <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label>                                   
                                    <textarea name="notes[]" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                </div>
                            </div>
                            
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <span class="btn btn-danger btn-md remove_row" style="margin-top: 20px;" id=""><i class="fa fa-times"></i></span>
                                </div>
                            </div>
                            
                            
                           
                        </div>`;
        
        $("#append_sections").append(row);
        $(".select2").select2();
        count++;
});
});

 $(document).on('click', '.remove_row', function() {
        $(this).closest('.porow').remove();
    });
</script>
