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

class Profile extends CI_Controller {
	public function __construct(){
		parent::__construct();
        $this->all_schedule_post();
        // is_valid();
	}
	
	/**
	  ** user my info
	**/
	public function my_info($slug)
	{
		$data = array();
		$data['page_title'] = "My Info";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$user_info = $this->common_m->get_user_info_by_slug($slug);

        if($user_info['ref_id'] == 0){
            $id = $user_info['id'];
            $user_id = $user_info['id'];
            $data['user'] = $this->common_m->get_user_info_by_id($id);
        }else{
            $id = $user_info['id'];
            $user_id = $user_info['ref_id'];
            $data['user'] = $this->common_m->get_user_info_by_id($id);
        }

        $data['id']=$id;
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['home'] = $this->common_m->get_home($id,limit($id));

        if(get_user_pricing_by_id($user_id,'vcard')==1 && is_active($user_id,'vcard')):
            if(!empty($data['user']) && $data['user']['vcard_status']==1 || $data['user']['vcard_status']==3):
                $this->vcard($user_info['username']);
            else:
                $this->profile($user_info['username']);
            endif;
        else:
    		$this->profile($user_info['username']);
        endif;
        if(!empty($id)){
            $this->load->helper('cookie');
            $this->common_m->count_post_hit($id,'users');
        }
        
	}

    public function vcard($slug){
        $data = array();
        $data['page_title'] = "My Info";
        $data['page'] = "Profile";
        $data['s_page'] = "vCard";
        $data['slug'] = $slug;


        $data['vcard_page'] = 1;
        $data['is_vcard'] = 1;

        $user_info = $this->common_m->get_user_info_by_slug($slug);

        if($user_info['ref_id'] == 0){
            $id = $user_info['id'];
            $user_id = $user_info['id'];
            $data['user'] = $this->common_m->get_user_info_by_id($id);
        }else{
            $id = $user_info['id'];
            $user_id = $user_info['ref_id'];
            $data['user'] = $this->common_m->get_user_info_by_id($id);
        }
        $data['id']= $user_id;
        $data['user_id']=$id;
        $data['home'] = $this->common_m->get_home($id,limit($user_id));
        $data['u_settings'] = $this->common_m->get_user_settings($id);
        $data['active_vcard'] = isset($data['user']['active_vcard'])?$data['user']['active_vcard']:0;
        $data['icon_list'] = $this->admin_m->get_my_vcard_info($id);
        $data['is_vcard'] = 1;
        $data['main_content'] = $this->load->view(get_vcard_by_slug($slug), $data, TRUE);
        $this->load->view('vcard_index', $data);
    }

    public function profile($slug){
        $data = array();
        $data['page_title'] = "My Info";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        $data['user'] = $this->common_m->get_user_info_by_slug($slug);
        $data['home'] = $this->common_m->get_home($id,limit($id));
        
         $data['is_vcard'] = 0;
        if(get_layouts_by_slug($slug)==2 || get_layouts_by_slug($slug)==3 || get_layouts_by_slug($slug)==5 ||  get_layouts_by_slug($slug)==6){
            $data['services'] = $this->common_m->select_all_by_user($id,'services',limit($id));
            $data['reviews'] = $this->common_m->select_all_by_user($id,'reviews',limit($id));
            $data['about'] = $this->common_m->get_about($id,limit($id));

            $data['skills'] = $this->common_m->select_all_by_user($id,'skills',limit($id));
            $data['all_resume'] = $this->common_m->get_resume_by_user($id,limit($id));

            $data['portfolio_type'] = $this->common_m->select_all_by_user($id,'portfolio_type',limit($id));
            $data['portfolio'] = $this->common_m->select_all_by_user($id,'portfolio',limit($id));

            $data['team'] = $this->common_m->select_all_by_user($id,'team',limit($id));
            $data['facts'] = $this->common_m->select_all_by_user($id,'facts',limit($id));
            
            $data['appointments'] = $this->common_m->get_my_appointments($id,'appointments');

            $data['contact'] = $this->common_m->single_select_by_user($id,'user_settings');
            /*blog*/
             $data['posts'] = $this->blog_m->get_my_all_post($id,$limit=4,$type="id",$order="DESC");
           
            /*blog*/
        }
        if(get_layouts_by_slug($slug)==7){
            $data['services'] = $this->common_m->select_all_by_user($id,'services',6);
            $data['reviews'] = $this->common_m->select_all_by_user($id,'reviews',4);
            $data['about'] = $this->common_m->get_about($id,limit($id));

            $data['skills'] = $this->common_m->select_all_by_user($id,'skills',6);
            $data['all_resume'] = $this->common_m->get_resume_by_user($id,4);

            $data['portfolio_type'] = $this->common_m->select_all_by_user($id,'portfolio_type',limit($id));
            $data['portfolio'] = $this->common_m->select_all_by_user($id,'portfolio',10);

            $data['team'] = $this->common_m->select_all_by_user($id,'team',4);
            $data['facts'] = $this->common_m->select_all_by_user($id,'facts',4);
            
            $data['appointments'] = $this->common_m->get_my_appointments($id,'appointments');

            $data['contact'] = $this->common_m->single_select_by_user($id,'user_settings');
            /*blog*/
             $data['posts'] = $this->blog_m->get_my_all_post($id,$limit=4,$type="id",$order="DESC");
        }
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/home', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
       
    }

