<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Review extends CI_Controller {
	public function __construct() {
		parent::__construct();

		// REDIRECT TO COMPANY IF HAS NOT SELECTED COMPANY YET
		if (!$this->session->userdata('companyID')) redirect(base_url().'company','refresh');

		$this->load->library('layouts');
        $this->load->model('general_model');
		$this->lang->load("kelayakan", $this->session->userdata('language'));

	}

	public function index(){
		$companyID = $this->session->userdata('companyID');
		$data['method'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_method',NULL,NULL,NULL,array('methodCode')));
		if($data['method']) {
			$rs = NULL;
			foreach($data['method'] as $key=>$value) {
				$code = $value['methodCode'];
				$rs[$code] = $this->ObjToArray($this->general_model->get_byFilter('_ref_review', array('*'), array('is_delete'=>0, 'parentID'=>0, 'methodID'=>$value['methodID'])));
				if($rs[$code]){
					foreach($rs[$code] as $index=>$item){
						$rs[$code][$index]['children'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_review', array('*'), array('is_delete'=>0, 'parentID'=>$item['ID'])));
						$rs[$code][$index]['answer'] = $this->ObjToArray($this->general_model->get_byFilter('review', array('*'), array('ref_reviewID'=>$item['ID'], 'companyID'=>$companyID)),true);
					}
				}
			}
			$data['rs'] = $rs;
		} else {
			$data['rs']['A'] = NULL;
			$data['rs']['B'] = NULL;
			$data['rs']['C'] = NULL;
		}

		$this->layouts->view('review_all',array(),$data);
	}

	public function detail($ID = 0){

		if ($ID==0) $this->index();

		$data['ID'] = $ID;
		$data['companyID'] = $this->session->userdata('companyID');

		// GET DETAIL OF REVIEW SUBJECT
		$data['review'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_review', array('*'), array('ID'=>$ID,'is_delete'=>0), array(), array('ID')));
		$data['review'] = ($data['review']) ? $data['review'][0] : NULL;
		$data['review']['answer'] = $this->ObjToArray($this->general_model->get_byFilter('review', array('*'), array('ref_reviewID'=>$ID, 'companyID'=>$data['companyID'])),true);

		// GET REVIEW METHOD CATEGORY
		$data['method'] = ($data['review']) ? $this->getMethod($data['review']['methodID']) : NULL;

		// GET DETAIL CHILDREN
		if( $children = $this->ObjToArray($this->general_model->get_byFilter('_ref_review', array('ID','code','text'), array('parentID'=>$ID,'is_delete'=>0))) ) {
			foreach($children as $child) {
				$data['children'][$child['ID']] = $child;
				$data['children'][$child['ID']]['details'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_review_detail', array('*'), array('reviewID'=>$child['ID'],'is_delete'=>0)));
				if($data['children'][$child['ID']]['details']) {
					foreach($data['children'][$child['ID']]['details'] as &$detail) {
						$detail['answer'] = $this->ObjToArray($this->general_model->get_byFilter('review_detail', array('*'), array('ref_review_detailID'=>$detail['ID'], 'companyID'=>$data['companyID'])),true);
					}
				}
			}
		} else {
			$data['children'][$ID]['ID'] = $data['review']['ID'];
			$data['children'][$ID]['code'] = $data['review']['code'];
			$data['children'][$ID]['text'] = $data['review']['text'];
			$data['children'][$ID]['details'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_review_detail', array('*'), array('reviewID'=>$ID), array(), array('ID')));
			if($data['children'][$ID]['details']) {
				foreach($data['children'][$ID]['details'] as &$detail) {
					$detail['answer'] = $this->ObjToArray($this->general_model->get_byFilter('review_detail', array('*'), array('ref_review_detailID'=>$detail['ID'], 'companyID'=>$data['companyID'])),true);
				}
			}
		}

		// GET ANSWER


		// GET ATTACHMENTS
		$data['attachment'] = $this->general_model->get_byFilter('review_attachment', array('*'), array('reviewID'=>$ID), array(), array('ID')) ;

        // echo $this->db->last_query();
        // var_dump($aaaaaaaa);
        // exit();


		$data['table'] = null; //$this->general_model->get_byFilter($tname='_ref_review_sql',$select=array('*'),$filter=array('reviewID'=>$ID),$join=array(),$order=array('ID')) ;

		$this->layouts->view('review_detail',array(),$data);
	}

	public function save() {
		$post = $this->input->post();
		$ID = 0;
		if($post) {
			$this->load->model('review/reviewMain');
			// UPDATE REVIEW MAIN TABLE
			$ID = $post['ID'];
			$companyID = $post['companyID'];

			$upd['companyID'] = $post['companyID'];
			$upd['ref_reviewID'] = $post['ID'];
			$upd['comment'] = $post['comment'];
			$upd['status'] = $post['status'];
			$upd['mark'] = (strtoupper($upd['status']) == "BELUM SELESAI") ? NULL : $post['mark'];

			if ($this->reviewMain->updateMain($companyID, $reviewID, $upd)) {
				foreach($post['detAnswer'] as $reviewID=>$review_detail) {
					foreach($review_detail as $ref_review_detailID=>$answer) {
						$data = NULL;
						$data['companyID'] = $companyID;
						$data['reviewID'] = $reviewID;
						$data['ref_review_detailID'] = $ref_review_detailID;
						$data['answer'] = $answer;

						if($answer == 1) {
							$data['comment'] = 'MEMATUHI';
						} elseif($answer == 2) {
							$data['comment'] = $post['detComment'][$reviewID][$ref_review_detailID];
						} elseif($answer == 3) {
							$data['comment'] = 'TIDAK BERKAITAN';
						} else {
							$data['comment'] = NULL;
						}

						$this->reviewMain->updateDetail($companyID, $reviewID, $ref_review_detailID, $data);
					}
				}
				/*
				echo "<pre>";
				print_r($data);
				echo "</pre>";

				echo "<pre>";
				print_r($post);
				echo "</pre>";
				*/
			}
		}

		$url = base_url().'review'.(($reviewID) ? '/detail/'.$ID : NULL);
		redirect($url,'refresh');
	}

    public function printDetails($reviewID = 0) {
        if ($reviewID==0) $this->index();

        $data['method'] = $this->ObjToArray($this->general_model->get_byFilter('_ref_method',NULL,NULL,NULL,array('methodCode')));
        // echo $this->db->last_query();
        $this->load->model('review/children');
		$reviewHeader = $this->ObjToArray($this->children->getReviewPrintHeader($reviewID));
        $reviewHeader = $reviewHeader[0];
        $reviewHeaderChildren = $this->ObjToArray($this->children->getReviewChildrenPrintHeader($reviewID));
        $reviewBody = $this->ObjToArray($this->children->getReviewPrintBody($reviewID));

        $body = array(
			'table' => array(
				'header' => array(
                    'reviewHeader' => array(
                        'Kod',
                        'Audit',
    					'Objektif Audit',
                        'Perkara'
                    ),
                    'reviewInfo' => $reviewHeader,
                    'childrenInfo' => $reviewHeaderChildren
				),
				'right-column' => array(),
				'total-column' => array(),
				'body' => array(
                    'detailHeader' => array(
                        'Bil',
                        'Butiran',
                        'Ulasan',
                        'Keputusan'
                    ),
                    'detailInfo' => $reviewBody
                )
			)
		);
        $header = array();
		$header['title'] = 'Butiran Semakan Audit';
        // var_dump($data['method']);
		$this->load->view('template/reports/header.php', $header);
		$this->load->view('template/reports/reviewDetail.php', $body);
		$this->load->view('template/reports/footer.php');
    }

	private function getMethod($id) {
		$rs = $this->general_model->get_byFilter('_ref_method', array('*'), array('methodID'=>$id)) ;
		if($rs) {
			return $this->ObjToArray($rs[0]);
		} else {
			return NULL;
		}
	}

	private function ObjToArray($obj,$single=false) {
		return (!empty($obj)) ? json_decode(json_encode(($single)?$obj[0]:$obj),true) : NULL;
	}





}
