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

class Addplugtype extends BaseController{
    private $dbHandle;

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

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

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