<?php

namespace App\Http\Controllers;

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

class ProprietaireController 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()
    {
        $entreprise = null;
        return view('proprietaires.create', ['entreprise' =>  $entreprise]);
    }

    public function createEntreprise($id)
    {
        $entreprise = Entreprise::find($id);
        return view('proprietaires.create', ['entreprise' =>  $entreprise]);
    }

    public function coproprietaire(){
        $proprietaire = DB::table('proprietaires')->latest('id')->first();
        
        return view('coproprietaires.create',[
            'proprietaire' => $proprietaire
        ]);
    }

    public function coproprietaireStore(Request $request){
        $proprietaire = new Proprietaire;
        $proprietaire->pro_num = random_int(0,1000);
        $proprietaire->civilite = $request->input('civilite');
        $proprietaire->nom = $request->input('nom');
        $proprietaire->prenom = $request->input('prenom');
        $proprietaire->adresse = $request->input('adresse');
        $proprietaire->cp = $request->input('cp');
        $proprietaire->ville = $request->input('ville');
        $proprietaire->pays = $request->input('pays');
        $proprietaire->iban = $request->input('iban');
        $proprietaire->bic = $request->input('bic');
        $proprietaire->telephone_portable = $request->input('telephone_portable');
        $proprietaire->email = $request->input('email');
        $proprietaire->statut_matrimonial = $request->input('statut_matrimonial');
        $proprietaire->date_naissance = $request->input('date_naissance');
        $proprietaire->ville_naissance = $request->input('ville_naissance');
        $proprietaire->departement_naissance = $request->input('departement_naissance');
        $proprietaire->pays = $request->input('pays');
        $proprietaire->entreprise_id = $request->input('entreprise_id');
        $proprietaire->save();
        $coproprietaireID = DB::table('proprietaires')->select('id')->latest('id')->first();
        $proprietaireID = $request->input('proprietaire');
        $proprietaire = Proprietaire::find($proprietaireID);
        $proprietaire->copropietaires()->attach($coproprietaireID->id);
        
        return redirect()->route('home')->with('success','Un propriétaire a été créé.');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $proprietaire = new Proprietaire;
        $proprietaire->pro_num = random_int(0,1000);
        $proprietaire->civilite = $request->input('civilite');
        $proprietaire->nom = $request->input('nom');
        $proprietaire->prenom = $request->input('prenom');
        $proprietaire->adresse = $request->input('adresse');
        $proprietaire->cp = $request->input('cp');
        $proprietaire->ville = $request->input('ville');
        $proprietaire->pays = $request->input('pays');
        $proprietaire->iban = $request->input('iban');
        $proprietaire->bic = $request->input('bic');
        $proprietaire->telephone_portable = $request->input('telephone_portable');
        $proprietaire->email = $request->input('email');
        $proprietaire->statut_matrimonial = $request->input('statut_matrimonial');
        $proprietaire->date_naissance = $request->input('date_naissance');
        $proprietaire->ville_naissance = $request->input('ville_naissance');
        $proprietaire->departement_naissance = $request->input('departement_naissance');
        $proprietaire->pays = $request->input('pays');
        $proprietaire->entreprise_id = $request->input('entreprise_id');
        $proprietaire->save();

        return redirect()->route('questions.conjoint');
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Proprietaire  $proprietaire
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $entreprise = null;
        $proprietaire = Proprietaire::with('biens')->where('id',$id)->first();
        return view('proprietaires.edit', [
            'proprietaire' => $proprietaire,
            'entreprise' => $entreprise,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Proprietaire  $proprietaire
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $proprietaire = Proprietaire::find($id);
        $proprietaire->update($request->all());
        return redirect()->back()->with('success', 'Les modifications ont été prises en compte.');
    }

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

    public function succes () {
        return redirect()->route('home')->with('success','Un propriétaire a été créé.');
    }
}
