<?php
namespace App\Controllers;
use Config\Services;

class Getvehiclesbydetails extends BaseController{
    private $dbHandle;

    public function index(){
        $this->dbHandle = \Config\Database::connect();
        $dataToReturn = [];
        $dataToReturn['load'] = [];
        $modules = [];

        $makecode = $this->request->getVar('make');
        $modelcode = $this->request->getVar('model');
        $engdesc = $this->request->getVar('engdesc');
        $year = $this->request->getVar('year');
        
        $R = $this->dbHandle->query("select vfam, vfamlen, modulesub, module, make, makecode, model, modelcode, vinyear as 'year', engine, englayout, fueltypeprimary, displacementcc, enginecylinders, engdesc, genfr, gento, yearpos, bodytype, seats, doors 
            from `3V_supplementary` where `makecode` = '{$makecode}' and `modelcode` = '{$modelcode}' and `engdesc` = '{$engdesc}' and (`genfr` <= '{$year}' and `gento` >= '{$year}') group by 
            `modulesub`, `module`, `vfam`, `vfamlen`, `make`, `makecode`, `model`, `modelcode`, `year`, `engine`, `englayout`, `fueltypeprimary`, `displacementcc`, `enginecylinders`, `engdesc`, `genfr`, `gento`, `yearpos`, `bodytype`, `seats`, `doors` 
            order by vfamlen DESC, module ASC");

        if( $R->getNumRows()>0 ){
            $dataToReturn['status'] = 0;

            foreach($R->getResultArray() as $W){
                $modules[] = $W['module'];
                $W['years'][] = ["year" => $year];
                $dataToReturn['load'][] = $W;
            }

            $dataToReturn['modules'] = $modules;
            
        }else{
            //ERROR - LOOKUP RESULTS FAILED
            $dataToReturn['status'] = 3;
            $dataToReturn['extra'] = "na_lookup";
            $dataToReturn['load'] = ["makecode"=>$makecode, "modelcode"=>$modelcode, "engdesc"=>$engdesc, "year"=>$year];
        }

        return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }

    

    

    // private function getPhotoFamily($source, $photoref){
    //     $photfam = $photoref;
    //     $pic = str_replace('.jpg', '', substr($photoref, strrpos($photoref, '/') + 1));

    //     if($pic != 'nophoto'){//variants exist
    //         switch($source){

    //             default:
    //             for($i = 1; $i <= 50; $i++){
    //                 $potfile = "/home/avvs/public_html/assets/vphotos/{$source}/{$pic}_{$i}.jpg";
    //                 //$potfile = "C:/xampp/htdocs/autotariff_gha/assets/vphotos/{$source}/{$pic}_{$i}.jpg";
    //                 if(file_exists($potfile)){
    //                     $photfam .= ','.base_url("assets/vphotos/{$source}/{$pic}_{$i}.jpg");
    //                 }else{
    //                     break;
    //                 }
    //             }



    //             default:
    //             if(!($vin == 'N/A' || $vin == '')){//VIN search
    //                 $potfile = "/home/avvs/public_html/assets/vphotos/{$source}/{$photoref}";
    //                 //$potfile = "C:/xampp/htdocs/autotariff_gha/assets/vphotos/{$source}/{$photoref}";
    //                 if(file_exists($potfile)){
    //                     $phot = base_url("assets/vphotos/{$source}/{$photoref}");
    //                 }
    //             }


    //         }
    //     }
    //     $photfam = str_replace('/wip', '', $photfam);
    //     return $photfam;
    // }
}