<?php
namespace Application\Service\DB\Admin;

use Application\Command\Swoole\Server\WebSocketCommand;
use Application\Service\Extension\ErrorHandler;
use Interop\Container\ContainerInterface;
use Swoole\Coroutine\Http\Client;
use function Co\run;
use Laminas\Db\Sql\Where;

class Websitefiling extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_admin_websitefiling';
        parent::__construct($dbAdapter, $container);
    }

    public function getCurrentWebsiteFiling()
    {
        $imgAppendParamStr = "style=origin&";   // 主要是为了png的透明背景

        $referer = '&bucket=login_url';

        $curDomain = $_SERVER['HTTP_HOST'];

        $domainArr = [$curDomain];
        $domainExplode = explode('.', $curDomain);

        $filings_domain = '';   // 备案的一级域名
        if (count($domainExplode) == 2) {
            $domainArr[] = "www.{$curDomain}";
            $filings_domain = $curDomain;
        }else if($domainExplode[0] == 'www') {
            array_shift($domainExplode);
            $filings_domain = implode('.', $domainExplode);
            $domainArr[] = $filings_domain;
        }

        $adminWebsiteWhere = new Where();
        $adminWebsiteWhere->equalTo('is_del',0);
        $adminWebsiteWhere->in('company_link',$domainArr);
        $listWhere['where'] = $adminWebsiteWhere;
        $listWhere['type'] = $_POST['type'] ?: 0;

        $listDatas = $this->LocalService()->adminWebsitefiling->fetchOne($listWhere);
        if(!empty($listDatas)) {

            if(!empty($listDatas['website_logo'])) {
                $website_logo = $this->montagereferer($listDatas['website_logo'],$referer);
                $listDatas['website_logo'] = str_ireplace('?path=', "?{$imgAppendParamStr}path=", $website_logo );

            }
            if(!empty($listDatas['website_icon'])) {
                $website_icon = $this->montagereferer($listDatas['website_icon'],$referer);
                $listDatas['website_icon'] = str_ireplace('?path=', "?{$imgAppendParamStr}path=", $website_icon );
            }
            if(!empty($listDatas['websitehead_icon'])) $listDatas['websitehead_icon'] = str_ireplace('?path=', "?{$imgAppendParamStr}path=", $listDatas['websitehead_icon'] );

            $listDatas['web_name'] = !empty($listDatas['web_name']) ? $listDatas['web_name'] : '';
            $listDatas['center_name'] = !empty($listDatas['center_name']) ? $listDatas['center_name'] : '';

            $listDatas['filings_domain'] = $filings_domain;
        }

        return $listDatas ?: [];
    }

    /**
     * Notes : 拼接登录图片返回的来源标识
     * @param $Url
     * @param $referer
     * @return false|string
     * Author:唐伯虎不虎
     * Date:2024/7/1 17:31
     */
    public function montagereferer($Url,$referer)
    {
        $pathArr = json_decode($Url,true);
        $pathArr[0]['url'] .= $referer;
        return json_encode($pathArr);
    }
}