<?php
namespace App\Controllers;

class Clqms extends BaseController {

	private function getProductList() {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            //CURLOPT_URL => 'http://summitcrm.local/api/getProductList',
            CURLOPT_URL => 'https://clqms.services-summit.my.id/api/getProductList',
            #CURLOPT_URL => 'http://clqms-server.local/api/getProductList',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        $data = json_decode($response, true);
		
		return $data;
    }

	public function index() {
		$db = \Config\Database::connect();
		// get data from clqms
		$data['clqms_products'] = $this->getProductList();
		
		// query products data from crm
		$qproductid = '';
		foreach ($data['clqms_products'] as $qdata) {
			$qproductid .= $qdata['productid'].",";
		}
		$qproductid = rtrim($qproductid,',');
		
		$sql = "SELECT p.productid, pc.productname, s.`sitename`, p.`productnumber` FROM products p 
			LEFT JOIN productcatalog pc ON pc.`catalogid`=p.`catalogid`
			LEFT JOIN sites s ON s.`siteid`=p.`siteid`
			WHERE p.productid IN ($qproductid)";
		$query = $db->query($sql);
		$data['products'] = $query->getResultArray();
		
		return view('clqms_index', $data);
	}
	
}