<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Viettel Class
 *
 * @author dzungnv02
 *
 */

class Sendmessage extends MX_Controller
{
	private $_xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";

	public function __construct()
	{
		parent::__construct();
	}

	public function index ()
	{
		error_reporting(E_ALL);
		$this->load->model('Services_model');
		//$this->Services_model->cardPaidProcess($aryParams);
		//echo $this->Services_model->processMORequest('01662860000', strtoupper('sbg DDT 0979947802 123456'));
		$aryParams = array(
			'EWalletID' => 0,
			'CardSerial' => '122346',
			'PinCard' => '123456789',
		);
		echo $this->Services_model->cardPaidProcess($aryParams);
	}

	public function testmo() {
		$url = 'http://10.58.38.162/violetservice/soanbai/mo?wsdl';

		$aryParams = array(
				'username' => 'sbg',
				'password' => 'sbg571285',
				'requestID' => '4',
				'userID' => '841662860000',
				'receiverID' => '84988568786',
				'serviceID' => '8062',
				'commandCode' => 'SBG',
				'contentType' => '0',
				'info' => 'SBG DK',
				'receiveTime' => date('Ymd H:i:s')
		);

		$client = new nusoap_client($url, false);
		$client->useHTTPPersistentConnection();
		$xml = $this->load->view('mo', null, true);

		$operator = 'MOReceiver';
		$operation = 'http://tempuri.org/MOReceiver';

		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;
		$result = $client->send($this->_xmlHeader.$xml, $operation);
		//$result = $client->call('MOReceiver', $aryParams ,$operator ,$operation);

		echo $client->responseData;

		/* $err = $client->getError();

		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}

		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>"; */
	}

	public function send($msg, $receiverID)
	{
		$url = $this->config->item('mt_service_url');
		$username = 'sbg';
		$password = 'sbg56ab77cdsgb';
		$operator = 'InsertMT';
		$operation = 'http://tempuri.org/InsertMT';

		$aryMatch = array();
		if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
			$receiverID = '84'. $aryMatch[2];
		}

		$aryParams = array(
				'User' => $username,
				'Pass' => $password,
				'CPCode' => '601',
				'UserID' => '841662860000',
				'RequestID' => '4',
				'ReceiverID' => $receiverID,
				'ServiceID' => '8062',
				'CommandCode' => 'SBG',
				'ContentType' => '0',
				'Info' => urldecode($msg).' - Host: '.$_SERVER['HTTP_HOST'].' - '. date('Y-m-d H:i:s')//'Test MT '. date('Y-m-d H:i:s'),
		);

		$xml = $this->load->view('mt', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;

		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();

		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}

		var_dump($result);

		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";

	}

	public function topup ()
	{
		$url = $this->config->item('topup_service_url');

		//echo file_get_contents($url);
		//exit();

		$operation = 'http://tempuri.org/ITopupService/TopUp';

		$username = 'sbg';
		$password = 'sbg56ab77cdsgb';

		$aryParams = array(
			'EWalletID' => 0,
			'CardSerial' => '1111',
			'PinCard' => '222',
		);
		$xml = $this->load->view('topup', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;

		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();

		if ($client->fault) {
		    echo "<h2>Fault loi:</h2><pre>";
		    print_r($result);
		    echo "</pre>";
		}
		else {
		    $error = $client->getError();
		    if ($error) {
		        echo "<h2>Error</h2><pre>" . $error . "</pre>";
		    }
		    else {
		        echo "<h2>Ket Qua</h2><pre>";
		        var_dump($result);
		        echo "</pre>";
		    }
		}
		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";

	}

	public function chargingws ()
	{
		//$op = $this->input->get('op', TRUE);
		//$op = trim($op) == '' ? 'Balance' : 'processCharging';
		$url = $this->config->item('charging_service_url');
		$content = file_get_contents ($url.'?op=Charge');
		header('Content-Type: text/html; charset=utf-8');
		echo $content;
	}

	public function getBalance()
	{
		//GetAmountBalance
		$url = $this->config->item('charging_service_url');

		$username = $this->config->item('charging_username');
		$password = $this->config->item('charging_password');
		$operator = 'GetAmountBalance';
		$operation = 'http://tempuri.org/Balance';

		$aryParams = array(
				'user' => $username,
				'pass' => $password,
				'MSISDN' => '01662860000',
		);

		$xml = $this->load->view('chargingBalance', $aryParams, true);
		$client = new nusoap_client($url.'?op=Balance');
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;

		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();

		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}

		var_dump($result);

		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
	}

	public function charging ()
	{
		$url = $this->config->item('charging_service_url');

		$username = $this->config->item('charging_username');
		$password = $this->config->item('charging_password');

		$operator = 'processCharging';
		$operation = 'http://tempuri.org/Charge';

		$aryParams = array(
				'user' => $username,
				'pass' => $password,
				'MSISDN' => '01662860000',
				'Charging' => '100',
				'Contents' => 'Namta-Active'
		);

		$xml = $this->load->view('charging', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;

		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();

		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}

		var_dump($result);

		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
	}

	public function getCollaborators () {
		$client = new SoapClient($this->config->item('bccs_service_url'), array(  'trace'      => true,
                'exceptions' => true,
             ));
		$username = $this->config->item('bccs_username');
		$password = $this->config->item('bccs_password');
		$wscode = 'getListShopAndStaftInfo';

		$input = new Input($username, $password, $wscode/* , 2, 1,5000 */);
		$param = new Param();

		$params = array(
				"Input" => $input,
				"rawData" => "rawData",
				"Param" => $param,
		);

		var_dump($params);

		$response = $client->__soapCall("gwOperation", array($params));
		var_dump($response);
	}


}


class Input {
	function Input ($user, $pass, $wscode/*,  $type, $page, $displaypage */) {
		$this->user = $user;
		$this->pass = $pass;
		$this->wscode = $wscode;
		$this->param = null;
		$this->rawData = '';
		/* $this->type = $type;
		 $this->page = $page;
		$this->displaypage = $displaypage; */
	}
}

class Param {
	function Param () {
		$this->name = '';
		$this->value = '';
	}
}
	function fix_latin1_mangled_with_utf8_maybe_hopefully_most_of_the_time($str)
	{
		return preg_replace_callback('#[\\xA1-\\xFF](?![\\x80-\\xBF]{2,})#', 'utf8_encode_callback', $str);
	}

	function utf8_encode_callback($m)
	{
		return utf8_encode($m[0]);
	}