<?php

namespace App\Http\Controllers;

use App\Models\IqImage;
use App\Models\IqSite;
use Request;


class SitesController extends Controller
{

    public function __construct()
    {
        $this->middleware('admin');
    }

    public function index()
    {
      //
    }

    public function show($site)
    {
        $data = [
            'value' => IqSite::find(1),
            'site' => $site,
        ];
        return view('admin.site.edit', $data);
    }

    public function store($site)
    {
        $data = Request::all();
        $data['products'] = isset($data['products']) ? $data['products'] : null;
        $data['set_products'] = isset($data['set_products']) ? $data['set_products'] : null;

        if($site == "new"){
           // $model = IqSite::create($data);
        }else{
            $model = IqSite::find(1);
            $model->fill($data);
            $model->save();
        }

        \Session()->flash('alert-save', '1');
        return redirect(route('admin_sites', ['start']));

    }


    // Upload FILE  -----------------------------------------------------------------------------------------------------------------------

    public function imageUpload($site){

        $model = IqSite::find(1);

        try {
            $image = \App\Services\Slim::getImages('images')[0];

            if ( isset($image['output']['data']) )
            {

                // Base64 of the image
                $data = $image['output']['data'];
                $file_ex = array( 'image/jpeg' => 'jpg', 'image/png' => 'png');

                if (!isset($file_ex[$image['output']['type']])) {
                    \Session()->flash('alert-danger', 'File is not jpg or png!');
                    return redirect(route('admin_sites', [$model->slug]));
                }

                $ext = $file_ex[$image['output']['type']];
                // Original file name
                $name = $image['output']['name'];
                $name = \App\Services\Slim::sanitizeFileName($name);
                $path = 'images/iq_images/';

                $image_name = "";
                do {
                    $image_name = uniqid('', false) . '_' . $name;
                } while (\Storage::disk('public')->exists($path.$image_name));

                $data = \Storage::disk('public')->put(
                    $path.$image_name,
                    $data
                );

                $iq_image = IqImage::create([
                    'filename' => $image_name,
                    'original_name' => $image['output']['name'],
                    'ext' => $ext,
                    'mine' => $image['output']['type'],
                    'size' => $image['input']['size']
                ]);

                $model->iq_image_id = $iq_image->id;
                $model->save();

                \Session()->flash('alert-success', "Datei hochgeladen");
                return redirect(route('admin_sites', [$model->slug]));
            }
            \Session()->flash('alert-danger', "Datei leer");
            return redirect(route('admin_sites', [$model->slug]));

        }
        catch (Exception $e) {
            \Session()->flash('alert-danger', "Fehler".$e);
            return redirect(route('admin_sites', [$model->slug]));
        }
    }

    public function imageDelete($site, $image_id){

        $iq_image = IqImage::findOrFail($image_id);
        $model = IqSite::find(1);


        if($iq_image->id == $model->iq_image->id){
            $file =  'images/iq_images/'.$iq_image->filename;
            \Storage::disk('public')->delete($file);
            $model->iq_image_id = NULL;
            $model->save();
            $iq_image->delete();


            \Session()->flash('alert-success', "Datei gelöscht");
            return redirect(route('admin_sites', [$model->slug]));

        }
        \Session()->flash('alert-danger', "Datei nicht gefunden");
        return redirect(route('admin_sites', [$model->slug]));

    }

    public function imageAttribute($site, $image_id, $attr, $val = false){

        $iq_image = IqImage::findOrFail($image_id);

        $iq_image->{$attr} = $val;
        $iq_image->save();

        \Session()->flash('alert-success', "Wert gespeichert");
        return redirect()->back();

    }

}