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

class Getpartsbydetails extends BaseController{
    private $dbHandle;

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

        $vfam = $this->request->getVar('vfam');
        $year = $this->request->getVar('year');

        $R = $this->dbHandle->query("select probrand, afpartno, plugtype, mileage, note, proqty, price, specificprice, oemno, mileage from `3P_plugmaster` where `stock`='yes' and `vfam` = '{$vfam}' and ((partgenfr <= '{$year}' and partgento >= '{$year}') or 1) group by probrand, afpartno, plugtype, oemno, mileage, proqty, price, specificprice, mileage, note order by probrand ASC, afpartno ASC, price ASC");
        if( $R->getNumRows()>0 ){
            $A = $R->getResultArray();

            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A;
        }else{
            //ERROR - PARTS MATCHING FAILED
            $dataToReturn['status'] = 3;
            $dataToReturn['extra'] = "na_part";
            $dataToReturn['load'] = ["vfam"=>$vfam, "year"=>$year];
        }

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

    
/*
$R = $this->dbHandle->query("select vehiclemake, model, vin, vfam, partgenfr, partgento, probrand, supplierid, afpartno, oemno, plugtype, material, proqty, localref, comment, oemman, price, specificprice, mileage from `3P_plugmaster` where `stock`='yes' and `vfam` = '{$vfam}' group by probrand, afpartno, price, vehiclemake, model, vin, vfam, partgenfr, partgento, supplierid, oemno, plugtype, material, proqty, localref, comment, oemman, specificprice order by probrand ASC, afpartno ASC, price ASC");

$this->dbHandle = \Config\Database::connect();
        $dataToReturn = [];
        $modules = [];

        $vfam = $this->request->getVar('vfam');
        $year = $this->request->getVar('year');

        // $R = $this->dbHandle->query("select * from `3P_plugmaster` where `stock`='yes' and `vfam` = '{$vfam}' and (partgenfr <= '{$year}' and partgento >= '{$year}')");
        $R = $this->dbHandle->query("select * from `3P_plugmaster` where `stock`='yes' and `vfam` = '{$vfam}'");
        if( $R->getNumRows()>0 ){
            $A = $R->getResultArray();

            $dataToReturn['status'] = 0;
            $dataToReturn['load'] = $A;
        }else{
            //ERROR - PARTS MATCHING FAILED
            $dataToReturn['status'] = 3;
            $dataToReturn['extra'] = "na_part";
            $dataToReturn['load'] = ["vfam"=>$vfam, "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;
    // }
}