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

class AddModel extends BaseController{
    private $dbHandle;

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

        $now = time();
         
        $data = [
            'make' => ucwords($this->request->getVar('make')),
            'model' => ucwords($this->request->getVar('model')),
            'note' => $this->request->getVar('note'),
            'personnel' => $this->request->getVar('personnel'),
            'transactdate' => date('Y-m-d H:i:s', $now)
        ];
        
        //check for existence
        if( sizeof( $this->dbHandle->query("select * from `vehiclemodels` where 
            `make` = '{$data['make']}' and 
            `model` = '{$data['model']}'
          ")->getResultArray()) > 0){//exists
            $dataToReturn['status'] = 1;
        }else{
            $this->dbHandle->table('vehiclemodels')->insert($data);
            $E = $this->dbHandle->error();
            $dataToReturn['status'] = $E['code'];
        }

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