<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class CompanyController extends Controller
{
   public function index(){
       $companies = Company::query()->latest()->get();
       return view('admin.companies.index',compact('companies'));
   }
    public function edit($id){
        $company = Company::FindOrFail($id);
        return view('admin.companies.edit',compact('company'));
    }
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'company_name' => 'required',
            'status' => 'required',
        ]);
        $input = $request->all();
        $company = Company::findOrFail($id);
        $company->update($input);
        return redirect()->route('admin.companies.index')
            ->with('success', 'تم تعديل بيانات الشركة بنجاح');
    }

    public function destroy(Request $request)
    {
        $company = Company::findOrFail($request->companyid);
        $company->delete();
        return redirect()->route('admin.companies.index')
            ->with('success', 'تم حذف الشركة بنجاح');
    }

    public function editClient($id){

        $client = Client::findOrFail($id); 
        $company = Company::findOrFail($client->company_id);

        return view('admin.companies.edit-user',compact('client','company'));
    }

    public function updateClient(Request $request){
        $client = Client::findOrFail($request->id);
        $data = $request->all();
        if($request->password !== null){
            
            $data['password'] = Hash::make($request->password);
        }
        $client->update($data);
        return redirect()->route('admin.companies.index')
            ->with('success', 'تم تحديث بيانات العميل بنجاح');
    }

}
