@extends('backEnd.layouts.master')
@section('title','Office Expenses')
@section('css')
<link href="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
<link href="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-responsive-bs5/css/responsive.bootstrap5.min.css" rel="stylesheet" type="text/css" />
<link href="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-buttons-bs5/css/buttons.bootstrap5.min.css" rel="stylesheet" type="text/css" />
<link href="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-select-bs5/css/select.bootstrap5.min.css" rel="stylesheet" type="text/css" />
@endsection

@section('content')
<div class="container-fluid">
    
    <!-- start page title -->
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <a href="{{route('office-expenses.create')}}" class="btn btn-primary rounded-pill"><i class="fe-plus"></i> Add Expense</a>
                </div>
                <h4 class="page-title">Office Expenses</h4>
            </div>
        </div>
    </div>       
    <!-- end page title -->
    
    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card bg-light">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Total Expenses</h6>
                    <h2 class="text-danger">Tk. {{ number_format($totalExpenses, 2) }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-light">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">This Month</h6>
                    <h2 class="text-warning">Tk. {{ number_format($monthlyTotal, 2) }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-light">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Recent Expenses</h6>
                    <h2 class="text-info">{{ $recentExpenses->count() }}</h2>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Category Summary -->
    @if(count($categorySummary) > 0)
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Category Summary (This Month)</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        @foreach($categorySummary as $category => $amount)
                        <div class="col-md-4 mb-3">
                            <div class="d-flex justify-content-between align-items-center p-3 bg-light rounded">
                                <span><strong>{{ $category }}</strong></span>
                                <span class="badge bg-primary">Tk. {{ number_format($amount, 2) }}</span>
                            </div>
                        </div>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif
    
    <!-- Search and Filter -->
   <div class="row mb-3">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <form method="GET" class="row g-3">
                    <div class="col-md-2">
                        <input type="text" name="keyword" class="form-control" placeholder="Search..." value="{{ request('keyword') }}">
                    </div>
                    <div class="col-md-2">
                        <select name="category" class="form-control">
                            <option value="">All Categories</option>
                            @foreach($categories as $cat)
                            <option value="{{ $cat }}" @if(request('category') == $cat) selected @endif>{{ $cat }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="start_date" class="form-control" value="{{ request('start_date') }}">
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="end_date" class="form-control" value="{{ request('end_date') }}">
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary rounded-pill w-100"><i class="fe-search"></i> Search</button>
                    </div>
                    <div class="col-md-2">
                        <a href="{{ route('office-expenses.index') }}" class="btn btn-secondary rounded-pill w-100"><i class="fe-x"></i> Reset</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
   </div>
   
   <div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="datatable-buttons" class="table table-striped dt-responsive nowrap w-100">
                        <thead>
                            <tr>
                                <th>SL</th>
                                <th>Expense Title</th>
                                <th>Category</th>
                                <th>Amount</th>
                                <th>Expense Date</th>
                                <th>Description</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                    
                        <tbody>
                            @foreach($data as $key=>$value)
                            <tr>
                                <td>{{$loop->iteration}}</td>
                                <td><strong>{{$value->expense_title}}</strong></td>
                                <td><span class="badge bg-soft-primary text-primary">{{$value->expense_category}}</span></td>
                                <td><strong class="text-danger">Tk. {{number_format($value->amount, 2)}}</strong></td>
                                <td>{{ $value->expense_date->format('d-m-Y') }}</td>
                                <td>{{ Str::limit($value->description, 50) }}</td>
                                <td>
                                    <div class="button-list">
                                        <a href="{{route('office-expenses.edit', $value->id)}}" class="btn btn-sm btn-primary" title="Edit"><i class="fe-edit"></i></a>
                                        <form method="POST" action="{{route('office-expenses.destroy')}}" class="d-inline" onsubmit="return confirm('Are you sure?');">
                                            @csrf
                                            <input type="hidden" value="{{$value->id}}" name="id">
                                            <button type="submit" class="btn btn-sm btn-danger" title="Delete"><i class="fe-trash-2"></i></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="mt-3">
                    {{$data->links('pagination::bootstrap-4')}}
                </div>
            </div>
        </div>
    </div>
   </div>
</div>

@endsection

@section('js')
<script src="{{asset('/public/backEnd/')}}/assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
<script src="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
<script src="{{asset('/public/backEnd/')}}/assets/libs/datatables.net-responsive-bs5/js/responsive.bootstrap5.min.js"></script>
@endsection
