@extends ('layouts/template')

@section('content')

    <div class="p-3 mcp-theme-1" id="processing-bill-matrix">

        <div class="card">
            <!-- totals -->
            <div class="card-header px-2 py-2 font-weight-bold bg-white">
                <div class="d-flex align-items-baseline mb-1">
                    <div class="font-size-16 width-100px">{{friendly_money($grandTotal)}}</div>
                    <div class="font-size-16">Total</div>
                </div>
                @foreach($companies as $name => $total)
                    @if(intval(round($total)) !== 0)
                        <div class="d-flex align-items-baseline">
                            <div class="width-100px">{{friendly_money($total)}}</div>
                            <div>{{$name}}</div>
                        </div>
                    @endif
                @endforeach
            </div>
            <div class="card-header px-2 py-1 d-flex align-items-center border-bottom-0">
                <strong class="text-nowrap">
                    <i class="fas fa-user"></i>
                    Pro Financials
                </strong>
                <span class="mx-2">for</span>
                <div class="width-200px">
                    <select provider-search data-pro-uid="{{ @$targetPro->uid }}"
                            name="proUid" class="form-control form-control-sm mr-auto width-200px min-width-unset"
                            onchange="fastLoad('/practice-management/pro-financials/' + this.value)">
                        <option value="" {{!@$targetPro ? 'selected' : ''}}>All Pros</option>
                    </select>
                </div>
                <a href="#" class="ml-auto" onclick="$('.debited-row').toggleClass('d-none');return false;">Toggle Debited</a>
                @if(@$targetPro)
                    <a href="/practice-management/pro-financials" class="ml-2">Clear Filter</a>
                @endif
            </div>
            <div class="card-body p-0">
                <table class="table table-sm table-striped table-hover p-0 m-0">
                    <thead class="bg-light">
                    <tr>
                        <th>Pro</th>
                        <th>Balance Owed</th>
                        <th class="">Recent Debits</th>
                        <th class="w-50">Debit Bills</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($fPros as $row)
                        <tr>
                            <td class="text-nowrap">{{$row->name_first}} {{$row->name_last}}</td>
                            <td><b>${{friendly_money($row->balance)}}</b>
                                <div class="mt-2">
                                    @foreach($row->companyPros as $companyPro)
                                        @if($companyPro->balance)
                                            <div class="mb-1 d-flex align-items-center text-secondary">
                                                <b class="text-sm mr-2 text-nowrap">${{friendly_money($companyPro->balance)}}</b>
                                                <span class="text-sm text-nowrap">{{$companyPro->company->name}}</span>
                                            </div>
                                        @endif
                                    @endforeach
                                </div>
                            </td>
                            <td>
                                @foreach($row->recentDebits() as $debit)
                                    <div class="d-flex align-items-center mb-1">
                                        <span class="mr-2">{{'$' . friendly_money($debit->amount)}}</span>
                                        <span class="text-secondary mr-2">{{friendly_date_time($debit->created_at, false)}}</span>
                                        <span class="text-secondary mr-2">{{$debit->company ? $debit->company->name : ''}}</span>
                                    </div>
                                @endforeach
                            </td>
                            <td>
                                <div>
  
                                    <div moe class="ml-1" relative>
                                        <a class="text-danger" href="" show start>Create debit bill</a>
                                        <form url="/api/bill/createForDebit" right>
                                            <div class="form-group">
                                                <label for="" class="control-label">Company</label>
                                                <select name="debitCompanyProUid" class="form-control input-sm">
                                                    <option value="">--select--</option>
                                                    @foreach($row->companyPros as $companyPro)
                                                        <option value="{{$companyPro->uid}}">{{$companyPro->company->name}}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="form-group">
                                                <label for="" class="control-label">Amount</label>
                                                <input type="number" step=".01" name="amount" class="input-sm form-control">
                                            </div>
                                            <div class="form-group">
                                                <label for="" class="control-label">Description</label>
                                                <textarea name="description" class="input-sm form-control"></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label for="" class="control-label">Description Data</label>
                                                <textarea name="descriptionData" class="input-sm form-control"></textarea>
                                            </div>
                                            <div class="form-group">
                                                <label for="" class="control-label">Effective Date</label>
                                                <input type="date" name="effectiveDate" class="input-sm form-control">
                                            </div>
                                            <div class="mb-0">
                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                    
                                </div>
                                <table class="table table-sm table-striped">
                                    <thead>
                                        <tr>
                                            <th>Date</th>
                                            <th>Amount</th>
                                            <th>Details</th>
                                            <th>Debited?</th>
                                            <th>Paid?</th>
                                            <th>Verified?</th>
                                            <th></th>
                                        </tr>
                                        @foreach($row->debitBills as $debitBill)
                                        <tr class="{{$debitBill->has_debit_pro_been_debited ? 'debited-row d-none' : ''}}">
                                            <td>{{friendly_date($debitBill->effective_date)}}</td>
                                            <td>${{friendly_money($debitBill->debit_pro_expected_amount)}}</td>
                                            <td>
                                                {{$debitBill->debit_description ?: ''}}
                                            </td>
                                            <td>{{$debitBill->has_debit_pro_been_debited?'Yes':'No'}}</td>
                                            <td>{{$debitBill->debit_pro_expected_amount?'Yes':'No'}}</td>
                                            <td>{{$debitBill->is_verified?'Yes':'No'}}</td>
                                            <td>
                                                @if($debitBill->is_verified)
                                                    <div moe class="ml-1" relative>
                                                        <a class="text-danger" href="" show start>Undo Mark As Verified</a>
                                                        <form url="/api/bill/undoMarkAsVerified" right>
                                                            <input type="hidden" name="uid" value="{{$debitBill->uid}}">

                                                            <div class="form-group">
                                                                <label for="" class="control-label">Are you sure?</label>
                                                            </div>
                                                            
                                                            <div class="mb-0">
                                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                @else 
                                                    <div moe class="ml-1" relative>
                                                        <a class="text-danger" href="" show start>Mark As Verified</a>
                                                        <form url="/api/bill/markAsVerified" right>
                                                            <input type="hidden" name="uid" value="{{$debitBill->uid}}">

                                                            <div class="form-group">
                                                                <label for="" class="control-label">Are you sure?</label>
                                                            </div>
                                                            
                                                            <div class="mb-0">
                                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                @endif
                                                
                                                <div moe class="ml-1" relative>
                                                    <a class="text-danger" href="" show start>Debit expected amount</a>
                                                    <form url="/api/bill/debitDebitProAmount" right>
                                                        <input type="hidden" name="uid" value="{{$debitBill->uid}}">

                                                        <div class="form-group">
                                                            <label for="" class="control-label">Amount</label>
                                                            <input type="number" step=".01"  name="debitDebitProAmount" class="form-control input-sm">
                                                        </div>
                                                        
                                                        <div class="mb-0">
                                                            <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                            <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                        </div>
                                                    </form>
                                                </div>

                                                <div moe class="ml-1" relative>
                                                    <a class="text-danger" href="" show start>Debit different amount</a>
                                                    <form url="/api/bill/debitDebitProDifferentAmount" right>
                                                        <input type="hidden" name="uid" value="{{$debitBill->uid}}">
                                                        
                                                        <div class="form-group">
                                                            <label for="" class="control-label">Amount</label>
                                                            <input type="number" step=".01"  name="differentDebitProAmount" class="form-control input-sm">
                                                        </div>
                                                        
                                                        <div class="mb-0">
                                                            <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                            <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                        @endforeach
                                    </thead>
                                </table>       
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        <div class="mt-3">
            {{$fPros->withQueryString()->links()}}
        </div>
    </div>
@endsection
