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

class Master extends Admin_Core_Controller {

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

    // Lead Start

    public function all_lead_source()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_lead'] = $this->setting->get_all_lead_source();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/all_lead_source');
        $this->load->view('admin/include/footer');
    }

    function add_new_lead()
    {
        if ($this->setting->add_new_lead()) 
        {
            $this->session->set_userdata('success', "Lead Successfully Added");
            redirect(base_url().'admin/master/all_lead_source');
        } else {
            $this->session->set_userdata('error', " Lead Added Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_lead($id)
    {
        if ($this->setting->delete_lead($id)) 
        {
            $this->session->set_userdata('success', "Lead Successfully Deleted");
            redirect(base_url().'admin/master/all_lead_source');
        } else {
            $this->session->set_userdata('error', " Lead Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    function update_lead($id)
    {
        if ($this->setting->update_lead($id)) 
        {
            $this->session->set_userdata('success', "Lead Successfully Updated");
            redirect(base_url().'admin/master/all_lead_source');
        } else {
            $this->session->set_userdata('error', " Lead Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    // Lead End

    // Follow Up Start

    public function all_followup_mode()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_mode'] = $this->setting->get_all_followup_mode();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/all_followup_mode');
        $this->load->view('admin/include/footer');
    }

    function add_new_followup_mode()
    {
        if ($this->setting->add_new_followup_mode()) 
        {
            $this->session->set_userdata('success', "Follow Up Mode Successfully Added");
            redirect(base_url().'admin/master/all_followup_mode');
        } else {
            $this->session->set_userdata('error', " Follow Up Mode Added Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_followup_mode($id)
    {
        if ($this->setting->delete_followup_mode($id)) 
        {
            $this->session->set_userdata('success', "Follow Up Mode Successfully Deleted");
            redirect(base_url().'admin/master/all_followup_mode');
        } else {
            $this->session->set_userdata('error', " Follow Up Mode Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    function update_followup_mode($id)
    {
        if ($this->setting->update_followup_mode($id)) 
        {
            $this->session->set_userdata('success', "Follow Up Mode Successfully Updated");
            redirect(base_url().'admin/master/all_followup_mode');
        } else {
            $this->session->set_userdata('error', " Follow Up Mode Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    // Follow Up End

    // Batch Start

    public function all_batch()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_batch'] = $this->setting->get_all_batch();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/view_all_batch');
        $this->load->view('admin/include/footer');
    }

    function add_batch()
    {
        $atp_id = user_id();
        $this->load->model('course_model', 'course');
        $this->load->model('user_model', 'user');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->course->get_all_course_for_admission_by_atp_id(user_id());
        $data['all_instractor'] = $this->user->get_all_instructor_by_atp_id();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/add_new_batch');
        $this->load->view('admin/include/footer');
    }

    function add_new_batch()
    {
        $atp_id = user_id();
        if ($this->setting->add_new_batch($atp_id)) 
        {
            $this->session->set_userdata('success', "Batch Successfully Added");
            redirect(base_url().'admin/master/all_batch');
        } else {
            $this->session->set_userdata('error', "Batch Added Failed");
            redirect($this->agent->referrer());
        }
    }

    function edit_batch($id)
    {
        $atp_id = user_id();
        $this->load->model('course_model', 'course');
        $this->load->model('user_model', 'user');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->course->get_all_course_for_admission_by_atp_id(user_id());
        $data['batch'] = $this->setting_model->get_all_batch_by_id($id);
        $data['all_instractor'] = $this->user->get_all_instructor_by_atp_id();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/edit_batch');
        $this->load->view('admin/include/footer');
    }

    function delete_batch($id)
    {
        if ($this->setting->delete_batch($id)) 
        {
            $this->session->set_userdata('success', "Batch Successfully Deleted");
            redirect(base_url().'admin/master/all_batch');
        } else {
            $this->session->set_userdata('error', "Batch Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    function update_batch($id)
    {
        if ($this->setting->update_batch($id)) 
        {
            $this->session->set_userdata('success', "Batch Successfully Updated");
            redirect(base_url().'admin/master/all_batch');
        } else {
            $this->session->set_userdata('error', " Batch Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    public function batch_details($id)
    {
        $this->load->model('user_model', 'user');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_batch_details'] = $this->user->get_student_by_batch_and_atp_id($id);
        $data['batch'] = $this->setting_model->get_all_batch_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/instructor/batch_details');
        $this->load->view('admin/include/footer');
    }

    function batch_progress()
    {
        $this->load->model('user_model', 'user');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        if(isset($_POST['search'])) 
        {
            $this->load->model('course_model','course');
            $batch_id = $this->input->post('batch');
            $user_id = user()->user_id;
            $this->db->select('batch_course');
            $this->db->where('batch_id', $batch_id);
            $this->db->where('instructor_id', $user_id);
            $batch = $this->db->get('student_batch')->row();

            $this->db->where('is_instractor', 1);
            $this->db->where('inst_att_course_id', $batch->batch_course);
            $al_insta_att = $this->db->get('instructor_attendance')->result();

            $this->db->where('ch_course_id', $batch->batch_course);
            $ll_lession_data = $this->db->get('course_lession_and_chapter')->result();

            $this->db->select('a.*, b.user_name, b.user_id');
            $this->db->from('student_course a');
            $this->db->join('users b', 'b.user_id = a.st_course_student_id');
            $this->db->where('a.st_course_batch', $this->input->post('batch'));
            $all_student = $this->db->get()->result();


            $l_data = 0;
            foreach ($ll_lession_data as $les_data) 
            {
                $l_data += count(json_decode($les_data->ch_chapters));
            }

            $lll_dat = $l_data * count($all_student);
            

            $data['batch_progress'] = (($lll_dat - count($al_insta_att))*100) /$lll_dat ;


            $data['all_batch'] = $this->user->get_all_batch_by_inst_id(user()->user_id);
            $data['all_lession'] = $this->course->get_all_lession_by_course_id($this->input->post('batch'));
            $data['batch_id'] = $this->input->post('batch');
            $data['all_student'] = $all_student;
            $this->load->view('admin/include/header', $data);
            $this->load->view('admin/instructor/batch_progress');
            $this->load->view('admin/include/footer');
        }
        else
        {
            $data['batch_progress'] = '';
            $data['all_batch'] = $this->user->get_all_batch_by_inst_id(user()->user_id);
            $this->load->view('admin/include/header', $data);
            $this->load->view('admin/instructor/batch_progress');
            $this->load->view('admin/include/footer');
        }
    }

    // Batch  End

    //Account Head Start

    function all_account_head()
    {
        $this->load->model('user_model', 'user');
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_account_head'] = $this->setting_model->all_account_head();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/master/all_account_head');
        $this->load->view('admin/include/footer');
    }

    function add_new_account_head()
    {
        if ($this->setting->add_new_account_head()) 
        {
            $this->session->set_userdata('success', "Account Head Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Account Head Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_account_head($id)
    {
        if ($this->setting->delete_account_head($id)) 
        {
            $this->session->set_userdata('success', "Account Head Successfully Deleted");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Account Head Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    function update_account_head($id)
    {
        if ($this->setting->update_account_head($id)) 
        {
            $this->session->set_userdata('success', "Account Head Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Account Head Updated Failed");
            redirect($this->agent->referrer());
        }
    }


    //Account Head End


}
