<?php

class XDSController extends BaseController {

    public function serveXDSPrincipal()
    {
        $email = Auth::user()->email;

        $user = DB::table('users')
            ->where('email','=',$email)
            ->first();

        return View::make('individual.xds.consumer-credit-report')
            ->with('user',$user);
    }

    public function handleXDSPrincipal()
    {
        //assign values to variables
        $strUser = "Statusdata_uat";
        $strPwd = "93eb1864";

        $client = new SoapClient ("https://www.uat.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters = array ("strUser"=>$strUser, "strPwd"=>$strPwd);
        $configQry = $client->Login ($parameters);
        //remove special characters
        $ticket = preg_replace ("~/~", "\\\\", $configQry->LoginResult);

        $ConnectTicket = $ticket;
        $EnquiryReason = "";
        $ProductId = "15";
        $IdNumber = "9110250459086";
        $PassportNo = "";
        $FirstName = "Rachael";
        $Surname = "Maphopha";
        $BirthDate = '1991-10-25';
        $YourReference = "";
        $VoucherCode = "";
        $client = new SoapClient ("https://www.uat.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters = array (
            "ConnectTicket"=>$ConnectTicket,"EnquiryReason"=>$EnquiryReason,"ProductId"=>$ProductId,"IdNumber"=>$IdNumber,"PassportNo"=>$PassportNo,"FirstName"=>$FirstName,"Surname"=>$Surname,"BirthDate"=>$BirthDate,"YourReference"=>$YourReference,
            "VoucherCode"=>$VoucherCode);
        $configQry = $client->ConnectConsumerMatch ($parameters);



        $result = $configQry->ConnectConsumerMatchResult;
        $loadResult = simplexml_load_string($result);
        $enquiryID = $loadResult->ConsumerDetails->EnquiryID;
        $enquiryResultID = $loadResult->ConsumerDetails->EnquiryResultID;
        $EnquiryID = $enquiryID;
        $EnquiryResultID = $enquiryResultID;
        $ProductID='15';
        $BonusXML ='';
        $client = new SoapClient ("https://www.uat.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters=array("ConnectTicket"=>$ConnectTicket,'EnquiryID'=>$EnquiryID,'EnquiryResultID'=>$EnquiryResultID,'ProductID'=>$ProductID,'EnquiryID'=>$EnquiryID,'BonusXML'=>$BonusXML);
        $configQry2 = $client->ConnectGetResultBinary($parameters);

        //$result2 = $configQry2->ConnectGetResultResult;
        //$loadResult2 = simplexml_load_string($result2);

        $pdfFile = $configQry2->ConnectGetResultBinaryResult;

        $file = 'XDS Consumer Credit Report.pdf';
        file_put_contents($file, $pdfFile);

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }

        return Redirect::back()->with('view-success', 'SUCCESS: Your consumer credit report will be mailed to you shortly.');

    }

    public function serveXDSCommercial()
    {
        $email = Auth::user()->email;

        $user = DB::table('users')
            ->where('email','=',$email)
            ->first();

        return View::make('commercial.xds.commercial-report')
            ->with('user',$user);
    }

    public function handleXDSCommercial()
    {
        //assign values to variables
        $strUser = "Statusdata_uat";
        $strPwd = "93eb1864";

        $client = new SoapClient ("https://www.web.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters = array ("strUser"=>$strUser, "strPwd"=>$strPwd);
        $configQry = $client->Login ($parameters);
        //remove special characters
        $ticket = preg_replace ("~/~", "\\\\", $configQry->LoginResult);

        $ConnectTicket = $ticket;
        $Reg1 = '2015';
        $Reg2 = "046144";
        $Reg3="07";
        $BusinessName ="BOTSE BJA LERATO TRADING AND PROJECTS";
        $VatNo ="";
        $SoleProID ="";
        $YourReference ="";
        $VoucherCode ="";
        dd($ConnectTicket);

        $client = new SoapClient ("https://www.web.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters = array (
            'ConnectTicket'=>$ConnectTicket,'Reg1'=>$Reg1,'Reg2'=>$Reg2,'Reg3'=>$Reg3,'BusinessName'=>$BusinessName,'VatNo'=>$VatNo,'SolePropIDNo'=>$SoleProID,'YourReference'=>$YourReference);
            $configQry = $client->ConnectBusinessMatch($parameters);


            //ConnectBusinessMatchResult

        $result = $configQry->ConnectBusinessMatchResult;
        $loadResult = simplexml_load_string($result);
        $enquiryID = $loadResult->CommercialDetails->EnquiryID;
        $enquiryResultID = $loadResult->CommercialDetails->EnquiryResultID;
        $EnquiryID = $enquiryID;
        $EnquiryResultID = $enquiryResultID;
        $ProductID='41';
        $BonusXML ='';
        $client = new SoapClient ("https://www.web.xds.co.za/xdsconnect/XDSConnectWS.asmx?WSDL");
        $parameters=array("ConnectTicket"=>$ConnectTicket,'EnquiryID'=>$EnquiryID,'EnquiryResultID'=>$EnquiryResultID,'ProductID'=>$ProductID,'EnquiryID'=>$EnquiryID,'BonusXML'=>$BonusXML);
        $configQry2 = $client->ConnectGetResultBinary($parameters);

        $pdfFile = $configQry2->ConnectGetResultBinaryResult;

        $file = 'XDS Business Credit Report.pdf';
        file_put_contents($file, $pdfFile);

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }


    }

    public function serverTestReportView()
    {

        return View::make('pdf.xdsConsumerReport');
    }

}
