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

class Profile extends CI_Controller
{
    // Session Authoriesed
    public function __construct()
    {
        parent::__construct();
        
        $customer = $this->session->userdata('customer');
        if (empty($customer)) {
            $this->session->set_flashdata('error', 'Please login to continue.');
            redirect(base_url());
        }
        $column['cust_id'] = $customer['cust_id'];
        $table             = 'tbl_customer';
        
        // loading common model
        $this->load->model('Common_model');
        $result = $this->Common_model->Result($column, $table);
        if ($result['cust_password'] == $customer['cust_password']) {
            $login = true;
        } else {
            $login = false;
        }
        
        if (empty($customer) || $login == false) {
            $this->session->set_flashdata('error', 'Please login to continue.');
            redirect(base_url());
        }
    }
    
    public function index() {
        // loading common model
        $this->load->model('Common_model');
        
        $column['id'] = '1';
        $table        = 'tbl_settings';
        
        // we are getting results from common modal
        $result = $this->Common_model->Result($column, $table);
        
        $columntop_cities['status'] = '1';
        $tabletop_cities            = 'tbl_top_cities';
        // we are getting results from common modal
        $resulttop_cities           = $this->Common_model->ResultsWhere2($columntop_cities, $tabletop_cities);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_mid_category';
        // we are getting results from common modal
        $mid_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_end_category';
        // we are getting results from common modal
        $end_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column32['status']    = '1';
        $table32               = 'tbl_mid_category';
        // we are getting results from common modal
        $data['mid_category2'] = $this->Common_model->ResultsWhere2($column32, $table32);
        
        $column33['status']    = '1';
        $table33               = 'tbl_end_category';
        // we are getting results from common modal
        $data['end_category2'] = $this->Common_model->ResultsWhere2($column33, $table33);
        
        $column31['status'] = 1;
        $table31            = 'tbl_top_cities';
        // we are getting results from common modal
        $top_cities         = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        $column31['status'] = 1;
        $table31            = 'tbl_social';
        // we are getting results from common modal
        $social             = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        /**************************************************/
        $customer             = $this->session->userdata('customer');
        $column123['cust_id'] = $customer['cust_id'];
        $table123             = 'tbl_customer';
        /**************************************************/
        // loading common model
        $this->load->model('Common_model');
        $result123        = $this->Common_model->Result($column123, $table123);
        $data['customer'] = $result123;
        /**************************************************/
        
        // we are setting result in data array as $data
        $data['data']         = $result;
        $data['top_cities']   = $top_cities;
        $data['top_category'] = $top_category;
        $data['mid_category'] = $mid_category;
        $data['end_category'] = $end_category;
        $data['social']       = $social;
        $data['page']         = 'my profile';
        
        // we are loading customer dashboard
        $this->load->view('website/my_profile', $data);
    }
    
        public function profile() {
        // loading common model
        $this->load->model('Common_model');
        
        $column['id'] = '1';
        $table        = 'tbl_settings';
        
        // we are getting results from common modal
        $result = $this->Common_model->Result($column, $table);
        
        $columntop_cities['status'] = '1';
        $tabletop_cities            = 'tbl_top_cities';
        // we are getting results from common modal
        $resulttop_cities           = $this->Common_model->ResultsWhere2($columntop_cities, $tabletop_cities);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_mid_category';
        // we are getting results from common modal
        $mid_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_end_category';
        // we are getting results from common modal
        $end_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column32['status']    = '1';
        $table32               = 'tbl_mid_category';
        // we are getting results from common modal
        $data['mid_category2'] = $this->Common_model->ResultsWhere2($column32, $table32);
        
        $column33['status']    = '1';
        $table33               = 'tbl_end_category';
        // we are getting results from common modal
        $data['end_category2'] = $this->Common_model->ResultsWhere2($column33, $table33);
        
        $column31['status'] = 1;
        $table31            = 'tbl_top_cities';
        // we are getting results from common modal
        $top_cities         = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        $column31['status'] = 1;
        $table31            = 'tbl_social';
        // we are getting results from common modal
        $social             = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        /**************************************************/
        $customer             = $this->session->userdata('customer');
        $column123['cust_id'] = $customer['cust_id'];
        $table123             = 'tbl_customer';
        /**************************************************/
        // loading common model
        $this->load->model('Common_model');
        $result123        = $this->Common_model->Result($column123, $table123);
        $data['customer'] = $result123;
        /**************************************************/
        
        // we are setting result in data array as $data
        $data['data']         = $result;
        $data['top_cities']   = $top_cities;
        $data['top_category'] = $top_category;
        $data['mid_category'] = $mid_category;
        $data['end_category'] = $end_category;
        $data['social']       = $social;
        $data['page']         = 'my profile';
        
        // we are loading customer dashboard
        $this->load->view('website/profile', $data);
    }

    
    
