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

class Profile extends CI_Controller {

	public function __construct(){
		parent::__construct();
		check_login_user();
		check_valid_user();
		is_valid();
	}

	public function index()
	{
		$data = array();
		$data['page_title'] = "Home";
      $data['page'] = "Profile";
      $data['s_home'] = false;
      $data['social_sites'] = $this->admin_m->get_social_icon(1);
      $data['home'] = $this->admin_m->get_home();
		$data['main_content'] = $this->load->view('admin/dashboard/profile/home', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function edit_home($id)
	{
		$data = array();
		$data['page_title'] = "Home";
		$data['page'] = "Profile";
		$data['s_home'] =$this->admin_m->single_select_by_id($id,'home');
		valid_user($data['s_home']['user_id']);

		$data['home'] = $this->admin_m->get_home();
		$data['social_sites'] = $this->admin_m->get_social_icon(1);
		$data['main_content'] = $this->load->view('admin/dashboard/profile/home', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	
	/**
	  *** add home content
	**/ 
	public function add_home(){
		is_test();
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('about_me', 'About Me Overview', 'trim|required|xss_clean');
		$this->form_validation->set_rules('about_me_long', 'About Me', 'trim');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/'));
			}else{	

				$data = array(
					'title' => $this->input->post('title',true),
					'user_id' => auth('id'),
					'about_me' => $this->input->post('about_me'),
					'about_me_long' => $this->input->post('about_me_long'),
					'designations' => $this->input->post('designations',true),
					'created_at' => d_time(),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'home');
				}else{
					$insert = $this->admin_m->update($data,$id,'home');
				}

				if($insert){
					if(isset($_POST['social_sites'])):
						foreach ($_POST['social_sites'] as $key => $site) {
							$user_site = get_user_social_sites($site);
							$data = array(
								'site_id' => $site,
								'site_value' => $_POST[$site],
								'user_id' => auth('id'),
							);
							if($user_site['site_id']==$site){
								$this->admin_m->update($data,$user_site['id'],'user_social_sites');
							}else{
								$this->admin_m->insert($data,'user_social_sites');
							}
						}
					endif;
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/'));
				}	
		}
	}



	

	public function vcard()
	{
		$data = array();
		$data['page_title'] = "vCard";
		$data['page'] = "vCard";
		$data['data'] = false;
		$data['social_sites'] = $this->admin_m->get_social_icon(0);
    	$data['u_info']=$this->admin_m->get_user_info();
    	$data['sites']=$this->admin_m->get_my_vcard_info(auth('id'));
    	$data['active_vcard'] = isset($data['u_info']->active_vcard)?$data['u_info']->active_vcard:0;
		$data['main_content'] = $this->load->view('admin/dashboard/profile/vcard', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

/**
   ** default Image Upload for services and reviews
 **/ 
	public function upload_images($id=0,$table)
	{
		is_test();
		if(!empty($_FILES['images']['name'])){
			if($table=='services'){
				$directory = 'uploads/services/';
			}elseif($table=='reviews'){
				$directory = 'uploads/reviews/';
			}else{
				$directory = 'uploads/';
			}
			$dir = $directory;
			$name = $_FILES['images']['name'];
			list($txt, $ext) = explode(".", $name);
			$image_name = md5(time()).".".$ext;
			$tmp = $_FILES['images']['tmp_name'];
		   if(move_uploaded_file($tmp, $dir.$image_name)){
			    $url = $dir.$image_name;
			    $data = array('images' => $url);
				$this->admin_m->update($data,$id,$table);	
		   }else{
		      echo "image uploading failed";
		   }
		}

	}

	/**
	  ** user skill 
	**/

	public function skills()
	{
		$data = array();
		$data['page_title'] = "Skills";
        $data['page'] = "Profile";
        $data['s_skill'] = false;
        $data['skills'] = $this->admin_m->select_all_by_user('skills');
		$data['main_content'] = $this->load->view('admin/dashboard/profile/skills', $data, TRUE);
		$this->load->view('admin/index',$data);
	}


	/**
	  ** edit user skill
	**/

	public function edit_skills($id)
	{
		$data = array();
		$data['page_title'] = "Skills";
        $data['page'] = "Profile";
        $data['s_skill'] =$this->admin_m->single_select_by_id($id,'skills');
        $data['skills'] = $this->admin_m->select_all_by_user('skills');
        valid_user($data['s_skill']['user_id']);
		$data['main_content'] = $this->load->view('admin/dashboard/profile/skills', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add_skills
	**/ 
	public function add_skills(){
		is_test();
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('level', 'level', 'trim|required|integer|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->skills();
			}else{	
				$data = array(
					'title' => $this->input->post('title',true),
					'user_id' => auth('id'),
					'level' => $this->input->post('level',true),
					'created_at' => d_time(),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'skills');
				}else{
					$insert = $this->admin_m->update($data,$id,'skills');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/skills'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/skills'));
				}	
		}
	}

	/**
	  ** user services
	**/

	public function services()
	{
		$data = array();
		$data['page_title'] = "Services";
		$data['page'] = "Profile";
		$data['is_add'] = false;
		$data['data'] = false;
		$data['services'] = $this->admin_m->select_all_by_user('services');
		$data['social_icons'] = $this->admin_m->get_social_icon(0);
		$data['main_content'] = $this->load->view('admin/dashboard/about/services', $data, TRUE);
		$this->load->view('admin/index',$data);
	}


	public function order_list()
	{
		$data = array();
		$data['page_title'] = "Services";
		$data['page'] = "Profile";
		$data['is_add'] = false;
		$data['data'] = false;
		$data['order_list'] = $this->admin_m->get_my_customer_order_list('order_list');
		$data['main_content'] = $this->load->view('admin/dashboard/activities/services_order_list', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function add_new_services()
	{
		$data = array();
		$data['page_title'] = "Services";
		$data['page'] = "Profile";
		$data['is_add'] = true;
		$data['data'] = false;
		$data['services'] = $this->admin_m->select_all_by_user('services');
		$data['social_icons'] = $this->admin_m->get_social_icon(0);
		$data['main_content'] = $this->load->view('admin/dashboard/about/services', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function service_settings($type='')
	{
		$data = array();
		$data['page_title'] = "Service Settings";
		$data['page'] = "Settings";
		$data['is_add'] = true;
		$data['data'] = false;
		$data['type'] = $type;
		$data['countries'] = $this->admin_m->select('countries');
		$data['service_settings'] = $this->admin_m->single_select_by_user(auth('id'),'service_settings');
		$data['main_content'] = $this->load->view('admin/dashboard/about/service_settings', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  ** edit user services
	**/

	public function edit_services($id)
	{
		$data = array();
		$data['page_title'] = "Services";
        $data['page'] = "Profile";
        $data['is_add'] = true;
        $data['data'] =$this->admin_m->single_select_by_id($id,'services');
        $data['services'] = $this->admin_m->select_all_by_user('services');
        $data['social_icons'] = $this->admin_m->get_social_icon(0);
        valid_user($data['data']['user_id']);
		$data['main_content'] = $this->load->view('admin/dashboard/about/services', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add services
	**/ 
	public function add_services(){
		is_test();
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('details', 'Details', 'required|xss_clean');
		$this->form_validation->set_rules('overview', 'Overview', 'required|xss_clean|max_length[300]');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->services();
			}else{	
				$service_type = $this->input->post('service_type',true);

				$data = array(
					'title' => $this->input->post('title',true),
					'user_id' => auth('id'),
					'icon' => $this->input->post('icon',true),
					'service_type' => $service_type,
					'details' => $this->input->post('details'),
					'overview' => $this->input->post('overview',true),
					'price' => $this->input->post('price',true),
					'video_link' => $this->input->post('video_link'),
					'created_at' => d_time(),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'services');
				}else{
					$insert = $this->admin_m->update($data,$id,'services');
				}

				if($insert){
					$this->upload_images($insert,'services');
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/services'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/services'));
				}	
		}
	}


	public function add_service_settings(){
		is_test();
		$this->form_validation->set_rules('country_id', 'Currency', 'trim|required|xss_clean');
		$this->form_validation->set_rules('bank_details', 'Bank Details', 'xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/service_settings'));
			}else{	
				$data = array(
					'country_id' => $this->input->post('country_id',true),
					'bank_details' => $this->input->post('bank_details'),
					'pay_button_status' => $this->input->post('pay_button_status'),
					'user_id' => auth('id'),
				);
				$id = $this->input->post('id');
				if($id==0){
					if(!array_key_exists("created", $data)){
		            $data['created_at'] = d_time();
		        	};
					$insert = $this->admin_m->insert($data,'service_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'service_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/service_settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/service_settings'));
				}	
		}
	}

	/**
	  ** user about 
	**/
	public function about()
	{
		$data = array();
		$data['page_title'] = "About Me";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['home'] = $this->admin_m->get_about(0);
        $data['my_info'] = $this->admin_m->single_select_by_user(auth('id'),'home');
		$data['main_content'] = $this->load->view('admin/dashboard/about/home', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  ** edit about
	**/

	public function edit_about($id)
	{
		$data = array();
		$data['page_title'] = "About Me";
        $data['page'] = "Profile";
        $data['home'] = $this->admin_m->get_about(0);
        $data['data'] =$this->admin_m->get_about($id);
        $data['my_info'] = $this->admin_m->single_select_by_user(auth('id'),'home');
        if(!empty($data['data'])):
	        valid_user($data['data'][0]['user_id']);
	    else:
	    	redirect(base_url('dashboard'));
	    endif;
		$data['main_content'] = $this->load->view('admin/dashboard/about/home', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add about
	**/ 
	public function add_about(){
		is_test();
		$this->form_validation->set_rules('full_name', 'Label', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->about();
			}else{
				$dob = $this->input->post('dob',true);
				isset($dob) && !empty($dob)?$dob = $dob:$dob='0000-00-00';
				$data = array(
					'user_id' => auth('id'),
					'full_name' =>  $this->input->post('full_name',true),
					'nationality' =>  $this->input->post('nationality',true),
					'dob' => $dob,
					'created_at' => d_time(),
				);
				$id = $this->input->post('id',true);
				if($id==0){
					$insert = $this->admin_m->insert($data,'about');
				}else{
					$insert = $this->admin_m->update($data,$id,'about');
				}

				if($insert){
					$label = $this->input->post('label',true);
					$i=0;	
						if(!empty($label)){
							foreach ($label as $key => $value) {
								$data = array(
									'about_id' => $insert,
									'label' => $value,
									'value' => $this->input->post('value',true)[$i],
								);
								$i++;
								$this->admin_m->insert($data,'about_content');
							}
						}

					// update old fields value
					$label_ex = $this->input->post('label_ex',true);
					$j=0;	
						if(!empty($label_ex)){
							foreach ($label_ex as $key => $value_ex) {
								$data_ex = array(
									'about_id' => $insert,
									'label' => $value_ex,
									'value' => $this->input->post('value_ex',true)[$j],
								);
								$this->admin_m->update($data_ex,$this->input->post('ex_id')[$j],'about_content');
								$j++;
							}
						}
					$this->upload_images($insert,'about');
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/about'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/about'));
				}	
		}
	}


	/**
	  ** user review
	**/

	public function reviews()
	{
		$data = array();
		$data['page_title'] = "Reviews";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['reviews'] = $this->admin_m->select_all_by_user('reviews');
		$data['main_content'] = $this->load->view('admin/dashboard/about/reviews', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function edit_reviews($id)
	{
		$data = array();
		$data['page_title'] = "Reviews";
        $data['page'] = "Profile";
        $data['data'] =$this->admin_m->single_select_by_id($id,'reviews');
        $data['reviews'] = $this->admin_m->select_all_by_user('reviews');
        valid_user($data['data']['user_id']);
		$data['main_content'] = $this->load->view('admin/dashboard/about/reviews', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add_reviews
	**/ 
	public function add_reviews(){
		is_test();
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('designation', 'Designation', 'trim|required|xss_clean');
		$this->form_validation->set_rules('comments', 'Comments', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->reviews();
		}else{	
			$data = array(
				'name' => $this->input->post('name',true),
				'user_id' => auth('id'),
				'designation' => $this->input->post('designation',true),
				'comments' => $this->input->post('comments'),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'reviews');
			}else{
				$insert = $this->admin_m->update($data,$id,'reviews');
			}

			if($insert){
				$this->upload_images($insert,'reviews');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/reviews'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/reviews'));
			}	
		}
	}


	/**
	  ** user resume
	**/

	public function resume()
	{
		$data = array();
		$data['page_title'] = "Resume";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['type_data'] = false;
        $data['resume_type'] = $this->admin_m->select_all_by_user('resume_type');
        $data['social_icons'] = $this->admin_m->get_social_icon(0);
        $data['resume'] = $this->admin_m->get_resume_by_user();
		$data['main_content'] = $this->load->view('admin/dashboard/profile/resume', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	
	/**
	  ** edit resume
	**/
	public function edit_resume($id)
	{
		$data = array();
		$data['page_title'] = "Resume";
        $data['page'] = "Profile";
        $data['type_data'] =false;
        $data['resume_type'] = $this->admin_m->select_all_by_user('resume_type');
        $data['data'] =$this->admin_m->single_select_by_id($id,'resume');
        $data['resume'] = $this->admin_m->get_resume_by_user();
        $data['social_icons'] = $this->admin_m->get_social_icon(0);

        valid_user($data['data']['user_id']);

		$data['main_content'] = $this->load->view('admin/dashboard/profile/resume', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  ** edit resume type
	**/

	public function edit_resume_type($id)
	{
		$data = array();
		$data['page_title'] = "Resume";
        $data['page'] = "Profile";
        $data['type_data'] =$this->admin_m->single_select_by_id($id,'resume_type');
        $data['resume_type'] = $this->admin_m->select_all_by_user('resume_type');
        $data['data'] =false;
        $data['resume'] = $this->admin_m->get_resume_by_user();
        $data['social_icons'] = $this->admin_m->get_social_icon(0);

        valid_user($data['type_data']['user_id']);
		$data['main_content'] = $this->load->view('admin/dashboard/profile/resume', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add resume
	**/ 
	public function add_resume(){
		is_test();
		$this->form_validation->set_rules('resume_type', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('designation', 'Designation', 'trim|required|xss_clean');
		$this->form_validation->set_rules('details', 'Details', 'trim|required|xss_clean');
		$this->form_validation->set_rules('start_year', 'Start Year', 'trim|required|xss_clean|exact_length[4]');
		$this->form_validation->set_rules('end_year', 'End Year', 'trim|xss_clean|exact_length[4]');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->resume();
		}else{	
			$data = array(
				'title' => $this->input->post('title',true),
				'type_id' => $this->input->post('resume_type',true),
				'user_id' => auth('id'),
				'designation' => $this->input->post('designation',true),
				'details' => $this->input->post('details',true),
				'start_year' => $this->input->post('start_year',true),
				'end_year' => $this->input->post('end_year',true),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'resume');
			}else{
				$insert = $this->admin_m->update($data,$id,'resume');
			}

			if($insert){
				$this->upload_images($insert,'reviews');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/resume'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/resume'));
			}	
		}
	}

	/**
	  *** add_resume_type
	**/ 
	public function add_resume_type(){
		is_test();
		$this->form_validation->set_rules('type_name', 'Name', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->resume();
		}else{	
			$data = array(
				'type_name' => $this->input->post('type_name',true),
				'icon' => $this->input->post('icon',true),
				'user_id' => auth('id'),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'resume_type');
			}else{
				$insert = $this->admin_m->update($data,$id,'resume_type');
			}

			if($insert){
				$this->upload_images($insert,'reviews');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/resume'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/resume'));
			}	
		}
	}


	/**
	  ** user portfolio
	**/
	public function portfolio()
	{
		$data = array();
		$data['page_title'] = "Portfolio";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['portfolio_type'] = $this->admin_m->select_all_by_user('portfolio_type');
        $data['portfolio'] = $this->admin_m->select_all_by_user('portfolio');
		$data['main_content'] = $this->load->view('admin/dashboard/profile/portfolio', $data, TRUE);
		$this->load->view('admin/index',$data);
	}


	/**
	  ** edit portfolio
	**/

	public function edit_portfolio_type($id)
	{
		$data = array();
		$data['page_title'] = "Portfolio";
        $data['page'] = "Profile";
        $data['data'] =$this->admin_m->single_select_by_id($id,'portfolio_type');
        $data['portfolio_type'] = $this->admin_m->select_all_by_user('portfolio_type');
        $data['portfolio'] = $this->admin_m->select_all_by_user('portfolio');
        valid_user($data['data']['user_id']);
		$data['main_content'] = $this->load->view('admin/dashboard/profile/portfolio', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	
	/**
	  *** add_portfolio_type
	**/ 
	public function add_portfolio_type(){
		is_test();
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->portfolio();
		}else{	
			$data = array(
				'name' => $this->input->post('name',true),
				'user_id' => auth('id'),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'portfolio_type');
			}else{
				$insert = $this->admin_m->update($data,$id,'portfolio_type');
			}

			if($insert){
				$this->upload_images($insert,'reviews');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/portfolio'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/portfolio'));
			}	
		}
	}

	/**
	  ***  add portfolio
	**/ 
	public function add_portfolio()
	{
		is_test();
	    $data = array();
	    $is_video =$this->input->post('is_video');
 		if(isset($is_video) && $is_video==1){
 			$files =$this->input->post('link');
 		}else{
 			 $files = $_FILES['file']['name'];
 		}

	    if (!empty($files)) {
	        $filesCount = count($files);

	        $total = count($this->admin_m->select_all_by_user('portfolio'));
        	$limit = limit(auth('id'));
        	if($limit !=0){
        		$total_up = $total+$filesCount;
        		if($total_up > $limit){
        			$check =0;
        			$this->session->set_flashdata('error', 'Sorry You Uploaded maximum portfolios');
        			echo json_encode(array('st'=>0,'msg'=>'Sorry You Uploaded maximum portfolios',));
        			exit();
        		}else if($total > $limit){
        			$check =0;
        			echo json_encode(array('st'=>0,'msg'=>'Sorry You reached the maximum limit',));
        			$this->session->set_flashdata('error', 'Sorry You reached the maximum limit');
        			exit();
        		}else{
        			$check =1;
        		}
        		
        	}else{
        		$check = 1;
        	}



        if($check ==1):
        	if(isset($is_video) && $is_video==1):
    			foreach ($files as $key => $value) {
    				$data = array(
						'user_id' => auth('id'),
						'type_id' => $_POST['type_id'],
						'image' => get_youtube_thumb($value),
						'thumb' => get_youtube_thumb($value),
						'link' => get_embeded($value),
						'title' => $this->input->post('titles')[$key],
						'is_video' => $is_video,
						'created_at' => d_time(),
					);
					$this->admin_m->insert($data,'portfolio');
    			};
    			echo json_encode(array('st'=>1,'msg'=>'Added Successfully'));
    			exit();
        	else:
		        for ($i = 0; $i < $filesCount; $i++) {
		              $_FILES['uploadFile']['name'] = str_replace(",","_",$_FILES['file']['name'][$i]);
		              $_FILES['uploadFile']['type'] = $_FILES['file']['type'][$i];
		              $_FILES['uploadFile']['tmp_name'] = $_FILES['file']['tmp_name'][$i];
		              $_FILES['uploadFile']['error'] = $_FILES['file']['error'][$i];
		              $_FILES['uploadFile']['size'] = $_FILES['file']['size'][$i];

		              //Directory where files will be uploaded
		              $uploadPath = 'uploads/portfolio/';


		              $config['upload_path'] = $uploadPath;
		              // Specifying the file formats that are supported.
		              $config['allowed_types'] = 'jpg|jpeg|png|gif';
		              $config['overwrite'] = TRUE;
					  $config['encrypt_name'] = TRUE;
		              $this->load->library('upload', $config);
		              $this->upload->initialize($config);
		              // resize library
		              $this->load->library('image_lib');

		              if ($this->upload->do_upload('uploadFile')) {
		                  $fileData = $this->upload->data();
		                  $uploadData[$i]['file_name'] = $fileData['file_name'];
		                  // resize
				            $config = array(
							    'source_image'      => $fileData['full_path'], 
							    'new_image'         => $uploadPath.'/thumb', //path to
							    'maintain_ratio'    => true,
							    'width'             => 600,
							    'height'            => 600
							);
							    $this->image_lib->initialize($config);
							    $this->image_lib->resize();
							// resize
							    
		              }else{
		              	$error = array('error' => $this->upload->display_errors());
	                    foreach ($error as $value) {
	                    	$msg = $value;
	                    }
	                    echo json_encode(array('st'=>0,'msg'=>$msg,));
	                    exit();
		              }

		        };
		          
		        if (!empty($uploadData)) {
		          $list=array();
		            $j=0;foreach ($uploadData as $value) {
			          	$data = array(
			          		'type_id' => $_POST['type_id'],
			          		'link' => $this->input->post('link')[$j],
			          		'title' => $_POST['title'][$j],
			          		'user_id' => auth('id'),
							'is_video' => $is_video,
			          		'image' => $uploadPath.$value['file_name'],
			          		'thumb' => $uploadPath.'thumb/'.$value['file_name'],
			          		'created_at' => d_time(),
			          	);
			          	//insert image into database query
			          	$this->admin_m->insert($data,'portfolio');
			         $j++; 	
		          	}
		    		echo json_encode(array('st'=>1,));
			  	}else{
			    	$msg = 'Please insert an image';
			    	echo json_encode(array('st'=>0,'msg'=>$msg,));
			    };

			endif;
		endif;

	    }
	}




/**
   ** user image uploader
 **/ 
	public function upload_profile_image()
	{
		is_test();
	    $data = array();
	    if (!empty($_FILES['file']['name'])) {
	        $filesCount = count($_FILES['file']['name']);
	        for ($i = 0; $i < $filesCount; $i++) {
	              $_FILES['uploadFile']['name'] = str_replace(",","_",$_FILES['file']['name'][$i]);
	              $_FILES['uploadFile']['type'] = $_FILES['file']['type'][$i];
	              $_FILES['uploadFile']['tmp_name'] = $_FILES['file']['tmp_name'][$i];
	              $_FILES['uploadFile']['error'] = $_FILES['file']['error'][$i];
	              $_FILES['uploadFile']['size'] = $_FILES['file']['size'][$i];

	              //Directory where files will be uploaded
	              $uploadPath = 'uploads/profile/';


	              $config['upload_path'] = $uploadPath;
	              // Specifying the file formats that are supported.
	              $config['allowed_types'] = 'jpg|jpeg|png';
	              $config['overwrite'] = TRUE;
				  $config['encrypt_name'] = TRUE;
	              $this->load->library('upload', $config);
	              $this->upload->initialize($config);
	              // resize library
	              $this->load->library('image_lib');

	              if ($this->upload->do_upload('uploadFile')) {
	                  $fileData = $this->upload->data();
	                  $uploadData[$i]['file_name'] = $fileData['file_name'];
	                  // resize
			            $config = array(
						    'source_image'      => $fileData['full_path'], 
						    'new_image'         => $uploadPath.'/thumb', //path to
						    'maintain_ratio'    => true,
						    'width'             => 250,
						    'height'            => 250
						);
						    $this->image_lib->initialize($config);
						    $this->image_lib->resize();
						// resize
						    
	              }else{
	              	$error = array('error' => $this->upload->display_errors());
                    foreach ($error as $value) {
                    	$msg = $value;
                    }
                    echo json_encode(array('st'=>0,'msg'=>$msg,));
                    exit();
	              }

	        }
	          
	        if (!empty($uploadData)) {
	          $list=array();
	          	foreach ($uploadData as $value) {
		          	$data = array(
		          		'image' => $uploadPath.$value['file_name'],
		          		'thumb' => $uploadPath.'thumb/'.$value['file_name'],
		          	);
		          	//insert image into database query
		          	$this->admin_m->update(array('thumb' => $uploadPath.'thumb/'.$value['file_name']),auth('id'),'users');
	          	}
	    		echo json_encode(array('st'=>1,'img'=>$uploadPath.'thumb/'.$value['file_name']));
		  	}else{
		    	$msg = 'Please insert an image';
		    	echo json_encode(array('st'=>0,'msg'=>$msg,));
		    }

	    }
	}



	/**
	  ** user settings
	**/

	public function settings()
	{
		$data = array();
		$data['page_title'] = "Settings";
        $data['page'] = "Settings";
        $data['data'] = false;
        $data['setting'] = $this->admin_m->single_select_by_user(auth('id'),'user_settings');
		$data['main_content'] = $this->load->view('admin/dashboard/activities/settings', $data, TRUE);
		$this->load->view('admin/index',$data);
	}
	
	/**
	  *** add themes
	**/ 
	public function add_themes(){
		is_test();
		$this->form_validation->set_rules('animation', 'Animation', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->settings();
			}else{	
				$data = array(
					'user_id' => auth('id'),
					'is_download' => !empty($this->input->post('is_download',true))?$this->input->post('is_download',true):0,
					'animation' => !empty($this->input->post('animation',true))?$this->input->post('animation',true):0,
					'is_facebook' => !empty($this->input->post('is_facebook',true))?$this->input->post('is_facebook',true):0,
					'is_share' => !empty($this->input->post('is_share'))?$this->input->post('is_share',true):0,
					'created_at' => d_time(),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'user_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/settings'));
				}	
		}
	}


	/**
	  *** add themes
	**/ 
	public function add_loader(){
		$this->form_validation->set_rules('preloader', 'Pre-loader', 'trim|xss_clean|required');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->settings();
			}else{	
				$data = array(
					'user_id' => auth('id'),
					'preloader' => $this->input->post('preloader',true),
				);
				
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'user_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/settings'));
				}	
		}
	}


	/**
	  *** add settings
	**/ 
	public function add_settings(){
		is_test();
		$this->form_validation->set_rules('address', 'Address', 'trim|xss_clean');
		$this->form_validation->set_rules('share_this_key', 'Sharethis key', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->settings();
			}else{	
				$data = array(
					'user_id' => auth('id'),
					'phone' => $this->input->post('phone',true),
					'email' => $this->input->post('email',true),
					'address' => $this->input->post('address',true),
					'gmap' => $this->input->post('gmap'),
					'share_this_key' => $this->input->post('share_this_key'),
					'created_at' => d_time(),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($data,'user_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/settings'));
				}	
		}
	}

