<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class WebsiteFaqs extends Model {

    //
    public function prices() {
        return $this->hasMany(PackagePrices::class, 'package_id', 'id');
    }

    public function facilities() {
        return $this->hasMany(PackageFacilities::class, 'package_id', 'id');
    }

    public function category() {
        return $this->hasOne(Categories::class, 'id', 'category_id');
    }

    public static function saveFaqs($type, $type_id, $faqs) {


        if (!empty($faqs)) {
            $i = 0;
            foreach ($faqs as $faq) {
                
                $array = json_decode($faq["data"], true);
                //d($faq["position"],1);
                $id = $array["id"];
                $array["type"] = $type;
                $array["type_id"] = $type_id;
                $array["position"] = $faq["position"];
                unset($array["faqs"]);
                unset($array["id"]);
               

                if (empty($id)) {
                    if(!isset($faq["remove"])){
                        $array["created_at"] = date("Y-m-d H:i:s");
                        self::insertGetId($array);
                    }
                  
                } else {
                    if(isset($faq["remove"]) && $faq["remove"]==1 ){
                         self::where("id", $id)->delete();
                    }else{
                         self::where("id", $id)->update($array);
                    }
                }
                $i++;
            }
        }
    }

}
