<?php

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

class Exercise extends MY_BackendController {

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

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

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

    public function insert() {
        $input = $this->input->post();
        if (!empty($input)) {
            if (count($_FILES) > 0) { //mandatory
                $fileExt = pathinfo($_FILES['exercise']['name'], PATHINFO_EXTENSION);
                $tenantno = $this->session->userdata('tenant_no');
//                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,
                );
                $exerciseCode = $this->Exercise_model->insert($data);
//                $exerciseCode = $this->exercises_model->add($data);
                $newName = UPLOADSPATH . DS . $tenantno . DS . "exercises" . DS . $exerciseCode . "." . $fileExt;
                //        $newName = "..\\..\\cache\\" . $exerciseCode . "." . $fileExt;
//                log_message('debug', 'path:' . $newName);
                rename($_FILES['exercise']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            header('location: /exercise');
        }
    }
    
    public function edit() {
        $data = $this->Exercise_model->one($this->input->post('id'));
         $this->load->model('Course_model');
        $courses = $this->Course_model->all();
        $this->load->model('Tenant_model');
        $tenantlist = $this->Tenant_model->all();
        $this->view('backend/exercise/edit', ['status' => $this->Exercise_model->status, 'data' => $data,'courses'=>$courses,'tenantlist'=>$tenantlist]);
    }

    // Insert / Update
    public function save() {
        $input = $this->input->post();
        $id = $input['id'];
        if (!empty($input)) {            
            $data = array(                
                'title' => $this->input->post('title'),
                'exercise_date'=>$this->input->post('exercise_date'),
                'course_id'=>$this->input->post('course_id'),
                'remark' => $this->input->post('remark'),
                'tenant_no' => $this->input->post('tenant_no'),
                'status'=>$this->input->post('status'),
            );
        }
        $this->Exercise_model->save($data, $id);
        header('location:/exercise');
    }

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

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

}