	public function add_email_settings(){
		is_test();
		$this->form_validation->set_rules('contact_email', 'Admain Email', 'required|trim|xss_clean|valid_email');


		if($this->input->post('email_type') ==2){
			$this->form_validation->set_rules('smtp_port', 'SMTP PORT', 'trim|required|xss_clean|numeric');
			$this->form_validation->set_rules('smtp_host', 'SMTP HOST', 'trim|required|xss_clean');
			$this->form_validation->set_rules('smtp_password', 'SMTP Email Password', 'trim|required|xss_clean');
		}

		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/settings'));
		}else{	
			$data = array(
				'user_id' => auth('id'),
				'contact_email' => $this->input->post('contact_email',TRUE),
				'smtp_host' => $this->input->post('smtp_host'),
				'smtp_port' => $this->input->post('smtp_port',TRUE),
				'email_type' => $this->input->post('email_type',TRUE),
				'smtp_password' => $this->input->post('smtp_password',TRUE),
			);
			$id = $this->input->post('id',TRUE);

			if($id != 0):
				$insert = $this->admin_m->update($data,$id,'user_settings');
			else:
				$insert = $this->admin_m->insert($data,'user_settings');
			endif;

			if($insert){
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/settings'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/settings'));
			}	
		}
	}



	public function manage_features()
	{
		$data = array();
		$data['page_title'] = "Manage Features";
        $data['page'] = "Manage Features";
        $data['data'] = false;
        $data['pricing'] = $this->admin_m->get_all_users_features_by_id(auth('id'));
        $data['features'] = $this->admin_m->get_users_features();
		$data['main_content'] = $this->load->view('admin/dashboard/activities/manage_features', $data, TRUE);
		$this->load->view('admin/index',$data);
	}


	/**
	  *** delete user by admin
	**/ 
	public function delete_portfolio($id)
	{
		is_test();
		$img = single_select_by_id($id,'portfolio');

		$del= $this->admin_m->delete($id,"portfolio");
		if($del):
			delete_image_from_server($img['thumb']);
			delete_image_from_server($img['image']);
			$msg = 'Item Successfully Deleted';
			echo json_encode(array('st' => 1, 'msg'=> $msg));
		endif;
	}

	public function delete_cv()
	{
		is_test();
		$img = single_select_by_id(auth('id'),'home');
		$data = ['document' => ''];
		$del= $this->admin_m->update_user($data,auth('id'),"home");
		if($del):
			delete_image_from_server($img['document']);
			$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
			redirect($_SERVER['HTTP_REFERER']);
		endif;
	}

	public function delete_cover()
	{
		is_test();
		$img = single_select_by_id(auth('id'),'users');
		$data = ['cover_photo' => ''];
		$del= $this->admin_m->update($data,auth('id'),"users");
		if($del):
			delete_image_from_server($img['cover_photo']);
			$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
			redirect($_SERVER['HTTP_REFERER']);
		endif;
	}


	/**
	  *** delete layout banner
	**/ 
	public function delete_layout_banner($id)
	{
		is_test();
		$img = $this->admin_m->get_banner_img($id);
		$data = array($id =>'');
		$del= $this->admin_m->update_user($data,auth('id'),'features_images');
		if($del):
			delete_image_from_server($img);
			$msg = 'Banner Successfully Deleted';
			echo json_encode(array('st' => 1, 'msg'=> $msg));
		endif;
	}


	/**
   ** default Image Upload for services and reviews
 **/ 
	public function upload_cv()
	{
		is_test();
		if(!empty($_FILES['file']['name'])){
			$directory = 'uploads/files/';	
			$dir = $directory;
			$name = $_FILES['file']['name'];
			list($txt, $ext) = explode(".", $name);
			$image_name = md5(time()).".".$ext;
			$tmp = $_FILES['file']['tmp_name'];
		   if(move_uploaded_file($tmp, $dir.$image_name)){
			    $url = $dir.$image_name;
			    $data = array('document' => $url);
				$this->admin_m->update_user($data,auth('id'),'home');	
				echo json_encode(array('st'=>1,'msg'=>'Upload successfully'));
		   }else{
		      echo "image uploading failed";
		   }
		}

	}



	public function appointment()
	{
		$data = array();
		$data['page_title'] = "Appointment";
        $data['page'] = "Appointment";
        $data['appointment_date'] = $this->admin_m->select_all_by_user('my_appointments');
		$data['main_content'] = $this->load->view('admin/dashboard/profile/appointment', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add home content
	**/ 
	public function add_appointment(){
		is_test();
		$this->form_validation->set_rules('days[]', 'Days', 'trim|required|xss_clean');
		$this->form_validation->set_rules('start_time[]', 'Start Time', 'trim|required|xss_clean');
		$this->form_validation->set_rules('end_time[]', 'End Time', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/appointment/'));
		}else{	
			 $this->admin_m->delete_appointment('appointments');
			$days = $this->input->post('days');
			
			if(!empty($days)):
				$i=1;
				foreach ($days as $key => $day):
					$data = array(
						'days' => $day,
						'start_time' => $this->input->post('start_time',true)[$day],
						'end_time' => $this->input->post('end_time',true)[$day],
						'user_id' => auth('id'),
						'created_at' => d_time(),
					);
					 $insert = $this->admin_m->insert($data,'appointments');
				$i++;
				endforeach;
			endif;
			
			if($insert){
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/appointment'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/appointment'));
			}	
		}
	}

	public function delete_appointment($id){
		is_test();
		$del=$this->admin_m->delete($id,"my_appointments");
		if($del){
			$this->session->set_flashdata('success', 'Appointment Deleted');
			redirect(base_url('admin/profile/appointment'));
		}else{
			$this->session->set_flashdata('error', 'Somthing worng. Error!!');
			redirect(base_url('admin/profile/appointment'));
		}
	}


	public function layouts()
	{
		$data = array();
		$data['page_title'] = "Layouts";
        $data['page'] = "Layouts";
		$data['main_content'] = $this->load->view('admin/dashboard/activities/layouts', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function change_layouts($type,$value){
		$data = array(
			$type => $value,
		);
		$this->admin_m->update_profile($data,'users');
		echo json_encode(array('st'=>1,));
	}

	public function layouts_banner()
	{
		$data = array();
		$data['page_title'] = "Layouts Banner";
        $data['page'] = "Layouts";
		$data['main_content'] = $this->load->view('admin/dashboard/activities/layouts_banner', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function add_vcard_info()
	{
		is_test();
		$info = user_info();
		if($info->email == $this->input->post('email')){
			$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean|valid_email');
		}else{
			$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean|required|valid_email|is_unique[users.email]',array('is_unique'=>'Sorry This email already exits'));
		}
		$this->form_validation->set_rules('website', 'Website', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/vcard'));
			}else{	

				$data = array(
					'email' => $this->input->post('email',TRUE),
					'website' => $this->input->post('website',TRUE),
					'phone' => $this->input->post('phone',TRUE),
					'whatsapp' => $this->input->post('whatsapp',TRUE),
				);
				$insert = $this->admin_m->update_profile($data,'users');
				if($insert){
					
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/vcard'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/vcard'));
				}	
		}
	}



	public function upload_banner_img($field_name)
	{
		is_test();
	    $data = array();
	    if (!empty($_FILES['file']['name'])) {
	        $filesCount = count($_FILES['file']['name']);
	        for ($i = 0; $i < $filesCount; $i++) {
	              $_FILES['uploadFile']['name'] = str_replace(",","_",$_FILES['file']['name'][$i]);
	              $_FILES['uploadFile']['type'] = $_FILES['file']['type'][$i];
	              $_FILES['uploadFile']['tmp_name'] = $_FILES['file']['tmp_name'][$i];
	              $_FILES['uploadFile']['error'] = $_FILES['file']['error'][$i];
	              $_FILES['uploadFile']['size'] = $_FILES['file']['size'][$i];

	              //Directory where files will be uploaded
	              $uploadPath = 'uploads/site_images/';

	              $config['upload_path'] = $uploadPath;
	              // Specifying the file formats that are supported.
	              $config['allowed_types'] = 'jpg|jpeg|png|gif';
	              $config['overwrite'] = TRUE;
				  $config['encrypt_name'] = TRUE;
	              $this->load->library('upload', $config);
	              $this->upload->initialize($config);
	              // resize library
	              $this->load->library('image_lib');

	              if ($this->upload->do_upload('file')) {
	                  $fileData = $this->upload->data();
	                  $uploadData[$i]['file_name'] = $fileData['file_name'];
			            
	              }else{
	              	$error = array('error' => $this->upload->display_errors());
                    foreach ($error as $value) {
                    	$msg = '<div class="alert alert-warning alert-dismissible fade show" role="alert">
						  <strong>Error!</strong> '.$value.'
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
						    <span aria-hidden="true">&times;</span>
						  </button>
						</div>' ;
                    }
                    echo json_encode(array('st'=>0,'msg'=>$msg,));
                    exit();
	              }

	        }
	          
	        if (!empty($uploadData)) {
	          $list=array();
	            $j=0;foreach ($uploadData as $value) {
		          	$data = array(
		          		'user_id' => auth('id'),
		          		$field_name => $uploadPath.$value['file_name'],
		          	);

		          	//insert image into database query
		          	$check = $this->admin_m->select_by_user('features_images');
		          	if(!empty($check)){
		          		$this->admin_m->update_user($data,auth('id'),'features_images');
		          	}else{
		          		$this->admin_m->insert($data,'features_images');
		          	}
		          	echo json_encode(array('st'=>1, 'url'=> $uploadPath.$value['file_name']));
		         $j++; 	
	          	}
	    		
		  	}else{
		    	$msg = '<div class="alert alert-warning alert-dismissible fade show" role="alert">
						  <strong>Error!</strong> Please Select image
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
						    <span aria-hidden="true">&times;</span>
						  </button>
						</div>';
		    	echo json_encode(array('st'=>0,'msg'=>$msg,));
		    }

	    }
	}

	
	/**
	  ** user Team
	**/

	public function team()
	{
		$data = array();
		$data['page_title'] = "Team Members";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['team'] = $this->admin_m->select_all_by_user('team');
		$data['main_content'] = $this->load->view('admin/dashboard/activities/meet_team', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function edit_team($id)
	{
		$data = array();
		$data['page_title'] = "Reviews";
        $data['page'] = "Profile";
        $data['data'] =$this->admin_m->single_select_by_id($id,'team');
        $data['team'] = $this->admin_m->select_all_by_user('team');
		$data['main_content'] = $this->load->view('admin/dashboard/activities/meet_team', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add_reviews
	**/ 
	public function add_team(){
		is_test();
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('designation', 'Designation', 'trim|required|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->team();
		}else{	
			$data = array(
				'name' => $this->input->post('name',true),
				'user_id' => auth('id'),
				'designation' => $this->input->post('designation',true),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'team');
			}else{
				$insert = $this->admin_m->update($data,$id,'team');
			}

			if($insert){
				$this->upload_images($insert,'team');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/team'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/team'));
			}	
		}
	}



	/**
	  ** Fact items
	**/

	public function facts()
	{
		$data = array();
		$data['page_title'] = "Facts";
        $data['page'] = "Profile";
        $data['data'] = false;
        $data['facts'] = $this->admin_m->select_all_by_user('facts');
        $data['social_icons'] = $this->admin_m->get_social_icon(0);
		$data['main_content'] = $this->load->view('admin/dashboard/activities/facts', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function edit_facts($id)
	{
		$data = array();
		$data['page_title'] = "Facts";
        $data['page'] = "Profile";
        $data['data'] =$this->admin_m->single_select_by_id($id,'facts');
        $data['facts'] = $this->admin_m->select_all_by_user('facts');
        $data['social_icons'] = $this->admin_m->get_social_icon(0);
		$data['main_content'] = $this->load->view('admin/dashboard/activities/facts', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	/**
	  *** add_reviews
	**/ 
	public function add_facts(){
		is_test();
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('icon', 'Icon', 'trim|required|xss_clean');
		$this->form_validation->set_rules('total_item', 'Items Number', 'trim|required|xss_clean|numeric');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			$this->facts();
		}else{	
			$data = array(
				'name' => $this->input->post('name',true),
				'user_id' => auth('id'),
				'icon' => $this->input->post('icon'),
				'total_item' => $this->input->post('total_item'),
				'created_at' => d_time(),
			);
			$id = $this->input->post('id');
			if($id==0){
				$insert = $this->admin_m->insert($data,'facts');
			}else{
				$insert = $this->admin_m->update($data,$id,'facts');
			}

			if($insert){
				$this->upload_images($insert,'facts');
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
				redirect(base_url('admin/profile/facts'));
			}else{
				$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
				redirect(base_url('admin/profile/facts'));
			}	
		}
	}


	public function item_delete($id,$table){
		is_test();
		$del=$this->admin_m->item_delete($id,$table);
		if($del){
			$this->session->set_flashdata('success', 'Item Deleted');
			redirect($_SERVER['HTTP_REFERER']);
		}else{
			$this->session->set_flashdata('error', 'Somthing worng. Error!!');
			redirect($_SERVER['HTTP_REFERER']);
		}
	}


	public function delete_about_content($id,$table){
		is_test();
		$del=$this->admin_m->delete($id,$table);
		if($del){
			$this->session->set_flashdata('success', 'Item Deleted');
			redirect($_SERVER['HTTP_REFERER']);
		}else{
			$this->session->set_flashdata('error', 'Somthing worng. Error!!');
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function deactive_account($status,$username){
		is_test();
		$user = get_id_by_slug($username);
		$data = array(
			'is_deactived' => $status
		);

		$up=$this->admin_m->update($data,$user,'users');
		if($up){
			if($status==1):
				$this->session->set_flashdata('success', 'Your account is Deactive Now');
			else:
				$this->session->set_flashdata('success', 'Your account is Active Now');
			endif;
			redirect($_SERVER['HTTP_REFERER']);
		}else{
			$this->session->set_flashdata('error', 'Somthing worng. Error!!');
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function add_color(){
		$data = array(
			'colors' => substr($this->input->post('colors'), 1),
		);
		$insert = $this->admin_m->update($data,auth('id'),'users');
		if($insert){
			$this->session->set_flashdata('success','Save Change Successful');
			redirect($_SERVER['HTTP_REFERER']);
		}else{
			$this->session->set_flashdata('error','Somethings Were Wrong!!');
			redirect($_SERVER['HTTP_REFERER']);
		}
		
	}

	public function add_recaptcha(){
		is_test();
		$this->form_validation->set_rules('g_site_key', 'Site Key', 'trim|required|xss_clean|required');
		$this->form_validation->set_rules('g_secret_key', 'Secret Key', 'required|trim|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/settings'));
			}else{	
				$data = array(
					'is_recaptcha' => $this->input->post('is_recaptcha',TRUE),
					'g_site_key' => $this->input->post('g_site_key',TRUE),
					'g_secret_key' => $this->input->post('g_secret_key',TRUE),
				);
				$cap_data = array(
					'user_id' => auth('id'),
					'recaptcha_config' => json_encode($data),
				);
				$id = $this->input->post('id');
				if($id==0){
					$insert = $this->admin_m->insert($cap_data,'user_settings');
				}else{
					$insert = $this->admin_m->update($cap_data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/settings'));
				}	
		}
	}

	public function rating()
	{
		$data = array();
		$data['page_title'] = "Rating";
        $data['page'] = "Rating";
        $data['facts'] = $this->admin_m->select_all_by_user('facts');
		$data['main_content'] = $this->load->view('admin/dashboard/activities/rating', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function add_rating(){
		is_test();
		$this->form_validation->set_rules('rating', 'Rating', 'trim|xss_clean|required');
		if ($this->form_validation->run() == FALSE) {
				$this->session->set_flashdata('error', validation_errors());
				redirect(base_url('admin/profile/rating'));
			}else{	
				$check_rating = $this->admin_m->check_my_rating();

				if($check_rating ==1):
					$this->session->set_flashdata('error', 'You already give you feedback');
					redirect(base_url('admin/profile/rating'));
				endif;

				
					$data = array(
						'rating' => $this->input->post('rating',TRUE),
						'msg' => $this->input->post('msg',TRUE),
						'action_id' => auth('id'),
						'created_at' => d_time(),
					);

					$insert = $this->admin_m->insert($data,'users_rating');

					if($insert){
					$this->session->set_flashdata('success', !empty(lang('rating_submit'))?lang('rating_submit'):'Rating Submited Successfully');
					redirect(base_url('admin/profile/rating'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/rating'));
				}	
		}
	}

	public function reset_admin(){
		$data =  array(
				'password' => md5(1234),
			);
		$update = $this->admin_m->update($data,1,'users');
		if($update){
			echo json_encode(array('st'=>1));
		}else{
			echo json_encode(array('st'=>0));
		}
	}

	public function vcard_status(){
		$data =  array(
				'vcard_status' => $this->input->post('vcard_status',TRUE),
			);
		$update = $this->admin_m->update($data,auth('id'),'users');
		$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
		redirect($_SERVER['HTTP_REFERER']);
	}

	public function add_card_cover()
	{


		ini_set('memory_limit', '-1');
		$icon_color = substr($this->input->post('vcard_icon_color'), 1);
		$bg_color = substr($this->input->post('vcard_bg_color'), 1);
		if(!empty($_FILES['file']['name'])){
			$directory = 'uploads/';	
			$dir = $directory;
			$name = $_FILES['file']['name'];
			list($txt, $ext) = explode(".", $name);
			$image_name = md5(time()).".".$ext;
			$tmp = $_FILES['file']['tmp_name'];
		   if(move_uploaded_file($tmp, $dir.$image_name)){
			    $url = $dir.$image_name;
			    $data = array('cover_photo' => $url);
				$this->admin_m->update($data,auth('id'),'users');	
				$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					
		   }else{
		   		$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
		   }

		}

		$c_data = array('vcard_bg_color' => $bg_color,'vcard_icon_color' => $icon_color,'active_vcard' =>$this->input->post('active_vcard'));
		$this->admin_m->update($c_data,auth('id'),'users');
	   redirect(base_url('admin/profile/vcard'));

	}


	public function error_404()
	{
		$data = array();
		$data['page_title'] = "Error 404";
		$this->load->view('404');
	}



	public function seo_settings()
	{
		
		$data = array();
		$data['page_title'] = "Seo Settings";
        $data['page'] = "Settings";
        $data['settings'] = $this->common_m->get_user_settings(auth('id'));
		$data['main_content'] = $this->load->view('admin/dashboard/activities/seo_settings', $data, TRUE);
		$this->load->view('admin/index',$data);
	}

	public function add_seo_settings(){
		is_test();
    	$id = $this->input->post('id');
		$this->form_validation->set_rules('title', 'Title', 'trim|xss_clean|required');
		$this->form_validation->set_rules('description', 'Description', 'trim|xss_clean|required');
		$this->form_validation->set_rules('keywords', 'keywords', 'trim|xss_clean|required');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/seo_settings'));
			}else{	
				$social = array(
					'title' => $this->input->post('title',TRUE),
					'description' => $this->input->post('description',TRUE),
					'keywords' => $this->input->post('keywords',TRUE),
				);

				$data = array(
					'user_id' => auth('id'),
					'seo_settings' => json_encode($social),
				);
				if($id==0){
					$insert = $this->admin_m->insert($data,'user_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/seo_settings'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/seo_settings'));
				}	
		}
	}



public function paypal_config()
{
	is_test();
	$id =  $this->input->post('id',true);
	$is_paypal =  $this->input->post('is_paypal',true);
	$is_live =  $this->input->post('is_live',true);
	if($is_paypal==1):
		$this->form_validation->set_rules('paypal_email', 'Paypal Business mail', 'trim|xss_clean|required');
	else:
		$this->form_validation->set_rules('paypal_email', 'Paypal Business mail', 'trim|xss_clean');
	endif;

	if ($this->form_validation->run() == FALSE) {
		$this->session->set_flashdata('error', validation_errors());
		redirect($_SERVER['HTTP_REFERER']);
	}else{	
		$paypal = array(
			'is_paypal' => isset($is_paypal)?$is_paypal:0,
			'is_live' => isset($is_live)?$is_live:0,
			'paypal_email' => $this->input->post('paypal_email',TRUE),
		);
		$data = array(
			'user_id' => auth('id'),
			'paypal_config' => json_encode($paypal),
		);

		if(is_allow()==1):
			if($id ==0){
				if(!array_key_exists("created", $data)){
		            $data['created_at'] = d_time();
		        }
				$insert = $this->admin_m->insert($data,'service_settings');
			}else{
				$insert = $this->admin_m->update($data,$id,'service_settings');
			}
		endif;
		if($insert){
			$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
			redirect(base_url('admin/profile/service_settings'));
		}else{
			$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
			redirect(base_url('admin/profile/service_settings'));
		}	
	}
}

public function stripe_config()
{
	is_test();
	$id =  $this->input->post('id',true);
	$is_stripe =  $this->input->post('is_stripe',true);
	if($is_stripe==1):
		$this->form_validation->set_rules('public_key', 'Stripe Public key', 'trim|xss_clean|required');
		$this->form_validation->set_rules('secret_key', 'Stripe Secret key', 'trim|xss_clean|required');
	else:
		$this->form_validation->set_rules('public_key', 'Stripe Public key', 'trim|xss_clean');
		$this->form_validation->set_rules('secret_key', 'Stripe Secret key', 'trim|xss_clean');
	endif;

	if ($this->form_validation->run() == FALSE) {
		$this->session->set_flashdata('error', validation_errors());
		redirect($_SERVER['HTTP_REFERER']);
	}else{	
		$stripe = array(
			'is_stripe' => isset($is_stripe)?$is_stripe:0,
			'public_key' => $this->input->post('public_key',TRUE),
			'secret_key' => $this->input->post('secret_key',TRUE),
		);
		
		$data = array(
			'user_id' => auth('id'),
			'stripe_config' => json_encode($stripe),
		);
		if(is_allow()==1):
			if($id ==0){
				if(!array_key_exists("created", $data)){
		            $data['created_at'] = d_time();
		        }
				$insert = $this->admin_m->insert($data,'service_settings');
			}else{
				$insert = $this->admin_m->update($data,$id,'service_settings');
			}
		endif;
		if($insert){
			$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
			redirect(base_url('admin/profile/service_settings'));
		}else{
			$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
			redirect(base_url('admin/profile/service_settings'));
		}	
	}
}


public function razorpay_config()
{
	is_test();
	$id =  $this->input->post('id',true);
	$is_razorpay =  $this->input->post('is_razorpay',true);
	if($is_razorpay==1):
		$this->form_validation->set_rules('key_id', 'Razorpay  key ID', 'trim|xss_clean|required');
		$this->form_validation->set_rules('secret_key', 'Razorpay Secret key', 'trim|xss_clean|required');
	else:
		$this->form_validation->set_rules('secret_key', 'Razorpay  key', 'trim|xss_clean');
	endif;

	if ($this->form_validation->run() == FALSE) {
		$this->session->set_flashdata('error', validation_errors());
		redirect($_SERVER['HTTP_REFERER']);
	}else{	
		$razorpay = array(
			'is_razorpay' => isset($is_razorpay)?$is_razorpay:0,
			'key_id' => $this->input->post('key_id',TRUE),
			'secret_key' => $this->input->post('secret_key',TRUE),
		);
		
		$data = array(
			'user_id' => auth('id'),
			'razorpay_config' => json_encode($razorpay),
		);
		if(is_allow()==1):
			if($id ==0){
				if(!array_key_exists("created", $data)){
		            $data['created_at'] = d_time();
		        }
				$insert = $this->admin_m->insert($data,'service_settings');
			}else{
				$insert = $this->admin_m->update($data,$id,'service_settings');
			}
		endif;
		if($insert){
			$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
			redirect(base_url('admin/profile/service_settings'));
		}else{
			$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
			redirect(base_url('admin/profile/service_settings'));
		}	
	}
}

	public function check_coupon_code()
	{
		$code = $this->input->post('coupon_code',true);
		$price = $this->input->post('price',true);
		$available = $this->admin_m->check_coupon_code($code);
		if(!empty($available)){
			if($available['total_used'] < $available['total_limit']){
				$percent_price = get_percent($price,$available['discount']);
				$total_price = $price - $percent_price;
				$data = [
					'is_discount'=>true,
					'discount' => $available['discount'],
					'total_user'=>$available['total_used']+1,
					'total_price' => $total_price,
					'percent_price'=>$percent_price,
					'coupon_id'=>$available['id']
				];
					$this->session->set_userdata('discount_ss',$data);
				 	echo json_encode(['st'=>1,'msg'=>'','price'=>$total_price,'percent_price'=>$percent_price]);
				}else{
					$msg = '<div class="alert alert-warning alert-dismissible fade show" role="alert">
							  <strong>'.lang('sorry').'!</strong> '.lang('coupon_code_reached_the_max_limit').'
							  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
							    <span aria-hidden="true">&times;</span>
							  </button>
							</div>' ;
				echo json_encode(['st'=>0,'msg'=>$msg,]);
			};
		}else{
				$msg = '<div class="alert alert-warning alert-dismissible fade show" role="alert">
						  <strong>'.lang('sorry').'</strong> '.lang('coupon_code_not_exists').'
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
						    <span aria-hidden="true">&times;</span>
						  </button>
						</div>' ;
			echo json_encode(['st'=>0,'msg'=>$msg,]);
		}
		
	}


	public function pwa()
	{
		$data = array();
		$data['page_title'] = "PWA";
		$data['page'] = "PWA";
		$data['settings'] = $this->common_m->get_user_settings(auth('id'));
		$data['main_content'] = $this->load->view('admin/dashboard/activities/pwa', $data, TRUE);
		$this->load->view('admin/index',$data);

		$file = base_url('uploads/pwa/avatar.png');
		$filePath = 'assets/images/avatar.png';
		/* Store the path of destination file */
		$destinationFilePath = 'uploads/pwa/avatar.png';
		if (!file_exists($file)) {
			copy($filePath, $destinationFilePath);
		}
		
	}
	public function add_pwa(){
		is_test();
		if(is_allow() !=1):
			exit();
		endif;
    	$id = $this->input->post('id');
		$this->form_validation->set_rules('title', 'Title', 'trim|xss_clean|required');
		$this->form_validation->set_rules('theme_color', 'Theme Color', 'trim|xss_clean');
		$this->form_validation->set_rules('background_color', 'background Color', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE) {
			$this->session->set_flashdata('error', validation_errors());
			redirect(base_url('admin/profile/pwa/'));
			}else{	
				$old_img = $this->input->post('old_img');
				$logo = $this->upload_logo($old_img);
				$is_pwa_active = $this->input->post('is_pwa_active');

				$pwaData = array(
					'title' => $this->input->post('title',TRUE),
					'theme_color' => substr($this->input->post('theme_color',TRUE),1),
					'background_color' => substr($this->input->post('background_color',TRUE),1),
					'is_pwa_active' => isset($is_pwa_active)?$is_pwa_active :0,
					'logo' => !empty($logo)?$logo:$old_img,
				);
				$data = array(
					'pwa_config' => json_encode($pwaData),
				);


				if($id==0){
					$insert = $this->admin_m->insert($data,'user_settings');
				}else{
					$insert = $this->admin_m->update($data,$id,'user_settings');
				}

				if($insert){
					$this->session->set_flashdata('success', !empty(lang('success_text'))?lang('success_text'):'Save Change Successful');
					redirect(base_url('admin/profile/pwa'));
				}else{
					$this->session->set_flashdata('error', !empty(lang('error_text'))?lang('error_text'):'Somethings Were Wrong!!');
					redirect(base_url('admin/profile/pwa'));
				}	
		}
	}

	public function upload_logo()
	{
		
		if(!empty($_FILES['images']['name'])){
			$directory = 'uploads/pwa/';
			$dir = $directory;
			$name = $_FILES['images']['name'];
			list($txt, $ext) = explode(".", $name);
			$image_name = md5(time()).".".$ext;
			$tmp = $_FILES['images']['tmp_name'];
			if(move_uploaded_file($tmp, $dir.$image_name)){
				$url = $dir.$image_name;
				$data = array('logo' => $url);
				return $url;
			}else{
				echo "image uploading failed";
			};
		}

	}

}
