<?php

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

class Note extends MY_BackendController {

    public function __construct() {
        parent::__construct();
        $this->load->model('Note_model');
    }

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

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

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

    public function insert() {
        $input = $this->input->post();
        if (!empty($input)) {
            if (!empty($_FILES)) {//mandatory
                $file = $_FILES['note'];
                if (file_exists($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
                    $fileExt = pathinfo($_FILES['note']['name'], PATHINFO_EXTENSION);
                    $tenantno = $this->session->userdata('tenant_no');
//                    log_message('debug','tenantno:'.$tenantno);
//                log_message('debug','ext:'.$fileExt);//pdf
                    $data = array(
                        'unit_id' => $this->input->post('unit_id'),
                        'title' => $this->input->post('title'),
                        'remark' => $this->input->post('remark'),
                        'tenant_no' => $tenantno,
                        'ext' => $fileExt,
                    );
                    $noteCode = $this->Note_model->insert($data);
//                $noteCode = $this->notes_model->add($data);
					$this->_check_path(UPLOADSPATH . DS . $tenantno);
					$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "notes");
                    $newName = UPLOADSPATH . DS . $tenantno . DS . "notes" . DS . $noteCode . "." . $fileExt;
                    //        $newName = "..\\..\\cache\\" . $noteCode . "." . $fileExt;
//                log_message('debug', 'path:' . $newName);
                    rename($file['tmp_name'], $newName);
                    chmod($newName, 0644);
                }
            }
            header('location: /note');
        }
    }

    public function edit() {
        $data = $this->Note_model->one($this->input->post('id'));
        $this->view('backend/note/edit', ['status' => $this->Note_model->status, 'data' => $data, 'tenant_no'=>$this->session->userdata('tenant_no')]);
    }

    // Insert / Update
    public function save() {
        $input = $this->input->post();
        $id = $input['id'];
        if (!empty($input)) {
            $tenantno = $this->session->userdata('tenant_no');
            $fileExt = '';
            $filename=$this->Note_model->one($id)['filename'];
            if (!empty($_FILES)) {
                $file = $_FILES['note'];
                if (file_exists($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
                    $fileExt = pathinfo($file['name'], PATHINFO_EXTENSION);
                    $noteCode = $this->Note_model->one($id)['note_code'];
                    $filename = $noteCode . "." . $fileExt;
					$this->_check_path(UPLOADSPATH . DS . $tenantno);
					$this->_check_path(UPLOADSPATH . DS . $tenantno . DS . "notes");
                    $newName = UPLOADSPATH . DS . $tenantno . DS . "notes" . DS . $filename;
                    rename($file['tmp_name'], $newName);
                    chmod($newName, 0644);
                }
            }
            $data = array(
                'unit_id' => $this->input->post('unit_id'),
                'title' => $this->input->post('title'),
                'remark' => $this->input->post('remark'),
                'tenant_no' => $tenantno,
                'status' => $this->input->post('status'),
                'ext' => $fileExt,
                'filename'=>$filename,
            );
        }
        $this->Note_model->save($data, $id);
        header('location:/note');
    }

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

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

}
