<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Image;
use Validator;
use App\Slider;
use Alert;
use Intervention\Image\Exception\NotReadableException;

class SliderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //getting data
        $sliders = Slider::latest()->get();
        return view('backend.slider',compact(['sliders']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Slider $model)
    {
        //validating data
        $action = $request->action_button;
        
        if($action == "Create"){
            $rules = [
                'image_title' => 'required',
                'photo' => 'required|mimes:png,gif,jpeg,jpg|max:2048',
                'publish_status' => 'required',
                'description' => 'required',
                'address' => 'required',
            ];
        }

        else{
            $rules = [
                'image_title' => 'required',
                'publish_status' => 'required',
                'description' => 'required',
                'address' => 'required',
            ];
        }
        
        //checking the rules
        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Crosschek your data, the system allows PNG,GIF,JPEG,JPG formats of not more than 2MB."]);
        }

        else{

            //getting the photo details
            $photo = $request->file('photo');
            $photo_image = Image::make($photo);
            $photo_destination = public_path('/uploads/sliders/');
            $photo_file = time().uniqid().".".$photo->getClientOriginalExtension();//to save in db
            $resized = $photo_image->resize(1378,918);
            $resized->save($photo_destination.$photo_file);

            //getting other data
            $data_save = [
                'image_title' => $request->image_title,
                'image_attachment' => $photo_file,
                'publish_status' => $request->publish_status,
                'description' => $request->description,
                'address' => $request->address,
            ];

            //saving data
            $data = $model::create($data_save);

            if($data){
                return response()->json(['success'=>"Slider Uploaded Successfully."]);
            }
            else{
                return response()->json(['errors'=>"Slider not uploaded."]);
            }
        }
    }

    /**
     * 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  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = Slider::findOrFail($id);
        return response()->json(['result'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Slider $model)
    {
        
        //getting the validation
        $rules = [
            'address' => 'required',
            'publish_status' => 'required',
            'description' => 'required',
            'image_title' => 'required',
        ];

        //checking the rules
        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Crosschek your data, the system allows PNG, GIF, JPEG, JPG formats of not more than 2MB."]);
        }

        else{

            //getting old details
            $id = $request->hidden_data;

            $previously = $model::where('id',$id)->get();

            if($previously->isEmpty()){
                return response()->json(['errors'=>['No such Record in Sliders']]);
            }
            else{
                foreach($previously as $row){
                    $image = $row->image_attachment;
                }
            }
            
            //getting the photo details
            $photo = $request->file('photo');

            if($photo == ''){
                //no any new photo updated
                $data_update = [
                    'publish_status' => $request->publish_status,
                    'description' => $request->description,
                    'address' => $request->address,
                    'image_title' => $request->image_title,
                ];

                //saving data
                $data = $model::where('id',$id)->update($data_update);

                if($data){
                    return response()->json(['success'=>"Slider Image Updated Successfully."]);
                }
                else{
                    return response()->json(['errors'=>"Unable to update Slider."]);
                }
            }

            //we are having new attachment
            else{
                //deleting the old photo
               $data_delete = \File::delete(public_path("uploads/sliders/$image"));

                $photo_image = Image::make($photo);
                $photo_destination = public_path('/uploads/sliders/');
                $photo_file = time().uniqid().".".$photo->getClientOriginalExtension();//to save in db
                $resized = $photo_image->resize(1378,918);
                $resized->save($photo_destination.$photo_file);

                //getting other data
                $data_update = [
                    'image_title' => $request->image_title,
                    'image_attachment' => $photo_file,
                    'publish_status' => $request->publish_status,
                    'description' => $request->description,
                    'address' => $request->address,
                ];

                //saving data
                $data = $model::where('id',$id)->update($data_update);

                if($data){
                    return response()->json(['success'=>"Slider updated Successfully."]);
                }
                else{
                    return response()->json(['errors'=>"Unable to update Slider."]);
                }
            }
            
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        //
        $data_delete = Slider::where('id',$id)->get();

        //getting specific image and file
        foreach($data_delete as $row){
            $image = $row->image_attachment;
        }

        if(\File::exists(public_path("uploads/sliders/$image"))){
            //deleting photo
            \File::delete(public_path("uploads/sliders/$image"));
            $data = Slider::findOrFail($id);

            if($data->delete()){
                return response()->json(['success'=>"Slider Deleted Successfully."]);
            }
            else{
                return response()->json(['errors'=>"Unable to Delete Slider."]);
            }
        }
        else{
            return response()->json(['errors'=>"Unable to delete Slider."]);
        }
    }
}
