<?php

namespace App\Admin\Repositories;

use App\Models\DistAppearance as Model;
use Carbon\Carbon;
use Dcat\Admin\Repositories\EloquentRepository;

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

    /*
     * 获取一个标签
     */
    public static function getOneById($id)
    {
        return Model::where('id', $id)->first();
    }

    public static function selectOptions($valueShowId = false)
    {
        $data = Model::where('enabled', 1)->get();
        $options = [];
        foreach ($data as $item) {
            if ($valueShowId) {
                $options[$item->id] = $item->id.' - '.$item->title;
            } else {
                $options[$item->id] = $item->title;
            }
        }
        return $options;
    }

    /*
     * 设置状态为已导入
     */
    public static function setStatusToImported($id)
    {
        $row = Model::where('id', $id)->first();
        if ($row) {
            $row->updated_at = Carbon::now();
            $row->imported = 1;
            $row->save();
        }
        return true;
    }

    /*
     * 切换主题操作，生成销应商主题模版与变量
     * (如果原本就有模板与变量，不会重复生成)
     */
    public static function switchTheme($appearanceId,$distId) {
        //复制原始模版到指定分销商
        DistAppearanceTemplate::copyTemplateToDist($appearanceId, $distId);
        DistAppearanceVariable::copyAppearanceVariable($appearanceId, $distId);
        //发报到正式环境
        DistAppearancePublishList::publish($appearanceId,$distId);
        //清除缓存
        DistAdminDistributor::clearCache($distId);
        return true;
    }

    /*
     * 初始化分销商模版与变量
     */
    public static function initTheme($appearanceId,$distId)
    {
        //请空模版与变量
        DistAppearanceTemplate::deleteTemplates($appearanceId,$distId);
        DistAppearanceVariable::deleteVariable($appearanceId, $distId);
        //重新生成模版与变量
        self::switchTheme($appearanceId, $distId);
    }
}
