<?php Fun::lang() ?>
@extends('layouts.app-accounting')
@section('content')
    <div class="page">
        <div class="container">
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3>{{__('administrator.create_journal')}}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {!! Form::open(array('route' => 'journal.store','method'=>'POST')) !!}
            <div class="row">
                <div class="col-sm-12">
                    <div class="master-form">
                        <div class="title-note">
                            <h3><span class="fa fa-exclamation-triangle"></span> {{__('administrator.create')}}
                                <i>({{__('administrator.fields_in_red_are_required')}})</i></h3>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="required" for="name">{{__('administrator.date')}} </label>
                            </div>
                            <div class="col-sm-9">
                                {!! Form::text('date', date('d-m-Y'), array('placeholder' => 'Date','class' => 'form-control date_picker','id' => 'date','required')) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="required" for="name">{{__('administrator.voucher_type')}}</label>
                            </div>
                            <div class="col-sm-9">
                                {!! Form::select('voucher_type', getVoucherType() , null , ['class' => 'form-control select2','id'=>'voucher_type']) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="required" for="name">{{__('administrator.supplier')}}</label>
                            </div>
                            <div class="col-sm-9 col-xs-12">
                                {!! Form::select('journal_by_supplier', $supplier, null, ['class' => 'form-control select2','id'=>'supplier_id']) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="" for="name">{{__('administrator.reference_number')}}</label>
                            </div>
                            <div class="col-sm-9 col-xs-12">
                                {!! Form::text('journal_reference_number', null, array('placeholder' => 'Reference Number','class' => 'form-control', 'id' => 'voucher_ref_number')) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="" for="name">{{__('administrator.voucher_ref')}}</label>
                            </div>
                            <div class="col-sm-9 col-xs-12">
                                {!! Form::text('voucher_ref', $voucher_ref, array('placeholder' => 'Voucher Ref','class' => 'form-control', 'id' => 'voucher_ref','readonly','required')) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="col-sm-3 col-xs-12 ">
                                <label class="" for="name">{{__('administrator.description')}}</label>
                            </div>
                            <div class="col-sm-9 col-xs-12">
                                {!! Form::textarea('description', null, ['id' => 'description', 'rows' => 4, 'cols' => 54, 'class' => 'form-control']) !!}
                            </div>
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <table class="table table-bordered table-striped table table-condensed table-hover">
                                <thead>
                                <tr>
                                    <th width="20%">{{__('administrator.account_type')}}</th>
                                    <th width="20%">{{__('administrator.account_name')}}</th>
                                    <th width="20%">{{__('administrator.account_name')}}</th>
                                    <th>{{__('administrator.ref_number')}}</th>
                                    <th>{{__('administrator.debit')}}</th>
                                    <th>{{__('administrator.credit')}}</th>
                                    <th>{{__('administrator.action')}}</th>
                                </tr>
                                </thead>
                                <tbody id="tbody">
                                <tr>
                                    <td>{!! 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>{!! 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 class="col-sm-12 col-xs-12">
                            <div class="form-group footer-save">
                                <button type="submit" class=" button_primary pull-right"><span
                                            class="fa fa-save"></span> {{__('administrator.save')}}</button>
                                <button type="button" onClick="history.go(-1)"
                                        class=" button_warning pull-right">
                                    <span class=" fa fa-arrow-circle-left"></span> {{__('administrator.back')}}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {!! Form::close() !!}
        </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>

@endsection

@section('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: "{{ route('search-account-code')}}",
                method: "POST",
                dataType: "json",
                data: {
                    obj: obj,
                    _token: '{{ 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>{!! 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>
@endsection
