<?php



namespace App\Models;



use Illuminate\Database\Eloquent\Model;

use App\Models\nn_color_lang;

use DB;

use LaravelLocalization;



class nn_color extends Model

{

    protected $table = 'nn_color';



    protected $fillable = [

        'color_code'

    ];



    //_-_ CMS SIDE

    /*

    	Get function

    */

    public static function getColor()

    {

    	$color = DB::table('nn_color')

                            ->where(['nn_color_lang.lang' => getCurrentLocale()])

                            ->join('nn_color_lang', 'nn_color.id', '=', 'nn_color_lang.color_id')

                            ->select('nn_color.id', 'nn_color_lang.name', 'nn_color.color_code')

                            ->orderBy('nn_color.created_at', 'desc')

                            ->get();



        return $color;

    }



	/*

    	Create function

    */

    public static function createColor($input)

    {

        $color = self::create($input);



    	$insertedId = $color->id;

        $input['color_id'] = $insertedId;



        foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties){



            $input['lang'] = $localeCode;



            nn_color_lang::create($input);

            

        }



        return $color;

    }



    /*

        Edit function

    */

    public static function editColor($colorId)

    {

        $color = DB::table('nn_color')

            ->where('nn_color.id', '=', $colorId)

            ->join('nn_color_lang', 'nn_color.id', '=', 'nn_color_lang.color_id')

            ->select('nn_color.id', 'nn_color_lang.id as lang_id', 'nn_color.color_code', 'nn_color_lang.name')

            ->orderBy('nn_color.created_at', 'desc')

            ->get();



       return $color;

    }



    /*

        Update function

    */

    public static function updateColor($input)

    {

        $color = nn_color::where('id', '=', $input['id'])

                            ->firstOrFail();;

        $color->update($input);



        $colorLang = nn_color_lang::where('id', '=', $input['lang_id'])

                                    ->where('lang', '=', $input['lang'])

                                    ->firstOrFail();

        $colorLang->update($input);

    }



    //_-_ CMS SIDE

    public static function getColorItemsForSite($ids)

    {

        $color = DB::table('nn_color')

            ->where(['nn_color_lang.lang' => getCurrentLocale()])

            ->whereIn('nn_color.id', $ids)

            ->join('nn_color_lang', 'nn_color.id', '=', 'nn_color_lang.color_id')

            ->select('nn_color.id', 'nn_color_lang.name', 'nn_color.color_code')

            ->orderBy('nn_color.created_at', 'desc')

            ->get();



        return $color;

    }

}

