<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;

class Lang extends Model
{
    use ModelTrait;
    
    protected $table   = 'langs';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $guarded = ['default'];

    public static function configLangs()
    {
        $langs = Lang::where('buy', 1)->get(['slug'])->toArray();
        $langs = "<?php\nreturn ".var_export(array_flatten($langs), true).';';

        \File::put(config_path('langs.php'), $langs);
    }

    // 同步语言数据
    public static function syncLangData($lang)
    {
        // 同步产品
        Lang::syncDescription(ProductDescription::class, 'product_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步新闻
        Lang::syncDescription(NewsDescription::class, 'news_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步案例
        Lang::syncDescription(CasesDescription::class, 'case_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步相册
        Lang::syncDescription(AlbumDescription::class, 'album_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步下载中心
        Lang::syncDescription(DownloadDescription::class, 'download_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步视频中心
        Lang::syncDescription(VideoDescription::class, 'video_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步招聘
        Lang::syncDescription(JobDescription::class, 'job_id', $lang, ['city', 'title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        // 同步自定义页面
        Lang::syncDescription(PageDescription::class, 'page_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'img']);

        // 同步分类
        Lang::syncDescription(CaseCateDescription::class, 'case_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(DownloadCateDescription::class, 'download_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(JobCateDescription::class, 'job_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(NewsCateDescription::class, 'news_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(PageCateDescription::class, 'page_cid', $lang, ['cname', 'summary', 'img']);
        Lang::syncDescription(ProductCateDescription::class, 'product_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(VideoCateDescription::class, 'video_cid', $lang, ['cname', 'seo_title', 'seo_keywords', 'seo_description', 'img']);
        Lang::syncDescription(AlbumCateDescription::class, 'album_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description', 'img']);

        // block description
        Lang::syncDescription(BlockDescription::class, 'block_id', $lang, ['title', 'content', 'extend', 'imgs']);
        Lang::syncDescription(NavDescription::class, 'nav_id', $lang, ['cname', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description', 'imgs']);
        Lang::syncDescription(PhotoGroupDescription::class, 'photo_group_id', $lang, ['title', 'summary', 'f_title', 'text1', 'text2', 'text3', 'imgs']);

        Lang::syncDescription(AdDescription::class, 'ad_id', $lang, ['title', 'summary', 'img', 'url', 'order'], 'code');
        Lang::syncDescription(ProductIntroduction::class, 'product_id', $lang, ['title', 'content'], 'code');
        Lang::syncDescription(ProductAttribute::class, 'product_id', $lang, ['text'], 'attribute_id');
    }
    
    // 同步语言描述
    public static function  syncDescription($model, $foreignKey, $lang, $fields, $otherKey = '')
    {
        $contents = $model::where('lang', config('setting.lang'))->get();
        foreach ($contents as $content)
        {
            if ($otherKey) {
                $exist = $model::where([$foreignKey => $content->$foreignKey, 'lang' => $lang, $otherKey => $content->$otherKey])->exists();
            } else {
                $exist = $model::where([$foreignKey => $content->$foreignKey, 'lang' => $lang])->exists();
            }

            if(!$exist) {
                $model = new $model;
                $model->lang = $lang;
                $model->$foreignKey= $content->$foreignKey;
                if ($otherKey) {
                    $model->$otherKey= $content->$otherKey;
                }

                foreach ($fields as $filed) {
                    $model->$filed = $content->$filed;
                }
                $model->save();
            }
        }

        return true;
    }

    // 同步语言描述
    public static function syncSettingData($lang)
    {
        $contents = Setting::where('lang', config('setting.lang'))->get();
        foreach ($contents as $content)
        {
            $exist = Setting::where(['name' => $content->name, 'lang' => $lang])->exists();
            if(!$exist) {
                Setting::create([
                    'lang' => $lang,
                    'name' => $content->name,
                    'value' => $content->value,
                ]);
            }
        }
        return true;
    }

}
