<?php Fun::lang() ?>
@extends('layouts.app-accounting')

@section('content')

<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>{{__('administrator.balance_sheet')}}</h3>
                    </div>
                    <div class="pull-right">
                        <!-- <a href="{{route('journal.create')}}" class="btn btn-info fa fa-plus"> Add New</a> -->
                    </div>
                </div>
            </div>
            <form action="{{route('print.balance-sheet')}}" method="POST">
                @csrf
                {{method_field('POST')}}
                <div class="title-bar col-sm-12">
                    <?php
                    $start  = new DateTime('first day of this month');
                    $end    = new DateTime('last day of this month');
                    $from   = $start->format('d/m/Y');
                    $to     = $end->format('d/m/Y');
                    ?>
                    <span class="title-tab">
                        <span class="text-tab"> Balance Sheet >> <span id="start_date">{{$from}}</span> >> <span id="end_date">{{$to}}</span> </span>
                        <span class="btn-toggle btn btn-primary btn-xs pull-right" style="margin-left:5px"><i class="fa fa-caret-down"></i></span>
{{--                        @can('Balance-Sheet-Print')--}}
                        <button type="submit" class="btn btn-tab btn-xs btn-warning pull-right"><i class="fa fa-print"></i></button>
                        {{--@endcan--}}
                        <input type="hidden" name="report" value="Balance Sheet">
                    </span>
                </div>
                <div class="row col-sm-12">
                    <div class="col-sm-12 toggle-div" style="display:none">
                        <hr>
                        {{--<div class="form-group  col-sm-4 col-xs-4">--}}
                            {{--<label>Select Project</label>--}}
                            {{--{!! Form::select('journal_by_project', $projects, null, ['class' => 'form-control select2','id'=>'project_id']) !!}--}}
                        {{--</div>--}}
                        <div class="col-sm-4">
                            <label for="">Start Date</label>
                            <div class="form-group">
                                <input type="text" name="from" class="form-control start_date date_picker">
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <label for="">End Date</label>
                            <div class="form-group">
                                <input type="text" name="to" id="" class="form-control end_date date_picker">
                            </div>
                        </div>
                        <div class="col-sm-12" style="text-align: right">
                            <label for="">&nbsp;</label>
                            <div class="form-group">
                                <button type="button" class="btn btn-warning btn-md" onclick="search()"><i class="fa fa-search"></i> Search</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">
            <div class="form-wrapper">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="table-responsive">
                            <table id="table_journal" class="table table-bordered table-striped table table-condensed table-hover">
                                <thead>
                                    <tr>
                                        <th>{{__('administrator.account_code')}}#</th>
                                        <th>{{__('administrator.account_name')}}</th>
                                        <th>{{__('administrator.debit')}}</th>
                                        <th>{{__('administrator.credit')}}</th>
                                    </tr>
                                </thead>
                                <tbody id="tbody"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </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>

@endsection

@section('js')
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url: "{{ route('accounting-report.get-balance-sheet')}}",
            method: "get",
            dataType: "text",
            data: {
                _token: '{{ csrf_token() }}',
            },
            success: function(data) {
                $('#tbody').append(data);
            }
        });
    });
    $(document).on('click', '.clickable-row', function() {
        window.location = $(this).data("href");
    });
    $(document).on('click', '.btn-toggle', function() {
        $('.toggle-div').toggle('fast');
    });

    function search() {
        var from = $('.start_date').val();
        var to = $('.end_date').val();
        var project = $('#project_id').val();
        if (from == "" || to == "") {
            $.alert({
                title: 'Error',
                icon: 'fa fa-warning',
                type: 'orange',
                content: 'Something went wrong, please retry again.' +
                    '<hr>' +
                    'Note: No Date Selected',
            });
            return false;
        } else {
            $.ajax({
                url: "{{ route('accounting-report.get-balance-sheet')}}",
                method: "POST",
                dataType: "text",
                data: {
                    from: from,
                    to: to,
                    project: project,
                    _token: '{{ csrf_token() }}',
                },
                success: function(data) {
                    $('#start_date').text(from);
                    $('#end_date').text(to);
                    $('#tbody').empty();
                    $('#tbody').append(data);
                }
            });
        }
    }
</script>
@endsection
