@extends('layouts.admin')

@section('title', 'Expenses - Centipid ISP CRM')
@section('breadcrumb_parent', 'FINANCE')
@section('breadcrumb_current', 'Expenses')

@section('content')
<div class="space-y-6">
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-3xl font-extrabold text-white tracking-tight">ISP <span class="text-orange-500">expenses.</span></h1>
            <p class="text-xs text-slate-400 mt-1">Operational costs, upstream bandwidth lease, equipment purchases & payroll.</p>
        </div>
        <button onclick="openRecordExpenseModal()" class="flex items-center gap-2 btn-orange font-bold text-xs px-4 py-2.5 rounded-xl shadow-lg shadow-orange-500/20">
            <i data-lucide="plus" class="w-4 h-4"></i>
            <span>Record Expense</span>
        </button>
    </div>

    <div class="custom-card p-5 rounded-2xl flex items-center justify-between">
        <div>
            <p class="text-[10px] font-extrabold uppercase tracking-widest text-slate-400">Total Expenses Logged</p>
            <p class="text-3xl font-extrabold text-rose-400 mt-1">Rs {{ number_format($totalExpenses, 2) }}</p>
        </div>
        <span class="text-xs font-mono text-slate-400 bg-[#222226] px-3 py-1.5 rounded-xl border border-[#2a2a2e]">
            Audit Tracked
        </span>
    </div>

    <!-- Data Table -->
    <div class="custom-card rounded-2xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs text-slate-300">
                <thead class="bg-[#1c1c1f] text-slate-400 uppercase text-[10px] font-extrabold tracking-wider border-b border-[#2a2a2e]">
                    <tr>
                        <th class="py-4 px-6">CATEGORY</th>
                        <th class="py-4 px-6">DESCRIPTION</th>
                        <th class="py-4 px-6">VENDOR</th>
                        <th class="py-4 px-6">AMOUNT</th>
                        <th class="py-4 px-6">METHOD</th>
                        <th class="py-4 px-6 text-right">DATE</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#2a2a2e] font-medium">
                    @forelse($expenses as $ex)
                        <tr class="hover:bg-[#1f1f23] transition-colors">
                            <td class="py-4 px-6">
                                <span class="px-2.5 py-1 rounded bg-[#222226] text-[10px] font-bold text-orange-400 border border-[#2a2a2e]">
                                    {{ $ex->category }}
                                </span>
                            </td>
                            <td class="py-4 px-6 font-bold text-white">{{ $ex->description }}</td>
                            <td class="py-4 px-6 text-slate-300">{{ $ex->vendor ?? 'N/A' }}</td>
                            <td class="py-4 px-6 font-extrabold text-rose-400 text-sm">Rs {{ number_format($ex->amount, 2) }}</td>
                            <td class="py-4 px-6 text-slate-400 text-[11px]">{{ $ex->payment_method }}</td>
                            <td class="py-4 px-6 text-right font-mono text-slate-400 text-[11px]">{{ $ex->expense_date->format('M d, Y') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="py-8 text-center text-slate-500">No operational expenses logged.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="p-4 border-t border-[#2a2a2e]">
            {{ $expenses->links() }}
        </div>
    </div>
</div>

<!-- Modal: Record Expense -->
<div id="recordExpenseModal" class="fixed inset-0 z-[9999] hidden bg-black/80 backdrop-blur-sm flex items-center justify-center p-4">
    <div class="custom-panel rounded-2xl max-w-md w-full p-6 shadow-2xl space-y-4">
        <div class="flex items-center justify-between border-b border-[#2a2a2e] pb-3">
            <h3 class="text-base font-bold text-white flex items-center gap-2">
                <i data-lucide="plus-circle" class="w-5 h-5 text-orange-500"></i>
                <span>Record Operating Expense</span>
            </h3>
            <button onclick="closeRecordExpenseModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form action="{{ route('admin.finance.storeExpense') }}" method="POST" class="space-y-4">
            @csrf
            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Category *</label>
                <select name="category" required class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                    <option value="Bandwidth">Upstream Bandwidth</option>
                    <option value="Equipment">Hardware & Equipment</option>
                    <option value="Electricity">Electricity & Power Utility</option>
                    <option value="Rent">Tower & POP Station Rent</option>
                    <option value="Salary">Staff Payroll</option>
                    <option value="Maintenance">Maintenance & Repairs</option>
                </select>
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Description *</label>
                <input type="text" name="description" required placeholder="e.g. 50 ONU Modems purchase" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Amount (Rs) *</label>
                    <input type="number" step="0.01" name="amount" required placeholder="12500.00" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Vendor / Payee</label>
                    <input type="text" name="vendor" placeholder="e.g. Airtel Enterprise" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Expense Date *</label>
                    <input type="date" name="expense_date" value="{{ date('Y-m-d') }}" required class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Payment Method</label>
                    <select name="payment_method" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                        <option value="Bank Transfer">Bank Transfer</option>
                        <option value="Cash">Cash</option>
                        <option value="UPI">UPI / Mobile Money</option>
                    </select>
                </div>
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-[#2a2a2e]">
                <button type="button" onclick="closeRecordExpenseModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Cancel</button>
                <button type="submit" class="btn-orange text-xs px-5 py-2.5 rounded-xl">Save Expense</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('scripts')
<script>
    function openRecordExpenseModal() {
        document.getElementById('recordExpenseModal').classList.remove('hidden');
    }
    function closeRecordExpenseModal() {
        document.getElementById('recordExpenseModal').classList.add('hidden');
    }
</script>
@endpush
