<?php

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

class Unit extends MY_BackendController {

    public function __construct() {
        parent::__construct();
        $this->load->model('Unit_model');
    }
    
    public function index() {
        $this->view('backend/unit/index', [
			'tenantno' => $this->session->userdata('tenant_no'),
		]);
    }

    public function add() {         
        $this->view('backend/unit/add', []);
    }
    
    public function insert() {
         $input = $this->input->post();
        if (!empty($input)) {            
            $params = ['course_id'=>$input['course_id'],'title'=>$input['title'],'remark'=>$input['remark'],'tenant_no'=>$this->session->userdata('tenant_no')];
            $newid = $this->Unit_model->insert($params);
            header('location: /unit');
        }
    }

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

    // Insert / Update
    public function save() {
        $input = $this->input->post();
        $params = ['course_id'=>$input['course_id'],'title'=>$input['title'],'remark'=>$input['remark'], 'tenant_no'=>$this->session->userdata('tenant_no'), 'status'=>$input['status']];
        $this->Unit_model->save($params,$input['id']);
        header('location:/unit');
    }

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

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

}
