<?php

namespace App\Http\Controllers;

use App\Models\Dealer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class DealerController extends Controller
{

    public function index()
    {

        $dealers = Dealer::all();


        return view('pages.admin.master-dealer.dealer-list', compact('dealers'));
    }


    public function addDealer()
    {
        return view('pages.admin.master-dealer.dealer-add');
    }


    public function store(Request $request)
    {
        $companyName = $request->input('company_name');
        $contactNumber = $request->input('contact_number');
        $email = $request->input('email');
        $state = $request->input('state') == 'active' ? 'active' : 'inactive';
        $status = $request->input('status');


        $dealer = Dealer::create([
            'company_name' => $companyName,
            'contact_number' => $contactNumber,
            'email' => $email,
            'state' => $state,
            'status' => $status,
            'password' => Hash::make($contactNumber),
        ]);

        if ($dealer) {
            return redirect()->route('admin.dealers')->with('success', 'Dealer added successfully');
        } else {
            return back()->with('error', 'Dealer not added');
        }
    }

}
