<?php

namespace App\Admin\Repositories;

use App\Models\DistAdminDistributor as Model;
use App\Models\DistProductCategory;
use App\Models\DistProduct;
use App\Models\SitePagesTag;
use App\Models\SitePages;
use App\Models\SitePagesTagRelationship;
use Carbon\Carbon;
use Dcat\Admin\Repositories\EloquentRepository;
use App\Models\SiteBanner;

class DistAdminDistributor extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public static function  getClientCodeById($id)
    {
        return Model::where('id', $id)->value('client_code');
    }

    /*
     * 查找最新的N个标签
     */
    public static function selectOptionsNew($limit=30)
    {
        return Model::orderBy('created_at', 'desc')->limit($limit)->pluck('client_code', 'id');
    }



    /*
     * 清缓存
     */
    public static function clearCache($distId,$timeOut = 2)
    {
        //使用二级域名清缓存
        $domain = self::getDomain($distId,0);
        $url = $domain . '/?__clear_cache=1';
        curlGet($url,$timeOut);
    }

    /*
     * 得到指定分销商的域名
     * type 0:当前域名 1:二级域名 2:自定义域名
     */
    public static function getDomain($distId,$type=0)
    {
        $model = new Model();
        return $model->getDomain($distId,$type);
    }
    /*
     * 获取一个标签
     */
    public static function getOneById($id)
    {
        return Model::where('id', $id)->first();
    }

    /*
     * 始始化分销商
     */
    public static function initDist($distId)
    {
        //初始化数据，现在用默认，以后有可能不同主题不同
        self::default($distId);
    }

    //默认
    public static function default($distId)
    {
        //生成默认的banner
        $siteBanner = new SiteBanner();
        $bannerRow = $siteBanner->create([
            'title' => 'Default Banner',
            'image_url' => 'static/common/images/banner-1.jpg',
            'show' => 1,
            'order' => 0,
            'position' => 1,
            'dist_id'=>$distId,
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
        ]);

        //生成默认产品分类
        $distProductCategory = new DistProductCategory();
        $categoryRow = $distProductCategory->create([
            'parent_id' => 0,
            'name' => 'Default Category',
            'order' => 0,
            'enabled' => 1,
            'dist_id'=>$distId,
            'seo_title' => 'Default Category',
            'slug' => 'default-category',
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
        ]);
        //生成默认产品
        $distProduct = new DistProduct();
        $productRow = $distProduct->create([
            'category_id' => $categoryRow->id,
            'title' => 'Default Product',
            'sku' => 'sku001',
            'order' => 0,
            'enabled' => 1,
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'default-product',
            'seo_title' => 'Default Product',
            'issuance_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
        ]);
        $productRow->save();
        //生成默认tab
        $sitePagesTag = new SitePagesTag();
        $tagRow = $sitePagesTag->create([
            'name' => 'News',
            'dist_id'=>$distId,
            'slug' => 'news',
            'seo_title' => 'News',
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
        ]);
        //生成默认页面
        $sitePages = new SitePages();
        $pageRow = $sitePages->create([
            'title' => 'Default Page',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'default-page',
            'seo_title' => 'Default Page',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 0,
        ]);
        //生成绑定关系
        $sitePagesTagRelationship = new SitePagesTagRelationship();
        $sitePagesTagRelationship->create([
            'tag_id' => $tagRow->id,
            'pages_id' => $pageRow->id,
        ]);
        //生成about-us
        $aboutUs = $sitePages->create([
            'title' => 'About Us',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'about-us',
            'seo_title' => 'About Us',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 1,
            'template_file' => config('dictionary.landing_page_default_template')
        ]);
        //生成privacy
//        $privacy = $sitePages->create([
//            'title' => 'Privacy',
//            'status' => 1,
//            'author' => 'admin',
//            'dist_id'=>$distId,
//            'content' => 'Default Content',
//            'slug' => 'privacy',
//            'seo_title' => 'Privacy',
//            'post_date' => Carbon::now(),
//            'created_at'=>Carbon::now(),
//            'updated_at'=>Carbon::now(),
//            'page_type' => 1,
//            'template_file' => config('dictionary.landing_page_default_template')
//        ]);
        //生成sales
        $sales = $sitePages->create([
            'title' => 'Sales',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'sales',
            'seo_title' => 'Sales',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 1,
            'template_file' => config('dictionary.landing_page_default_template')
        ]);
        //生成service
        $service = $sitePages->create([
            'title' => 'Services',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'services',
            'seo_title' => 'Services',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 1,
            'template_file' => config('dictionary.landing_page_default_template')
        ]);
        //生成faqs
        $faqs = $sitePages->create([
            'title' => 'FAQs',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'faqs',
            'seo_title' => 'FAQs',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 1,
            'template_file' => config('dictionary.landing_page_default_template')
        ]);
        //生成contact
        $contact = $sitePages->create([
            'title' => 'Contact us',
            'status' => 1,
            'author' => 'admin',
            'dist_id'=>$distId,
            'content' => 'Default Content',
            'slug' => 'contact-us',
            'seo_title' => 'Contact us',
            'post_date' => Carbon::now(),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now(),
            'page_type' => 1,
            'template_file' => config('dictionary.landing_page_contact_us_template')
        ]);
        //生成菜单
        $siteMenu = new SiteMenu();
        $menuConfig = [
            'collections_type' => '',
            'product_category' => '',
            'pages_tag' => '',
            'product' => '',
            'pages' => '',
            'url' => '',
            'landing_page' => '',
        ];
        /*
         * ==========================================
         * 顶部菜单
         * ==========================================
         */
        //----------------主页------------------
        $data = [];
        $newMenuConfig = $menuConfig;
        $menuType = 0; //主页
        $data[1] = [
            'parentId' => 0,
            'title' => 'Home',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$menuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];

        //----------------Products------------------
        $menuType = 0; //主页
        $newMenuConfig = $menuConfig;
        $data[2] = [
            'parentId' => 0,
            'title' => 'Products',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$menuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];
        //----------------Products Category------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['collections_type'] = 0;
        $newMenuConfig['product_category'] = $categoryRow->id;
        $menuType = 1;//collections
        $data[3] = [
            'parentId' => 2,
            'title' => 'Default Category',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];
        //----------------news------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['collections_type'] = 1;
        $newMenuConfig['pages_tag'] = $tagRow->id;
        $menuType = 1;//collections
        $data[4] = [
            'parentId' => 0,
            'title' => 'News',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];
        //----------------About Us------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $aboutUs->id;
        $menuType = 4;//pages
        $data[5] = [
            'parentId' => 0,
            'title' => 'About Us',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];
        //----------------Contact------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $contact->id;
        $menuType = 4;//pages
        $data[6] = [
            'parentId' => 0,
            'title' => 'Contact',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 0,
        ];
        $siteMenu->createMenuBatch($data);

        /*
         * ==========================================
         * 底部菜单
         * ==========================================
         */
        //----------------SUPPORT------------------
        $data = [];
        $newMenuConfig = $menuConfig;
        $menuType = 0; //主页
        $data[1] = [
            'parentId' => 0,
            'title' => 'SUPPORT',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$menuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];
        //----------------FAQ------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $faqs->id;
        $menuType = 4;//pages
        $data[2] = [
            'parentId' => 1,
            'title' => 'FAQ',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];
        //----------------Sales------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $sales->id;
        $menuType = 4;//pages
        $data[3] = [
            'parentId' => 1,
            'title' => 'Sales',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];

        //----------------COMPANY------------------
        $newMenuConfig = $menuConfig;
        $menuType = 0; //主页
        $data[4] = [
            'parentId' => 0,
            'title' => 'COMPANY',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$menuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];

        //----------------Home------------------
        $newMenuConfig = $menuConfig;
        $menuType = 0; //主页
        $data[5] = [
            'parentId' => 4,
            'title' => 'Home',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$menuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];

        //----------------About Us------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $aboutUs->id;
        $menuType = 4;//pages
        $data[6] = [
            'parentId' => 4,
            'title' => 'About Us',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];
        //----------------Services------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $service->id;
        $menuType = 4;//pages
        $data[7] = [
            'parentId' => 4,
            'title' => 'Services',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];

        //----------------News------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['collections_type'] = 1;
        $newMenuConfig['pages_tag'] = $tagRow->id;
        $menuType = 1;//collections
        $data[8] = [
            'parentId' => 4,
            'title' => 'News',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];
        //----------------Contact------------------
        $newMenuConfig = $menuConfig;
        $newMenuConfig['landing_page'] = $contact->id;
        $menuType = 4;//pages
        $data[9] = [
            'parentId' => 4,
            'title' => 'Contact',
            'distId'=>$distId,
            'uri' =>  $siteMenu->generateUri($menuType,$newMenuConfig,$distId),
            'menuType' => $menuType,
            'menuConfig' => $newMenuConfig,
            'menuLocation' => 1,
        ];

        $siteMenu->createMenuBatch($data);
    }


}
