<?php

namespace App\Http\Controllers\Parametres;

use App\Http\Controllers\Parametres\Base\ParametreBaseController;
use App\Http\Requests\Parametres\StoreRequest;
use App\Http\Requests\Parametres\UpdateRequest;
use App\Models\PortefeuilleAcheteur;
use App\Models\PortefeuilleResponsableAchat;
use APPLICATION;

class PortefeuilleAcheteurController extends ParametreBaseController
{
    public function __construct()
    {
        parent::__construct('portefeuilles-acheteurs', 'Portefeuille responsable achat');
    }

    /**
     * Display a listing of the resource.
     *
     * @param int $portefeuille_resp_achat_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function index(int $portefeuille_resp_achat_id)
    {
        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        if (isset($portefeuilleResponsableAchat)) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                return view('parametres.portefeuilles-acheteurs.index', [
                    'parametres' => PortefeuilleAcheteur::where('portefeuille_responsable_achat_id', $portefeuille_resp_achat_id)->paginate(APPLICATION::ITEMS_PER_PAGE),
                    'parent' => $portefeuilleResponsableAchat,
                    'parametres_url' => array($portefeuille_resp_achat_id),
                    'route' => $this->_route,
                    'has_enfant' => false
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param int $portefeuille_resp_achat_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create(int $portefeuille_resp_achat_id)
    {
        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        if (isset($portefeuilleResponsableAchat)) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                return view('parametres.portefeuilles-acheteurs.create', [
                    'parent' => $portefeuilleResponsableAchat,
                    'type_parent' => $this->_type_parent,
                    'route' => $this->_route,
                    'store_parametre' => $portefeuille_resp_achat_id
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param StoreRequest $request
     * @param int $portefeuille_resp_achat_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(StoreRequest $request, int $portefeuille_resp_achat_id)
    {
        $validated = $request->validated();

        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        if (isset($portefeuilleResponsableAchat)) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                PortefeuilleAcheteur::firstOrCreate(array_merge(
                    $validated,
                    ['portefeuille_responsable_achat_id' => $portefeuille_resp_achat_id]
                ));
                return redirect()->route('portefeuilles-acheteurs.index', $portefeuille_resp_achat_id)->with('success', 'Portefeuille acheteur ajouté avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $portefeuille_resp_achat_id
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function edit(int $portefeuille_resp_achat_id, int $id)
    {
        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        $portefeuilleAcheteur = PortefeuilleAcheteur::find($id);
        if (isset($portefeuilleResponsableAchat) && isset($portefeuilleAcheteur) && $portefeuilleAcheteur->portefeuille_responsable_achat_id == $portefeuille_resp_achat_id) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                return view('parametres.portefeuilles-acheteurs.edit', [
                    'parametre' => $portefeuilleAcheteur,
                    'parent' => $portefeuilleResponsableAchat,
                    'type_parent' => $this->_type_parent,
                    'route' => $this->_route,
                    'update_parametre' => array($portefeuille_resp_achat_id)
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param int $portefeuille_resp_achat_id
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(UpdateRequest $request, int $portefeuille_resp_achat_id, int $id)
    {
        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        $portefeuilleAcheteur = PortefeuilleAcheteur::find($id);
        if (isset($portefeuilleResponsableAchat) && isset($portefeuilleAcheteur) && $portefeuilleAcheteur->portefeuille_responsable_achat_id == $portefeuille_resp_achat_id) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                $validated = $request->validated();
                $portefeuilleAcheteur->update($validated);
                return redirect()->route('portefeuilles-acheteurs.index', $portefeuille_resp_achat_id)->with('success', 'Portefeuille acheteur mis à jour avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $portefeuille_resp_achat_id
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(int $portefeuille_resp_achat_id, int $id)
    {
        $portefeuilleResponsableAchat = PortefeuilleResponsableAchat::find($portefeuille_resp_achat_id);
        $portefeuilleAcheteur = PortefeuilleAcheteur::find($id);
        if (isset($portefeuilleResponsableAchat) && isset($portefeuilleAcheteur) && $portefeuilleAcheteur->portefeuille_responsable_achat_id == $portefeuille_resp_achat_id) {
            if ($this->checkAccesStructure($portefeuilleResponsableAchat->structure_id)) {
                if ($portefeuilleAcheteur->users->count() > 0) {
                    return redirect()->back()->withErrors('Impossible de supprimer le portefeuille acheteur car des utilisateurs l\'utilise.');
                }
                $portefeuilleAcheteur->delete();
                return redirect()->route('portefeuilles-acheteurs.index', $portefeuille_resp_achat_id)->with('success', 'Portefeuille acheteur supprimé avec succès.');
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }
}
