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

class Addvehicle extends BaseController{
    private $dbHandle;

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

        $now = time();
         
        $data = [
            'vin'       => $this->request->getVar('vin'),
            'modulecat' => $this->request->getVar('modulecat'),
            'module'    => $this->request->getVar('module'),
            'len'       => $this->request->getVar('len'),
            'vfam'      => $this->request->getVar('vfam'),
            'slen'      => $this->request->getVar('slen'),
            'vinprefix' => $this->request->getVar('vinprefix'),
            'make'      => $this->request->getVar('make'),
            'model'     => $this->request->getVar('model'),
            'year'      => $this->request->getVar('year'),
            'yearbegin' => $this->request->getVar('yearbegin'),
            'yearend'   => $this->request->getVar('yearend'),
            'engine'    => $this->request->getVar('engine'),
            'englayout' => $this->request->getVar('englayout'),
            'fueltype'  => $this->request->getVar('fueltype'),
            'capacity'  => $this->request->getVar('capacity'),
            'inlitres'  => $this->request->getVar('inlitres'),
            'noofplugs' => $this->request->getVar('noofplugs'),
            'engdesc'   => $this->request->getVar('engdesc'),
            'oemno'     => $this->request->getVar('oemno'),
            'afbrand'   => $this->request->getVar('afbrand'),
            'partno'    => $this->request->getVar('partno'),
            'localref'  => $this->request->getVar('localref'),
            'plugnote'  => $this->request->getVar('plugnote'),
            'body'      => $this->request->getVar('body'),
            'seats'     => $this->request->getVar('seats'),
            'doors'     => $this->request->getVar('doors'),
            'datasource' => $this->request->getVar('datasource'),
            
            'note' => $this->request->getVar('note'),
            'personnel' => $this->request->getVar('personnel'),
            'transactdate' => date('Y-m-d H:i:s', $now)
        ];
        
        $this->dbHandle->table('vehicledescriptions')->insert($data);
        $E = $this->dbHandle->error();
        $dataToReturn['status'] = $E['code'];
        
        //build the other tables before returning
        switch(strtolower($data['modulecat'])){
            case "mo":
            $D = [
                'vin'       =>$data['vin'],
                'vfam'      =>$data['vfam'],
                'year'      =>0,
                'vinprefix' =>'',
                'yearfrom'  =>$data['yearbegin'],
                'yearto'    =>$data['yearend'],
                'datasource'   =>$data['datasource']
            ];
            for($i = $D['yearfrom']; $i <= $D['yearto']; $i++){
                $D['year'] = $i;
                $D['vinprefix'] = $D['vfam']."*".$this->getYearCode($i, $D['yearfrom'], $D['yearto']);
                $this->dbHandle->table('moyt')->insert($D);
            }
            break;
            
            case "ma":
            $D = [
                'vin'=>$data['vin'],
                'vfam'=>$data['vfam'],
                'module'=>$data['module'],
                'len'=>$data['len'],
                'year'=>0,
                'slen'=>$data['slen'],
                'sfrom'=>'',
                'sto'=>'',
                'count'=>0,
                'yearfrom'=>$data['yearbegin'],
                'yearto'=>$data['yearend'],
                'datasource'=>$data['datasource']
            ];
            for($i = $D['yearfrom']; $i <= $D['yearto']; $i++){
                $D['year'] = $i;
                $this->dbHandle->table('mayt')->insert($D);
            }
            break;
        }
        
        return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    private function getYearCode($year, $from, $to){
        $codes = ["A","B","C","D","E"];
        return $codes[array_rand($codes)];
    }
}