@extends('layouts.web')

@section('css')
<link href="{{ asset('css/dataTables.bootstrap4.min.css') }}" rel="stylesheet" />
<link href="{{ asset('css/sweetalert2.min.css') }}" rel="stylesheet" />
<link href="{{ asset('css/bootstrap-datepicker.min.css') }}" rel="stylesheet" />
@endsection

@section('content')
<div class="container-fluid pt-3">

    @can('isSuperadmin')
    <div class="row justify-content-between">
        <div class="col-4">
            <form action="{{ route('finance.submit') }}"
            novalidate class="{{ ($errors->any()) ? 'was-validated' : 'needs-validation' }}"
            method="post"
            >
            @csrf
                <div class="form-group">
                    <label for="monthYear">รายรับ - รายจ่าย ประจำเดือน <span class="text-red">*</span></label>
                    <div class="input-group mb-3">
                        <input type="text" class="form-control datepiacker" id="monthYear" name="monthYear" placeholder="รายรับ - รายจ่าย ประจำเดือน" autocomplete="off" value="{{$month . '/'. $year}}" required>
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                        </div>
                        <button type="submit" class="btn btn-outline-success show-outline-btn ml-2" value="add-student">แสดง</button>
                    </div>
                    @error('monthYear')
                    <div class="text-danger">{{  $message }}</div>
                    @enderror
                </div>
            </form>
        </div>
        <div class="col-4 text-right">
            <a href="#printModal" data-toggle="modal" data-target="#printModal" class="btn btn-outline-secondary"><i class="fas fa-print"></i> พิมพ์</a>
            <!-- Modal -->
            <div class="modal fade" id="printModal" tabindex="-1" role="dialog" aria-labelledby="printModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered" style="width:450px;">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title-card" id="printModalLabel">พิมพ์รายงาน</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-6 text-left">
                                    รายงานรวมทุกสาขา
                                </div>
                                <div class="col-6">
                                    <a href="{{ route('pdf.finance', ['0', $month, $year, 'รายได้']) }}" target="_blank" class="btn btn-outline-success show-outline-btn"><i class="fas fa-print"></i> รายได้</a>
                                    <a href="{{ route('pdf.finance', ['0', $month, $year, 'รายจ่าย']) }}" target="_blank" class="btn btn-outline-danger ml-3"><i class="fas fa-print"></i> รายจ่าย</a>
                                </div>
                            </div>
                            <hr>
                            @foreach($branchs as $keyIndex => $branch)
                                <div class="row">
                                    <div class="col-6 text-left">
                                        รายงานสาขา{{ $branch->name }}
                                    </div>
                                    <div class="col-6">
                                        <a href="{{ route('pdf.finance', [$branch->id, $month, $year, 'รายได้']) }}" target="_blank" class="btn btn-outline-success show-outline-btn"><i class="fas fa-print"></i> รายได้</a>
                                        <a href="{{ route('pdf.finance', [$branch->id, $month, $year, 'รายจ่าย']) }}" target="_blank" class="btn btn-outline-danger ml-3"><i class="fas fa-print"></i> รายจ่าย</a>
                                    </div>
                                </div>
                                @if(!$loop->last)
                                <hr>
                                @endif
                            @endforeach
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">ปิด</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End modal -->
        </div>
    </div>
        <ul class="nav nav-tabs">
            <li class="nav-item">
                <a class="nav-link active" data-toggle="tab" href="#all" >ทุกสาขา</a>
            </li>
            @foreach($branchs as $keyIndex => $branch)
            <li>
                <a class="nav-link " href="#branch{{ $keyIndex }}" data-toggle="tab">{{ $branch->name }}</a>
            </li>
            @endforeach
        </ul>

        <div class="tab-content">
        <!-- All branch -->
        <div class="tab-pane active" id="all" >
            <div class="row mt-3" >
            <div class="col-6">
                <div class="card card-outline card-success">
                    <div class="card-header">
                        <h3 class="card-title">รายรับประจำเดือน : @if($total_income_all_branch!="")<span class="text-bold text-primary">{{ number_format($total_income_all_branch) }}</span> บาท @endif</h3>
                        <br>
                        <div class="row mt-2">
                            <div class="col-2">
                                <h6>เงินสด</h6>
                            </div>
                            <div class="col-10">
                                <h6>: @if($total_income_all_branch_cash!="")<span class="text-bold text-primary">{{ number_format($total_income_all_branch_cash) }}</span> บาท @endif</h6>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-2">
                                <h6>โอน</h6>
                            </div>
                            <div class="col-10">
                                <h6>: @if($total_income_all_branch_transfer!="")<span class="text-bold text-primary">{{ number_format($total_income_all_branch_transfer) }}</span> บาท @endif</h6>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-2">
                                <h6>บัตรเครดิต</h6>
                            </div>
                            <div class="col-10">
                                <h6>: @if($total_income_all_branch_credit!="")<span class="text-bold text-primary">{{ number_format($total_income_all_branch_credit) }}</span> บาท @endif</h6>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <table class="table finance-table" id="income-table">
                        <thead>
                            <tr>
                                <th scope="col" width="10%">#</th>
                                <th scope="col" width="20%">วันที่</th>
                                <th scope="col" width="30%">สาขา</th>
                                <th scope="col" width="20%">ประเภท</th>
                                <th scope="col" width="20%">รายรับ</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($income_all_branch as $indexKey => $income)
                            <tr>
                                <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                <td class="align-middle">{{ formatDateFromDB($income->billDate) }}</td>
                                <td class="align-middle">{{ $income->name }}</td>
                                <td class="align-middle">{{ $income->method }}</td>
                                <td class="align-middle">{{ number_format($income->amount) }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                      </table>
                    </div>
                </div>
            </div>
            <div class="col-6">
                <div class="card card-outline card-success">
                    <div class="card-header">
                        <h3 class="card-title">รายจ่ายประจำเดือน : <span class="text-bold text-danger">{{ number_format($total_expense_all_branch) }}</span> บาท</h3>
                        <br>
                        <div class="row mt-2">
                            <div class="col-2">
                                <h6>เงินสด</h6>
                            </div>
                            <div class="col-10">
                                <h6>: @if($total_expense_all_branch_cash!="")<span class="text-bold text-danger">{{ number_format($total_expense_all_branch_cash) }}</span> บาท @endif</h6>
                            </div>
                        </div>
                        <div class="row mb-4">
                            <div class="col-2">
                                <h6>โอน</h6>
                            </div>
                            <div class="col-10">
                                <h6>: @if($total_expense_all_branch_transfer!="")<span class="text-bold text-danger">{{ number_format($total_expense_all_branch_transfer) }}</span> บาท @endif</h6>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <table class="table finance-table" id="outcome-table">
                            <thead>
                                <tr>
                                    <th scope="col" width="10%">#</th>
                                    <th scope="col" width="20%">วันที่</th>
                                    <th scope="col" width="30%">สาขา</th>
                                    <th scope="col" width="20%">ประเภท</th>
                                    <th scope="col" width="20%">รายจ่าย</th>
                                </tr>
                            </thead>
                            <tbody>
                            @foreach ($expense_all_branch as $indexKey => $expense)
                                <tr>
                                    <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                    <td class="align-middle">{{ formatDateFromDB($expense->date) }}</td>
                                    <td class="align-middle">{{ $expense->name }}</td>
                                    <td class="align-middle">{{ $expense->method }}</td>
                                    <td class="align-middle">{{ number_format($expense->amount) }}</td>
                                </tr>
                            @endforeach
                            </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @foreach($income_branchs as $x => $income_branch)
        <div class="tab-pane" id="branch{{$x}}" >
            <div class="row mt-3">
                <div class="col-6">
                    <div class="card card-outline card-success">
                        <div class="card-header">
                            <h3 class="card-title">รายรับประจำเดือน : @if($total_income_branch[$x]!="")<span class="text-bold text-primary">{{ number_format($total_income_branch[$x]) }}</span> บาท @endif</h3>
                            <br>
                            <div class="row mt-2">
                                <div class="col-2">
                                    <h6>เงินสด</h6>
                                </div>
                                <div class="col-10">
                                    <h6>: @if($total_income_branch_cash[$x]!="")<span class="text-bold text-primary">{{ number_format($total_income_branch_cash[$x]) }}</span> บาท @endif</h6>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-2">
                                    <h6>โอน</h6>
                                </div>
                                <div class="col-10">
                                    <h6>: @if($total_income_branch_transfer[$x]!="")<span class="text-bold text-primary">{{ number_format($total_income_branch_transfer[$x]) }}</span> บาท @endif</h6>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-2">
                                    <h6>บัตรเครดิต</h6>
                                </div>
                                <div class="col-10">
                                    <h6>: @if($total_income_branch_credit[$x]!="")<span class="text-bold text-primary">{{ number_format($total_income_branch_credit[$x]) }}</span> บาท @endif</h6>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <table class="table finance-table" id="income-table">
                                <thead>
                                    <tr>
                                        <th scope="col" width="10%">#</th>
                                        <th scope="col" width="20%">วันที่</th>
                                        <th scope="col" width="30%">สาขา</th>
                                        <th scope="col" width="20%">ประเภท</th>
                                        <th scope="col" width="20%">รายรับ</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($income_branch as $indexKey => $income)
                                    <tr>
                                        <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                        <td class="align-middle">{{ formatDateFromDB($income->billDate) }}</td>
                                        <td class="align-middle">{{ $income->name }}</td>
                                        <td class="align-middle">{{ $income->method }}</td>
                                        <td class="align-middle">{{ number_format($income->amount) }}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-6">
                    <div class="card card-outline card-success">
                        <div class="card-header">
                            <h3 class="card-title">รายจ่ายประจำเดือน : @if($total_expense_branch[$x]->total!="")<span class="text-bold text-danger">{{ number_format($total_expense_branch[$x]->total) }}</span> บาท @endif</h3>
                            <br>
                            <div class="row mt-2">
                                <div class="col-2">
                                    <h6>เงินสด</h6>
                                </div>
                                <div class="col-10">
                                    <h6>: @if($total_expense_branch_cash[$x]->total!="")<span class="text-bold text-danger">{{ number_format($total_expense_branch_cash[$x]->total) }}</span> บาท @endif</h6>
                                </div>
                            </div>
                            <div class="row mb-4">
                                <div class="col-2">
                                    <h6>โอน</h6>
                                </div>
                                <div class="col-10">
                                    <h6>: @if($total_expense_branch_transfer[$x]->total!="")<span class="text-bold text-danger">{{ number_format($total_expense_branch_transfer[$x]->total) }}</span> บาท @endif</h6>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <table class="table finance-table" id="income-table">
                                <thead>
                                    <tr>
                                        <th scope="col" width="10%">#</th>
                                        <th scope="col" width="20%">วันที่</th>
                                        <th scope="col" width="30%">สาขา</th>
                                        <th scope="col" width="20%">ประเภท</th>
                                        <th scope="col" width="20%">รายจ่าย</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($expense_branch[$x] as $indexKey => $expense)
                                    <tr>
                                        <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                        <td class="align-middle">{{ formatDateFromDB($expense->date) }}</td>
                                        <td class="align-middle">{{ $expense->name }}</td>
                                        <td class="align-middle">{{ $expense->method }}</td>
                                        <td class="align-middle">{{ number_format($expense->amount) }}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @endforeach
        </div> <!-- end tab-content -->
    @endcan

    @can('isAdminOrUser')
    <div class="row">
        <div class="col-3">
            <form action="{{ route('finance.submit') }}"
            novalidate class="{{ ($errors->any()) ? 'was-validated' : 'needs-validation' }}"
            method="post"
            >
            @csrf
                <div class="form-group">
                    <label for="date">รายรับ - รายจ่าย ประจำวัน<span class="text-red">*</span></label>
                    <div class="input-group mb-3">
                        <input type="text" class="form-control datepiacker" id="monthYear" name="monthYear" placeholder="รายรับ - รายจ่าย ประจำวัน" autocomplete="off" value="{{$month . '/'. $year}}" required>
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                        </div>
                        <button type="submit" class="btn btn-outline-success show-outline-btn ml-2" value="add-student">แสดง</button>
                    </div>
                    @error('date')
                    <div class="text-danger">{{  $message }}</div>
                    @enderror
                </div>
            </form>
        </div>
    </div>
    <div class="row mt-3" >
        <div class="col-6">
            <div class="card card-outline card-success">
                <div class="card-header">
                    <h3 class="card-title">รายรับประจำวัน : <span class="text-bold text-primary">@if($total_income!=""){{ number_format($total_income) }}@else 0 @endif</span> บาท</h3>
                    <br>
                    <div class="row mt-2">
                        <div class="col-2">
                            <h6>เงินสด</h6>
                        </div>
                        <div class="col-10">
                            <h6>: <span class="text-bold text-primary">@if($total_income_cash!=""){{ number_format($total_income_cash) }} @else 0 @endif</span> บาท</h6>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-2">
                            <h6>โอน</h6>
                        </div>
                        <div class="col-10">
                            <h6>: <span class="text-bold text-primary">@if($total_income_transfer!=""){{ number_format($total_income_transfer) }} @else 0 @endif</span> บาท</h6>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-2">
                            <h6>บัตรเครดิต</h6>
                        </div>
                        <div class="col-10">
                            <h6>: <span class="text-bold text-primary">@if($total_income_credit!=""){{ number_format($total_income_credit) }} @else 0 @endif</span> บาท </h6>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <table class="table finance-table" id="income-table">
                    <thead>
                        <tr>
                            <th scope="col" width="10%">#</th>
                            <th scope="col" width="30%">วันที่</th>
                            <th scope="col" width="40%">ประเภท</th>
                            <th scope="col" width="20%">รายรับ</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($incomes as $indexKey => $income)
                        <tr>
                            <td class="align-middle">{{ $indexKey + 1 }}.</td>
                            <td class="align-middle">{{ formatDateFromDB($income->billDate) }}</td>
                            <td class="align-middle">{{ $income->method }}</td>
                            <td class="align-middle">{{ number_format($income->amount) }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                  </table>
                </div>
            </div>
        </div>
        <div class="col-6">
            <div class="card card-outline card-success">
                <div class="card-header">
                    <h3 class="card-title">รายจ่ายประจำเดือน : <span class="text-bold text-danger">@if($total_expense!=""){{ number_format($total_expense) }}@else 0 @endif</span> บาท</h3>
                    <br>
                    <div class="row mt-2">
                        <div class="col-2">
                            <h6>เงินสด</h6>
                        </div>
                        <div class="col-10">
                            <h6>: <span class="text-bold text-danger">@if($total_expense_cash!=""){{ number_format($total_expense_cash) }}@else 0 @endif</span> บาท</h6>
                        </div>
                    </div>
                    <div class="row mb-4">
                        <div class="col-2">
                            <h6>โอน</h6>
                        </div>
                        <div class="col-10">
                            <h6>: <span class="text-bold text-danger">@if($total_expense_transfer!=""){{ number_format($total_expense_transfer) }}@else 0 @endif</span> บาท</h6>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <table class="table finance-table" id="outcome-table">
                        <thead>
                            <tr>
                                <th scope="col" width="10%">#</th>
                                <th scope="col" width="30%">วันที่</th>
                                <th scope="col" width="40%">ประเภท</th>
                                <th scope="col" width="20%">รายจ่าย</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($expenses as $indexKey => $expense)
                            <tr>
                                <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                <td class="align-middle">{{ formatDateFromDB($expense->date) }}</td>
                                <td class="align-middle">{{ $expense->method }}</td>
                                <td class="align-middle">{{ number_format($expense->amount) }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endcan
</div>
@endsection

@section('footerscript')
<script src="{{ asset('js/sweetalert2.min.js') }}"></script>
<script src="{{ asset('js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('js/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ asset('js/bootstrap-datepicker.min.js') }}"></script>
<script src="{{ asset('js/bootstrap-datepicker.th.min.js') }}"></script>
<script src="{{ asset('js/custom.js') }}"></script>
<script src="{{ asset('js/finance.js') }}"></script>
@endsection


