<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once '../database.php';
require '../../vendor/autoload.php';

if(!isset($_GET['supplier']))
{
    die('Specify Supplier');
}

use Google\Cloud\Translate\V2\TranslateClient;

$translate = new TranslateClient([
    'key' => 'AIzaSyB7tl5zL51ug2YiJQQ46tQrc4T2zTMHuDQ'
]);

class Translate extends database
{
    public function getProducts($supplier)
    {
        $sql = 'SELECT * FROM tb_product WHERE cl_supplier=? AND cl_deleted_at IS NULL AND cl_id NOT IN (SELECT cl_product_id FROM tb_product_title)';
        return $this->callQuery($sql,[$supplier]);
    }
    
    public function updateTitle($titleTable, $id)
    {
        if(!$this->fetchRow('tb_product_title', ['cl_product_id' => $id]))
        {
            $this->insert('tb_product_title', $titleTable);
        }
        else
        {
            $this->update('tb_product_title', $titleTable, ['cl_product_id' => $id]);
        }
        
       
    }
}
     
$supplier = $_GET['supplier'];

$obj = new Translate();
$products = $obj->getProducts($supplier);

$titleTable = [];
                                                
$count = 0;

foreach($products AS $product)
{
   
   if($product['cl_supplier'] == 'action' || $product['cl_supplier'] == 'makant' || $product['cl_supplier'] == 'difox')
   {
        $titleTable = [];
        $titleTable['cl_product_id'] = $product['cl_id'];
        
        if($product['cl_name'] != null || trim($product['cl_name']) != '')
        {
        
            $result = $translate->translate($product['cl_name'], [
                'source' => 'en',
                'target' => 'lv'
            ]);
            $titleTable['cl_lv'] = $result['text'];
            
            $result = $translate->translate($product['cl_name'], [
                'source' => 'en',
                'target' => 'lt'
            ]);
            $titleTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'en',
                'target' => 'ee'
            ]);
            $titleTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'en',
                'target' => 'fi'
            ]);
            $titleTable['cl_fi'] = $result['text'];
            $titleTable['cl_en'] = $product['cl_name'];
        }
        
        

        
   }
   else if($product['cl_supplier'] == 'abpl')
   {
        $titleTable = [];
        $titleTable['cl_product_id'] = $product['cl_id'];
        
        if($product['cl_name'] != null || trim($product['cl_name']) != '')
        {
        
            $result = $translate->translate($product['cl_name'], [
                'source' => 'pl',
                'target' => 'lv'
            ]);
            $titleTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'pl',
                'target' => 'lt'
            ]);
            $titleTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'pl',
                'target' => 'ee'
            ]);
            $titleTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'pl',
                'target' => 'fi'
            ]);
            $titleTable['cl_fi'] = $result['text'];

            $result = $translate->translate($product['cl_name'], [
                'source' => 'pl',
                'target' => 'en'
            ]);
            $titleTable['cl_en'] = $result['text'];
        }

        
   }
        
    $obj->updateTitle($titleTable, $product['cl_id']);
    
}
