@extends('backend.layouts.master')
@section('section-title', 'Report')
@section('page-title', 'Customer Due')
@push('css')
<style>
    @media print{
        table,table th,table td {
            color:black !important;
        }

        .h-hide {
            display: none;
        }
    }
</style>
@endpush
@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="card m-b-30">
                <div class="card-body">
                    <form action="{{ route('report.customer-due') }}" method="GET">
                        @php
                            $custommer = App\Models\Customer::get();
                        @endphp
                        <div class="row h-hide">
                            <div class="col-md-3 mt-3">
                                <select name="customer_id" id="" class="select2">
                                    <option value="">All Customer</option>
                                    @foreach ($custommer as $item)
                                        <option value="{{ $item->id }}" {{ ($customer_id == $item->id)? 'selected' : '' }}>{{ $item->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3 mt-3">
                                <input type="text" placeholder="Enter Phone No" name="phone_no" value="{{ $phone_no }}" class="form-control">
                            </div>
                            <div class="col-md-6 col-12 mt-3">
                                <div class="row d-flex justify-content-center">
                                    <div class="col-md-12 col-12 ">
                                        <button type="submit" class="btn btn-primary">Filter</button>
                                        <a href="{{ route('customer.index') }}" class="btn btn-primary" >Reset</a>
                                        <a href="#" class="btn btn-primary float-right" onclick="window.print()">Print</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="table-responsive mt-3">
                        <h4 style="text-align: center; font-weight:bold; margin-top:50px;">Customer Due Report</h4>
                        <table id="datatable-buttons" class="table table-striped table-bordered text-center">
                            <thead class="table-primary">
                                <tr>
                                    <th>#SL</th>
                                    <th>Name & Email & Phone</th>
                                    <th>Total Invoice</th>
                                    <th>Paid Invoice</th>
                                    <th>Due Invoice</th>
                                    <th>Previous Due</th>
                                    <th>Total Due</th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $sub_total = 0;
                                @endphp
                                @forelse($customers as $data)
                                    @php
                                    $count_cus = App\Models\Invoice::where('customer_id', $data->id)->count();
                                    $count_tra = App\Models\Transaction::where('customer_id', $data->id)->count();
                                    $open_balance = open_balance_customer($data->id, $data->open_receivable, $data->open_payable);
                                    $inv_total = App\Models\Invoice::where('customer_id', $data->id)->sum('total_amount');
                                    $inv_paid = App\Models\Invoice::where('customer_id', $data->id)->sum('total_paid');
                                    $inv_due = App\Models\Invoice::where('customer_id', $data->id)->sum('total_due');
                                    $sub_total += ($inv_due + $open_balance);
                                    @endphp
                                    @if( $inv_due + $open_balance > 0)
                                    <tr>
                                        <td>{{ $loop->index + 1 }}</td>
                                        <td>{{ $data->name }} <br> {{ $data->phone }} <br> {{ ($data->email == Null)?'NULL':$data->email; }}</td>
                                        <td class="font-weight-bold">{{ $inv_total }} TK</td>
                                        <td class="font-weight-bold">{{ $inv_paid }} TK</td>
                                        <td class="font-weight-bold">{{ $inv_due }} TK</td>
                                        <td class="font-weight-bold">{{ $open_balance }} TK</td>
                                        <td class="font-weight-bold">
                                            {{ $inv_due + $open_balance }} TK
                                        </td>
                                    </tr>
                                    @endif
                                @empty
                                    <tr>
                                        <td colspan="100%" class="text-center text-danger">No Data Available</td>
                                    </tr>
                                @endforelse
                            </tbody>
                            <tfooter>
                                <tr style="background: #3393f2; font-size: 20px; font-width: 700; font-family:sans-serif">
                                    <td colspan="4"></td>
                                    <td colspan="1"> <strong> Total Price: </strong></td>
                                    <td colspan="2"><strong> {{number_format($sub_total,2)}} TK </strong></td>
                                </tr>
                            </tfooter>
                        </table>
                        {{ $customers->onEachSide(1)->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection
