<?php



namespace App\Http\Controllers\nn_cms;



use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use App\Models\nn_collection;

use App\Models\nn_color;

use App\Models\nn_file;

use LaravelLocalization;



class ManagerColor extends ManagerSiteController

{

    // Auth check

//    public function __construct()

//    {

//        parent::__construct();

//        $this->middleware('auth');

//    }



    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index()

    {

        $data['color'] = nn_color::getColor();



        return view('nn_cms.pages.nn_color.index', $data);

    }



    /**

     * Show the form for creating a new resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function create()

    {

        $data['color'] = nn_color::getColor();



        return view('nn_cms.pages.nn_color.create', $data);

    }



    /**

     * Store a newly created resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @return \Illuminate\Http\Response

     */

    public function store(Request $request)

    {

        // validation

        $this->validate($request, [

            'name' => 'required',

            'color_code' => 'required'

        ]);



        $color = nn_color::createColor($request->all());



        $insertedId = $color->id;



        return redirect(getCurrentLocale() . '/manager/color/' . $insertedId . '/edit');

    }



    /**

     * 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($colorId)

    {

        $data['colorId'] = $colorId;



        $data['color'] = nn_color::editColor($colorId);



        return view('nn_cms.pages.nn_color.edit', $data);

    }



    /**

     * Update the specified resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  int  $id

     * @return \Illuminate\Http\Response

     */

    public function update(Request $request, $id)

    {



        if($request->ajax()){



            $data['success'] = 0;



            // validation

            $this->validate($request, [

                'name' => 'required',

                'color_code' => 'required'

            ]);



            if(nn_color::updateColor($request->all())){



                $data['success'] = 1;



            }



            return $data;

        }

    }



    /**

     * Remove the specified resource from storage.

     *

     * @param  int  $id

     * @return \Illuminate\Http\Response

     */

    public function destroy(Request $request)

    {

        $colorId = $request->id;



        $color = nn_color::find($colorId);



        $color->delete();

    }

}

