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

class Parents extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Parents_model');
    }

    public function listing()
    {
        $request = $this->input->get();
        $data = $this->Parents_model->listing($request);
        echo json_encode($this->Parents_model->build_datatables($request['draw'], $this->Parents_model->totalCount(), $this->Parents_model->filteredCount($request), $data), JSON_UNESCAPED_UNICODE);
    }

    public function create_parent()
    {
        $request = $this->input->post();
        if (empty($request)) {
            echo json_encode([
                'success' => false,
                'data' => [
                    'error' => 1,
                    'result' => [],
                ],
            ], JSON_UNESCAPED_UNICODE);
            exit;
        }
        $this->load->model('Student_model');
        $student = $this->Student_model->getStudentFromStudentNo($request['student_code']);
        $data = [
            'tenant_no' => $this->session->userdata('tenant_no'),
            'student_id' => $student['id'],
            'password' => $request['password'],
            'signed_password' => $request['signed_password'],
            'chi_name' => $request['chi_name'],
            'eng_name' => $request['eng_name'],
            'mobile' => $request['mobile'],
            'email' => $request['email'],
            'remark' => $request['remark'],
        ];
        $id = $this->Parents_model->createParentBatch([
            $data
        ], $request['student_code']);
        $parent = $this->Parents_model->one($id);
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => [
                    'username' => $parent['username'],
                ],
            ],
        ], JSON_UNESCAPED_UNICODE);
    }

    public function update_parent()
    {
        $request = $this->input->post();
        if (!array_key_exists('action', $request) || empty($request)) {
            echo json_encode([
                'success' => false,
                'data' => [
                    'error' => 1,
                    'result' => [],
                ],
            ], JSON_UNESCAPED_UNICODE);
            exit;
        }

        switch ($request['action']) {
            case 'reset_password':
                $this->Parents_model->resetPassword($request['username']);
                break;
            case 'update_info':
                $data = [
                    'chi_name' => $request['chi_name'],
                    'eng_name' => $request['eng_name'],
                    'mobile' => $request['mobile'],
                    'email' => $request['email'],
                    'status' => $request['status'],
                    'remark' => $request['remark'],
                ];
                $this->Parents_model->updateInformation($data, $request['username']);
                break;
        }
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => [],
            ],
        ], JSON_UNESCAPED_UNICODE);
    }

    public function search_student()
    {
        $request = $this->input->get();
        $this->load->model('Student_model');
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => $this->Student_model->searchStudent($request),
            ],
        ], JSON_UNESCAPED_UNICODE);
    }

    public function attach_student()
    {
        $request = $this->input->get();
        $this->Parents_model->attachStudent($request['username'], $request['student_code']);
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => [],
            ],
        ], JSON_UNESCAPED_UNICODE);
    }

    public function detach_student()
    {
        $request = $this->input->get();
        $this->Parents_model->detachStudent($request['username'], $request['student_code']);
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => [],
            ],
        ], JSON_UNESCAPED_UNICODE);
    }
}
