<?php

namespace Onestartup\Landings\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Onestartup\Landings\Model\Landing;

use Onestartup\Landings\Requests\RequestCreateLanding;
use Onestartup\Landings\Requests\RequestEditLanding;

class AdminLandingsController extends Controller{
	public function index(){
		$landings = Landing::paginate(30);
		return view("landings::landings.index")->with("landings", $landings);
	}

	public function create(){
		$formats = $this->formats();
		return view("landings::landings.create")->with("formats", $formats);
	}

	public function edit($id){
		$formats = $this->formats();
		$landing = Landing::findOrFail($id);
		return view("landings::landings.edit")->with("landing", $landing)->with("formats", $formats);
	}

	public function store(RequestCreateLanding $request){
		$landing = new Landing($request->all());

		$landing->save();
        if (isset($request->portada)) {
            
            $file = $request->file('portada');
            $nombre = $file->getClientOriginalName();
            $nombre_file = str_replace(' ', '_', $nombre);
            $ubicacion_donde_guarda ='landings/banners/'.$landing->id.'/'.$nombre_file;
            \Storage::disk('local')->put($ubicacion_donde_guarda,  \File::get($file));
            $landing->image_path = $ubicacion_donde_guarda;
            $landing->save();
        } 

		return redirect()->route("landings.admin.landings.index")->with('message_success', "Landing guardada correctamente.");
	}

	public function update(RequestEditLanding $request, $id){
		$landing = Landing::findOrFail($id);
		$landing->fill($request->all());
        $landing->save();
		if (isset($request->portada)) {
            
            $file = $request->file('portada');
            $nombre = $file->getClientOriginalName();
            $nombre_file = str_replace(' ', '_', $nombre);
            $ubicacion_donde_guarda ='landings/banners/'.$landing->id.'/'.$nombre_file;
            \Storage::disk('local')->put($ubicacion_donde_guarda,  \File::get($file));
            $landing->image_path = $ubicacion_donde_guarda;
            $landing->save();
        } 

        return redirect()->route("landings.admin.landings.index")->with('message_success', "Landing guardada correctamente.");
	}

	public function destroy($id){
		$landing = Landing::findOrFail($id);

		$landing->delete();

        return redirect()
            ->back()
            ->with('message_danger', 'Landing eliminada correctamente.');
	}

	public function deleteBanner($id){
		$landing = Landing::findOrFail($id);
        $landing->image_path = null;
        $landing->save();

        return redirect()
                ->back()
                ->with('message_success', 'Imagen eliminada correctamente');
	}

	private function formats(){
		$formats = [];
		if (is_dir(base_path() . '/resources/views/vendor/onestartup/landings/landing/layouts')) {
			$files = \File::allFiles(resource_path('views/vendor/onestartup/landings/landing/layouts/'));
		}else{
			$files = \File::allFiles(__DIR__.'/../views/public/layouts');
		}
		foreach($files as $file){
			$name = str_replace(".blade.php", "", basename((string)$file));
			$formats[$name] = $name;
		}
		return $formats;
	}
}	