<?php

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

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

    public function insert()
    {
        $request = $this->input->post();

        $data = [
            'name' => $request["course_name"],
            'description' => $request['course_detail'],
            'code' => $request['course_id'],
            'course_category_id' => $request['course_mode'],
            'duration' => $request['course_time'],
            'price' => $request['price'],
            "mode" => "period",
            'course_location' => $request['course_location'],
            'recommand_discount_amount' => $request['recommand'] == "1" ? $request['recommand_discount_amount'] : "0",
            'recommand_discount_detail' => $request['recommand'] == "1" ? $request['recommand_discount_detail'] : " ",
            'double_discount_amount' => $request['double'] == "1" ? $request['double_discount_amount'] : "0",
            'double_discount_detail' => $request['double'] == "1" ? $request['double_discount_detail'] : "",
            'remark' => $request['ps']
        ];

        $this->Course_model->insert($data);
        redirect('/backend/course/');
    }

    public function edit()
    {
        $get = $this->input->get();
        $this->load->model("Course_model");
        $data = $this->Course_model->get_id($get["id"]);
        $this->load->model("Event_course_model");
        $data['course_event'] = $this->Event_course_model->listing_by_Course_id($get['id']);
        
        $data['_inactive_loading'] = true;

        $this->view('backend/course/edit', $data);
    }

    public function update()
    {
        $request = $this->input->post();

        $data = [
            'name' => $request["course_name"],
            'description' => $request['course_detail'],
            'code' => $request['course_id'],
            'course_category_id' => $request['course_mode'],
            'duration' => $request['course_time'],
            'price' => $request['price'],
            'course_location' => $request['course_location'],
            'recommand_discount_amount' => $request['recommand'] == "1" ? $request['recommand_discount_amount'] : "0",
            'recommand_discount_detail' => $request['recommand'] == "1" ? $request['recommand_discount_detail'] : " ",
            'double_discount_amount' => $request['double'] == "1" ? $request['double_discount_amount'] : "0",
            'double_discount_detail' => $request['double'] == "1" ? $request['double_discount_detail'] : "",
            'remark' => $request['ps']
        ];
        $this->load->model("Course_model");
        $this->Course_model->update($data, $request['id']);
        redirect("backend/course/index");
    }

    public function delete()
    {
        $ids = $this->input->post();
        $this->load->model('Course_model');
        foreach ($ids as $id) {
            $this->Course_model->delete($id);
        }
        redirect('backend/course/index');
    }
}
