<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Deal\Deal;
use Modules\Main\Models\Company\CompanyAdmin;
use Modules\Main\Models\City;

class ClientsTableController extends Controller
{
    public function index(Request $request)
    {
        return view(
            'clients.table',
            [
                'status' => $request->status,
                'cities' => City::all()
            ]
        );

    }

    public function getAllDealsInCompany(Request $request)
    {
        $user = auth()->user();
        if (!$admin = CompanyAdmin::where('user_id', $user->id)->first())
        {
            abort(401);
        }
        $deals = Deal::whereIn('agent_id', function ($query) use ($admin)
        {
            $query->select('id');
            $query->from('agents');
            $query->where('company_id', $admin->company_id);
        })->get();
        return view(
            'clients.table',
            [
                'deals'  => $deals,
                'status' => $request->status,
                'cities' => City::all()
            ]
        );
    }
}
