<style>
    /* enable absolute positioning */
    .inner-addon { 
        position: relative; 
    }

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

    /* align icon */
    .left-addon .glyphicon  { left:  0px;}
    .right-addon .glyphicon { right: 0px;}

    /* add padding  */
    .left-addon input  { padding-left:  30px; }
    .right-addon input { padding-right: 30px; }

    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('<?php echo base_url(); ?>assets/images/page-loader-1.gif') 50% 50% no-repeat ;
        display: none;
    }
</style>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <button type="button"  onclick="openaddnew();" class="btn btn-custom waves-effect waves-light" >Create New <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                </div>
                <h4 class="page-title">Cash Expenses:</h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <!--<h4 class="header-title m-t-0 m-b-30">Selection:</h4>-->
                    <div class="row">
                        <div class="col-lg-12">
                         <!----Form --->
                            <div class="row">
                                <div class="col-lg-6">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label for="addamount" class="control-label">Amount</label>
                                                <input class="form-control decimal" name="addamount" id="addamount" />
                                            </div> 
                                        </div> 
                                    </div>
                                    
                                </div>
                                <div class="col-lg-6">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label for="adddate" class="control-label">Date</label>
                                                <input type="text" class="form-control" placeholder="Date" id="adddate" name="adddate">
                                            </div> 
                                        </div> 
                                    </div>

                                </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="adddescription" class="control-label">Description</label>
                                    <textarea class="form-control" name="adddescription" id="adddescription"></textarea>
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                        <div class="col-lg-3">
                            <div class="form-group">
                                <label for="debit_account_head" class="control-label">Expense Type</label>
                                <select name="debit_account_head" id="debit_account_head" class="form-control">
                                    <?php foreach($expense_accounts as $ah){ ?>                                        
                                    <option value="<?php echo $ah['id_account_heads']; ?>"><?php echo $ah['account_head']; ?></option>                                        
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                            <div class="col-lg-3">
                            <div class="form-group">
                                <label for="credit_account_head" class="control-label">Incurred From</label>
                                <select name="credit_account_head" id="credit_account_head" class="form-control">
                                    <?php foreach($cash_account as $ah){ ?>
                                    <option value="<?php echo $ah['id_account_heads']; ?>"><?php echo $ah['account_head']; ?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>
                            <div class="col-lg-3">
                            <div class="form-group">
                                <label for="supplier" class="control-label text-danger">Supplier</label>
                                <select name="supplier" id="supplier" class="form-control">
                                    <?php foreach($suppliers as $supplier){ ?>
                                    <option <?php if(strtolower($supplier['supplier_name']) =='other'){echo 'selected="selected" ';} echo ' value="'.$supplier['id_business_supplier'].'"'; ?>><?php echo $supplier['supplier_name'].' '.$supplier['contact_person']; ?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>
                        <div class="col-lg-3">
                            <div class="form-group">
                                <label for="staff" class="control-label text-danger">Staff</label>
                                <select name="staff" id="staff" class="form-control">
                                    <option value="0">Staff</option>
                                    <?php foreach($staff as $s){ ?>
                                    <option value="<?php echo $s->id_staff; ?>"><?php echo $s->staff_fullname; ?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>
                    </div>    
                    <div class="row">
                        <div class="col-md-12 ">
                            <button onclick="addrow();" class="btn  btn-custom waves-effect waves-light pull-right"><i class="fa fa-arrow-down"></i> Add</button>
                        </div>
                    </div>                
                </div>
            </div>
        </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">
                    <h4 class="header-title m-t-0 m-b-30">&nbsp;</h4>
                    <table id="tblexpenses" class="table table-striped table-bordered dt-responsive nowrap record" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th>Row</th>
                                <th>Desctiption</th>
                                <th>From Account</th>
                                <th>Supplier/Staff</th>
                                <th>Expense Account</th>
                                <th>Expense Date</th>
                                <th>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                    <button onclick="savedata();" type="button" class="btn btn-custom waves-effect waves-light pull-right"><span>Save</span></button>
                </div>
            </div><!-- end col -->
        </div>
        <!-- end row -->

      
        
        <!--Edit Account Voucher Start-->
        <div id="editvoucher" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editvoucher" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Edit a Voucher</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="editdescription" class="control-label">Description</label>
                                    <textarea class="form-control" name="editdescription" id="editdescription"></textarea>
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="editamount" class="control-label">Amount</label>
                                            <input class="form-control" name="editamount" id="editamount" />
                                        </div> 
                                    </div> 
                                </div>
                                
                                <div class="row hidden">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="editstatus" class="control-label">Status</label>
                                            <select name="editstatus" id="editstatus" class="form-control">
                                                <option value="Active">Active</option>
                                                <option value="InProcess">In Process</option>
                                                <option value="Cancelled">Cancelled</option>
                                            </select>
                                        </div> 
                                    </div> 
                                </div>

                            </div>
                            <div class="col-lg-6">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="editdate" class="control-label">Date</label>
                                            <input type="text" class="form-control" placeholder="Date" id="editdate" name="editdate">
                                        </div> 
                                    </div> 
                                </div>

                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="hidden" name="editaccountvoucherid" id="editaccountvoucherid" />
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button onclick="updatevoucher();" type="button" class="btn btn-custom waves-effect waves-light orderloader"><span>Save</span></button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--Edit Account Voucher End-->

        <script type="text/javascript">

       

        $(".numeric").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });


        $(".decimal").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57) ) {

                return false;
            }
        });

            $(document).ready(function() {

                $("#debit_account_head").select2();
                $("#supplier").select2();
                $("#staff").select2();

                toastr.options = {
                    "closeButton": true,
                    "debug": true,
                    "newestOnTop": true,
                    "progressBar": true,
                    "positionClass": "toast-bottom-center",
                    "preventDuplicates": true,
                    "onclick": null,
                    "showDuration": "300",
                    "hideDuration": "1000",
                    "timeOut": "5000",
                    "extendedTimeOut": "1000",
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                }

                $('#adddate').datepicker({
                    autoclose: true,
                    todayHighlight: true,
                    format: 'yyyy-mm-dd'
                });            

            });
           
            function addnew(){
                
                if($("#adddate").val()==""){
                    return false;
                }
                
                var debit_amounts=$("#debit_account_head").val() +'|'+ $('#addamount').val();
                var credit_amounts=$("#credit_account_head").val() +'|'+ $('#addamount').val();
                var debit_accounts=[];
                var credit_accounts=[];
                debit_accounts.push(debit_amounts);
                credit_accounts.push(credit_amounts);
                

                var business_partner=2; business_partner_id=0; business_partner_name='';
                console.log($("#staff option:selected").val());
               if($("#staff option:selected").val() > 0){
                    business_partner=3;
                    business_partner_id= $("#staff option:selected").val();
                    business_partner_name= $("#staff option:selected").text();
               }else{
                    business_partner=2;
                    business_partner_id= $("#supplier option:selected").val();
                    business_partner_name= $("#supplier option:selected").text();
               }



                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url();?>accounting_controller/insert_account_voucher',
                    data: {
                        description: $('#adddescription').val(),
                        voucher_amount: $('#addamount').val(),                        
                        voucher_date: $('#adddate').val(),
                        business_partner: business_partner,
                        business_partner_id: business_partner_id,
                        business_partner_name: business_partner_name,
                        cost_center:1,
                        cost_center_name:'Outlet',
                        debit_accounts: debit_accounts,
                        credit_accounts: credit_accounts,
                        voucher_type: 1,
                        payment_mode: 'Cash'
                        
                    },
                    success: function(data){
                        var result = data.split("|");
                        if (result[0] === "success") {
                            toastr.success('Account Voucher Added!', 'Done!');
                            location.reload();
                        } else {
                            swal({
                                title: "Error",
                                text: result[1],
                                type: "error",
                                confirmButtonText: 'OK!'
                            });
                        }    
                    }
                });
            }
      

            function addrow(){

                if($("#addamount").val()=='' || $("#adddate").val()=='' || $("#adddescription").val()==''){

                    return false;
                }

                


                var html=''; var x=0;
                var rowCount = $('#tblexpenses tr').length;


                html+="<tr id="+rowCount+">";
                html+="<td row='"+rowCount+"' >"+rowCount+"</td>";
                html+="<td key='description' >"+$("#adddescription").val()+"</td>";

                
                html+="<td key='credit_account_head' value='"+$("#credit_account_head option:selected").val()+"'>"+$("#credit_account_head option:selected").text()+"</td>";
                if($("#staff").val()==0){
                    html+="<td key='supplier' bptype='2' value='"+$("#supplier option:selected").val()+"'>"+$("#supplier option:selected").text()+"</td>";    
                }else{
                    html+="<td key='staff' bptype='3' value='"+$("#staff option:selected").val()+"'>"+$("#staff option:selected").text()+"</td>";
                }
                
                html+="<td key='debit_account_head' value='"+$("#debit_account_head option:selected").val()+"'>"+$("#debit_account_head option:selected").text()+"</td>";
                html+="<td key='voucher_date' value='"+$("#adddate").val()+"'>"+$("#adddate").val()+"</td>";
                html+="<td key='voucher_amount' value='"+$("#addamount").val()+"'>"+$("#addamount").val()+"</td>";
                html+="<td key='remove' value=''><button onclick='removerow("+rowCount+");' class='btn-sm btn-danger' ><i class='fa fa-remove'></i></button></td>";
                $('#tblexpenses').append(html);

            }

            function removerow(row){
                var tr = $('tr[id="'+row+'"]');
                $(tr).remove();
            }

            function savedata(){
                var debit_accounts=[];
                var credit_accounts=[];

                var TableData = new Array();
                $('#tblexpenses tr').each(function(row, tr) {
                    TableData[row] = {
                        "credit_accounts": [$(tr).find('td:eq(2)').attr('value')+'|'+ $(tr).find('td:eq(6)').attr('value')]
                        ,"debit_accounts": [$(tr).find('td:eq(4)').attr('value')+'|'+ $(tr).find('td:eq(6)').attr('value')]
                        ,"business_partner_id": $(tr).find('td:eq(3)').attr('value')
                        ,"business_partner_name": $(tr).find('td:eq(3)').text()
                        ,"business_partner": $(tr).find('td:eq(3)').attr('bptype')                        
                        ,"voucher_date": $(tr).find('td:eq(5)').attr('value')
                        ,"voucher_amount": $(tr).find('td:eq(6)').attr('value')
                        ,"description": $(tr).find('td:eq(1)').text()
                        ,"cost_center":1
                        ,"cost_center_name":'Outlet'
                        ,"voucher_type":1
                        ,"payment_mode":'Cash'
                    }
                });

                TableData.shift();

                console.log(TableData);

                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url();?>accounting_controller/insert_multiple_vouchers',
                    data: {
                         'tableData' : TableData
                    },
                    success: function(data){
                        var result = data.split("|");
                        if (result[0] === "success") {
                            toastr.success('Account Voucher Added!', 'Done!');
                            location.reload();
                        } else {
                            swal({
                                title: "Error",
                                text: result[1],
                                type: "error",
                                confirmButtonText: 'OK!'
                            });
                        }    
                    }

                });

            }
        </script>
