<?php

namespace App\Controllers;

use App\Models\AreasModel;
use CodeIgniter\Controller;

class Areas extends BaseController {

	protected $data = array();
	
	function __construct() {
		$this->data['zoneclass'] = array('PROP'=>'Province', 'KAB'=> 'Kabupaten', 'KOTA' => 'Kota');
	} 
	
	public function index() {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM areas";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['areas'] = $results;
		return view('areas_index', $data);
	}
		
	public function edit($areaid = null) {
		$db = \Config\Database::connect();
		$data = array();
		
		if($areaid!= 0) {
			$sql = "SELECT * from areas where areaid='$areaid'";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['areas'] = $results;
		}
		
		if ($this->request->getMethod() === 'POST') {
			$rules = [
				'areatype' => 'required',
				'areaname' => 'required',
				'description' => 'required'
			];
			$data['new_value'] = [
				'areatype' => $this->request->getVar('areatype'),
				'areaname' => $this->request->getVar('areaname'),
				'description' => $this->request->getVar('description')
			];
			if($this->validate($rules)){
				if($areaid!= 0 ) {
					$areasModel = new AreasModel();	
					$areasModel->update($areaid, $data['new_value']);
					return view('form_success');
				} else {
					$areasModel = new AreasModel();
					//$areasModel->set('createdate', 'NOW()', FALSE);
					$areasModel->insert($data['new_value']);
					return view('form_success');
				}
			} else {
				$data['validation'] = $this->validator;
				return view('areas_editor',$data);
			}
		}
		return view('areas_editor', $data);
	}
	
	public function areazone_edit($areaid = null) {
		$db = \Config\Database::connect();
		$sql = "SELECT * FROM areas WHERE areaid='$areaid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['areas'] = $results;
		$sql = "SELECT az.areazoneid, az.zoneid, z.zonename FROM areazone az 
			left join zones z on z.zoneid=az.zoneid
			WHERE az.areaid='$areaid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['areazone'] = $results;
		
		if ($this->request->getMethod() === 'POST') {
			$areaid = $this->request->getVar('areaid');
			$zoneid = $this->request->getVar('zoneid');
			$areazoneid_delete = $this->request->getVar('areazoneid_delete');

			$rules = [
				'areaid' => 'required',
				'zoneid' => 'required'
			];

			if($this->validate($rules)){
				if($areazoneid_delete!='') {
					$areazoneid_delete = explode(' ',$areazoneid_delete);
					foreach( $areazoneid_delete as $areazoneid ) {
						if($areazoneid != 0) {
							//$sql = "update sitecontact set enddate=now() where sitecontactid='$sitecontactid'";
							$sql = "delete from areazone where areazoneid='$areazoneid'";
							$query = $db->query($sql);	
						}
					}
				}
				foreach($zoneid as $i => $qzoneid) { 
					if($qzoneid <> '') {
						// insert query
						$sql = "insert into areazone(areaid, zoneid) 
							VALUES ('$areaid', '$qzoneid')
							on duplicate key update areaid='$areaid'";
						$db->query($sql);
					}
				}
				return view('form_success');
			} else {
				$data['validation'] = $this->validator;
				return view('areazone_edit',$data);
			}
		}
		return view('areazone_edit', $data);
	}
	
	public function areazone_newrow() {
		$db = \Config\Database::connect();
		$sql = "SELECT zoneid, zonename FROM zones where zoneclass in ('KOTA','KAB')";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['zones'] = $results;
		return view('areazone_newrow', $data);
	}
	
}