<?php

namespace App\Http\Controllers;

use App\Models\Entreprise;
use App\Models\Proprietaire;
use Illuminate\Http\Request;
use DB;

class EntrepriseController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $entreprises = Entreprise::orderBy('created_at')->get();
        return view('entreprises.create', $entreprises);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'nom' => 'required',
            'rcs' => 'required',
            'siret' => 'required',
            'cp' => 'required',
            'type' => 'required',
            'dirigeant' => 'required',
            'telephone' => 'required',
            'email' => 'required',
            'adresse' => 'required',
            'ville' => 'required',
            'capital' => 'required',
            'kbis' => 'required',
            'bilan' => 'required',
        ]);

        $datas = $request->all();

        $idEntreprise = DB::table('entreprises')->latest('id')->first();

        $datas['id'] = $idEntreprise->id + 1;

        Entreprise::create($datas);

        return redirect()->route('proprietaire-with-entreprise.create', $datas['id']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Entreprise  $entreprise
     * @return \Illuminate\Http\Response
     */
    public function show(Entreprise $entreprise)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Entreprise  $entreprise
     * @return \Illuminate\Http\Response
     */
    public function edit(Entreprise $entreprise)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Entreprise  $entreprise
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Entreprise $entreprise)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Entreprise  $entreprise
     * @return \Illuminate\Http\Response
     */
    public function destroy(Entreprise $entreprise)
    {
        //
    }
}
