<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Commission;
use App\Models\User;
use Illuminate\Http\Request;

class AdminCommissionHistoryController extends Controller
{
    public function CommissionHistory()
    {
        $users = User::where(['status' => 1])->where('role', '!=', 'admin')->get();
        $commissions = Commission::with(['commissionGivenTo'])->latest()->paginate(10);
        return view('adminDashboard.commission-history', compact('users', 'commissions'));
    }

    public function CommissionHistorySearch(Request $request)
    {
        $request->validate([
            'user_mobile' => 'required'
        ]);
        $commissions = Commission::with(['commissionGivenTo'])->whereRelation('commissionGivenTo', 'mobile', 'like', '%' . $request->user_mobile . '%')->latest()->limit(500)->get();
        foreach ($commissions as $key => $commission) {
            $commissions[$key]->created = date_format($commission->created_at, "Y-m-d H:i:s");
        }
        return response()->json(['status' => 'success', 'commissions' => $commissions]);
    }
}
