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

class Simpleform extends BaseController{
    private $dbHandle;

    public function index(){
        $this->dbHandle   = \Config\Database::connect();
        
        $paramsar = explode("~|~", $this->request->getVar('params'));
        $valuesar = explode("~|~", $this->request->getVar('values'));
        $destination = $this->request->getVar('destination');
        
        $dataToReturn = [];
        $now = time();
        $data = [];
        
        for($i=0; $i<count($paramsar); $i++){
            $data[$paramsar[$i]] = $valuesar[$i];
        }
        $data['personnel'] = $this->request->getVar('personnel');
        $data['transactdate'] = date('Y-m-d H:i:s', $now);
        
        $this->dbHandle->table($destination)->insert($data);
        $E = $this->dbHandle->error();
        $dataToReturn['status'] = $E['code'];

        return json_encode($dataToReturn, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
    
    public function collectinfo(){
        $this->dbHandle   = \Config\Database::connect();
        
        $paramsar = explode("~|~", $this->request->getVar('params'));
        $valuesar = explode("~|~", $this->request->getVar('values'));
        $destination = $this->request->getVar('destination');
        
        $dataToReturn = [];
        $now = time();
        $data = [];
        
        for($i=0; $i<count($paramsar); $i++){
            $data[$paramsar[$i]] = $valuesar[$i];
        }
        $data['relatedbundle'] = $this->request->getVar('relatedbundle');
        $data['type'] = $this->request->getVar('type');
        $data['transactdate'] = date('Y-m-d H:i:s', $now);
        
        $this->dbHandle->table($destination)->insert($data);
        $E = $this->dbHandle->error();
        $dataToReturn['status'] = $E['code'];

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