<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/6/11
 * Time: ����9:28
 * �����ת����
 */

namespace common\api;

use Yii;
use common\models\CategoryCity;

class HostServer
{
    public $hostA;
    public $hostId = 0;
    //2020-4-29  陈方利要求修改
    public $shangwu = '<script>(function() {var _53code = document.createElement("script");_53code.src = "https://www111.53kf.com/code/code/610925db680654214fdfc80e706668a57/2";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(_53code, s);})();</script>';
    public $webconfig;

    public function __construct()
    {
        $name = Yii::$app->request->getHostName();
        $name = explode('.', $name);
        $this->hostA = $name[0];
        $this->CityPid();

        //�����š�logo��53���
        $this->webconfig = $this->getWebCofig();
        !empty($this->webconfig['bridge_link']) ? $this->shangwu = $this->webconfig['bridge_link'] : '';
    }

    public function CityPid()
    {
        $model = new \common\models\CategoryCity();
        $model->pid = 0;
        $rows = $model->getList([]);
        $A = array_column($rows, 'id', 'a');

        if (isset($A[$this->hostA])) {
            $this->hostId = $A[$this->hostA];
            return;
        }

        if ($this->hostA[0] == 'm') {
            $this->hostA = substr($this->hostA, 1);
            if (isset($A[$this->hostA])) {
                $this->hostId = $A[$this->hostA];
            }
        }
    }

//    /**
//     * �ض�����������ض�������ͨ
//     * */
//    public function Cityshangwu()
//    {
//        //�ض�����id
//        //���� 39���麣�� 69����ɽ�� 74
//        $data = false;
//        $input = \Yii::$app->request->get();
//        $city = ['������','������','��ָɽ��','��ͤ��'];
//        $model = new \common\models\CategoryCity();
//        if (!empty($input['city'])){
//            $row = $model->FindById($input['city']);
//            if ($row){
//                $data = $row->city_name;
//            }
//        }
//
//        if (!empty($input['hid'])) {
//            if (!is_numeric($input['hid'])) {
//                $ex = explode('-', $input['hid']);
//                if (!empty($ex[1]) && is_numeric($ex[1])) {
//                    $input['hid'] = $ex[1];
//                }
//            }
//            $row = \common\models\House::find()->select(['pfg_category_city.city_name'])->where(['pfg_house.id' => $input['hid'],])->innerJoin('pfg_category_city', 'pfg_house.city=pfg_category_city.id')->asArray()->one();
//            if ($row) {
//                $data = $row['city_name'];
//            }
//        }
//        if ($data){
//            $bool = in_array($data,$city);
//            if ($bool){
//                $this->shangwu = '<script language="javascript" src="http://swt.shunfangw.com/JS/LsJS.aspx?siteid=MRG74456440&float=1&lng=cn"></script>';
//
//            }
//        }
//        return $this->shangwu;
//    }


    /**
     * ��������ҳ���������ͨ
     * */
    public function Cityshangwu()
    {
        $input = \Yii::$app->request->get();
        $model = new \common\models\CategoryCity();

        //��������
        if (Yii::$app->hostserver->hostId != 0 && empty($input['city'])) {
            $shangwu = $this->getShangWuTong(Yii::$app->hostserver->hostId);
            if (!empty($shangwu)) {
                $this->shangwu = $shangwu;
            }
        }

        //�����
        if (!empty($input['city'])) {
            $shangwu = $this->getShangWuTong($input['city']);

            if (!empty($shangwu)) {
                $this->shangwu = $shangwu;
            }
        }

        //�жϸ�¥������
        if (!empty($input['hid'])) {

            if (!is_numeric($input['hid'])) {
                $ex = explode('-', $input['hid']);
                if (!empty($ex[1]) && is_numeric($ex[1])) {
                    $input['hid'] = $ex[1];
                }
            }

            $row = \common\models\House::find()->select(['pfg_house.city', 'pfg_house.customer_service'])->where(['pfg_house.id' => $input['hid']])->asArray()->one();

            if (!empty($row['customer_service'])) {
                $this->shangwu = $row['customer_service'];
            } else {
                $city = \common\models\CategoryCity::find()->andWhere(['id' => $row['city']])->asArray()->one();

                if ($city['pid'] != 0) {
                    $shangwu = CategoryCity::find()->select(['dialogue'])->andWhere(['id' => $city['id']])->asArray()->one()['dialogue'];
                    if (!empty($shangwu)) {
                        $this->shangwu = $shangwu;
                    } else {
                        $shangwu = CategoryCity::find()->select(['dialogue'])->andWhere(['id' => $city['pid']])->asArray()->one()['dialogue'];
                        !empty($shangwu) ? $this->shangwu = $shangwu : '';
                    }
                }
            }
        }

        return $this->shangwu;
    }


    public function getShangWuTong($city)
    {
        $model = new \common\models\CategoryCity();
        $row = $model->FindById($city);

        $shangwu = $model::find()->select(['dialogue'])->andWhere(['id' => $row['id']])->asArray()->one()['dialogue'];

        if (empty($shangwu) && $row['pid'] != 0) {
            $shangwu = $model::find()->select(['dialogue'])->andWhere(['id' => $row['pid']])->asArray()->one()['dialogue'];
        }

        return $shangwu;
    }

    public function getWebCofig()
    {
        $cofigmodel = new \common\models\Config();
        $wconfig = $cofigmodel::find()->asArray()->one();
        if (!empty($wconfig)) {
            empty($wconfig['logo']) ? '' : $wconfig['logo'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['setsite'] . $wconfig['logo'];
            empty($wconfig['wechat']) ? '' : $wconfig['wechat'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['setsite'] . $wconfig['wechat'];
        }
        return $wconfig;
    }
}