<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\UserT;
use Yii;
use yii\web\Response;

class TpyPhoneController extends BaseController
{

    /**
     * 初始化,判断工号来确定身份,调用不同的接口
     */
    public $url1 = '';
    public $url2 = '';

    public function init()
    {
        parent::init();

        //根据工号首数字判断地域
        $username = $this->my->username;
        $username_pre = substr($username,0,1);
//        $ext_phones = $this->my->ext_phone;

        //2 、 4、 8开头的工号为北京的
//        $username_pre == 2 || $username_pre == 4 || $username_pre == 8
        if($username_pre == 2 || $username_pre == 4 || $username_pre == 8){
//            $this->url1 = 'http://111.198.22.139:6983/uncall_lib/index.php?wsdl';
            $this->url1 = 'http://111.198.22.178:9090/uncall_api/index.php?wsdl';
            $this->url2 = "http://111.198.22.178:9090/uncall_api/index.php?wsdl";
        }else{
            //其余开头为不用软电话或江西的,江西的如果工号在1000-1063之间(包括),采用新端口
            $ext_phone = $this->my->ext_phone;
            if(1000 <= $ext_phone && $ext_phone <= 1031){
//                $this->url1 = 'http://111.206.16.247/cti/soap_api/index.php?wsdl';
//                $this->url2 = "http://111.206.16.247/cti/soap_api/index.php?wsdl";
//                $this->url1 = 'http://113.195.134.146:9090/uncall_lib/index.php?wsdl';
//                $this->url2 = "http://113.195.134.146:9090/uncall_lib/index.php?wsdl";
//                $this->url1 = 'http://113.195.134.146:8088/uncall_api/index.php?wsdl';
//                $this->url2 = "http://113.195.134.146:8088/uncall_api/index.php?wsdl";
                $this->url1 = 'http://218.87.219.74:8088/uncall_api/index.php?wsdl';
                $this->url2 = "http://218.87.219.74:8088/uncall_api/index.php?wsdl";


//                $this->url1 = 'http://119.36.246.115:9099/uncall_api/index.php?wsdl';
//                $this->url2 = "http://119.36.246.115:9099/uncall_api/index.php?wsdl";
            }else if(4000 <= $ext_phone && $ext_phone <= 4047){
//                $this->url1 = 'http://59.110.156.204:6983/uncall_lib/index.php?wsdl';
//                $this->url2 = "http://59.110.156.204:6983/uncall_lib/index.php?wsdl";
//                $this->url1 = 'http://59.110.156.204:6983/uncall_lib/index.php?wsdl';
//                $this->url2 = "http://59.110.156.204:6983/uncall_lib/index.php?wsdl";
                $this->url1 = 'http://218.87.219.74:8088/uncall_api/index.php?wsdl';
                $this->url2 = "http://218.87.219.74:8088/uncall_api/index.php?wsdl";


            }else if(3000 <= $ext_phone && $ext_phone <= 3047){
                $this->url1 = 'http://119.36.246.115:9090/uncall_api/index.php?wsdl';
                $this->url2 = "http://119.36.246.115:9090/uncall_api/index.php?wsdl";
            }else{
//                $this->url1 = 'http://113.195.134.146:8088/uncall_api/index.php?wsdl';
//                $this->url2 = "http://113.195.134.146:8088/uncall_api_lib/index.php?wsdl";
//                if($ext_phone==2011 || $ext_phone==2010 || $ext_phone==2008 || $ext_phone==2009 || $ext_phone==2007 || $ext_phone==2006 || $ext_phone==2052 || $ext_phone==2042 || $ext_phone==2054 || $ext_phone==2049 || $ext_phone==2045 || $ext_phone==2050){
//                    $this->url1 = 'http://111.198.22.178:6983/uncall_lib/index.php?wsdl';
//                    $this->url2 = "http://113.195.134.130:6983/uncall_api_lib/index.php?wsdl";
//                }else{
//
//                }
                $this->url1 = 'http://218.87.219.74:8088/uncall_api/index.php?wsdl';
                $this->url2 = "http://218.87.219.74:8088/uncall_api/index.php?wsdl";

            }

            //都改成这个
//            $this->url1 = 'http://113.195.134.146:8088/uncall_api/index.php?wsdl';
//            $this->url2 = "http://113.195.134.146:8088/uncall_api_lib/index.php?wsdl";

        }
    }

    /**
     ***********************************
     * 拨打电话
     * 功能 拨打电话
     * @author liukangle
     ***********************************
     */
    public function actionCallTo(){

        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $ext_phone = $request->post('ext_phone');
//        var_dump($ext_phone);
        $phone_num = $request->post('phone_num');
        $action_id = $ext_phone.time();
        $result = array();
        $result['success'] = false;
        $result['msg'] = '呼叫失败';
//        $this->url1
        //接口调用
        $soapClient = new \SoapClient($this->url1);
//        var_dump($this->url1);
        //返回xml格式
        $reslut = $soapClient->OnClickCall($ext_phone,$phone_num,$action_id);
//        var_dump($reslut);
        //XML转数组
        $reslut_last = $this->simplest_xml_to_array($reslut);
        if(!$reslut_last || $reslut_last['result'] == 0){
            return $result;
        }

        switch($reslut_last['OnClickCall']['Response'])
        {
            case 'Success':
                $result['success'] = true;
                $result['msg'] = '呼叫成功';
            break;
            case 'Ringing':
                $result['msg'] = '振铃中';
            break;
            case 'Busy':
                $result['msg'] = '不可用';
            break;
            case 'Connection Failed':
                $result['msg'] = '连接话盒失败';
            break;
            default:
                $result['msg'] = '意外错误';
        }
        return $result;
    }

    /**
     ***********************************
     * 挂断电话
     * 功能 挂断电话
     * @author liukangle
     ***********************************
     */
    public function actionHangUp(){

        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $ext_phone = $request->post('ext_phone');
        $result = array();
        $result['success'] = false;
        $result['msg'] = '挂断失败';

        //接口调用
        $soapClient = new \SoapClient($this->url2);
        //返回xml格式
        $reslut = $soapClient->HangUp($ext_phone);
        //XML转数组
        $reslut_last = $this->simplest_xml_to_array($reslut);

        if(!$reslut_last || $reslut_last['result'] == 1){
            return $result;
        }

        switch($reslut_last['hangUp']['Response'])
        {
            case 'Success':
                $result['success'] = true;
                $result['msg'] = '挂机成功';
                break;
            case 'error':
                $result['msg'] = '挂机失败';
                break;
            default:
                $result['msg'] = '意外错误';
        }
        return $result;
    }

    /**
     ***********************************
     * XML转数组
     * 功能 XML转数组
     * @author liukangle
     ***********************************
     */
    function simplest_xml_to_array($xmlstring="") {
        return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
    }
}