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

class Home extends OWN_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->customer_login_check();
        //$this->load->model('notificationsetupmodel/notificationsetupmodel','notificationsetupmodel');
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
        $this->load->model('incomesetupmodel/incomesetupmodel', 'incomesetupmodel');
    }

    public function index() {
        // $data['countNotification']=  $this->notificationsetupmodel->countNotification();
        //  $this->load->view('common/header_c');
        //  $this->load->view('common/menu_c');
        //  $this->load->view('customerPanel/dashbord');
        //  $this->load->view('common/footer_c');
         $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $id=$admin_id;
            $data['customerById']=$this->CREGModel->getcustomerById($id);
            if(!empty($data['customerById']))
            {
                 $this->load->view('common/header_c');
                 $this->load->view('common/menu_c');
                $this->load->view('customerPanel/customerProfile',$data);
                $this->load->view('common/footer_c');
            }
            else
            {
                $errormessage['errormessage']="Your Are Not ";
                $this->session->set_userdata($errormessage);
                redirect(base_url('customercontrolar','refresh'));
            }
        }
        else
        {
             redirect(base_url('customercontrolar','refresh'));
        }
    }

    public function viewincome() {
        $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) {
            $customerId=$this->session->userdata('customerId');

            $data['basic_info']     = $this->incomesetupmodel->getcustomerById($customerId);
            $data['allincome']      = $this->incomesetupmodel->getincomeByCustomerId($customerId);
            $data['total_cost']     = $data['basic_info']['total_cost'];

            if (!empty($data['allincome'])) {
                $this->load->view('common/header_c');
                $this->load->view('common/menu_c');
                $this->load->view('customerPanel/viewAccountInfo',$data);
                $this->load->view('common/footer_c');
            } else {
                $errormessage['errormessage'] = "  No Account Information  Available......!";
                $this->session->set_userdata($errormessage);
                redirect(customer_url().'/home/index', 'refresh');
            }
        }
        else
        {
             redirect(base_url('customercontrolar','refresh'));
        }
    }


    public function editProfile()
    {
        $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $Id=$admin_id;
            $data['customerById']=$this->CREGModel->getcustomerById($Id);
            if(!empty($data['customerById']))
            {
                $this->load->view('common/header_c');
                $this->load->view('common/menu_c');
                $this->load->view('customerPanel/editCustomerProfile',$data);
                $this->load->view('common/footer_c');
            }
            else
            {
               redirect(customer_url().'/home/index', 'refresh');
            }
        }
        else
        {
            redirect(base_url('customercontrolar','refresh')); 
        }

    }

    public function updateProfile()
    {
        $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnUpdate']))
              {
                    $this->load->helper(array('form', 'url'));
                    $this->load->library('form_validation');
                    $config = array(
                    // array(
                    //     'field' => 'data[applicant_name]',
                    //     'label' => 'Application Name',
                    //     'rules' => 'required|xss_clean|trim|alpha_spaces'
                    // ),
                    
                    
                    array(
                        'field' => 'data[phone]',
                        'label' => 'Phone',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[mobile]',
                        'label' => 'Mobile',
                        'rules' => 'xss_clean'
                    ),
                    array(
                        'field' => 'data[email]',
                        'label' => 'Email',
                        'rules' => 'trim|xss_clean'
                    ),
                    
                    array(
                        'field' => 'data[presentAddress]',
                        'label' => 'Present Address',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[permanentAddress]',
                        'label' => 'Permanent Address',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    
                    
                    array(
                        'field' => 'data[address]',
                        'label' => 'Address',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[office]',
                        'label' => 'Office',
                        'rules' => 'xss_clean|trim'
                    ),
                    
                    array(
                        'field' => 'data[nominee_mobile]',
                        'label' => 'Nominee Mobile',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[nominee_phone]',
                        'label' => 'nominee phone',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[nominee_email]',
                        'label' => 'Nominee Email',
                        'rules' => 'trim|xss_clean'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == FALSE) {
                    $this->editProfile();
                }else{

                    $data = $this->input->post('data', TRUE);
                    $data['id'] = $this->input->post('id', TRUE);
                    $this->load->library('upload', $config);
                    $config['upload_path'] = './uploadimage/customerImage/';
                    $config['allowed_types'] = 'png|jpeg|jpg|pdf|docx|doc';
                    $config['max_size'] = '800';
                    $config['max_width'] = '800';
                    $config['max_height'] = '600';
                    $udata = '';
                    $udata1 = '';

                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('photo')) 
                    {              
                        $udata = array('upload_data' => $this->upload->data());
                        if(empty($udata['upload_data']['file_name'])){unset($udata1['upload_data1']['file_name']);}
                        else{$data['photo'] = "uploadimage/customerImage/" . $udata['upload_data']['file_name'];}
                    }
            
                    // if ($this->upload->do_upload('nominee_photo')) 
                    // {                
                    //     $udata1 = array('upload_data1' => $this->upload->data());
                    //     if(empty($udata1['upload_data1']['file_name'])){unset($udata1['upload_data1']['file_name']);}
                    //     else{$data['nominee_photo'] = "uploadimage/customerImage/" . $udata1['upload_data1']['file_name'];}
                    // }
                    //echo '<pre>';print_r($udata1);die();
                    
                    $execution=$this->CREGModel->updateCustomerById($data);
                    if(!empty($execution))
                    {
                        $message['message']="Customer Info Updated";
                        $this->session->set_userdata($message);
                        redirect(customer_url().'/home/index', 'refresh');
                    }
                    else
                    {
                        $errormessage['errormessage']="Customer Info Not Updated";
                        $this->session->set_userdata($errormessage);
                       redirect(customer_url().'/home/index', 'refresh');
                    }
                    }
              }
              else
              {
                   redirect(customer_url().'/home/index', 'refresh');
              }
        }
        else
        {
            redirect(base_url('customercontrolar','refresh'));
        }
    }



    public function change_password_form()
    {
        $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $this->load->view('common/header_c');
            $this->load->view('common/menu_c');
            $this->load->view('customerPanel/changePassword');
            $this->load->view('common/footer_c');
            
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }


    public function change_password()
    {
        $admin_id=$this->session->userdata('id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');   
            $config = array(
               array(
                     'field'   => 'data[courrentPassword]', 
                     'label'   => 'Current Password', 
                     'rules'   => 'required|xss_clean'
                  ),
                  array(
                     'field'   => 'data[newPassword]', 
                     'label'   => 'New Password', 
                     'rules'   => 'required'
                  ),
                  array(
                     'field'   => 'data[confirmPassword]', 
                     'label'   => 'Confirm Password', 
                     'rules'   => 'required'
                  )
              );
            $this->form_validation->set_rules($config); 
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('common/header_c');
                $this->load->view('common/menu_c');
                $this->load->view('customerPanel/changePassword');
                $this->load->view('common/footer_c');
            }
            else
            {
                $data= $this->input->post('data', TRUE);
                //print_r($data);die();
                if(!empty($data))
                {
                     $data['id']=$this->session->userdata('id');
                     $data['applicant_name']=$this->session->userdata('applicant_name');
                     $data['customerId']=$this->session->userdata('customerId');
                     $data['customer_access']=$this->session->userdata('customer_access');
                     // $data['admin_access']=$this->session->userdata('admin_access');
                     // $data['employeeId']=$this->session->userdata('employeeId');
                     // $data['admin_level']=$this->session->userdata('admin_level');
                     //print_r($data);die();
                     $checkpass=$this->CREGModel->checkpassword($data);
                     //print_r($checkpass);die();
                     if(!empty($checkpass))
                    {
                        if($data['newPassword']==$data['confirmPassword'])
                        {
                              $data['password']=$data['confirmPassword'];
                              $execution=$this->CREGModel->updatePassword($data);
                        }
                        if(!empty($execution))
                        {
                            $message['message']='Password Changed';
                            $this->session->set_userdata($message);
                            redirect(customer_url()."/home",'refresh');
                        }
                        else
                        {
                            $errormessage['errormessage']='Password Not Changed';
                            $this->session->set_userdata($errormessage);
                            redirect(customer_url()."/home",'refresh');
                        }
                    }
                    else
                    {
                        $errormessage['errormessage']='Your Courrent Password is wrong';
                        $this->session->set_userdata($errormessage);
                        redirect(customer_url()."/home",'refresh');
                    }
                }
                else
                {
                    $errormessage['errormessage']='Password Information Missing';
                    $this->session->set_userdata($errormessage);
                    redirect(customer_url()."/home",'refresh');
                }
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }

    }

}