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

class Course extends MY_BackendController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Course_model');
    }

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

    public function add()
    {
        $this->load->model('Course_category_model');
        $result = $this->Course_category_model->all();
        if (count($result) == 0) {
            header("location: /course");
            return;
        }
        $this->view('backend/course/add', ['course_category' => $result, 'mode' => $this->Course_model->getMode()]);
    }

    public function insert()
    {
        $request = $this->input->post();
        $data = [
            'code' => $request['code'],
            'course_category_id' => $request['course_category_id'],
            'course_category_sub_id' => $request['course_subcategory_id'],
            'name' => $request['name'],
            'description' => $request['description'],
            'mode' => $request['mode'],
//            'price' => $request['price'],
//            'qty' => $request['qty'],
            'duration' => $request['duration'],
            'remark' => $request['remark'],
        ];
        $this->Course_model->insert($data);
        header('location: /course');
    }

    public function edit()
    {
        $request = $this->input->post();
        $result = $this->Course_model->one($request['id']);
        $this->load->model('Course_category_model');
        $course_category = $this->Course_category_model->all();
        $this->load->model('Course_subcategory_model');
        $course_subcategory = $this->Course_subcategory_model->all($result['course_category_id']);
        $this->view('backend/course/edit', ['status'=>$this->Course_model->status, 'course' => $result, 'course_category' => $course_category, 'course_subcategory' => $course_subcategory, 'mode' => $this->Course_model->getMode()]);
    }

    // Insert / Update
    public function save()
    {
        $request = $this->input->post();
        $data = [
            'code' => $request['code'],
            'course_category_id' => $request['course_category_id'],
            'course_category_sub_id' => $request['course_subcategory_id'],
            'name' => $request['name'],
            'description' => $request['description'],
            'mode' => $request['mode'],
            'status'=>$request['status'],
//            'price' => $request['price'],
//            'qty' => $request['qty'],
            'duration' => $request['duration'],
            'remark' => $request['remark'],
        ];
//        log_message('debug','code:'.$data['code']);
        $this->Course_model->save($data, $request['course_id']);
        header('location: /course');
    }

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

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