<?php

namespace App\Distributor\Repositories;

use App\Models\DistAdminDistributor as Model;
use Dcat\Admin\Repositories\EloquentRepository;

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



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


    /*
     * 得到当前分销商的域名
     * type 0:当前域名 1:二级域名 2:自定义域名
     */
    public static function getDomain($type=0)
    {
        $distId = getDistributorId();
        $model = new Model();
        return $model->getDomain($distId,$type);
    }

    /*
     * 得到分销商信息
     */
    public static function getInfo()
    {
        $id = getDistributorId();
        $row = Model::find($id);
        if ($row) {
            foreach ($row->getAttributes() as $key => $value) {
                $row->{$key} = $value ?? ''; // 如果值为 null,替换为空字符串
            }
        }
        return $row;
    }

    /*
     * 修改域名名称
     */
    public static function updateDomain($domainType,$customDomain)
    {
        $id = getDistributorId();
        $row = Model::find($id);
        $row->domain_type = $domainType;
        if ($domainType == 1) {
            $row->custom_domain = $customDomain;
        }
        $row->save();
    }

    public static function updateInfo($info)
    {
        $id = getDistributorId();
        $row = Model::find($id);
        $row->logo = $info['logo'];
        $row->site_name = $info['site_name'];
        $row->company_name = $info['company_name'];
        $row->company_address = $info['company_address'];
        $row->service_hotline = $info['service_hotline'];
        $row->whats_app = $info['whats_app'];
        $row->facebook = $info['facebook'];
        $row->instagram = $info['instagram'];
        $row->youtube = $info['youtube'];
        $row->linkedin = $info['linkedin'];
        $row->tiktok = $info['tiktok'];
        $row->copy_right = $info['copy_right'];//copy_right
        $row->seo_title = $info['seo_title'];//seo_title
        $row->seo_keywords = $info['seo_keywords'];//seo_keywords
        $row->seo_description = $info['seo_description'];//seo_description
        $row->save();
    }

    /*
     * 分销商切换主题
     */
    public static function enableTheme($appearanceId)
    {
        $appearanceId = intval($appearanceId);
        $distId = getDistributorId();
        $distAppearance = new DistAppearance();
        $appearanceRow = $distAppearance->model()->find($appearanceId);
        if ($appearanceRow && $appearanceRow->enabled == 1) {
            //修改分销商主题
            $row = Model::find($distId);
            $row->appearance_id = $appearanceId;
            $row->save();
            //切换主题
            DistAppearance::switchTheme($appearanceId, $distId);
            return true;
        }
        return false;
    }


    public static function all()
    {

        $result =  Model::all();
        return $result;
    }
    public static function tags_all()
    {
        $result = Model::all();

        // 遍历结果并返回数组
        $tags = [];
        foreach ($result as $item) {
            $tags[$item->id] = $item->client_code; // 假设 company_name 是你想要显示的字段
        }

        return $tags;
    }

    public static function getCompanyNamesByIds(array $ids)
    {
        return Model::whereIn('id', $ids)->pluck('company_name')->toArray();
    }
}