<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// use App\CMSPages;
// use App\Post;
// use DB;
// use Facades\App\Repository\Setting;
// use App\Pages;
// use App\Team;
// use App\WidgetSection;
// use App\Widgets;
use PDF;
use App\EmailsResponders;
use App\Helpers\Functions;



use Mail;


use Illuminate\Support\Facades\Http;


class TestController extends FrontController {

  protected $key;

  public function __construct()
  {
    $this->key = env('HUBSPOT_API_KEY');
  }

  public function index() {


    return   

            date('d/m/Y',strtotime('2021-08-09 00:00:00.000000'));
            //'test';
            $test = $this->getDataByObj('https://api.hubapi.com/contacts/v1/contact/email/' . $email = 'inaam@amixconsulting.com' . '/profile?hapikey=');

            return $test['vid'];
            
      
            $this->getDataByObj('https://api.hubapi.com/contacts/v1/contact/vid/26401/profile?hapikey=');
      
  }

    public function storeContact()
    {
          unset($_POST['_token']);

          $arr = array(
            'properties' => array(
                    array(
                    'property' => 'email',
                    'value' => $_POST['email']
                    ),
                    array(
                    'property' => 'firstname',
                    'value' => $_POST['firstname']
                    ),
                    array(
                    'property' => 'lastname',
                    'value' => $_POST['lastname']
                    ),
                    array(
                    'property' => 'phone',
                    'value' => $_POST['phone']
                    ),
                    array(
                    'property' => 'address',
                    'value' => $_POST['address']
                    ),

                    array(
                    'property' => 'city',
                    'value' => $_POST['city']
                    ),

                    array(
                    'property' => 'state',
                    'value' => $_POST['state']
                    ),

                    array(
                      'property' => 'zip',
                      'value' => $_POST['zip']
                      ), 
                      
                      array(
                        'property' => 'start_date',
                        'value' => date('d/m/Y')
                        ),

                      array(
                        'property' => 'address_line_two',
                        'value' => 'address2'
                      ),
    
                      array(
                        'property' => 'package',
                        'value' => 'your flo'
                      ),
    
                      array(
                        'property' => 'package_price',
                        'value' => 23.99
                      ),
    
                      array(
                        'property' => 'recurring_amount',
                        'value' => 0
                      ),   
                      
                      array(
                        'property' => 'boiler_service_complete',
                        'value' => false
                      ), 
    
                      array(
                        'property' => 'age_of_boiler',
                        'value' => 1
                      ), 
    
                      
                      array(
                        'property' => 'lifecyclestage',
                        'value' => 'customer'
                      ), 
    
                      array(
                        'property' => 'frequency_of_collection',
                        'value' => 'Anually'
                      ), 

                    
                
              )
        );

        // return $arr;

        $contactResponse = $this->setCurlData('https://api.hubapi.com/contacts/v1/contact?hapikey=',$arr);

        if(!$contactResponse['status']) {

          return $contactResponse;

        }

        $assignResponse = $this->assignToList($_POST['email']);

        if(!$assignResponse['status']) {
          
          return $assignResponse;

        }

        $dealResponse = $this->deal();

        if(!$dealResponse['status']) {
          
          return $assignResponse;

        }
        
        return [ 'status' => true ];
           
    }

    public function deal()
    {

          $arr = array(

            "associations" => [
              "associatedVids" => [
                251              
              ]
            ],


            'properties' => array(
                array(
                  "value" => 'click here "http://homefloltd.seo-local.co.uk/home-care/half-flo" to check your plan',
                  "name" => "dealname"
                ),
              
            )
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr));
        curl_setopt($ch, CURLOPT_URL, 'https://api.hubapi.com/deals/v1/deal?hapikey=' . $this->key);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

        curl_close($ch);

        $result = json_decode($response,true);
        if(array_key_exists('status',$result)){
          if($result['status'] == 'error' ){
            return ['status' => false , 'message' => $result['message'] ?? ''];          
          }
        }     

