<?php

namespace App\Controllers;

use App\Models\AccountsModel;
use CodeIgniter\Controller;

class Accounts extends Controller {

	protected $helper = ['form'];
	
	public function index() {
		$db = \Config\Database::connect();

		$sql = "select * from areas";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['areas'] = $results;

		if ($this->request->getMethod() === 'POST') {

			$accountname = strtolower($this->request->getVar('accountname'));
			$data['accountname'] = $accountname;
			
			$areaid = $this->request->getVar('areaid');
			$data['areaid'] = $areaid;
			
			$filterquery = '';
			if($accountname != '') { $filterquery .= " and lower(a.accountname) like '%$accountname%' "; }
			if($areaid != '') { $filterquery .= " and az.areaid='$areaid' "; }
			
			$sql = "SELECT a.accountid, a.zoneid, a.accountname, a.initial, a1.`accountname` AS parentname, a.createdate, a.enddate, ar.`areaname`, ar.areaid
				FROM accounts a
					LEFT JOIN accounts a1 ON a1.`accountid`=a.`parentaccount`
					LEFT JOIN areazone az ON az.`zoneid`=a.`zoneid`
					LEFT JOIN areas ar ON ar.`areaid`=az.`areaid`
				WHERE a.accountname is not null $filterquery
				ORDER BY
				CASE WHEN a.`parentaccount`=0 THEN a.`accountid` 
					ELSE a.`parentaccount` END,
				COALESCE(a.`parentaccount`, '0'),
				a.`accountname`";
				
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['accounts'] = $results;
		}
		
		return view('accounts_index', $data);
	}
	
	public function view($accountid = null) {
		$db = \Config\Database::connect();
		$sql = "SELECT a.*, a1.accountname AS parentname, z.zonename, z2.zonename AS province
        FROM accounts a
            LEFT JOIN accounts a1 ON a1.accountid = a.parentaccount
            LEFT JOIN zones z ON z.zoneid = a.zoneid
            LEFT JOIN zones z2 ON z2.zoneid = z.parentzoneid
			WHERE a.accountid='$accountid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['accounts'] = $results;
		return view('accounts_view', $data);
	}
		
	public function edit($accountid = null) {
		$db = \Config\Database::connect();

		if($accountid != 0) {
			$sql = "SELECT a.*, a1.`accountname` AS parentname 
				FROM accounts a
					LEFT JOIN accounts a1 ON a1.`accountid`=a.`parentaccount`
				WHERE a.accountid='$accountid'";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['accounts'] = $results;

			// query province / parentzone
			$zoneid = $results[0]['zoneid'];
			
			if($zoneid != '' || $zoneid != 0) {
				$sql = "select parentzoneid from zones where zoneid='$zoneid'";	
				$query = $db->query($sql);
				$results = $query->getResultArray();
				if(isset($results[0])) { $data['parentzoneid'] = $results[0]['parentzoneid']; } 
				else { $data['parentzoneid'] = ''; }
			}
		}
		
		if(isset($zoneid)) {
			$sql = "SELECT * from zones where zoneclass in ('KOTA','KAB')";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['zones'] = $results;
		}
		
		$sql = "SELECT * from zones where parentzoneid is null";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['parentzones'] = $results;
		
		$sql = "SELECT accountid, accountname FROM accounts WHERE parentaccount='0' and accountid<>'$accountid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['parentaccounts'] = $results;

		
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'accountid' => 'required',
				'accountname' => 'required'
			];
			$data['new_value'] = [
				'accountid' => $this->request->getVar('accountid'),
				'accountname' => $this->request->getVar('accountname'),
				'parentaccount'  => $this->request->getVar('parentaccount'),
				'accountnpwp'  => $this->request->getVar('accountnpwp'),
				'initial'  => $this->request->getVar('initial'),
				'street_1'  => $this->request->getVar('street_1'),
				'street_2'  => $this->request->getVar('street_2'),
				'street_3'  => $this->request->getVar('street_3'),
				'zoneid'  => $this->request->getVar('zoneid'),
				'zip'  => $this->request->getVar('zip'),
				'country'  => $this->request->getVar('country'),
				'email_1'  => $this->request->getVar('email_1'),
				'email_2'  => $this->request->getVar('email_2'),
				'phone'  => $this->request->getVar('phone'),
				'fax'  => $this->request->getVar('fax')
			];
			if($this->validate($rules)){
				if($accountid != 0 ) {
					$accountsModel = new AccountsModel();	
					$accountsModel->update($accountid, $data['new_value']);
					return view('form_success');
				} else {
					$accountsModel = new AccountsModel();
					$accountsModel->set('createdate', 'NOW()', FALSE);
					$accountsModel->insert($data['new_value']);
					return view('form_success');
				}
			} else {
				$data['validation'] = $this->validator;
				return view('accounts_editor',$data);
			}
		}
		$data['accountid']= $accountid;
		return view('accounts_editor', $data);
	}

	public function getcity($provinceid=null){
		$db = \Config\Database::connect();
		$sql = "SELECT * from zones where parentzoneid='$provinceid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$datas = "<option value=''>-</option>";
		foreach ($results as $data) {
			$qzoneid = $data['zoneid'];
			$qzonename = $data['zonename'];
			$datas .= "<option value='$qzoneid'>$qzonename</option>";
		}
		//return view('activities_getproduct', $datas);
		return $datas;
	}

	public function toggle($accountid = 0) {
		$db = \Config\Database::connect();
		$sql = "update accounts set enddate=
				case when enddate is not null then null
				else NOW()
				end
			where accountid='$accountid'";
		if($db->query($sql)) { return view('form_success'); } 
		else { return view('form_fail'); }
	}
	
}