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

class Student extends MY_BackendController
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Student_model');
    }

    public function index()
    {

        $this->view('backend/student/index', []);
    }

    public function add()
    {
        $this->load->model('Location_model');
        $this->load->model('School_district_model');
        $this->load->model('Student_grade_model');
        $this->load->model('Student_source_model');
        //        print_r ($this->School_district_model->district());
        $default = ['status' => 1];
        $this->view('backend/student/add', [
            'status' => $this->Student_model->status,
            'gender' => $this->Student_model->gender,
            'relationship' => $this->Student_model->relationship,
            'location' => $this->Location_model->location(),
            'school_district' => $this->School_district_model->district(),
            'student_grade' => $this->Student_grade_model->grade(),
            'student_source' => $this->Student_source_model->source(),
            'default' => $default,
        ]);
    }

    public function insert($student_no)
    {
        $record = $this->Student_model->getStudentFromStudentNo($student_no);
        $this->load->model('Location_model');
        $this->load->model('School_district_model');
        $this->load->model('School_model');
        $this->load->model('Student_grade_model');
        $this->load->model('Student_source_model');
        //        $id = 34;
        if ($record['district_id']) {
            $district = $this->Location_model->district($record['district_id']);
            $zone = [];
            if (!empty($district)) {
                foreach ($district as $dist) {
                    if ($dist['id'] == $record['area_id']) {
                        $zone = explode(",", $dist['chi_name']);
                        break;
                    }
                }
            }
        } else {
            $district = [];
            $zone = [];
        }
        $school = [];
        if ($record['school_district_id']) {
            $school_district = $this->School_district_model->one($record['school_district_id']);
            $school = $this->School_model->school($school_district['chi_name']);
        }
        $this->view('backend/student/insert', [
            'status' => $this->Student_model->student_status,
            'gender' => $this->Student_model->gender,
            'relationship' => $this->Student_model->relationship,
            'location' => $this->Location_model->location(),
            'district' => $district,
            'zone' => $zone,
            'school_district' => $this->School_district_model->district(),
            'school' => $school,
            'student_grade' => $this->Student_grade_model->grade(),
            'student_source' => $this->Student_source_model->source(),
            'default' => $record,
        ]);
    }

    public function edit()
    {
        $request = $this->input->post();
        $result = $this->Student_model->one($request['id']);
        $this->load->model('Location_model');
        $this->load->model('School_district_model');
        $this->load->model('School_model');
        $this->load->model('Student_grade_model');
        $this->load->model('Student_source_model');
        $this->load->model('Invoicing_model');
        $school_district = $this->School_district_model->district();
        $district = "";
        foreach ($school_district as $dd) {
            if ($dd['id'] == $result['school_district_id']) {
                $district = $dd['chi_name'];
                break;
            }
        }
        $school = [];
        if ($district != "") {
            $school = $this->School_model->school($district);
        }
        $district = $this->Location_model->district($result['district_id']);
        $area = [];
        foreach ($district as $d) {
            if ($d['id'] == $result['area_id']) {
                $area = explode(",", $d['chi_name']);
                break;
            }
        }
        $this->view('backend/student/edit', [
            'student' => $result,
            'gender' => $this->Student_model->gender,
            'relationship' => $this->Student_model->relationship,
            'location' => $this->Location_model->location(),
            'district' => $district,
            'area' => $area,
            'school_district' => $school_district,
            'school' => $school,
            'student_grade' => $this->Student_grade_model->grade(),
            'student_source' => $this->Student_source_model->source(),
            'student_status' => $this->Student_model->student_status,
            'student_invoices' => $this->Invoicing_model->getAllInvoicesByStudentId($request['id']),
        ]);
    }

    // Insert / Update
    public function save()
    {
        $request = $this->input->post();
        $data = [
            //            'code' => $request['code'],
            'chi_name' => $request['chi_name'],
            'eng_name' => $request['eng_name'],
            'gender' => $request['gender'],
            'dob' => $request['dob'],
            'district_id' => $request['district_id'],
            'area_id' => $request['area_id'],
            'zone' => $request['zone'],
            'address' => $request['address'],
            'school_district_id' => $request['school_district_id'],
            'school_id' => $request['school_id'],
            'grade' => $request['grade'],
            'source' => $request['source'],
            'recommended_by' => $request['recommended_by'],
            'status' => $request['status'],
            'remark' => $request['remark'],
        ];
        for ($i = 1; $i <= 3; $i++) {
            $data['content' . $i . '_name'] = $request['content' . $i . '_name'];
            $data['content' . $i . '_relationship'] = $request['content' . $i . '_relationship'];
            $data['content' . $i . '_phone'] = $request['content' . $i . '_phone'];
            $data['content' . $i . '_email'] = $request['content' . $i . '_email'];
        }
        $this->Student_model->save($data, $request['student_id']);
        header('location: /student');
    }

    public function delete()
    {
    }

    public function create_parent($student_no)
    {
        $student = $this->Student_model->createParent($student_no);
        $this->view('backend/student/create_parent', [
            'parents' => $student['parents'],
        ]);
    }

    public function attach_parent($student_no)
    {
        $student = $this->Student_model->getStudentFromStudentNo($student_no);
        $this->view('backend/student/attach_parent', [
            'student' => $student,
            'gender' => $this->Student_model->gender,
        ]);
        //		$this->Student_model->attachParent($student_no);
    }

    public function add_parent($student_no)
    {
        $student = $this->Student_model->getStudentFromStudentNo($student_no);
        $this->view('backend/student/add_parent', [
            'student' => $student,
            'gender' => $this->Student_model->gender,
        ]);
        //		$this->Student_model->attachParent($student_no);
    }

    public function search_student()
    {
        $params = $this->input->get();
        $studentNo = trim($params['student_no']);
        $studentName = trim($params['student_name']);
        // $studentCentreCode = trim($params['student_centre_code']);

        $this->load->model('Student_model');
        echo json_encode([
            'success' => true,
            'data' => [
                'error' => 0,
                'result' => $this->Student_model->search_student($studentNo, $studentName),
            ],
        ], JSON_UNESCAPED_UNICODE);
    }

    public function search_student_by_course()
    {
        $course_id = $this->input->get('id');
        if (isset($course_id)) {
            $this->load->model('Student_model');

            $students = $this->Student_model->search_student_by_course($course_id);
            echo json_encode(array('students' => $students));
        } else {
            echo json_encode([]);
        }
    }
}
