<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Alert;
use Validator;
use App\MembershipType;
use Image;
use File;

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

    /**
     * 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, MembershipType $model)
    {
        //getting the required values
        $member_rules = [
            'membership_title' => 'required|unique:membership_types',
            'description' => 'required',
            'publish_status' => 'required',
        ];

        //validating
        $error_check = Validator::make($request->all(),$member_rules);

        if($error_check->fails()){
            return response()->json(['errors'=>"You are Having an Error the Membership Title Exists."]);
        }

        else{
            //if we have thumbnail attachment
            if($thumbnail = $request->file('thumbnail')){
                $rules = [
                    'thumbnail' => 'required|mimes:png,jpg,jpeg,gif|max:1024',
                ];

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

                if($error->fails()){
                    return response()->json(['errors'=>"The System accept PNG, JPG, JPEG, GIF for Thumbnail Attachments not More than 1MB"]);
                }
                else{
                    //manipulating the thumbnail
                    $thumbnail_image = Image::make($thumbnail);
                    $thumbnail_destination = public_path('/uploads/membership_thumbnails/');
                    $thumbnail_file = time().uniqid().$thumbnail->getClientOriginalExtension();//to save in db
                    $resized = $thumbnail_image->resize(100,100);
                    $resized->save($thumbnail_destination.$thumbnail_file);
                }
            }

            else{
                $thumbnail_file = '';
            }

            //saving file
            //getting the values
            $data_membership = [
                'membership_title' => $request->membership_title,
                'description' => $request->description,
                'publish_status' => $request->publish_status,
                'thumbnail' => $thumbnail_file,
            ];

            $data = $model::create($data_membership);

            if($data){
                return response()->json(['success'=>"Membership Type Recorded Successfully"]);
            }
            else{
                return response()->json(['errors'=>"Unable to create Membership Type."]);
            }
        }
    }

    /**
     * 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 = MembershipType::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, MembershipType $model)
    {
        //getting the id
        $id = $request->hidden_data;
        
        //getting the required values
        $member_rules = [
            'membership_title' => 'required',
            'description' => 'required',
            'publish_status' => 'required',
        ];

        //validating
        $error_check = Validator::make($request->all(),$member_rules);

        if($error_check->fails()){
            return response()->json(['errors'=>"You are Having an Error the Membership Title Exists."]);
        }
        else{
            $previously = $model::where('id',$id)->get();

            //getting specific image and file
            foreach($previously as $data){
                $thumbnail_data = $data->thumbnail;
            }

            //checking if the new update has thumbnail
        if($thumbnail = $request->file('thumbnail')){

            //validating the new photo first before deleting the old
            $rules = [
                'thumbnail' => 'required|mimes:png,jpg,jpeg|max:2048',
            ];

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

            if($error->fails()){
                return response()->json(["errors"=>"The System accept PNG, JPG, JPEG, GIF for Thumbnails not More than 1MB."]);
            }
            else{

                 //deleting old thumbnail
                if(\File::exists(public_path("uploads/membership_thumbnails/$thumbnail_data"))){
                    \File::delete(public_path("uploads/membership_thumbnails/$thumbnail_data"));
                }

                //manipulating the photo
                $thumbnail_image = Image::make($thumbnail);
                $thumbnail_destination = public_path('/uploads/membership_thumbnails/');
                $thumbnail_file = time().uniqid().$thumbnail->getClientOriginalExtension();//to save in db
                $resized = $thumbnail_image->resize(100,100);
                $resized->save($thumbnail_destination.$thumbnail_file);
            }

            }
            else{
                $thumbnail_file = '';
            }

            //photos is not empty
            if($thumbnail != ''){
                
                $data_update = [
                    'membership_title' => $request->membership_title,
                    'description' => $request->description,
                    'publish_status' => $request->publish_status,
                    'thumbnail' => $thumbnail_file,
                ];    
            }

            else{
                $data_update = [
                    'membership_title' => $request->membership_title,
                    'description' => $request->description,
                    'publish_status' => $request->publish_status,
                ];   
            }
         
            $data = $model::where('id',$id)->update($data_update);

            if($data){
                return response()->json(['success'=>"Membership Type updated successfully."]);
            }
            else{
                return response()->json(['error'=>"Unable to update Membership Type."]);
            }
        }
    }

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

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

        if(\File::exists(public_path("uploads/membership_thumbnails/$thumbnail"))){
            //deleting file
            \File::delete(public_path("uploads/membership_thumbnails/$thumbnail"));

            $data = MembershipType::findOrFail($id);
            
            if($data->delete()){
                return response()->json(['success'=>"Membership type deleted successfully."]);
            }
            else{
                return response()->json(['errors'=>"Unable to delete record."]);
            }
        }
        else{
            return response()->json(['errors'=>"Unable to delete record."]);
        }
    }
}
