<?php

Class Student extends MY_BackendController{
    public function __construct(){
        parent::__construct();
       
    }
    
    public function index(){
        $data['_inactive_loading'] = true;
        $this->view('backend/student/index',$data);
    }

    public function insert(){
        $request = $this->input->post();
        $data = [
            'student_no' =>$request['student_no'],
            'chi_name' =>$request['chi_name'],
            'eng_name' =>$request['eng_name'],
            'gender' =>$request['gender'],
            'dob' =>$request['dob'],
            'recommand' =>$request['recommand'],
            'remark' =>$request['remark'],
            'content1_name'=>$request['contact_1_name'],
            'content1_phone' => $request['contact_1_phone'],
            'content1_email' => $request['contact_1_email'],
            'content1_relationship' =>$request['contact_1_relationship'],
            'content2_name'=>$request['contact_2_name'],
            'content2_phone' => $request['contact_2_phone'],
            'content2_email' => $request['contact_2_email'],
            'content2_relationship' =>$request['contact_2_relationship']

        ];
        $p_data = [
            "chi_name"=>$request["contact_1_name"],
            'mobile' =>$request['contact_1_phone'],
            'password'=>sha1($request['contact_1_phone']),
            'email'=>$request['contact_1_email'],
            'username'=>$request['contact_1_email']
        ];
        $sp_data = [
            "chi_name"=>$request["contact_2_name"],
            'mobile' =>$request['contact_2_phone'],
            'password'=>sha1($request['contact_2_phone']),
            'email'=>$request['contact_2_email'],
            'username'=>$request['contact_2_email']
        ];

        $this->load->model('Student_model');
        $student_id = $this->Student_model->create($data);
       
        $this->load->model("Parent_model");
        $parent_id = $this->Parent_model->create($p_data);
        $s_parent_id = $this->Parent_model->create($sp_data);
        $this->Parent_model->link($student_id,$parent_id['id']);
        $this->Parent_model->link($student_id,$s_parent_id['id']);
       
        

       redirect('backend/student/index');
      

    }

    public function edit(){
        $get = $this->input->get();
        $this->load->model("Student_model");
        $data = $this->Student_model->get_id($get['id']);
        $data['id'] = $get['id'];
        $data['_inactive_loading'] = true;
        $this->view('backend/student/edit',$data);
    }

    public function update(){
        $post = $this->input->post();
        $this->load->model("Student_model");
        $id = $post['id'];
        $this->Student_model->update_information($post, $id);
        redirect('backend/student/index');
    }

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

}