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

class User_model  extends CI_Model
{
    function __construct() {
        parent::__construct(); 
    }
     

    function get_user_by_id($id)
    {
        $this->db->where('user_id', $id);
        return $this->db->get('users')->row();
    }

    function get_all_instructor_by_atp_id()
    {
        $atp_id = user_id();
        $this->db->select('user_id, user_name');
        $this->db->where('user_atp', $atp_id);
        $this->db->where('is_instructor', 1);
        return $this->db->get('users')->result();
    }

    function update_profile_details_information($id) 
    {
        $data = array(
            'user_name'             => $this->input->post('user_name'), 
            'user_mobile'           => $this->input->post('user_mobile'), 
            'user_email'            => $this->input->post('user_email'), 
        );

        if(!empty($_FILES['user_photo']['name']))
        {
            $user_image  = $this->get_user_by_id($id);    
            if(file_exists($user_image->user_photo))
            {
                unlink($user_image->user_photo);
            }

            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

              // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        } 

        $this->db->where('user_id', $id);
        return $this->db->update('users', $data);
    }

    function check_current_password($password, $user_id) 
    {
        $this->db->where('user_id', $user_id);
        $user = $this->db->get('users')->row();
        

        if($user->user_password == hash("sha256", $password))
        {
            return TRUE;
        }
        else
        {
            return hash("sha256", $password);
        }
    }

    function update_profile_details_password($id) 
    {
        $data = array(
            'user_password' => hash("sha256", $this->input->post('new_password')), 
        );
        $this->db->where('user_id', $id);
        return $this->db->update('users', $data);
    }

    //Employee Start

    function get_all_employee_by_id($id)
    {
        if($id == 1 || $id == 2)
        {
            $this->db->where_in('user_atp', [1, 2]);
        }
        else
        {
            $this->db->where('user_atp', $id);
        }
        $this->db->where('is_employee', 1);
        
        return $this->db->get('users')->result();
    }

    function add_employee()
    {
        $data['user_name'] = $this->input->post('user_name');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_role'] = $this->input->post('user_role');
        $data['user_password'] = hash("sha256", $this->input->post('user_mobile'));
        $data['user_state'] = $this->input->post('user_state');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['employee_pay_type'] = $this->input->post('employee_pay_type');
        $data['employee_pay_value'] = $this->input->post('employee_pay_value');
        $data['is_employee'] = 1;
        $data['user_status'] = 1;
        $data['user_created_at'] = date('Y-m-d');
        $data['user_atp'] = user_id();
        if($this->input->post('user_role') == 'Instractor')
        {
            $data['is_instructor'] = 1;
        }
        if(!empty($_FILES['user_photo']['name']))
        {
            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }

        return $this->db->insert('users', $data);
    }

    function update_employee($id)
    {
        $data['user_name'] = $this->input->post('user_name');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_role'] = $this->input->post('user_role');
        $data['user_state'] = $this->input->post('user_state');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['employee_pay_type'] = $this->input->post('employee_pay_type');
        $data['employee_pay_value'] = $this->input->post('employee_pay_value');
        if($this->input->post('user_role') == 'Instractor')
        {
            $data['is_instructor'] = 1;
        }
        $data['user_atp'] = user_id();
        if(!empty($_FILES['user_photo']['name']))
        {
            $image = $this->get_employee_by_id($id);
            if(file_exists($image->user_photo))
            {
                unlink($image->user_photo);
            }

            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }
        $this->db->where('user_id', $id);
        return $this->db->update('users', $data);
    }

    function get_employee_by_id($id)
    {
        $atp_id = user()->user_id;
        $this->db->where('user_id', $id);
        // $this->db->where('user_atp', $atp_id);
        return $this->db->get('users')->row();
    }

    function delete_employee($id)
    {
        $image = $this->get_employee_by_id($id);
        if(file_exists($image->user_photo))
        {
            unlink($image->user_photo);
        }

        $atp_id = user()->id;
        $this->db->where('user_id', $id);
        $this->db->where('user_atp', $atp_id);
        return $this->db->delete('users');
    }

    public function get_user_by_login($username, $password) {
        // Get the user data from the database
        $query = $this->db->get_where('users', array('user_mobile' => $username, 'user_password' => hash("sha256", $password)));
        if ($query->num_rows() > 0) {
            // Return the user data
            return $query->row();
        } else {
            return false;
        }
    }

    public function get_user_by_user_id($id)
    {
        $this->db->where('user_id', $id);
        return $this->db->get('users')->row();
    }

    //Employee End
    
    //Branch Start

    function get_all_atp_for_admin()
    {
        $this->db->where('user_role', 'Branch');
        return $this->db->get('users')->result();
    }

    function add_atp()
    {
        $this->load->helper('string');
        $this->db->select('user_id');
        $this->db->order_by('user_id', 'desc');
        $this->db->where('user_role', 'ATP');
        $info = $this->db->get('users')->num_rows();
        $reg_no = $info + 1;
        $data['user_name'] = $this->input->post('user_name');
        // $data['user_f_name'] = $this->input->post('user_f_name');
        // $data['user_m_name'] = $this->input->post('user_m_name');
        // $data['user_l_name'] = $this->input->post('user_l_name');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_business_name'] = $this->input->post('user_business_name');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_role'] = 'Branch';
        $data['user_password'] = hash("sha256", $this->input->post('user_mobile'));
        $data['user_qualification'] = $this->input->post('user_qualification');
        $data['user_state'] = $this->input->post('user_state');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['user_reg_fee'] = $this->input->post('user_reg_fee');
        $data['user_rew_fee'] = $this->input->post('user_rew_fee');
        $data['user_rew_date'] = $this->input->post('user_rew_date');
        // $data['user_atp_lat'] = $this->input->post('user_atp_lat');
        // $data['user_atp_long'] = $this->input->post('user_atp_long');
        $data['user_reg_id'] = strtoupper(mb_substr($this->input->post('user_state'), 0, 2).mb_substr($this->input->post('user_dist'), 0, 2).mb_substr($this->input->post('user_taluka'), 0, 2).mb_substr($this->input->post('user_village'), 0, 2)).$reg_no;
        $data['user_status'] = 4;
        $data['user_created_at'] = date('Y-m-d');
        // $data['user_assign_programm'] = json_encode($this->input->post('user_assign_programm'));
        // $data['user_auth_key'] = random_string('alnum', 16);
        // $data['user_app_aff_link'] = $this->input->post('user_app_aff_link');
        // $data['user_is_taluka_head'] = $this->input->post('user_is_taluka_head') == 1 ? 1 : 0;
        $data['user_block'] = $this->input->post('user_block');
        
        if(!empty($_FILES['user_photo']['name']))
        {
            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }

        return $this->db->insert('users', $data);
    }

    function update_atp($id)
    {
        
        $data['user_name'] = $this->input->post('user_name');
        // $data['user_f_name'] = $this->input->post('user_f_name');
        // $data['user_m_name'] = $this->input->post('user_m_name');
        // $data['user_l_name'] = $this->input->post('user_l_name');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_business_name'] = $this->input->post('user_business_name');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_qualification'] = $this->input->post('user_qualification');
        $data['user_state'] = $this->input->post('user_state');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['user_reg_fee'] = $this->input->post('user_reg_fee');
        $data['user_rew_fee'] = $this->input->post('user_rew_fee');
        $data['user_rew_date'] = $this->input->post('user_rew_date');
        // $data['user_assign_programm'] = json_encode($this->input->post('user_assign_programm'));
        // $data['user_atp_lat'] = $this->input->post('user_atp_lat');
        // $data['user_atp_long'] = $this->input->post('user_atp_long');
        // $data['user_app_aff_link'] = $this->input->post('user_app_aff_link');
        // $data['user_is_taluka_head'] = $this->input->post('user_is_taluka_head') == 1 ? 1 : 0;
        $data['user_block'] = $this->input->post('user_block');
        
        if(!empty($_FILES['user_photo']['name']))
        {
            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }

        $this->db->where('user_id', $id);
        return $this->db->update('users', $data);
    }

    function change_status($id, $status)
    {
        $this->db->where('user_id', $id);
        return $this->db->update('users', ['user_status' => $status]);
    }

    // Branch End

    // Student Start

    function add_enquiry()
    {
        $atp = user_id();
        $data['user_name'] = $this->input->post('user_f_name').' '.$this->input->post('user_m_name').' '.$this->input->post('user_l_name');
        $data['user_f_name'] = $this->input->post('user_f_name');
        $data['user_m_name'] = $this->input->post('user_m_name');
        $data['user_l_name'] = $this->input->post('user_l_name');
        $data['user_qualification'] = $this->input->post('user_qualification');
        $data['user_school_college_name'] = $this->input->post('user_school_college_name');
        $data['user_aadhar_no'] = $this->input->post('user_aadhar_no');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_state'] = $this->input->post('user_state');
        $data['user_atp'] = $atp;
        $data['course_interest'] = json_encode($this->input->post('course_interest'));
        $data['inq_interest'] = $this->input->post('inq_interest');
        $data['inq_req_demo'] = $this->input->post('inq_req_demo');
        $data['inq_lead_source'] = $this->input->post('inq_lead_source');
        $data['inq_ref_no'] = $this->input->post('inq_ref_no');
        $data['assign_to'] = $this->input->post('assign_to');
        $data['inq_date'] = $this->input->post('inq_date');
        $data['inq_note'] = $this->input->post('inq_note');
        $data['status'] = 0;

        if($this->db->insert('student_enquiry', $data))
        {
            $all_course = [];
            foreach ($this->input->post('course_interest') as $key => $course) 
            {
                $this->db->select('title');
                $this->db->where('id', $course);
                $c_name = $this->db->get('course')->row();
                $all_course[] = $c_name->title;
            }
            $courseDetails = implode(', ', $all_course);
            // return student_inquiry_sms($this->input->post('user_mobile'), $data['user_name'], preg_replace('/[^A-Za-z0-9]/', ' ', $courseDetails), atp_details()->user_business_name, atp_details()->user_taluka);
            return true;
        }
        else
        {
            return false;
        }

         
    }  

    function update_enquiry($id)
    {
        $data['user_name'] = $this->input->post('user_f_name').' '.$this->input->post('user_m_name').' '.$this->input->post('user_l_name');
        $data['user_f_name'] = $this->input->post('user_f_name');
        $data['user_m_name'] = $this->input->post('user_m_name');
        $data['user_l_name'] = $this->input->post('user_l_name');
        $data['user_qualification'] = $this->input->post('user_qualification');
        $data['user_school_college_name'] = $this->input->post('user_school_college_name');
        $data['user_aadhar_no'] = $this->input->post('user_aadhar_no');
        $data['user_dob'] = $this->input->post('user_dob');
        $data['user_gender'] = $this->input->post('user_gender');
        $data['user_mobile'] = $this->input->post('user_mobile');
        $data['user_email'] = $this->input->post('user_email');
        $data['user_pin_code'] = $this->input->post('user_pin_code');
        $data['user_village'] = $this->input->post('user_village');
        $data['user_taluka'] = $this->input->post('user_taluka');
        $data['user_dist'] = $this->input->post('user_dist');
        $data['user_state'] = $this->input->post('user_state');
        $data['course_interest'] = json_encode($this->input->post('course_interest'));
        $data['inq_interest'] = $this->input->post('inq_interest');
        $data['inq_req_demo'] = $this->input->post('inq_req_demo');
        $data['inq_lead_source'] = $this->input->post('inq_lead_source');
        $data['inq_ref_no'] = $this->input->post('inq_ref_no');
        $data['assign_to'] = $this->input->post('assign_to');
        $data['inq_date'] = $this->input->post('inq_date');
        $data['inq_note'] = $this->input->post('inq_note');

        $this->db->where('id', $id);
        return $this->db->update('student_enquiry', $data);
    }

    function get_student_enquiry_by_id($id)
    {
         $this->db->where('id', $id);
         return $this->db->get('student_enquiry')->row();
    } 

    function get_all_enquiry_student_for_atp($id)
    {
        if(check_user_permission('student_enq_special_permission'))
        {
            $this->db->select('a.*, a.id as student_enq_id, b.user_name as assign_name, c.*');
            $this->db->from('student_enquiry a');
            $this->db->join('users b', 'b.user_id = a.assign_to', 'left');
            $this->db->join('lead_source_name c', 'c.lead_name_id = a.inq_lead_source', 'left');
            $this->db->where('a.status !=', 4);
            $this->db->where('a.user_atp', $id);
            $this->db->order_by('student_enq_id', 'DESC');
            return $this->db->get()->result();
        }
        else
        {
            $user_id = user()->user_id;
            $this->db->select('a.*, a.id as student_enq_id, b.user_name as assign_name, c.*');
            $this->db->from('student_enquiry a');
            $this->db->join('users b', 'b.user_id = a.assign_to', 'left');
            $this->db->join('lead_source_name c', 'c.lead_name_id = a.inq_lead_source', 'left');
            $this->db->where('a.status !=', 4);
            $this->db->where('a.user_atp', $id);
            $this->db->where('a.assign_to', $user_id);
            $this->db->order_by('student_enq_id', 'DESC');
            return $this->db->get()->result();
        }
    }

    function change_enquiry_status()
    {
        if($this->input->post('status') == 2)
        {
            $student = $this->get_student_enquiry_by_id($this->input->post('id'));

            $student_data = [
                'user_name' => $student->user_name,
                'user_f_name' => $student->user_f_name,
                'user_m_name' => $student->user_m_name,
                'user_l_name' => $student->user_l_name,
                'user_mobile' => $student->user_mobile,
                'user_email' => $student->user_email,
                'user_password' => hash("sha256", $student->user_mobile),
                'user_role' => 'Student',
                'user_gender' => $student->user_gender,
                'user_dob' => $student->user_dob,
                'user_qualification' => $student->user_qualification,
                'user_school_college_name' => $student->user_school_college_name,
                'user_aadhar_no' => $student->user_aadhar_no,
                'user_reg_id' => 'T'.rand(1000000000, 9999999999),
                'user_atp' => $student->user_atp,
                'user_state' => $student->user_state,
                'user_dist' => $student->user_dist,
                'user_taluka' => $student->user_taluka,
                'user_village' => $student->user_village,
                'user_pin_code' => $student->user_pin_code,
                'user_status' => 1,
                'user_created_at' => date('Y-m-d'),
            ];
            $this->db->insert('users', $student_data);
            $student_id = $this->db->insert_id();

            foreach (json_decode($student->course_interest) as $course_interest) 
            {
                $course = [
                    'st_course_course_id' => $course_interest,
                    'st_course_student_id' => $student_id,
                    'st_course_student_atp_id' => $student->user_atp,
                    'st_course_date' => date('Y-m-d'),
                    'st_course_status' => 0
                ];

                $this->db->insert('student_course', $course);
            }
        }

        $data = [
            'status' => $this->input->post('status')
        ];

        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('student_enquiry', $data);
    }

    function get_all_followup_student_for_atp($id, $atp_id)
    {
        $this->db->select('a.*, b.mode_name');
        $this->db->from('student_follow_up_details a');
        $this->db->join('student_follow_up_mode b', 'b.mode_id = a.details_mode', 'left');
        $this->db->where('a.details_student_id', $id);
        $this->db->where('a.details_atp_id', $atp_id);
        $this->db->order_by('details_id', "DESC");
        return $this->db->get()->result();
    }

    function add_new_follow_up_details($user_id, $student_id, $atp_id)
    {
        $data = [
            'details_student_id' => $student_id,
            'details_mode' => $this->input->post('details_mode'),
            'details_note' => $this->input->post('details_note'),
            'details_next_date' => $this->input->post('details_next_date'),
            'details_attand_emp_id' => $user_id,
            'details_atp_id' => $atp_id,
            'details_date' => date('Y-m-d')
        ];

        return $this->db->insert('student_follow_up_details', $data);
    }

    function get_all_admintted_student($id)
    {
        $this->db->order_by('user_id', 'desc');
        $this->db->where('user_atp', $id);
        $this->db->where('user_role', 'Student');
        return $this->db->get('users')->result();
    }

    function get_all_admintted_student_for_admin()
    {
        $this->db->order_by('user_id', 'desc');
        $this->db->where('user_role', 'Student');
        return $this->db->get('users')->result();
    }

    function pay_student_admission_fee($atp_id, $student_id)
    {
        $this->db->select('user_wallet_balance');
        $this->db->where('user_id', $atp_id);
        $atp = $this->db->get('users')->row();    

        $this->db->select('a.*, b.title, b.atp_course_price');
        $this->db->from('student_course a');
        $this->db->join('course b', 'b.id = a.st_course_course_id', 'left');
        $this->db->where('a.st_course_student_id', $student_id);
        $all_course = $this->db->get()->result();

        $pay_amount = 0;
        foreach ($all_course as $course) 
        {
            $pay_amount += $course->atp_course_price;
        }
        echo $pay_amount;
        if($pay_amount > $atp->user_wallet_balance)
        {
            return false;
        }
        else
        {
            $this->db->where('user_id', $student_id);
            $this->db->update('users', ['user_status' => 1]);

            $wal_bal = $atp->user_wallet_balance - $pay_amount;

            $this->db->where('user_id', $atp_id);
            return $this->db->update('users', ['user_wallet_balance' => $wal_bal]);
        }
    }

    function get_student_by_id_and_atp_id($student_id, $atp_id)
    {
        $this->db->where('user_id', $student_id);
        $this->db->where('user_atp', $atp_id);
        return $this->db->get('users')->row();
    }

    function get_active_student_by_atp_id($atp_id)
    {
        $this->db->where('user_role', 'Student');
        $this->db->where('user_status', 1);
        $this->db->where('user_atp', $atp_id);
        return $this->db->get('users')->result();
    }

    function get_active_student_by_atp_id_and_student_id($atp_id, $student_id)
    {
        $this->db->where('user_role', 'Student');
        $this->db->where('user_status', 1);
        $this->db->where('user_id', $student_id);
        $this->db->where('user_atp', $atp_id);
        return $this->db->get('users')->row();
    }

    function update_adminssion_student($id, $atp_id)
    {
        $data = [
            'user_name' => $this->input->post('user_f_name').' '.$this->input->post('user_m_name').' '.$this->input->post('user_l_name'),
            'user_f_name' => $this->input->post('user_f_name'),
            'user_m_name' => $this->input->post('user_m_name'),
            'user_l_name' => $this->input->post('user_l_name'),
            'user_qualification' => $this->input->post('user_qualification'),
            'user_school_college_name' => $this->input->post('user_school_college_name'),
            'user_aadhar_no' => $this->input->post('user_aadhar_no'),
            'user_gender' => $this->input->post('user_gende'),
            'user_mobile' => $this->input->post('user_mobile'),
            'user_email' => $this->input->post('user_email'),
            'user_pin_code' => $this->input->post('user_pin_code'),
            'user_village' => $this->input->post('user_village'),
            'user_taluka' => $this->input->post('user_taluka'),
            'user_dist' => $this->input->post('user_dist'),
            'user_state' => $this->input->post('user_state'),
            'user_created_at' => $this->input->post('user_created_at'),
            'user_dob' => $this->input->post('user_do'),
        ];


        if(!empty($_FILES['file_image']['name']))
        {
            $user_image  = $this->get_user_by_id($id);    
            if(file_exists($user_image->user_image))
            {
                unlink($user_image->user_image);
            }
            $_FILES['file']['name'] = $_FILES['file_image']['name'];
            $_FILES['file']['type'] = $_FILES['file_image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['file_image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['file_image']['error'];
            $_FILES['file']['size'] = $_FILES['file_image']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['file_image']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }

        if($this->input->post('image'))
        {
            $user_image  = $this->get_user_by_id($id);    
            if(file_exists($user_image->user_image))
            {
                unlink($user_image->user_image);
            }
            $img = $_POST['image'];
            $folderPath = "uploads/";
            $image_parts = explode(";base64,", $img);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            $fileName = uniqid().$_FILES['file_image']['name'].'.jpg';
            $file = $folderPath . $fileName;
            file_put_contents($file, $image_base64);
            $data['user_photo'] = 'uploads/' . $fileName;

        }

        $this->db->where('user_id', $id);
        $this->db->where('user_atp', $atp_id);
        return $this->db->update('users', $data);
    }

    function update_student($id)
    {
        $data = [
            'user_mobile' => $this->input->post('user_mobile'),
            'user_pin_code' => $this->input->post('user_pin_code'),
            'user_taluka' => $this->input->post('user_taluka'),
            'user_dist' => $this->input->post('user_dist'),
            'user_state' => $this->input->post('user_state'),
            //'course_interest' => json_encode($this->input->post('course_interest')),
            'user_status' => 1
        ];

        if(!empty($_FILES['user_photo']['name']))
        {
            $_FILES['file']['name'] = $_FILES['user_photo']['name'];
            $_FILES['file']['type'] = $_FILES['user_photo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['user_photo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['user_photo']['error'];
            $_FILES['file']['size'] = $_FILES['user_photo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['user_photo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['user_photo'] = 'uploads/' . $filename;

                }
            }
        }

        $this->db->where('user_id', $id);
        return $this->db->update('users', $data);
    }

    function add_new_batch()
    {
        foreach ($this->input->post('batch') as $key => $batch) 
        {
            // if($this->input->post('batch')[$key] == 1)
            // {
                $data = [
                    'st_course_batch' => $this->input->post('batch_id'),
                    'st_course_status' => 3,
                ];
            // }
            // else
            // {
            //     $data = [
            //         'st_course_batch' => 0,
            //         'st_course_status' => 1,
            //         'id' => $this->input->post('u_id')[$key]
            //     ];
            // }
            
            // echo '<pre>';
            // print_r($data);

            $this->db->where('st_course_id', $batch);
            $this->db->update('student_course', $data);
        }

        return true;
    }

    function add_new_instalment()
    {
        if(count($this->input->post('instalment_date')) == 0)
        {
            return false;
        }

        $down_payment = array(
            'instalment_student_id' => $this->input->post('fee_student_id'),
            'instalment_date' => date('Y-m-d'),
            'instalment_amount' => $this->input->post('fee_student_course_down_pay'),
            'instalment_course_id' => $this->input->post('fee_student_course'),
            'instalment_status' => 1,
            'is_down_payment' => 1,
            'instalment_pay_date' => date('Y-m-d'),
            'instalment_inv_id' => rand(10000, 99999)
        );
        $this->db->insert('student_payment_instalment', $down_payment);
        foreach ($this->input->post('instalment_date') as $key => $installment) 
        {
            $ins = [
                'instalment_student_id' => $this->input->post('fee_student_id'),
                'instalment_date' => $this->input->post('instalment_date')[$key],
                'instalment_amount' => $this->input->post('instalment_amount')[$key],
                'instalment_course_id' => $this->input->post('fee_student_course'),
                'instalment_status' => 0
            ];

            $this->db->insert('student_payment_instalment', $ins);
        }

        $data = [
            'fee_student_course_price' => $this->input->post('fee_student_course_price'),
            'fee_student_course_discount' => $this->input->post('fee_student_course_discount'),
            'fee_student_course_main_price' => $this->input->post('fee_student_course_main_price'),
            'fee_student_course_down_pay' => $this->input->post('fee_student_course_down_pay'),
            'fee_student_course_due' => $this->input->post('fee_student_course_due'),
            'fee_student_course_instalment' => $this->input->post('fee_student_course_instalment'),
            'fee_student_course_days' => $this->input->post('fee_student_course_days'),
            'fee_student_course_start_day' => $this->input->post('fee_student_course_start_day'),
            'fee_student_exam_approve' => $this->input->post('fee_student_exam_approve') == 1 ? 1 : 0,
            'fee_student_course_cgst' => $this->input->post('fee_student_course_cgst'),
            'fee_student_course_sgst' => $this->input->post('fee_student_course_sgst'),
            // 'st_course_status' => 2
        ];

        $this->db->where('st_course_course_id', $this->input->post('fee_student_course'));
        $this->db->where('st_course_student_id', $this->input->post('fee_student_id'));
        return $this->db->update('student_course', $data);
    }

    function add_new_adjustment()
    {

        $this->db->where('st_course_course_id', $this->input->post('fee_student_course'));
        $this->db->where('st_course_student_id', $this->input->post('fee_student_id'));
        $info = $this->db->get('student_course')->row();

        $data = [
            'fee_student_course_discount' => $this->input->post('fee_student_course_discount') + $info->fee_student_course_discount,
            'fee_student_course_due' => $this->input->post('fee_student_course_due'),
        ];
        $this->db->where('st_course_course_id', $this->input->post('fee_student_course'));
        $this->db->where('st_course_student_id', $this->input->post('fee_student_id'));
        return $this->db->update('student_course', $data);
    }

    function update_refund()
    {
        $data = [
            'st_course_refund_process_fee' => $this->input->post('st_course_refund_process_fee'),
            'st_course_refund_amount' => $this->input->post('st_course_refund_amount'),
            'st_course_refund_date' => date('Y-m-d'),
            'st_course_refund_inv_id' => date('Ymd').rand(1000, 9999),
            'st_course_status' => 6,
        ];
        $this->db->where('st_course_course_id', $this->input->post('fee_student_course'));
        $this->db->where('st_course_student_id', $this->input->post('fee_student_id'));
        return $this->db->update('student_course', $data);
    }

    public function update_instalment_amount()
    {
        $instalment = array(
            'instalment_pay_date' => $this->input->post('instalment_pay_date'),
            'instalment_status' => 1,
            'instalment_inv_id' => rand(10000, 99999)
        );

        $this->db->where('instalment_id', $this->input->post('instalment_id'));
        $this->db->update('student_payment_instalment', $instalment);

        $due_amt = $this->input->post('fee_student_course_due') - $this->input->post('payment_amt');
        if($due_amt == 0)
        {
            $pay_status = 1;
        }
        else
        {
            $pay_status = 0;
        }
        $data = array(
            'fee_student_course_due' => $due_amt,
            'st_is_course_payment_complete' => $pay_status
        );

        $this->db->where('st_course_id', $this->input->post('st_course_id'));
        return $this->db->update('student_course', $data);
    }

    public function get_student_all_course_by_student_id($atp_id, $id)
    {
        $this->db->select('a.*, b.title, c.*');
        $this->db->from('student_course a');
        $this->db->join('course b', 'b.id = a.st_course_course_id', 'left');
        $this->db->join('users c', 'c.user_id = a.st_course_student_id', 'left');
        $this->db->where('a.st_course_student_id', $id);
        $this->db->where('a.st_course_student_atp_id', $atp_id);
        $this->db->where('a.st_course_status > ', 0);
        return $this->db->get()->result();
    }

    public function get_student_all_course_by_student_id_for_refund($atp_id, $id)
    {
        $this->db->select('a.*, b.title, c.*');
        $this->db->from('student_course a');
        $this->db->join('course b', 'b.id = a.st_course_course_id', 'left');
        $this->db->join('users c', 'c.user_id = a.st_course_student_id', 'left');
        $this->db->where('a.st_course_student_id', $id);
        $this->db->where('a.st_course_student_atp_id', $atp_id);
        $this->db->where('a.st_course_status', 6);
        return $this->db->get()->result();
    }

    public function update_admission_date()
    {
        $data = array(
            'st_course_date' => $this->input->post('adm_date'), 
        );
        $this->db->where('st_course_id', $this->input->post('id'));
        return $this->db->update('student_course', $data);
    }

    public function update_admission_batch()
    {
        $data = array(
            'st_course_batch' => $this->input->post('batch'), 
            'st_course_status' => 3
        );
        $this->db->where('st_course_id', $this->input->post('id'));
        return $this->db->update('student_course', $data);
    }

    function student_course_payment($atp_id)
    {
        // $this->db->select('user_wallet_balance');
        // $this->db->where('user_id', $atp_id);
        // $atp = $this->db->get('users')->row();    

        
        // $pay_amount = $this->input->post('amt_main');
        
        // if($pay_amount > $atp->user_wallet_balance)
        // {
        //     return false;
        // }
        // else
        // {
            $this->db->where('st_course_id', $this->input->post('course_id'));
            return $this->db->update('student_course', ['st_course_status' => 1, 'st_course_date' => date('Y-m-d'), 'is_digital_certificate' => $this->input->post('is_digital_certificate')]);

            // $wal_bal = $atp->user_wallet_balance - $pay_amount;

            // $this->db->where('user_id', $atp_id);
            // return $this->db->update('users', ['user_wallet_balance' => $wal_bal]);
        //}
    }

    public function add_new_course_for_student()
    {
        $this->db->where('st_course_student_id', $this->input->post('student_id'));
        $this->db->where('st_course_course_id', $this->input->post('course_id'));
        $this->db->where('st_course_student_atp_id', $this->input->post('atp_id'));
        $all_data = $this->db->get('student_course')->row();
        if($all_data)
        {
            return false;
        }
        $data = array(
            'st_course_course_id' => $this->input->post('course_id'), 
            'st_course_student_id' => $this->input->post('student_id'),
            'st_course_student_atp_id' => $this->input->post('atp_id'),
            'st_course_date' => date('Y-m-d'),
            'st_course_status' => 0
        );

        return $this->db->insert('student_course', $data);
    }

    public function get_student_by_batch_and_atp_id($id)
    {
        $atp_id = user_id();
        $this->db->select('a.*, b.user_name, c.title, d.*');
        $this->db->from('student_course a');
        $this->db->join('users b', 'b.user_id = a.st_course_student_id');
        $this->db->join('course c', 'c.id = a.st_course_course_id');
        $this->db->join('student_batch d', 'd.batch_id = a.st_course_batch');
        $this->db->where('a.st_course_batch', $id);
        $this->db->where('a.st_course_student_atp_id', $atp_id);
        $this->db->where('a.st_course_status', 3);
        return $this->db->get()->result();
    }

    // Student End

    // Payout Start

    function get_all_salary_advance($month, $year)
    {
        $user_id = user_id();

        $this->db->select('user_id, user_name, user_role, is_instructor, employee_pay_value');
        $this->db->where('user_role !=', "Student");
        $this->db->where('user_atp', $user_id);
        $data = $this->db->get('users')->result();
        $array = [];
        foreach ($data as $value) 
        {
            $this->db->where('pay_emp_id', $value->user_id);
            $this->db->where('pay_atp_id', $user_id);
            $this->db->where('MONTH(pay_date)', $month);
            $this->db->where('YEAR(pay_date)', $year);
            $this->db->where('pay_is_advance', 1);
            $this->db->order_by('pay_date', 'ASC');
            $payout = $this->db->get('employee_payout')->result();
            $value->payout = $payout;
            array_push($array, $value);
        }
        return $array;
    }

    function get_all_salary_details($month, $year)
    {
        $user_id = user_id();
 
        $this->db->select('user_id, user_name, user_role, is_instructor, employee_pay_value, user_created_at');
        $this->db->where('user_role !=', "Student");
        $this->db->where('user_atp', $user_id);
        // $this->db->where('MONTH(user_created_at) >=', $month);
        $this->db->where('YEAR(user_created_at) <=', $year);
        $data = $this->db->get('users')->result();
        $array = [];
        foreach ($data as $value) 
        {
            $this->db->where('pay_emp_id', $value->user_id);
            $this->db->where('pay_atp_id', $user_id);
            $this->db->where('MONTH(pay_date)', $month);
            $this->db->where('YEAR(pay_date)', $year);
            $this->db->order_by('pay_date', 'ASC');
            $payout = $this->db->get('employee_payout')->result();
            $value->payout = $payout;
            array_push($array, $value);
        }
        return $array;
    }

    function add_advance_salary_details()
    {
        $user_id = user_id();

        $emp_details = $this->get_user_by_id($this->input->post('pay_emp_id'));

        if($this->input->post('pay_note'))
        {
            $exp_note = $emp_details->user_name.' Has Been Given '.$this->input->post('pay_amount').' Rupees For Advance Salary. Note : '.$this->input->post('pay_note');
        }
        else
        {
            $exp_note = $emp_details->user_name.' Has Been Given '.$this->input->post('pay_amount').' Rupees For Advance Salary';
        }

        $name = [
            'expense_head_id' => 0,
            'expense_amount' => $this->input->post('pay_amount'),
            'expense_note' => $exp_note,
            'expense_category' => 'Advance Salary',
            'expense_atp_id' => $user_id,
            'expense_date' => date('Y-m-d')
        ];

        $this->db->insert('expense_management', $name);

        $data = [
            'pay_emp_id' => $this->input->post('pay_emp_id'),
            'pay_atp_id' => $user_id,
            'pay_amount' => $this->input->post('pay_amount'),
            'pay_note' => $this->input->post('pay_note'),
            'pay_date' => $this->input->post('pay_date'),
        ];

        return $this->db->insert('employee_payout', $data);
    }

    function add_salary_details()
    {
        $user_id = user_id();

        $emp_details = $this->get_user_by_id($this->input->post('pay_emp_id'));

        if($this->input->post('pay_note'))
        {
            $exp_note = $emp_details->user_name.' Has Been Given '.$this->input->post('pay_amount').' Rupees For Full Salary. Note : '.$this->input->post('pay_note');
        }
        else
        {
            $exp_note = $emp_details->user_name.' Has Been Given '.$this->input->post('pay_amount').' Rupees For Full Salary';
        }

        $name = [
            'expense_head_id' => 0,
            'expense_amount' => $this->input->post('pay_amount'),
            'expense_note' => $exp_note,
            'expense_category' => 'Full Salary',
            'expense_atp_id' => $user_id,
            'expense_date' => date('Y-m-d')
        ];

        $this->db->insert('expense_management', $name);


        if($this->input->post('deduction_amount'))
        {
            $name = [
                'pay_emp_id' => $this->input->post('pay_emp_id'),
                'pay_atp_id' => $user_id,
                'pay_amount' => $this->input->post('deduction_amount'),
                'pay_note' => $this->input->post('ded_pay_note'),
                'pay_is_advance' => 2,
                'pay_date' => date('Y-m-d')
            ];

            $this->db->insert('employee_payout', $name);
        }

        $data = [
                'pay_emp_id' => $this->input->post('pay_emp_id'),
                'pay_atp_id' => $user_id,
                'pay_amount' => $this->input->post('pay_amount'),
                'pay_note' => $this->input->post('pay_note'),
                'pay_is_advance' => 0,
                'pay_date' => date('Y-m-d')
            ];

        return $this->db->insert('employee_payout', $data);
    }

    function get_all_my_payout_details($id)
    {
        $this->db->where('pay_emp_id', $id);
        $this->db->group_by('MONTH(pay_date)');
        $this->db->order_by('pay_date', 'desc');
        $data = $this->db->get('employee_payout')->result();
        $array = [];
        foreach ($data as $value) 
        {
            $this->db->where('pay_emp_id', $id);
            $this->db->where('MONTH(pay_date)', date('m', strtotime($value->pay_date)));
            $this->db->where('YEAR(pay_date)', date('Y', strtotime($value->pay_date)));
            $payout = $this->db->get('employee_payout')->result();
            $value->payout = $payout;
            array_push($array, $value);
        }
        return $array;
    }

    // Payout End

    function update_student_attandence()
    {
        foreach ($this->input->post('student_id') as $key => $student_id) 
        {
            $this->db->where('st_att_student_id', $student_id);
            $this->db->where('st_att_date', $this->input->post('date')[$key]);
            $this->db->where('st_att_batch', $this->input->post('batch')[$key]);
            $att_data = $this->db->get('student_attendance')->row();
            if($att_data)
            {
                $this->db->where('st_att_id', $att_data->st_att_id);
                $this->db->update('student_attendance', ['st_att_present_status' => $this->input->post('present_status_'.$key.'')]);
            }
            else
            {
                $data = [
                    'st_att_student_id' => $student_id,
                    'st_att_date' => $this->input->post('date')[$key],
                    'st_att_batch' => $this->input->post('batch')[$key],
                    'st_att_attend_by' => user()->user_id,
                    'st_att_present_status' => $this->input->post('present_status_'.$key.'')
                ];

                $this->db->insert('student_attendance', $data);
            }
        }

        return true;
    }

    //Batch Progress Start

    function get_all_batch_by_inst_id($user_id)
    {
        $this->db->where('instructor_id', $user_id);
        return $this->db->get('student_batch')->result();
    }

    //Batch Progress End

    // Dashboard Data Start

    public function get_all_enq_by_atp_id_and_month()
    {
        $user_id = user_id();
        $month = [];
        for ($i=1; $i < 13; $i++) 
        { 
            $this->db->where('MONTH(inq_date)', $i);
            $this->db->where('YEAR(inq_date)', date('Y'));
            $this->db->where('user_atp', $user_id);
            $month[] = $this->db->get('student_enquiry')->num_rows();
        }
        return $month;
    }

    public function get_all_admission_by_atp_id_and_month()
    {
        $user_id = user_id();
        $month = [];
        for ($i=1; $i < 13; $i++) 
        { 
            $this->db->where('MONTH(st_course_date)', $i);
            $this->db->where('YEAR(st_course_date)', date('Y'));
            $this->db->where('st_course_student_atp_id', $user_id);
            $month[] = $this->db->get('student_course')->num_rows();
        }
        return $month;
    }

    public function get_all_income_by_atp_id_and_month()
    {
        $user_id = user_id();
        $month = [];
        for ($i=1; $i < 13; $i++) 
        { 
            $this->db->select_sum('fee_student_course_main_price');
            $this->db->where('MONTH(st_course_date)', $i);
            $this->db->where('YEAR(st_course_date)', date('Y'));
            $this->db->where('st_course_student_atp_id', $user_id);
            $month[] = $this->db->get('student_course')->row();
        }
        return $month;
    }

    public function get_all_expance_by_atp_id_and_month()
    {
        $user_id = user_id();
        $month = [];
        for ($i=1; $i < 13; $i++) 
        { 
            $this->db->select_sum('expense_amount');
            $this->db->where('MONTH(expense_date)', $i);
            $this->db->where('YEAR(expense_date)', date('Y'));
            $this->db->where('expense_atp_id', $user_id);
            $month[] = $this->db->get('expense_management')->row();
        }
        return $month;
    }

    // Dashboard Data End
}

