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

class Addengseating extends BaseController{
    private $dbHandle;

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

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

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