@php Fun::lang() @endphp
@extends('adminlte::page')
@section('css')
    <style>
        /* .table-striped>tbody>tr:nth-child(odd){
            background-color: #c8e9ff;
        } */
    </style>
@stop
@section('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="#">Accounting Report</a></li>
                <li><a href="#">Trial Balance</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>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::open(['class'=>'form_search']) !!}

                <div class="col-sm-3">
                    <br>
                    <label for="">Project Name</label>
                    {!! Form::select('project_id', $project,null, ['class' => 'form-control select2 project_id','id'=>'project_id']) !!}
                </div>
                <div class="col-sm-3">
                    <br>
                    <label for="">Start Date</label>
                    <div class="form-group">
                        <input type="text" name="from" class="form-control date_picker start_date" value="">
                    </div>
                </div>
                <div class="col-sm-3">
                    <br>
                    <label for="">End Date</label>
                    <div class="form-group">
                        <input type="text" name="to" class="form-control date_picker end_date" value="{{date('Y-m-d')}}">
                    </div>
                </div>
                {!! Form::close() !!}

                <div class="col-sm-3">
                    <br>
                    <label for="">&nbsp;</label>
                    <div class="form-group">
                        <input type="hidden" value="{{$branch->branch_en}}" id="branch_en">
                        <input type="hidden" value="{{$branch->branch_kh}}" id="branch_kh">
                        <button type="button" class="btn-warning btn btn-md" onclick="search()"><i class="fa fa-search"></i> Search</button>
                        <button type="button" class="btn btn-info btn-lg fa fa-print print_button"> Print</button>
                        <button type="button" class="btn btn-success btn-lg fa fa-file-excel-o " onclick="tableToExcel('table_journal', 'trial balance')"> Excel</button>
                    </div>
                </div>
                {{--            <div class="col-sm-1">--}}
                {{--                <br>--}}
                {{--                <label for="">&nbsp;</label>--}}
                {{--                <div class="form-group">--}}
                {{--                    <button type="button" class="btn btn-success fa fa-cog form-control" id="run_audit" data-toggle="modal" data-target="#form-run-audit"> Run Audit</button>--}}
                {{--                </div>--}}
                {{--            </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>Account Name</th>--}}
                                    {{--                                        <th>Debit</th>--}}
                                    {{--                                        <th>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('administrator.accounting.reports.confirm-run-audit')--}}
@endsection

@section('js')
    <script type="text/javascript">
        let form_ = $('.form_search');
        $(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();
            var project_id = $('.project_id').val();

            $.ajax({
                url: "{{ route('accounting-report.get-trail-balance')}}",
                method: "POST",
                dataType: "text",
                data: {
                    from: from,
                    to: to,
                    project_id: project_id,
                    _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 () {
            console.log(form_.serialize());
            confirmForm("{{route('run_trial_balance')}}?"+form_.serialize());
        });
        $(document).on('click', '.print_button', function () {
            let company_name_kh = $('#branch_kh').val();
            let company_name_en = $('#branch_en').val();
            let report_name     = 'Report Trial Balance';
            $('#table_journal').printThis({
                importCSS: false,
                loadCSS: "{{ asset('assets/layouts/report.css') }}",
                afterPrint: function () {
                },
                header: `<div class="header pt_15 none_bg">
                        <div class="div2">
                            <img src="" alt="" class="logo">
                        </div>
                        <div class="div8">
                            <div class="title">
                                <h2>${company_name_kh}</h2>
                                <h2>${company_name_en}</h2>
                                <h3>${report_name}</h3>
                            </div>
                        </div>
                        <div class="div2">

                        </div>
                        <div class="div12 border-bottom">

                        </div>
                    </div>`,
            });
        });
    </script>
@endsection