        return ['status' => true ]; 
           
    }


    public function assignToList($email){

         
            $curl = curl_init();

            $endpoint = 'https://api.hubapi.com/contacts/v1/lists/54/add?hapikey=' . $this->key;

            curl_setopt_array($curl, array(
              CURLOPT_URL => $endpoint,
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_POSTFIELDS => '{
                "emails": [
                "' . $email . '"    
              ]
            }',
              CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
              ),
            ));
            
            $response = curl_exec($curl);
            
            curl_close($curl);

            $result = json_decode($response,true);

            if(count($result['updated']) == 0){
              return ['status' => false , 'message' => 'contact cannot assign'];          
            }      
            
            return ['status' => true ]; 

    }



    public function setCurlData($url,$payload)
    {
        $ch = curl_init();

        $endpoint = $url . $this->key;
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
        curl_setopt($ch, CURLOPT_URL, $endpoint);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

        curl_close($ch);

        $result = json_decode($response,true);

        if(array_key_exists('status',$result)){
          if($result['status'] == 'error' ){
            return ['status' => false , 'message' => $result['message'] ?? ''];          
          }
        }       
        
        return ['status' => true ]; 
    }

    public function getDataByObj($url)
    {
      $ch = curl_init();
     
      $endpoint = $url . $this->key;
     
      curl_setopt($ch, CURLOPT_URL,$endpoint);
     
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
      $res = curl_exec($ch);
     
      return $vid = json_decode($res,true);
    }


    public function mail()
    {


        $from = "no-reply@homeflo.co.uk";
        $to = "francisgill42@gmail.com";
        $subject = "Checking PHP mail";
        $message = '
      
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <div style="text-align:  center; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;  font-size: 18px; color:#4D4D4D;">
        <div style="width: 850px; padding: 50px; margin: auto;">
        <div style="width: 50%; text-align: left; display: block;"><img alt="" src="http://homefloltd.seo-local.co.uk/front/images/logo.png" />
        <h3 gilroy-bold="" style="color:#000; font-family: ">Dear, [First_Name]</h3>
        
        <p style="line-height:1.5;">Welcome to HomeFlo Ltd.</p>
  
        <p style="line-height:1.5;"> Congratulations. Your account has now been all setup and your (package name ) will start on (agreement start date)</p>
  
        <p>If you have any further questions please do not hesitate to contact us on customerservice@homeflo.co.uk</p>
        
        <p>Kind Regards</p>
        
        <p>HomeFlo Ltd </p>
        </div>
        
        <p style="color: #000; line-height: 2; text-align: left; font-weight: bolder;">&copy; 2021 HomeFlo Ltd. All Rights Reserved. Company Reg No: 12898663. VAT Number: 364048205<br />
        <span style="color: #F7941D; font-weight: bolder;">Address:</span> Capital Business Centre, 22 Carlton Road, South Croydon, CR2 0BS</p>
        </div>
        </div>
        ';
        $headers = "From:" . $from;
        mail($to,$subject,$message, $headers);
        echo "The email message was sent.";

        die;
      // return $this->pdf();

      // http://homefloltd.seo-local.co.uk/front/images/logo.svg
      $template['body'] = '
      
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <div style="text-align:  center; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;  font-size: 18px; color:#4D4D4D;">
      <div style="width: 850px; padding: 50px; margin: auto;">
      <div style="width: 50%; text-align: left; display: block;"><img alt="" src="http://homefloltd.seo-local.co.uk/front/images/logo.png" />
      <h3 gilroy-bold="" style="color:#000; font-family: ">Dear, [First_Name]</h3>
      
      <p style="line-height:1.5;">Welcome to HomeFlo Ltd.</p>

      <p style="line-height:1.5;"> Congratulations. Your account has now been all setup and your (package name ) will start on (agreement start date)</p>

      <p>If you have any further questions please do not hesitate to contact us on customerservice@homeflo.co.uk</p>
      
      <p>Kind Regards</p>
      
      <p>HomeFlo Ltd </p>
      </div>
      
      <p style="color: #000; line-height: 2; text-align: left; font-weight: bolder;">&copy; 2021 HomeFlo Ltd. All Rights Reserved. Company Reg No: 12898663. VAT Number: 364048205<br />
      <span style="color: #F7941D; font-weight: bolder;">Address:</span> Capital Business Centre, 22 Carlton Road, South Croydon, CR2 0BS</p>
      </div>
      </div>
      ';

      $emailsResponders = EmailsResponders::where('Title', 'User - Signup')->first();

      // $data["body"] = $emailsResponders->Content;
      // $data["title"] = $emailsResponders->Title;
      // $data["subject"] = $emailsResponders->Subject;
     

      // $replaces["Email"] = $email = 'francisgill1000@gmail.com';
      // $replaces["First_Name"] = $First_Name = 'francisgill';
      // $replaces["Package_Name"] = 'Your Flo';
      // $replaces["Start_Date"] = now();
      // $replaces["Website_Name"] = \URL::to('/');

      // $template = Functions::replaces($data, $replaces);

      



        try {

          Mail::send('front.emails.template', ['content' => $template["body"]], function ($m) use ($template) {
              $m->from(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
              $m->to('francisgill1000@gmail.com', 'test');
              $m->subject($template['subject'] = 'signip');
          });

      } catch (\Throwable $th) {
          throw $th;
      }

   
    }


    public function pdf()
    {

      $template = '
      <style>
      @page { margin-right: 15px; margin-left:15px; }
      body { margin-right: 15px; margin-left:15px; }
      </style>
      <div style="text-align:  center; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;  font-size: 18px; color:#4D4D4D;">
          <div style="width: 100%; padding: 20px; margin: auto;">
              <div style="display: block; width: 100%;">
                  <table style="width: 100%; font-size: 18px;  line-height: 2; color: #4D4D4D;">
                      <tbody>
                          <tr>
                              <td width="60%">
                                <img src="http://homefloltd.seo-local.co.uk/front/images/logo.png" style="margin:0; padding:0;" width="300" alt="">
                                  <h3 style="color:#000;">Billing Info</h3>
                                  <p style="line-height: 1.5;">Mr.Inaam Haq<br>
                                      07423 423 424<br>
                                      <a style="text-decoration: none; color:#4D4D4D ;" href="mailto:inaam@amixconsulting.com">inaam@amixconsulting.com</a> <br>
                                      72 Clapham Road
                                      Clapham Junction
                                      London
                                      SW17 8RT</p>
                              </td>
                              <td valign="top" width="40%" style="text-align:right;">
                                  <p style="color:#000; font-weight: 700; margin: 0; padding:0; font-size:34px;">INVOICE</p>

                                  <p style="line-height: 1.5; font-weight: bold; color:#000; font-size:20px;">
                                 
                                      <span style="color:#F7941D;">Invoice #10310</span> <br>
                                      DATE : 25 / 02 / 2021</p>

                                 
                              </td>
                          </tr>
                      </tbody>
                  </table>
              </div>
              <h3 style="color: #000;  text-align: left; font-weight: bold;">Package</h3>
              <div style="font-weight: bold; background-color: #2780E6; color: #fff; font-size: 30px; padding:20px;  text-align: left; border-radius: 30px;">
                  Full Flo Cover Plan <span style="float:right;"> £20.99 / <span style="font-size: 16px;">month</span> </span>
              </div>
              <h3 style="text-align: left;color: #F7941D; font-weight: bold;">What\'s Covered</h3>
              <table style="width: 100%; font-size: 16px;  line-height: 2; color: #4D4D4D;">
                  <tbody>
                      <tr>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Boiler & Controls</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Boiler Service</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Central Heating</td>
                      </tr>
                      <tr>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Gas Supply</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Pipe Plumbing</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Pipework Electrics</td>
                      </tr>
                      <tr>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Water Supply Pipe</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Drainage</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Taps & Toilets</td>
                      </tr>
                      <tr>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Security & Locks</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Roofing</td>
                          <td><img src="http://homefloltd.seo-local.co.uk/front/images/check-mark-png.png" height="15px" width="15px"> Pest Control</td>
                      </tr>

                      </tbody>
                      </table>
                      
                      <table style="width: 100%; font-size: 18px;  line-height: 1.5; color: #4D4D4D; padding-top:30px;">
                       <tbody>
                       <tr style="font-size: 30px; color:#000;  font-weight: bold; font-size:22px;">
                          <td colspan="2" >Net Amount</td>
                          <td  style="text-align: right;">£17.49</td>
                      </tr>
                      <tr style="font-size: 30px; color:#000; font-weight: bold; font-size:22px;">
                          <td colspan="2">VAT Amount</td>
                          <td  style="text-align: right;">£3.50</td>
                      </tr>

                      <tr style="font-size: 30px; color:#F7941D; font-weight: bold; font-size:22px;">
                      <td colspan="2">Total Amount</td>
                      <td  style="text-align: right;">$50.69</td>
                      </tr>

                      <tr style="font-size: 30px; color:#F7941D; font-weight: bold; font-size:22px;">
                      <td colspan="2">&nbsp;</td>
                     
                      </tr>

                      </tbody>
                      </table>
               
              <p style="color: #000; line-height: 1.5; text-align: left; font-size:16px;">
                  © 2021 HomeFlo Ltd. All Rights Reserved. Company Reg No: 12898663. VAT Number: 364048205 <br>
                  <span style="color: #F7941D;">Address:</span> Capital Business Centre, 22 Carlton Road, South Croydon, CR2 0BS
              </p>
          </div>
      </div>';

      $pdf = PDF::loadHTML($template)->stream('invoice.pdf');
      return $pdf;

      Mail::send('front.emails.template', ['content' => $template ], function ($m) use ( $template ) {
        $m->from(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
        $m->to("francisgill1000@gmail.com", "test")->subject("Welcome Email");
    });

    }




}