	public function about($slug)
	{
		$data = array();
		$data['page_title'] = "About";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$id = get_id_by_slug($slug);
		$data['id']=$id;
        $data['user']=users($slug);
		if(empty($id)){
        	redirect(base_url('error-404'));
        }
        $data['services'] = $this->common_m->select_all_by_user($id,'services',limit($id));
        $data['reviews'] = $this->common_m->select_all_by_user($id,'reviews',limit($id));
        $data['facts'] = $this->common_m->select_all_by_user($id,'facts',limit($id));
        $data['about'] = $this->common_m->get_about($id,limit($id));
		$data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/about', $data, TRUE);
		$this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
	}


    public function services($slug)
    {
        $data = array();
        $data['page_title'] = "Services";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        $data['user']=users($slug);
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['services'] = $this->common_m->select_all_by_user($id,'services',limit($id));
        $data['facts'] = $this->common_m->select_all_by_user($id,'facts',limit($id));
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/services', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }


     public function single_service($slug,$service_id)
    {
        $data = array();
        $data['page_title'] = "Services";
        $data['page'] = "Profile";
        $data['is_single_menu'] = 1;
        $data['is_flex'] = 0;
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        $data['service_id']=$service_id;
        $data['user']=users($slug);
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['service'] = $this->common_m->single_service_by_md5_id($id,$service_id);
        $data['service_settings'] = $this->admin_m->single_select_by_user($id,'service_settings');
        if(!empty($data['service_settings'])):
            $data['stripe'] = json_decode($data['service_settings']['stripe_config'],true);
            $data['paypal'] = json_decode($data['service_settings']['paypal_config'],true);
            $data['razorpay'] = json_decode($data['service_settings']['razorpay_config'],true);
        else:
            $data['stripe'] = '';
            $data['paypal'] = '';
            $data['razorpay'] = '';
        endif;
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/single_service', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }

	public function resume($slug)
	{
		$data = array();
		$data['page_title'] = "Resume";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$id = get_id_by_slug($slug);
		$data['id']=$id;
        $data['user']=users($slug);
		if(empty($id)){
        	redirect(base_url('error-404'));
        }
		$data['skills'] = $this->common_m->select_all_by_user($id,'skills',limit($id));
		$data['all_resume'] = $this->common_m->get_resume_by_user($id,limit($id));
		$data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/resume', $data, TRUE);
		$this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
	}

	public function portfolio($slug)
	{
		$data = array();
		$data['page_title'] = "Portfolio";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$id = get_id_by_slug($slug);
		$data['id']=$id;
        $data['user']=users($slug);
		if(empty($id)){
        	redirect(base_url('error-404'));
        }
        $data['portfolio_type'] = $this->common_m->select_all_by_user($id,'portfolio_type',limit($id));
        $data['portfolio'] = $this->common_m->select_all_by_user($id,'portfolio',limit($id));

         $data['team'] = $this->common_m->select_all_by_user($id,'team',limit($id));
        $data['facts'] = $this->common_m->select_all_by_user($id,'facts',limit($id));

		$data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/portfolio', $data, TRUE);
		$this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
	}

	public function blog($slug)
	{
		$data = array();
		$data['page_title'] = "Blog";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$id = get_id_by_slug($slug);
		$data['id']=$id;
        $data['user']=users($slug);
		if(empty($id)){
        	redirect(base_url('error-404'));
        }
        $data['posts'] = $this->blog_m->get_my_all_post($id,$limit=4,$type="id",$order="DESC");
		$data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/blog/blog', $data, TRUE);
		$this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
	}


    public function all_posts($slug)
    {
        $data = array();
        $data['page_title'] = "Blog";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['tags'] = $this->common_m->select_all_by_user($id,'tags',$limit=0);
        $data['categories'] = $this->common_m->select_all_by_user($id,'categories',$limit=0);
        $data['posts'] = $this->blog_m->get_my_all_post($id,$limit=0,$type="id",$order="DESC");
        $data['popular_posts'] = $this->blog_m->get_my_all_post($id,$limit=5,$type="hit",$order="DESC");
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/blog/all_posts', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }

    public function all_posts_by_category($slug,$cat_slug)
    {
        $data = array();
        $data['page_title'] = "Blog";
        $data['page'] = "Profile";
        $data['type_slug'] = 'category';
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['user']=users($slug);

        $data['id']=$id;
        if(empty($id)){
            redirect(base_url('error-404'));
        }

        $data['posts'] = $this->blog_m->all_posts_by_category($id,$cat_slug,$limit=0,$type="id",$order="DESC");

        $data['tags'] = $this->common_m->select_all_by_user($id,'tags',$limit=0);
        $data['categories'] = $this->common_m->select_all_by_user($id,'categories',$limit=0);
        $data['popular_posts'] = $this->blog_m->get_my_all_post($id,$limit=5,$type="hit",$order="DESC");
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/blog/all_posts', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }

    public function get_single_post_by_tag($slug,$tag_slug)
    {
        $data = array();
        $data['page_title'] = "Blog";
        $data['page'] = "Profile";
        $data['type_slug'] = 'Tags';
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['user']=users($slug);
        $data['id']=$id;
        if(empty($id)){
            redirect(base_url('error-404'));
        }

        $data['posts'] = $this->blog_m->get_single_post_by_tag($id,$tag_slug,$limit=0,$type="id",$order="DESC");
        
        $data['tags'] = $this->common_m->select_all_by_user($id,'tags',$limit=0);
        $data['categories'] = $this->common_m->select_all_by_user($id,'categories',$limit=0);
        $data['popular_posts'] = $this->blog_m->get_my_all_post($id,$limit=5,$type="hit",$order="DESC");
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/blog/all_posts', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }

    public function get_single_post_by_slug($slug,$post_slug)
    {
        $data = array();
        $data['page_title'] = "Blog";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        $data['user']=users($slug);
        if(empty($id)){
            redirect(base_url('error-404'));
        }

        $data['posts'] = $this->blog_m->get_single_post_by_slug($post_slug);

        //right side 
        $data['tags'] = $this->common_m->select_all_by_user($id,'tags',$limit=0);
        $data['categories'] = $this->common_m->select_all_by_user($id,'categories',$limit=0);
        //right side 
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/blog/single_post', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);

        // count visitor's hit
        if(isset($data['posts'][0]['id'])){
            $this->load->helper('cookie');
            $this->common_m->count_post_hit($data['posts'][0]['id'],'post');
        }
    }




    public function appointment($slug)
    {
        $data = array();
        $data['page_title'] = "Appointment";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        $data['user']=users($slug);
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['settings'] = $this->common_m->single_select_by_user($id,'user_settings');
        $data['appointments'] = $this->common_m->get_my_appointments($id,'appointments');
        $data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/appointment', $data, TRUE);
        $this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
    }


	public function contact($slug)
	{
		$data = array();
		$data['page_title'] = "Contacts";
		$data['page'] = "Profile";
		$data['slug'] = $slug;
		$id = get_id_by_slug($slug);
		$data['id']=$id;
        $data['user']=users($slug);
		if(empty($id)){
        	redirect(base_url('error-404'));
        }
        $data['contact'] = $this->common_m->single_select_by_user($id,'user_settings');
		$data['main_content'] = $this->load->view(get_view_layouts_by_slug($slug).'/contact', $data, TRUE);
		$this->load->view(get_view_layouts_by_slug($slug).'/index', $data);
	}

	//appointment mail
    public function appointment_mail($slug)
    {
        is_test();

           if(isset($_POST)):
            $setting = json_decode(single_select_by_user_slug($slug,'user_settings')['recaptcha_config']);
            if(isset($setting->is_recaptcha) && $setting->is_recaptcha==1):
                if($this->recaptcha($setting)==FALSE){
                    $msg = '<div class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry  ! </strong> Robot verification Failed
                    </div>';
                    echo json_encode(array('st' => 0, 'msg'=> $msg));
                    exit();
                }
            endif;

           
            $setting = get_setting();
            $u_info = get_user_info_by_slug($slug);
            $u_settings = get_user_settings($u_info['id']);
            $email = $this->input->post('email',TRUE);
            $title = $this->input->post('title',TRUE);
            $message = $this->input->post('message',TRUE);
            $date = $this->input->post('ap_date',TRUE);
            $data = array(
                'email' =>$email,
                'title' =>$title,
                'message' =>$message,
                'date' =>$date,
            );
            $send = $this->email_m->appointment_mail($slug,$data);
            if($send){
             $data = array(
                    'user_id' => $u_info['id'],
                    'title' => $title,
                    'email' => $email,
                    'ap_date' => $date,
                    'message' => $message,
                    'created_at' => d_time(),
                );
                $this->common_m->insert($data,'my_appointments');
                 $msg = '<div class="alert alert-success alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>'.lang('success').' </strong> '.lang('appointment_mail_sent').'
                    </div>';
                 echo json_encode(array('st'=>1,'msg'=>$msg));
            }else{
                echo json_encode(array('st'=>0,'msg'=>empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!'));
            }
        endif;
    }



    //send reset code to user email
    public function user_contact_mail($slug)
    {
        is_test();
        if(isset($_POST)):
            if(isset($setting['is_recaptcha']) && $setting['is_recaptcha']==1):
                if($this->recaptcha()==FALSE){
                    $msg = '<div class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry  ! </strong> Robot verification Failed
                    </div>';
                    echo json_encode(array('st' => 0, 'msg'=> $msg));
                    exit();
                }
            endif;
            $data = array(
                'username' =>$this->input->post('username',TRUE),
                'email' =>$this->input->post('email',TRUE),
                'subject' =>$this->input->post('subject',TRUE),
                'message' =>$this->input->post('message',TRUE),
                'slug' =>$this->input->post('slug',TRUE),
            );
            $send = $this->email_m->user_contact($data);
            if($send){
                $msg = '<div class="alert alert-success alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>'.lang('success').' </strong> '.lang('mail_sent').'
                    </div>';
                 echo json_encode(array('st'=>1,'msg'=>$msg));
             }else{
                 echo json_encode(array('st'=>0,'msg'=>empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!'));
             }
         endif;
    }
	
  
    public function all_schedule_post(){
        $posts = $this->blog_m->get_schedule_post();
        foreach ($posts as $post) {
           if($post['schedule_time'] < d_time()){
            $data = array(
                'is_schedule' =>0
            );
            $this->common_m->update($data,$post['id'],'post');
           } 
        }
    }

	public function error_404()
	{
		$data = array();
		$data['page_title'] = "Error 404";
		$this->load->view('404');
	}
	
    function check_all($dir){
       $check = $this->admin_m->check_files($dir);
       if($check==true):
        echo 'done';
       else:
        echo 'not found';
       endif;

    }

    public function recaptcha($setting){
        $recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
        $userIp=$this->input->ip_address();
        $secret = $setting->g_secret_key;

        $url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp;

        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $output = curl_exec($ch); 
        curl_close($ch);      
         
        $status= json_decode($output, true);
        if($status['success']){
            return TRUE;
        }else{
            return FALSE;
        }
    }


    public function count_copy($id){
        $user = single_select_by_id($id,'users');
        $data = array(
            'share_link' => $user['share_link'] + 1,
        );
        $this->common_m->update($data,$id,'users');
        echo json_encode(['st'=>1]);
    }


    public function get_time_by_date($id,$user_id)
    {
        $time = $this->common_m->get_time_using_id($id,$user_id);
       echo json_encode(['st'=>1,'end_time'=>$time['end_time'],'start_time'=>$time['start_time']]);
    }

	
    public function get_tables($table)
    {
        $data = $this->common_m->select($table);
        echo "<pre>";print_r($data);exit();
    }

    public function updates(){
        is_test();
        if(isset($_GET)){
            $field = $_GET['field'];
            $value = $_GET['value'];
            $id = $_GET['id'];
            $table = $_GET['table'];
            $data =  array(
                $field => $value,
            );
            $update = $this->admin_m->update($data,$id,$table);
            if($update){
                echo json_encode(array('st'=>1));
            }else{
                echo json_encode(array('st'=>0));
            }
        }
        
        
    }


    public function preview_vcard(){
        $data = array();
        if(isset($_GET['style'])){
            $style = $_GET['style'];
        }else{
            $style =1;
        }
        $settings = get_setting();
        if(isset($settings['demo_username']) && !empty($settings['demo_username'])):
            $slug = $settings['demo_username'];
        else:
            $slug = '';
        endif;

        $data['page_title'] = "My Info";
        $data['page'] = "Profile";
        $data['slug'] = $slug;
        $id = get_id_by_slug($slug);
        $data['id']=$id;
        if(empty($id)){
            redirect(base_url('error-404'));
        }
        $data['user'] = $this->common_m->get_user_info_by_slug($slug);
        $data['home'] = $this->common_m->get_home($id,limit($id));
        $data['active_vcard'] = isset($data['user']['active_vcard'])?$data['user']['active_vcard']:0;
        $data['icon_list'] = $this->admin_m->get_my_vcard_info($id);
        $data['is_vcard'] = 1;

        if(file_exists(APPPATH."views/card_layouts/vcard_{$style}.php")):
            $data['main_content'] = $this->load->view("card_layouts/vcard_{$style}", $data, TRUE);
        else:
            $data['main_content'] = $this->load->view("card_layouts/vcard_1", $data, TRUE);
        endif;
        $this->load->view('vcard_index', $data);
    }
}
