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

class Addmodule extends BaseController{
    private $dbHandle;

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

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

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