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

class Setups extends BaseController{
    private $dbHandle;

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

        $now = time();
        
        $companyid = 1001;
        $R = $this->dbHandle->query("select count(*) as 'all' from `vendors`");
        $A = $R->getResultArray();
        if(sizeof($A)>0){
            $companyid = $companyid + $A[0]['all'];
        }
        
        //get the district
        $region = $this->request->getVar('region');
        $town = $this->request->getVar('town');
        $mmda = '';
            
        $R = $this->dbHandle->query("select mmda from `locations` where region='{$region}' and town='{$town}' order by mmda DESC limit 1");
		$A = $R->getResultArray();
        if(sizeof($A)>0){
            $mmda = $A[0]['mmda'];
        }
         
        $data = [
            'companyid'         => $companyid,
            'companyname'       => ucwords($this->request->getVar('companyname')),
            'companyemail'      => $this->request->getVar('companyemail'),
            'companyphone'      => $this->request->getVar('companyphone'),
            'streetname'        => $this->request->getVar('streetname'),
            'streetno'          => $this->request->getVar('streetno'),
            'region'            => $region,
            'district'          => $mmda,
            'town'              => $town,
            'mdname'            => $this->request->getVar('mdname'),
            'mdphone'           => $this->request->getVar('mdphone'),
            'storekeepername'   => $this->request->getVar('storekeepername'),
            'storekeeperphone'  => $this->request->getVar('storekeeperphone'),
            'note'              => $this->request->getVar('note'),
            'personnel'         => $this->request->getVar('personnel'),
            'transactdate'      => date('Y-m-d H:i:s', $now)
        ];
        $data2 = [
            'uid'               => md5(uniqid(rand(), true)),
            'pubid'             => '',
            'firstname'         => $data['mdname'],
            'lastname'          => '',
            'phone'             => $data['mdphone'],
            'address'           => '',
            'email'             => $data['companyemail'],
            'password'          => '0000',
            'resourceid'        => $data['companyid'],
            'resourcetype'      => 'Vendor',
            'department'        => 'Business',
            'position'          => 'MD',
            'level'             => '',
            'note'              => 'Auto Generated',
            'transactdate'      => $data['transactdate'],
        ];
        
        $this->dbHandle->transStart();
        $this->dbHandle->table('vendors')->insert($data);
        $this->dbHandle->table('registry_general')->insert($data2);
        $this->dbHandle->transComplete();

        if($this->dbHandle->transStatus() === true) {
            $dataToReturn['status'] = 0;
            $dataToReturn['extra'] = $data2;
        }else{
            $dataToReturn['status'] = 1;
        }

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