    public function customer_password_update()
    {
        // loading common model
        $this->load->model('Common_model');
        
          $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        
        
        $column['id'] = '1';
        $table        = 'tbl_settings';
        
        // we are getting results from common modal
        $result = $this->Common_model->Result($column, $table);
        
        $columntop_cities['status'] = '1';
        $tabletop_cities            = 'tbl_top_cities';
        // we are getting results from common modal
        $resulttop_cities           = $this->Common_model->ResultsWhere2($columntop_cities, $tabletop_cities);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_mid_category';
        // we are getting results from common modal
        $mid_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_end_category';
        // we are getting results from common modal
        $end_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column32['status']    = '1';
        $table32               = 'tbl_mid_category';
        // we are getting results from common modal
        $data['mid_category2'] = $this->Common_model->ResultsWhere2($column32, $table32);
        
        $column33['status']    = '1';
        $table33               = 'tbl_end_category';
        // we are getting results from common modal
        $data['end_category2'] = $this->Common_model->ResultsWhere2($column33, $table33);
        
        $column31['status'] = 1;
        $table31            = 'tbl_top_cities';
        // we are getting results from common modal
        $top_cities         = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        $column31['status'] = 1;
        $table31            = 'tbl_social';
        // we are getting results from common modal
        $social             = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        /**************************************************/
        $customer             = $this->session->userdata('customer');
        $column123['cust_id'] = $customer['cust_id'];
        $table123             = 'tbl_customer';
        /**************************************************/
        // loading common model
        $this->load->model('Common_model');
        $result123        = $this->Common_model->Result($column123, $table123);
        $data['customer'] = $result123;
        /**************************************************/
        
        // we are setting result in data array as $data
        $data['data']         = $result;
        $data['top_cities']   = $top_cities;
        $data['top_category'] = $top_category;
        $data['mid_category'] = $mid_category;
        $data['end_category'] = $end_category;
        $data['social']       = $social;
        $data['page']         = 'Password Update';
        
        // we are loading customer dashboard
        $this->load->view('website/customer_password_update', $data);
    }
    
