<?php Fun::lang() ?>

<?php $__env->startSection('content'); ?>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Operation</a></li>
            <li><a href="#">Accounting</a></li>
            <li><a href="#">Add Journal</a></li>
        </ul>
    </div>
</div>
<div class="page">
    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-add">
                    <div class="pull-left">
                        <h3>Create Journal</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">
            <div class="form-wrapper">
                <?php echo Form::open(array('route' => 'journal.store','method'=>'POST')); ?>


                <div class="row">
                    <div class="form-group col-sm-6 col-xs-12" id="available-box">
                        <label class="required">Date</label>
                        <?php echo Form::text('date', date('d-m-Y'), array('placeholder' => 'Date','class' => 'form-control date_picker','id' => 'date','required')); ?>

                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Voucher Type</label>
                        <select name="voucher_type" id="voucher_type" class="select2" required>
                            <option value="">Please Select</option>
                            <option value="1">Cash Receipt</option>
                            <option value="2">Cash Disbursement</option>
                            <option value="3">Cash Reimbursement</option>
                            <option value="4">Cash Advance</option>
                            <option value="5">Advance Settlement</option>
                            <option value="6">Journal Entry/Adjustment</option>
                        </select>
                    </div>
                    <div class="form-group  col-sm-6 col-xs-12">
                        <label for="required">Select Project</label>
                        <?php echo Form::select('journal_by_project', $projects, null, ['class' => 'form-control select2','id'=>'project_id']); ?>

                    </div>
                    <div class="form-group  col-sm-6 col-xs-12">
                        <label for="required">Select Supplier</label>
                        <?php echo Form::select('journal_by_supplier', $supplier, null, ['class' => 'form-control select2','id'=>'supplier_id']); ?>

                    </div>
                    <div class="form-group  col-sm-6 col-xs-12">
                        <label>Reference Number</label>
                        <?php echo Form::text('journal_reference_number', null, array('placeholder' => 'Reference Number','class' => 'form-control', 'id' => 'voucher_ref_number')); ?>

                    </div>

                    <div class="form-group col-sm-6 col-xs-12" id="available-box">
                        <label class="required">Voucher Ref</label>
                        <?php echo Form::text('voucher_ref', $voucher_ref, array('placeholder' => 'Voucher Ref','class' => 'form-control', 'id' => 'voucher_ref','readonly','required')); ?>

                    </div>
                    <div class="form-group col-sm-6 col-xs-12" id="available-box">
                        <label class="">Description</label>
                        <?php echo Form::textarea('description', null, ['id' => 'description', 'rows' => 4, 'cols' => 54, 'class' => 'form-control']); ?>

                    </div>
                </div>
                <hr>
                <div class="row">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped table table-condensed table-hover">
                            <thead>
                                <tr>
                                    <th width="20%">Account Type</th>
                                    <th width="20%">Account Name</th>
                                    <th width="20%">Account Name</th>
                                    <th>Ref Number</th>
                                    <th>Debit</th>
                                    <th>Credit</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody id="tbody">
                                <tr>
                                    <td><?php echo Form::select('acc_type[]', $acc_type, null, ['class' => 'form-control select2 account_code','required']); ?></td>
                                    <td><select name="account_code[]" id="" class="select2 account_code" required></select></td>
                                    <td><select name="account_code1[]" id="" class="select2 account_code"></select></td>
                                    <td><input type="text" name="ref_number[]" class="form-control" placeholder="Reference Number" required></td>
                                    <td><input type="text" name="debit[]" value="0" class="form-control" placeholder="Debit" required></td>
                                    <td><input type="text" name="credit[]" value="0" class="form-control" placeholder="Credit" required></td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td><?php echo Form::select('acc_type[]', $acc_type, null, ['class' => 'form-control select2 account_code','required']); ?></td>
                                    <td><select name="account_code[]" id="" class="select2 account_code" required></select></td>
                                    <td><select name="account_code1[]" id="" class="select2 account_code"></select></td>
                                    <td><input type="text" name="ref_number[]" class="form-control" placeholder="Referece Number" required></td>
                                    <td><input type="text" name="debit[]" value="0" class="form-control" placeholder="Debit" required></td>
                                    <td><input type="text" name="credit[]" value="0" class="form-control" placeholder="Credit" required></td>
                                    <td><button class="btn btn-info btn-sm btn-add" type="button"><i class="fa fa-plus"></i></button></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <button type="submit" class="btn btn-sm btn-warning pull-right"><i class="fa fa-save"></i> Save</button>
                    </div>
                </div>
                <?php echo Form::close(); ?>

            </div>
        </div>
    </div>
</div>

<div class="modal fade bs-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h3 class="modal-title text-danger">Warning...!</h3>
            </div>
            <br />
            <div class="row">
                <div class="col-md-12">
                    <div class="col-md-10">
                        <h4>Receivable amount must be smaller than or equal available amount.</h4>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" class="button_primary"><i class="fa fa-close"></i> Close</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('js'); ?>
<script type="text/javascript">
    $(document).on('click', '.btn-add', function() {
        $('select').select2('destroy');
        $('#tbody').append(add_journal());
        $('.select2').select2();
    });
    $(document).on('change', '.account_code', function() {
        var obj     = $(this).find(":selected").val();
        var element = $(this);
        if (obj    != "") {
            account_code(obj,element);
        }
        else{
            element.parent().next().find('.account_code').empty();
        }
    });
    $(document).on('click', '.btn-remove', function() {
        $(this).parent().parent().remove();
    });

    function account_code(obj,element) {
        var div = "";
        $.ajax({
            url: "<?php echo e(route('search-account-code')); ?>",
            method: "POST",
            dataType: "json",
            data: {
                obj: obj,
                _token: '<?php echo e(csrf_token()); ?>',
            },
            success: function(data) {
                if(Object.keys(data).length){
                    element.parent().next().find('.account_code').empty().append(`<option value=""> Please Select </option>`);
                    $.each(data, function(index, value){
                        element.parent().next().find('.account_code').append($("<option></option>").attr("value",value.id).text(value.id +" | "+value.acc_name)); 
                    });
                }
                else{
                    element.parent().next().find('.account_code').empty();
                }  
            }
        });
    }

    function add_journal() {
        var tr = `<tr>
                    <td><?php echo Form::select('acc_type[]', $acc_type, null, ['class' => 'form-control select2 account_code','required']); ?></td>
                    <td><select name="account_code[]" id="" class="select2 account_code" required></select></td>
                    <td><select name="account_code1[]" id="" class="select2 account_code"></select></td>
                    <td><input type="text" name="ref_number[]" class="form-control" placeholder="Reference Number" required></td>
                    <td><input type="text" name="debit[]" value="0" class="form-control" placeholder="Debit" required></td>
                    <td><input type="text" name="credit[]" value="0" class="form-control" placeholder="Credit" required></td>
                    <td><button class="btn btn-danger btn-sm btn-remove"><i class="fa fa-trash"></i></button></td>
                </tr>`;
        return tr;
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>