<?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.trial_balance')}}</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.trial-balance')}}" method="POST">
                @csrf
                {{method_field('POST')}}
                <div class="title-bar col-sm-12">
                    <?php
                    $start  = new DateTime('first day of this month');
                    $from   = $start->format('d-m-Y');
                    ?>
                        <br>
                    <span class="title-tab">
                        <span class="text-tab"> Trial Balance >> <span id="start_date">{{$from}}</span> >> <span id="end_date">{{date('d-m-Y')}}</span> </span>
{{--                        @can('Trial-Balance-Print')--}}
                        <button type="submit" class="btn btn-tab btn-xs btn-warning pull-right"><i class="fa fa-print"></i></button>
                        {{--@endcan--}}
                        <span class="btn-toggle btn btn-primary btn-xs pull-right" style="margin-left:5px"><i class="fa fa-caret-down"></i></span>
                        <input type="hidden" name="report" value="Trial Balance">
                    </span>
                    <!-- <button type="submit" class="pull-right btn btn-xs btn-warning" data-toggle="tooltip" data-placement="top" title="Print Voucher"><i class="fa fa-print" aria-hidden="true"></i></button> -->
                </div>

                <div class="row">
                    <div class="col-sm-12 toggle-div" style="display:none">

                        <div class="col-sm-12">
                            <hr>
                            <div class="col-sm-5">
                                <label for="">Start Date</label>
                                <div class="form-group">
                                    <input type="text" name="from" class="form-control date_picker start_date">
                                </div>
                            </div>
                            <div class="col-sm-5">
                                <label for="">End Date</label>
                                <div class="form-group">
                                    <input type="text" name="to" class="form-control date_picker end_date">
                                </div>
                            </div>
                            <div class="col-sm-2">
                                <label for="">&nbsp;</label>
                                <div class="form-group">
                                    <button type="button" class="btn-warning btn btn-md" onclick="search()"><i class="fa fa-search"></i> Search</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="row" style="margin-bottom: 5px">
        <div class="col-sm-12 col-xs-12">

            <div class="pull-right">
{{--                @can('Accounting-Report-Trail-Balance-Run-Audit')--}}
                <button class="btn btn-primary btn-sm" id="run_audit" data-toggle="modal" data-target="#form-run-audit">{{__('administrator.run_audit')}}</button>
                {{--@endcan--}}
            </div>
        </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_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>
@include('Accounting.reports.confirm-run-audit')
@endsection

@section('js')
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url: "{{ route('accounting-report.get-trail-balance')}}",
            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();
        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-trail-balance')}}",
                method: "POST",
                dataType: "text",
                data: {
                    from: from,
                    to: to,
                    _token: '{{ csrf_token() }}',
                },
                success: function(data) {
                    $('#start_date').text(from);
                    $('#end_date').text(to);
                    $('#tbody').empty();
                    $('#tbody').append(data);
                }
            });
        }
    }
    $(document).on('click','#run_audit',function () {
        var jn_id = [];
        $('.clickable-row').each(function () {
            var row = $(this).data('value');
            jn_id.push(row);
        });
        $('#jn-id').val(jn_id);
    });
</script>
@endsection
