<?php

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

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

        $this->load->model('Rbac_permission_model', 'Permission_model');
    }

    public function index()
    {
        $data['allcomments'] = $this->Comment_model->get_all_comments();
        $data['controller'] = strtolower(static::class);
        if ($data['controller'] == 'comment_gro') {
            $data['items'] = $this->Comment_model->get_fixed_comments();
        } else {
            $data['items'] = $this->Comment_model->get_fixed_comments($this->getUserId());
        }
        $this->view('backend/comment/index', $data);
    }

    public function add()
    {
        $data['courses'] = $this->Course_model->all();
        $data['controller'] = strtolower(static::class);
        $this->view('backend/comment/add', $data);
    }

    public function edit()
    {
        $data['courses'] = $this->Course_model->all();
        $data['controller'] = strtolower(static::class);

        $data['id'] = $id = $this->input->get('id');
        $ret = $this->Comment_model->get_fixed_comment($id);
        $data['item'] = $ret['comment'];
        $data['dc'] = $ret['dc'];
        $data['pc'] = $ret['pc'];
        $template = 'backend/comment/edit';
        // print_r('<pre>');
        // print_r($data); 
        // print_r('</pre>');
        // die();
        $this->view($template, $data);
    }

    public function save()
    {
        $data = $this->input->post();
        $teacher_id = $this->session->userdata('userid');

        $this->Comment_model->create(array(
            'comment_ids' => implode(',', $data['comments']),
            'student_id' => $data['student_id'],
            'student_chi_name' => $data['student_chi_name'],
            // 'course' => trim(preg_replace('~[\r\n]+~', '', $data['course'])),
            'course_id' => $data['course_id'],
            'course_name' => $data['course_name'],
            'date' => $data['date'],
            'tailor_comment' => $data['tailor_comment'] ?? null,
            'teacher_id' => $teacher_id,
        ));
        echo json_encode('0');
    }

    public function update()
    {
        $data = $this->input->post();
        $id = $data['id'];
        $teacher_id = $this->session->userdata('userid');
        $params = array(
            'comment_ids' => implode(',', $data['comments']),
            'student_id' => $data['student_id'],
            'student_chi_name' => trim($data['student_chi_name']),
            // 'course' => trim(preg_replace('~[\r\n]+~', '', $data['course'])),
            'course_id' => $data['course_id'],
            'course_name' => trim($data['course_name']),
            'date' => $data['date'],
            'updatedate' => date(DATETIME_FORMAT),
            'tailor_comment' => $data['tailor_comment'] ?? null
        );
        if (strtolower(static::class) != 'comment_gro') {
            $params['teacher_id'] = $teacher_id;
        }
        $this->Comment_model->update($id, $params);
        echo json_encode('0');
    }

    public function delete($ids = null)
    {
        $this->Comment_model->delete($this->input->get('id'));
        echo json_encode('0');
    }
}
