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

class Addplugdescription extends BaseController{
    private $dbHandle;

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

        $now = time();
         
        $data = [
            'plugbrand' => $this->request->getVar('plugbrand'),
            'partno' => $this->request->getVar('partno'),
            'oemno' => $this->request->getVar('oemno'),
            'localref' => $this->request->getVar('localref'),
            'plugtype' => $this->request->getVar('plugtype'),
            '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 `plugdescriptions` where 
            `plugbrand` = '{$data['plugbrand']}' and 
            `partno` = '{$data['partno']}'
          ")->getResultArray()) > 0){//exists
            $dataToReturn['status'] = 1;
        }else{
            $this->dbHandle->table('plugdescriptions')->insert($data);
            $E = $this->dbHandle->error();
            $dataToReturn['status'] = $E['code'];
        }

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