@extends('layout')
@section('content')
<style>
    .input_upload {
        width:100px;
        height:35px;
        position:absolute;
        bottom:-34px;
        right:350px;
    }
    .account-sidebar {
        position: sticky;
        top: 20px;
        height: calc(100vh - 100px);
    }
    .account-sidebar .nav-link {
        border-radius: 0;
        padding: 12px 15px;
        color: #495057;
    }
    .account-sidebar .nav-link.active {
        background-color: #f8f9fa;
        color: #0d6efd;
        border-left: 3px solid #0d6efd;
        font-weight: 500;
    }
    .account-sidebar .nav-link:hover:not(.active) {
        background-color: #f8f9fa;
    }
</style>
<!-- begin::main content -->
<main class="main-content">
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar for account types -->
            <div class="col-md-3 col-lg-2">
                @include('accounting.accountant.sidebar')
            </div>

            <!-- Main content area -->
            <div class="col-md-9 col-lg-10">
                <div class="page-header">
                    <h3>{{ ucfirst(isset($type) ? $type : '') }} Accounts</h3>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="{{ asset('dashboard') }}">{{ __('message.dashboard') }}</a></li>
                            <li class="breadcrumb-item active" aria-current="page">{{ ucfirst(isset($type) ? $type : '') }}</li>
                        </ol>
                    </nav>
                </div>
                @php
                    $total_cr = 0;
                    $total_dr = 0;
                    $total_balance = 0;
                    if(isset($banks))
                    {
                        foreach ($banks as $key=>$account)
                        {
                            if($account->type == "received")
                            {
                                $total_cr += $account->amount; 
                            }                    
                            
                            if($account->type == "payment") 
                            {
                                $total_dr += $account->amount;
                            }                       
                                 
                        }
                    }
                    if(isset($advance_payment))
                    {
                        foreach ($advance_payment as $key=>$account)
                        {
                            if($account->type == "received")
                            {
                                $total_cr += $account->amount; 
                            }                    
                            
                            if($account->type == "payment") 
                            {
                                $total_dr += $account->amount;
                            }                       
                                 
                        }
                    }
                    if($total_cr >= $total_dr)
                    {
                        $total_balance = $total_cr - $total_dr;
                    }
                    
                    else
                    {
                        $total_balance = $total_dr - $total_cr;
                    }
                @endphp
                <div class="card">
                    <div class="card-header">
                        <form action="{{ asset('account/fetch-vendor') }}" method="get">
                            <div class="row mt-3">
                                <div class="col-md-12">   
                                    <div class="row">
                                        <div class="form-group col-md-3">
                                            <lable>Start Date</label>
                                            <input type="date" name="start_date" value="{{request('start_date')}}" class="form-control">
                                        </div>
                                        <div class="form-group col-md-3">
                                            <lable>End Date</label>
                                            <input type="date" name="end_date" value="{{request('end_date')}}" class="form-control">
                                        </div>
                                        <div class="form-group col-md-3">
                                            <lable>Vendor</label>
                                            <select name="customer_id" class="form-control select2">
                                                <option selected disabled>Select</option>
                                                @foreach($clients as $cl)
                                                    <option value="{{$cl->id}}" {{isset($_GET["customer_id"]) ? ($cl->id == $_GET["customer_id"] ? 'selected' : '') : ''}}>{{$cl->name}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="input-group col-md-3"> 
                                            <div class="pt-3">                                       
                                                <button class="btn border" type="submit">
                                                    <i class="ti ti-search fa-lg"></i>
                                                </button>
                                                <a class="btn btn-white border" href="{{ asset('account/fetch-vendor') }}">
                                                    <i class="ti ti-reload fa-lg"></i>
                                                </a>
                                            </div>
                                        </div> 
                                    </div>                                     
                                                                           
                                </div>
                                <div class="col-md-12">
                                    @if(isset($_GET["customer_id"]))
                                    <div class="card col-5">
                                        <div class="card-body">
                                            <div class="total-balance-box p-2 py-3 text-center">
                                                <h5 class="mb-0">Total Balance: <span class="text-success">₹ {{$total_balance }}</span></h5>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    @endif
                                </div>
                                
                            </div>
                        </form>
                    </div>
                    @if(isset($_GET["customer_id"]))
                        <div class="card-body">
                            @if (isset($banks))
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered">
                                        <thead class="">
                                            <tr>
                                                
                                                <th class="wrap">S.NO</th>
                                                <th>Date</th>
                                                                      
                                                <th>Cr</th>
                                                <th>Dr</th>
                                                <th>Balance</th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @php $count = 0; @endphp
                                            @if (!empty($banks))
                                                @foreach ($banks as $key=>$account)
                                                    @php $count = $key+1; @endphp
                                                <tr>
                                                        <td>{{ ++$key }}</td>
                                                        <td>{{$account->date}}</td>
                                                                                                  
                                                        <td>
                                                            
                                                            ₹ {{ $account->type == "received" ? $account->amount : "------" }}
                                                            
                                                        </td>
                                                        <td>
                                                            ₹ {{ $account->type == "payment" ? $account->amount : "------" }}
                                                            
                                                        </td>
                                                        <td>₹ {{ $account->amount }}</td>
                                                    
                                                        
                                                </tr>
                                                @endforeach
                                            
                                            @endif
                                            @if (isset($advance_payment) && !empty($advance_payment))
                                                    @foreach ($advance_payment as $key=>$account)
                                                    <tr>
                                                            <td>{{ ++$key+$count }}</td>
                                                            <td>{{$account->date}}</td>
                                                                                                      
                                                            <td>
                                                                
                                                                
                                                                ₹ {{ $account->type == "received" ? $account->amount : "------" }}
                                                                
                                                            </td>
                                                            <td>
                                                                ₹ {{ $account->type == "payment" ? $account->amount : "------" }}
                                                                
                                                            </td>
                                                            <td>₹ {{ $account->amount }}</td>
                                                        
                                                            
                                                    </tr>
                                                    @endforeach
                                               @endif
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <td colspan="4" class="text-right font-weight-bold">Total Cr</td>
                                                <td colspan="2" class="text-right font-weight-bold">₹ {{$total_cr}}</td>
                                            </tr>
                                            <tr>                                            
                                                <td colspan="4" class="text-right font-weight-bold">Total Dr</td>
                                                <td colspan="2" class="text-right font-weight-bold">₹ {{$total_dr}}</td>
                                            </tr>
                                            <tr>    
                                                                                    
                                                <td colspan="4" class="text-right font-weight-bold">Balance</td>
                                                <td colspan="2" class="text-right font-weight-bold">₹ {{$total_balance}}</td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                                
                                <div class="mt-3 d-flex justify-content-center">
                                    {{ $banks->links('pagination::bootstrap-4') }} 
                                </div>
                            @endif
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

</main>
<!-- end::main content -->
@endsection

@push('footer-script')
    <!-- begin::dataTable -->
    <script src="{{ asset('public/admin/js/examples/datatable.js') }}"></script>
    <!-- end::dataTable -->
    <script>
        $(document).ready(function() {
            $(".filter-company-btn").click(function(){
                $(".filter-form-box").toggleClass("d-none");
            });

            $("#checkAll").click(function() {
                $('.item_check:checkbox').not(this).prop('checked', this.checked);
            });
            

            $(".input_file_upload").on("change", function(){
                $(".import-form").submit();
            });

            $(".import-form").on("submit", function(event) {
                event.preventDefault();
                
                $.ajax({
                    type: "POST",
                    url: $(this).attr('action'),
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },                       
                    data: new FormData(this), 
                    processData: false,
                    contentType: false,
                    cache: false,
                    beforeSend: function() {
                        $(".import-content").html("<h5>Please wait uploading...</h5>");
                    },
                    success: function(response) {
                        if(response.message == "success") {
                            var div = `<div class='text-success text-center' style='font-size:50px'>
                                <i class='fa fa-check-circle' ></i>
                                <p style='font-size:20px'>Accounts Imported Successfully!</p>
                            </div>`;
                            $(".import-content").html(div);
                            setTimeout(function(){
                                window.location.reload();
                            }, 3000);
                        } else {
                            var div = `<div class='text-danger text-center' style='font-size:50px'>
                                <i class='fa fa-times-circle' ></i>
                                <p style='font-size:20px'>${response.message || 'Please check the datasheet!'}</p>
                            </div>`;
                            $(".import-content").html(div);
                        }
                    },
                    error: function(xhr, status, error) {
                        var div = `<div class='text-danger text-center' style='font-size:50px'>
                            <i class='fa fa-times-circle' ></i>
                            <p style='font-size:20px'>${xhr.responseJSON?.message || 'Please check the datasheet!'}</p>
                        </div>`;
                        $(".import-content").html(div);
                    }
                });
            });
        });
    </script>
@endpush