<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Question extends MY_BackendController {

	public $chineseNumber = [1 => '一', 2 => '二', 3 => '三', 4 => '四'];
    public function __construct() {
        parent::__construct();
        $this->load->model('Question_model');
    }

    public function index() {
        $this->view('backend/question/index', [
			'tenantno' => $this->session->userdata('tenant_no'),
		]);
    }

    public function add() {
        $this->view('backend/question/add', [
        	'chineseNumber' => $this->chineseNumber,
		]);
    }

	private function _check_path($path) {
		if (! file_exists($path)) {
			mkdir($path);
		}
	}

	public function insert() {
        $input = $this->input->post();
        if (!empty($input)) {
            $tenantno = $this->session->userdata('tenant_no');
            $fileExt = '';
            $solutionExt = '';
            if (!empty($_FILES)) {
                if (array_key_exists('picture', $_FILES)) {
                    $fileExt = pathinfo($_FILES['picture']['name'], PATHINFO_EXTENSION);
                }
                if (array_key_exists('solution_picture', $_FILES)) {
                    $solutionExt = pathinfo($_FILES['solution_picture']['name'], PATHINFO_EXTENSION);
                }
            }
//            log_message('debug','ext:'.$fileExt.';solutionext:'.$solutionExt);
            $data = array(
                'unit_id' => $this->input->post('unit_id'),
                'question' => $this->input->post('question'),
                'answer1' => $this->input->post('answer1'), 'answer2' => $this->input->post('answer2'), 'answer3' => $this->input->post('answer3'), 'answer4' => $this->input->post('answer4'),
                'correct' => $this->input->post('correct'),
                'solution' => $this->input->post('solution'),
                'solution_youtube' => $this->input->post('solution_youtube'),
                'remark' => $this->input->post('remark'),
                'ext' => $fileExt,
                'solution_ext' => $solutionExt,
                'tenant_no' => $tenantno,
            );
            $questionCode = $this->Question_model->insert($data);

			$this->_check_path(UPLOADSPATH . DS . $tenantno);
            if (!empty($fileExt)) {
				$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "questions");
                $newName = UPLOADSPATH . DS . $tenantno . DS . "questions" . DS . $questionCode . "." . $fileExt;
                rename($_FILES['picture']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            //todo:
            if (!empty($solutionExt)) {
				$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "solutions");
                $newName = UPLOADSPATH . DS . $tenantno . DS . "solutions" . DS . $questionCode . "." . $solutionExt;
                rename($_FILES['solution_picture']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            header('location: /question');
        }
    }

    public function edit() {
//    	log_message('debug','id:'.$this->input->post('id'));
        $data = $this->Question_model->one($this->input->post('id'));
        $this->view('backend/question/edit', ['status' => $this->Question_model->status, 'data' => $data, 'tenant_no'=>$this->session->userdata('tenant_no'), 'chineseNumber' => $this->chineseNumber]);
    }

    // Insert / Update
    public function save() {
        $input = $this->input->post();
        $id = $input['id'];
        if (!empty($input)) {
            $tenantno = $this->session->userdata('tenant_no');
            $fileExt = '';
            $solutionExt = '';
            $question = $this->Question_model->one($id);
            $filename=$question['filename'];
            $solutionfilename=$question['solution_filename'];
//            log_message('debug','empty files?'.empty($_FILES));
            if (!empty($_FILES)) {
//                 log_message('debug','array key picture??'.array_key_exists('picture', $_FILES));
				$this->_check_path(UPLOADSPATH . DS . $tenantno);
                if (array_key_exists('picture', $_FILES)) {
                    $file = $_FILES['picture'];
//                     log_message('debug','file_exists($file[tmp_name]) ?'.file_exists($file['tmp_name']) .';is uploaded file?'.is_uploaded_file($file['tmp_name']));
                    if (file_exists($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
//                        log_message('debug', $file['name']);    //thumb-jpg.png
                        $fileExt = pathinfo($file['name'], PATHINFO_EXTENSION);
//                        log_message('debug', $fileExt.';id:'.$id);   //png;id:45
                        $questionCode = $this->Question_model->one($id)['question_code'];
//                        log_message('debug','questioncode:'.$questionCode.';tenantno:'.$tenantno);//questioncode:QUESTION0002400002;tenantno:kids
                        $filename=$questionCode . "." . $fileExt;
                        $newName = UPLOADSPATH . DS . $tenantno . DS . "questions" . DS . $filename;
//                        log_message('debug','newname:'.$newName);//C:\xampp\htdocs\log-in-education\application\..\public\uploads\\kids\questions\QUESTION0002400002.png
						$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "questions");
                        rename($file['tmp_name'], $newName);
                        chmod($newName, 0644);
                    }
                }
                if (array_key_exists('solution_picture', $_FILES)) {
                    $file = $_FILES['solution_picture'];
                    if (file_exists($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
                        $solutionExt = pathinfo($file['name'], PATHINFO_EXTENSION);
                        $questionCode = $this->Question_model->one($id)['question_code'];
                        $solutionfilename = $questionCode . "." . $solutionExt;
                        $newName = UPLOADSPATH . DS . $tenantno . DS . "solutions" . DS . $solutionfilename;
						$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "solutions");
                        rename($file['tmp_name'], $newName);
                        chmod($newName, 0644);
                    }
                }
            }
            $data = array(
                'unit_id' => $this->input->post('unit_id'),
                'question' => $this->input->post('question'),
                'answer1' => $this->input->post('answer1'), 'answer2' => $this->input->post('answer2'), 'answer3' => $this->input->post('answer3'), 'answer4' => $this->input->post('answer4'),
                'correct' => $this->input->post('correct'),
                'solution' => $this->input->post('solution'),
                'solution_youtube' => $this->input->post('solution_youtube'),
                'remark' => $this->input->post('remark'),
                'ext' => $fileExt,
                'filename'=>$filename,
                'solution_ext' => $solutionExt,
                'solution_filename'=>$solutionfilename,
                'tenant_no' => $tenantno,
                'status' => $this->input->post('status'),
            );
        }
        $this->Question_model->save($data, $id);
        header('location:/question');
    }

    public function delete() {
        $request = $this->input->post();
        $ids = explode(',', $request['ids']);
//       log_message('debug','is ids array?'.is_array($ids));//true
        $this->Question_model->deleteMultiple($ids);
        header("location:/question");
    }

    public function deleteall() {
        $this->Question_model->deleteAll();
        header("location:/question");
    }

}
