<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Widgets;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\AdminController;
use App\Aws\FileSystem;
use App\Media;
class WidgetsController extends AdminController {

    public function __construct() {

        $this->middleware('auth');
        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/widgets";
        $this->breadcrumbs[1]['name'] = "Widgets";
    }

    public function index() {

        $widgets = Widgets::get();
        $data["pageHeader"] = false;
        $data["model"] = $widgets;
        $data["title"] = "Widgets";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/widgets/index', $data);
    }

    public function create() {

        $data["pageHeader"] = false;
        $data["title"] = "Widgets";
        $data["subTitle"] = "Create";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['model']['image'] = null;

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/widgets/create', $data);
    }

    public function save(Request $request) {

        $rules = [
            'Title' => 'required|string',
            'Content' => 'required|string',
        ];
        $input = $request->all();

        $message = [];
        $validator = Validator::make($input, $rules, $message);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            $input["CreatedDateTime"] = date("Y-m-d H:i:s");
            unset($input["_token"]);

            //image name
//            if ($request->hasFile('ImageName')) {
//
//                $array = config('params.dir_upload_widgets');
//                $fileName = rand(111, 999) . time();
//
//                $input['ImageName'] = FileSystem::storeImage($fileName, $input['ImageName'], $array["without_thumb"], $array["thumb"], 1800, 800);
//                //d($input['ImageName'],1);
//            }
            
            if (!empty($request->ImageName) && $request->ImageName != null) {
                $media_id = Media::getMediaID($request->ImageName);
                $input["ImageName"] = $request->ImageName;
                $input["image_media_id"] = $media_id;
            }


            Widgets::insertGetId($input);
//
            \Session::flash('success', 'Widget Added Succesfully!');
            return redirect('admin/widgets');
        }
    }

    public function show($id) {

        $q = new Widgets();
        $widgets = $q->find($id);

        $data['model'] = $widgets;
        $data["title"] = "Widgets";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "View";

        $data['breadcrumbs'] = $this->breadcrumbs;
        
        return view('admin/widgets/show', $data);
    }

    public function delete($id) {

        //if ($id != 1) {
        Widgets::where('PKWidgetID', $id)->delete();
        //}
        \Session::flash('error', 'Widget deleted successfully!');
        return redirect('admin/widgets');
    }

    public function edit($id) {

        $q = new Widgets();
        $widgets = $q->find($id);

        $data['model'] = $widgets;

        $data['model']['image'] = $widgets->ImageName;

        $data["title"] = "Widget";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/widgets/edit', $data)->with('id', $id);
    }

    public function update($id, Request $request) {

        $rules = [
            'Title' => 'required|string',
            'Content' => 'required|string',
        ];
        $input = $request->all();

        $message = [];

        $validator = Validator::make($input, $rules, $message);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            $input["UpdatedDateTime"] = date("Y-m-d H:i:s");

            unset($input["_token"]);

//            if ($request->hasFile('ImageName')) {
//
//                $array = config('params.dir_upload_widgets');
//                $fileName = rand(111, 999) . time();
//
//                $input['ImageName'] = FileSystem::storeImage($fileName, $input['ImageName'], $array["without_thumb"], $array["thumbnails"], 1800, 800);
//            }
            if (!empty($request->ImageName) && $request->ImageName != null) {
                $media_id = Media::getMediaID($request->ImageName);
                $input["ImageName"] = $request->ImageName;
                $input["image_media_id"] = $media_id;
            }

            Widgets::where('PKWidgetID', '=', $id)->update($input);

            \Session::flash('success', 'Widgets Updated successfully!');
            return redirect('admin/widgets');
            // d($input['Content']);             
        }
    }

    public function RemoveImage($id) {
//        $input['ImageName'] = null;
//        
//        $banner=Banners::where('PKBannerID', '=', $id)->update($input);
//
//        return $this->edit($id);
        //  get image name from id 
        $q = new Widgets();
        $widgets = $q->find($id);
        //get path
        $imagepath = config('params.dir_upload_widgets');
        //file exists
        foreach ($imagepath as $row) {
            $exist = Storage::disk('ftp')->exists($row . $widgets->ImageName);
            if ($exist == 1) {
                //delete from storage
                Storage::disk('ftp')->delete($row . $widgets->ImageName);
            }
        }
        //delete from db

        $input['ImageName'] = null;
        Widgets::where('PKWidgetID', '=', $id)->update($input);
        return $this->edit($id);
    }

}
