<?php defined('BASEPATH') OR exit('No direct script access allowed');

use GuzzleHttp\Client;

class Jewelry extends CI_Controller {

  public function __construct()
  {
    parent::__construct();   
    $this->load->library('email');

  } 

  function details()
  { 
      $field = [];
      $item = $this->uri->segment(2);
      $id = $this->input->get('id');
      if(!empty($id)) {
        $field['id'] = $id;
      }

      $field['item'] = $item;
      
      $data = $this->getRecord($field); 
            
      $this->session->set_userdata('isView', 1);
      $this->load->view('jewelry/jewelry_details',$data);
  }

  function email_us() {
      checkAjax();  
      header('Content-Type: application/json');

      $post = json_decode(file_get_contents("php://input"));
          
      $proId = $post->proId ?? '';
      $email = $post->email ?? '';

      $field['id'] = $proId;      
      $data = $this->getRecord($field); 

      $list = $data['record'];
      $list[0]['description'] = $post;

      $this->sendEmail($list, $email);
  }

  private function sendEmail($list, $email) {
    $data['list'] = $list;          

    //-----------------------------------------------------------------
    // $data['message_body'] = "";

    // $this->email->from(NOREPLY_EMAIL);
    // $this->email->to(SITE_EMAIL);
    // $this->email->subject('Jewelry Request');
    // $message = $this->load->view('email/jewelry_cart', $data, true);    
    // $this->email->message($message);
    //$this->email->send();

    //-----------------------------------------------------------------
    $data['message_body'] = '<p>Hi, <br/>Please forward this email to your assigned salesrep.</p>';

    $this->email->from(NOREPLY_EMAIL);
    $this->email->to($email);
    $this->email->subject('Jewelry Request');    
    $message = $this->load->view('email/jewelry_cart', $data, true);   
    $this->email->message($message);
    $this->email->send();

    //-----------------------------------------------------------------
    echo json_encode(['records'=> [], 'message'=>'Email sent successfully!']);
  }

  private function getRecord($field) {
    $record = [];
    // $token = $this->getToken();  

    $client = new Client();

    $headers = [
      'Content-Type' => 'application/json'
    ];    
      
    $response = $client->request('POST', 'https://belgiumdia.com/m_api/jewelry/details', [
      "headers" => $headers,
      "json" => $field
    ]);
      
    if($response->getStatusCode() > 300) {
        return '';
    }

    $body = $response->getBody();
    $parsedBody = json_decode($body, true);
    $record = $parsedBody['data'];

    if(empty($record)) {
      exit;
    }
    
    return $record;
    
  }

  private function getToken() { 

    $client = new Client();

    $headers = [
      'Content-Type' => 'application/x-www-form-urlencoded'
    ];
    $params = [
      'username' => APIUSER,
      'password' => APIPASSWORD
    ];
      
    $response = $client->request('POST', APIURL.'/get-token', [
      "headers" => $headers,
      'form_params' => $params
    ]);
      
    if($response->getStatusCode() > 300) {
        return '';
    }

    $body = $response->getBody();
    $parsedBody = json_decode($body);
    $token = $parsedBody->token;

    return $token;    
  }


}
