<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminOrderController extends Controller
{
    // সব ট্রানজেকশন এবং অর্ডারের লিস্ট দেখাবে
    public function index(Request $request)
    {
        $query = DB::table('transactions')
            ->join('users', 'transactions.user_id', '=', 'users.id')
            ->select('transactions.*', 'users.username', 'users.email')
            ->orderBy('transactions.id', 'desc');

        // সার্চ ফাংশনালিটি (TrxID বা Username দিয়ে খোঁজার জন্য)
        if ($request->has('search') && $request->search != '') {
            $search = $request->search;
            $query->where(function($q) use ($search) {
                $q->where('transactions.trx_id', 'like', "%$search%")
                  ->orWhere('users.username', 'like', "%$search%");
            });
        }

        // ফিল্টার (Credit/Debit)
        if ($request->has('type') && $request->type != '') {
            $query->where('transactions.transaction_type', $request->type);
        }

        $transactions = $query->paginate(20);

        // টোটাল স্ট্যাটাস কাউন্ট
        $totalDeposit = DB::table('transactions')->where('transaction_type', 'credit')->where('status', 'approved')->sum('amount');
        $totalSpent = DB::table('transactions')->where('transaction_type', 'debit')->sum('amount');

        return view('admin.orders.index', compact('transactions', 'totalDeposit', 'totalSpent'));
    }
}