    public function edit_profile_img()
    {
        $customer = $this->session->userdata('customer');
        if (!empty($_FILES['photo']['name'])) {
            
            $column['cust_id'] = $customer['cust_id'];
            $table             = 'tbl_customer';
            
            $result      = $this->Common_model->Result($column, $table);
            $current_img = $result['cust_image'];
            $current_img = './uploads/website/customer/' . $current_img;
            
            if (file_exists($current_img)) {
                unlink($current_img);
            }
            
            $path = $_FILES['photo']['name'];
            $ext  = pathinfo($path, PATHINFO_EXTENSION);
            
            $photo = 'Customer-' . $column['cust_id'] . '-' . rand() . '.' . $ext;
            
            $config['upload_path']   = 'uploads/website/customer';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name']     = $photo;
            
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('photo')) {
                $logo               = $this->upload->data();
                $data['cust_image'] = $photo;
                
                // we are updating data from tbl_settings
                $result = $this->Common_model->Edit($column, $data, $table);
                if ($result == true) {
                    $this->session->set_flashdata('success', 'SUCCESS!!  ' . strtoupper("'photo'") . ' is Updated Successfully.');
                    $_SESSION['customer']['cust_image'] = $photo;
                    redirect(base_url('profile/'));
                    exit();
                } else {
                    $this->session->set_flashdata('error', 'Something went Wrong.');
                    redirect(base_url('profile/'));
                    exit();
                }
            } else {
                $this->session->set_flashdata('error', 'Something went Wrong Image is Not Uploaded.');
                redirect(base_url('profile/'));
                exit();
            }
            
        } else {
            $this->session->set_flashdata('error', ' ' . 'photo' . '  can not be Empty.');
            redirect(base_url('profile/'));
        }
    }
    
    
    public function edit_profile_content($id)
    {
        $data = array();
        
        $table = 'tbl_customer';
        
        foreach ($_POST as $key => $value) {
            if ($key != 'cust_status') {
                if (empty($value)) {
                    $this->session->set_flashdata('error', '' . strtoupper("$key") . ' Can not be Empty.');
                    redirect(base_url('profile/'));
                    exit();
                }
            }
            if ($key != 'table' && $key != 'page') {
                $data[$key] = $value;
            }
        }
        
        $column['cust_id'] = $id;
        
        // we are updating data from tbl_p_type
        $result = $this->Common_model->Edit($column, $data, $table);
        if ($result == true) {
            $this->session->set_flashdata('success', 'SUCCESS!! Data is Updated Successfully.');
            
            foreach ($_POST as $key => $value) {
                if ($key != 'table' && $key != 'page') {
                    $data[$key]                 = $value;
                    $_SESSION['customer'][$key] = $value;
                }
            }
            
            redirect(base_url('profile/'));
            exit();
        } else {
            $this->session->set_flashdata('error', 'Something went Wrong .');
            redirect(base_url('profile/'));
            exit();
        }
        
    }
    
    public function edit_profile_password()
    {
        $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        
        if (isset($_POST['form1'])) {

    $valid = 1;

    if( empty($_POST['cust_password']) || empty($_POST['cust_re_password']) ) {
        $valid = 0;
        $error_message .= LANG_VALUE_138."<br>";
    }

    if( !empty($_POST['cust_password']) && !empty($_POST['cust_re_password']) ) {
        if($_POST['cust_password'] != $_POST['cust_re_password']) {
            $valid = 0;
            $error_message .= LANG_VALUE_139."<br>";
        }
    }
    
    if($valid == 1) {

        // update data into the database

        $password = strip_tags($_POST['cust_password']);
        
        $statement = $this->db->query("UPDATE tbl_customer SET cust_password=?,pass=? WHERE cust_id=?",array(md5($password),$password,$_SESSION['customer']['cust_id']));
        $_SESSION['customer']['cust_password'] =md5($password);  
        
        
        $xdata=['pass'=>$password];
        $this->db->where('uid',$_SESSION['customer']['uid']);
        $this->db->update('details',$xdata);

        $success_message = LANG_VALUE_141;
        
        $this->session->set_flashdata('success', $success_message);
        redirect(base_url('profile/customer_password_update'));
    }
    
    $this->session->set_flashdata('error', $error_message);
    redirect(base_url('profile/customer_password_update'));
}

    }
    
    public function edit_profile(){
        
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
        $customer = $this->session->userdata('customer');
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        $error_message = '';
        if (isset($_POST['form1'])) {

    $valid = 1;

    if(empty($_POST['cust_name'])) {
        $valid = 0;
        $error_message .= LANG_VALUE_123."<br>";
    }

    if(empty($_POST['cust_phone'])) {
        $valid = 0;
        $error_message .= LANG_VALUE_124."<br>";
    }

    // if(empty($_POST['cust_address'])) {
    //     $valid = 0;
    //     $error_message .= LANG_VALUE_125."<br>";
    // }

 

    if(empty($_POST['cust_city'])) {
        $valid = 0;
        $error_message .= LANG_VALUE_127."<br>";
    }

    if(empty($_POST['cust_state'])) {
        $valid = 0;
        $error_message .= LANG_VALUE_128."<br>";
    }

    if(empty($_POST['cust_zip'])) {
        $valid = 0;
        $error_message .= LANG_VALUE_129."<br>";
    }

    if($valid == 1) {

        // update data into the database
        $statement = $this->db->query("UPDATE tbl_customer SET cust_name=?,  cust_phone=?,  cust_city=?, cust_state=?, cust_zip=? WHERE cust_id=?",array(
                    strip_tags($_POST['cust_name']),
                    strip_tags($_POST['cust_phone']),
                    strip_tags($_POST['cust_city']),
                    strip_tags($_POST['cust_state']),
                    strip_tags($_POST['cust_zip']),
                    $customer['cust_id']
                ));
        $success_message = LANG_VALUE_130;
        $this->session->set_flashdata('success', $success_message);
        redirect(base_url('profile'));
    }
    
    $this->session->set_flashdata('error', $error_message);
    redirect(base_url('profile'));
}

    }
    
	public function get_district() {
        if(isset($_POST['state'])){
    		$state = $_POST['state'];
    
    
    		$column['state'] = $state;
    		$table = 'tbl_district';
    		// we are getting results from common modal
    		$result4 = $this->Common_model->ResultsWhere2($column, $table);
    
    
    		echo '<option value="" selected> Select District </option>';
    		foreach ($result4 as $row) {
    			echo '<option value="' . $row['district'] . '">' . $row['district'] . '</option>';
    		}
        }
	}
    
}