<?php

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

class Frontend extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('job_circular_model');
		$this->load->model('invoice_model');
		$this->load->model('estimates_model');
		$this->load->model('proposal_model');
		$this->load->model('kb_model');
		$this->load->helper('string');
		$this->load->library('gst');
	}

	function index()
	{
		$data['title'] = lang('job_posted_list');
		$data['subview'] = $this->load->view('frontend/job_vacancy', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data);
	}

	public function circular_details($id)
	{
		$data['title'] = lang('view_circular_details');

		//get all training information
		$data['circular_details'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

		$data['subview'] = $this->load->view('frontend/circular_details', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data);
	}

	public function apply_jobs($id)
	{
		$data['title'] = lang('view_circular_details');

		//get all training information
		$data['circular_info'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

		$data['subview'] = $this->load->view('frontend/apply_jobs', $data, false);
		$this->load->view('admin/_layout_modal_lg', $data);
	}

	public function save_job_application($id)
	{
		$data = $this->job_circular_model->array_from_post(array('name', 'email', 'mobile', 'cover_letter'));
		// Resume File upload
		if (!empty($_FILES['resume']['name'])) {
			$val = $this->job_circular_model->uploadFile('resume');
			$val == TRUE || redirect('frontend/circular_details/' . $id);
			$data['resume'] = $val['path'];
		}
		$data['job_circular_id'] = $id;

		$this->job_circular_model->_table_name = 'tbl_job_appliactions';
		$this->job_circular_model->_primary_key = 'job_appliactions_id';
		$job_appliactions_id = $this->job_circular_model->save($data);

		$circular_info = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

		$job_circular_email = config_item('job_circular_email');
		if (!empty($circular_info->designations_id)) {
			$design_info = $this->db->where('designations_id', $circular_info->designations_id)->get('tbl_designations')->row();
			$dept_head_id = $this->db->where('departments_id', $design_info->departments_id)->get('tbl_departments')->row();
			$user_info = $this->job_circular_model->check_by(array('user_id' => $dept_head_id->department_head_id), 'tbl_users');
			if (!empty($user_info)) {
				if (!empty($job_circular_email) && $job_circular_email == 1) {
					$email_template = email_templates(array('email_group' => 'new_job_application_email'), $dept_head_id->department_head_id, true);

					$message = $email_template->template_body;
					$subject = $email_template->subject;
					$name = str_replace("{NAME}", $data['name'], $message);
					$job_title = str_replace("{JOB_TITLE}", $circular_info->job_title, $name);
					$email = str_replace("{EMAIL}", $data['email'], $job_title);
					$mobile = str_replace("{MOBILE}", $data['mobile'], $email);
					$cover_letter = str_replace("{COVER_LETTER}", $data['cover_letter'], $mobile);
					$Link = str_replace("{LINK}", base_url() . 'admin/job_circular/view_jobs_application/' . $job_appliactions_id, $cover_letter);
					$message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);
					$data['message'] = $message;
					$message = $this->load->view('email_template', $data, TRUE);

					$params['subject'] = $subject;
					$params['message'] = $message;
					$params['resourceed_file'] = '';
					$params['recipient'] = $user_info->email;
					$this->job_circular_model->send_email($params);
				}
				$notifyUser = array($user_info->user_id);
			}
			if (!empty($notifyUser)) {
				foreach ($notifyUser as $v_user) {
					add_notification(array(
						'to_user_id' => $v_user,
						'description' => 'not_new_job_application',
						'icon' => 'globe',
						'link' => 'admin/job_circular/view_jobs_application/' . $job_appliactions_id,
						'value' => lang('by') . ' ' . $data['name'],
					));
				}
			}
			if (!empty($notifyUser)) {
				show_notification($notifyUser);
			}
		}
		// messages for user
		$type = "success";
		$message = lang('job_application_submitted');
		set_message($type, $message);
		if (empty($_SERVER['HTTP_REFERER'])) {
			redirect('frontend');
		} else {
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function jobs_posted_pdf($id)
	{
		$data['job_posted'] = $this->db->where('job_circular_id', $id)->get('tbl_job_circular')->row();

		$this->load->helper('dompdf');
		$view_file = $this->load->view('admin/job_circular/jobs_posted_pdf', $data, true);
		pdf_create($view_file, slug_it(lang('jobs_posted') . '- ' . $data['job_posted']->job_title));
	}

	public function view_invoice($id)
	{
		$data['title'] = lang('invoice_details');
		$id = url_decode($id);
		$data['invoice_info'] = $this->invoice_model->check_by(array('invoices_id' => $id), 'tbl_invoices');
		if (!empty($data['invoice_info'])) {
			$data['client_info'] = $this->invoice_model->check_by(array('client_id' => $data['invoice_info']->client_id), 'tbl_client');

			$lang = $this->invoice_model->all_files();
			foreach ($lang as $file => $altpath) {
				$shortfile = str_replace("_lang.php", "", $file);
				//CI will record your lang file is loaded, unset it and then you will able to load another
				//unset the lang file to allow the loading of another file
				if (isset($this->lang->is_loaded)) {
					$loaded = sizeof($this->lang->is_loaded);
					if ($loaded < 3) {
						for ($i = 3; $i <= $loaded; $i++) {
							unset($this->lang->is_loaded[$i]);
						}
					} else {
						for ($i = 0; $i <= $loaded; $i++) {
							unset($this->lang->is_loaded[$i]);
						}
					}
				}
				if (!empty($data['client_info']->language)) {
					$language = $data['client_info']->language;
				} else {
					$language = 'english';
				}
				$data['language_info'] = $this->lang->load($shortfile, $language, TRUE, TRUE, $altpath);
			}
		} else {
			set_message('error', lang('no_data_found'));
			redirect('frontend/');
		}

		$data['subview'] = $this->load->view('frontend/invoice/invoice_details', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data);
	}

	public function estimates($id)
	{
		$data['title'] = lang('invoice_details');
		$id = url_decode($id);
		$data['estimates_info'] = $this->estimates_model->check_by(array('estimates_id' => $id), 'tbl_estimates');
		$data['client_info'] = $this->estimates_model->check_by(array('client_id' => $data['estimates_info']->client_id), 'tbl_client');
		if (empty($data['estimates_info'])) {
			set_message('error', lang('no_data_found'));
			redirect('frontend/');
		}
		$lang = $this->invoice_model->all_files();
		foreach ($lang as $file => $altpath) {
			$shortfile = str_replace("_lang.php", "", $file);
			//CI will record your lang file is loaded, unset it and then you will able to load another
			//unset the lang file to allow the loading of another file
			if (isset($this->lang->is_loaded)) {
				$loaded = sizeof($this->lang->is_loaded);
				if ($loaded < 3) {
					for ($i = 3; $i <= $loaded; $i++) {
						unset($this->lang->is_loaded[$i]);
					}
				} else {
					for ($i = 0; $i <= $loaded; $i++) {
						unset($this->lang->is_loaded[$i]);
					}
				}
			}
			if (!empty($data['client_info']->language)) {
				$language = $data['client_info']->language;
			} else {
				$language = 'english';
			}
			$data['language_info'] = $this->lang->load($shortfile, $language, TRUE, TRUE, $altpath);
		}

		$data['subview'] = $this->load->view('frontend/estimate/estimates_details', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data);
	}

	public function proposals($id)
	{
		$data['title'] = lang('invoice_details');
		$id = url_decode($id);
		$data['proposals_info'] = $this->proposal_model->check_by(array('proposals_id' => $id), 'tbl_proposals');
		if (empty($data['proposals_info'])) {
			set_message('error', lang('no_data_found'));
			redirect('frontend/');
		}
		$data['subview'] = $this->load->view('frontend/proposals/proposals_details', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data);
	}

	public function pdf_invoice($id)
	{
		$data['title'] = "Invoice PDF"; //Page title
		// get all invoice info by id
		$data['invoice_info'] = $this->invoice_model->check_by(array('invoices_id' => $id), 'tbl_invoices');
		$this->load->helper('dompdf');
		$viewfile = $this->load->view('frontend/invoice/invoice_pdf', $data, TRUE);
		pdf_create($viewfile, slug_it('Invoice  # ' . $data['invoice_info']->reference_no));
	}

	public function pdf_estimates($id)
	{
		$data['estimates_info'] = $this->invoice_model->check_by(array('estimates_id' => $id), 'tbl_estimates');
		$data['title'] = "Estimates PDF"; //Page title
		$this->load->helper('dompdf');
		$viewfile = $this->load->view('frontend/estimate/estimates_pdf', $data, TRUE);
		pdf_create($viewfile, slug_it(lang('estimate') . '# ' . $data['estimates_info']->reference_no));
	}

	public function pdf_proposals($id)
	{
		$data['proposals_info'] = $this->proposal_model->check_by(array('proposals_id' => $id), 'tbl_proposals');
		$data['title'] = lang('proposal') . "PDF"; //Page title
		$this->load->helper('dompdf');
		$viewfile = $this->load->view('frontend/proposals/proposals_pdf', $data, TRUE);
		pdf_create($viewfile, slug_it(lang('proposals') . '# ' . $data['proposals_info']->reference_no));
	}

	// public function knowledgebase()
	// {
	//     $data['title'] = lang('knowledgebase');
	//     $data['all_kb_category'] = get_result('tbl_kb_category', array('type' => 'kb', 'status' => 1));
	//     $data['subview'] = $this->load->view('frontend/kb/kb_list', $data, TRUE);
	//     $this->load->view('frontend/_layout_main', $data); //page load
	// }

	public function knowledgebase()
	{
		$search = trim((string) $this->input->get('q', true));
		$selectedCategory = (int) $this->input->get('category', true);

		$categories = $this->db
			->where('type', 'kb')
			->where('status', 1)
			->order_by('sort', 'ASC')
			->order_by('kb_category_id', 'ASC')
			->get('tbl_kb_category')
			->result();

		$articles = $this->db
			->select('tbl_knowledgebase.*, tbl_kb_category.category AS category_name, tbl_kb_category.description AS category_description')
			->from('tbl_knowledgebase')
			->join('tbl_kb_category', 'tbl_kb_category.kb_category_id = tbl_knowledgebase.kb_category_id', 'left')
			->where('tbl_kb_category.type', 'kb')
			->where('tbl_kb_category.status', 1)
			->where('tbl_knowledgebase.status', 1)
			->where('tbl_knowledgebase.for_all', 'No')
			->order_by('tbl_knowledgebase.total_view', 'DESC')
			->order_by('tbl_knowledgebase.kb_id', 'DESC')
			->get()
			->result();

		$categoryCounts = array();
		if (!empty($articles)) {
			foreach ($articles as $article) {
				$cid = (int) $article->kb_category_id;
				if (!isset($categoryCounts[$cid])) {
					$categoryCounts[$cid] = 0;
				}
				$categoryCounts[$cid]++;
			}
		}

		$filteredArticles = array();
		if (!empty($articles)) {
			foreach ($articles as $article) {
				if (!empty($selectedCategory) && (int) $article->kb_category_id !== $selectedCategory) {
					continue;
				}

				if ($search !== '') {
					$haystack = mb_strtolower((string) ($article->title . ' ' . strip_tags((string) $article->description) . ' ' . $article->category_name));
					if (mb_strpos($haystack, mb_strtolower($search)) === false) {
						continue;
					}
				}
				$filteredArticles[] = $article;
			}
		}

		// Keep selected category valid.
		if (!empty($selectedCategory)) {
			$exists = false;
			foreach ($categories as $category) {
				if ((int) $category->kb_category_id === $selectedCategory) {
					$exists = true;
					break;
				}
			}
			if (!$exists) {
				$selectedCategory = 0;
			}
		}

		$currentCategory = null;
		if (!empty($selectedCategory)) {
			foreach ($categories as $category) {
				if ((int) $category->kb_category_id === $selectedCategory) {
					$currentCategory = $category;
					break;
				}
			}
		}

		// Group filtered articles by category for compact category cards.
		$grouped = array();
		foreach ($filteredArticles as $article) {
			$cid = (int) $article->kb_category_id;
			if (!isset($grouped[$cid])) {
				$grouped[$cid] = array();
			}
			$grouped[$cid][] = $article;
		}

		$data = array(
			'title' => lang('knowledgebase'),
			'categories' => $categories,
			'category_counts' => $categoryCounts,
			'articles' => $filteredArticles,
			'grouped_articles' => $grouped,
			'current_category' => $currentCategory,
			'selected_category' => $selectedCategory,
			'search' => $search,
		);

		$this->load->view('frontend/knowledgebase', $data);
	}

	public function knowledgebase_article($id = null)
	{
		$id = (int) $id;
		if (empty($id)) {
			show_404();
		}

		$article = $this->db
			->select('tbl_knowledgebase.*, tbl_kb_category.category AS category_name, tbl_kb_category.description AS category_description, tbl_kb_category.kb_category_id')
			->from('tbl_knowledgebase')
			->join('tbl_kb_category', 'tbl_kb_category.kb_category_id = tbl_knowledgebase.kb_category_id', 'left')
			->where('tbl_knowledgebase.kb_id', $id)
			->where('tbl_knowledgebase.status', 1)
			->where('tbl_knowledgebase.for_all', 'No')
			->where('tbl_kb_category.status', 1)
			->where('tbl_kb_category.type', 'kb')
			->get()
			->row();

		if (empty($article)) {
			show_404();
		}

		$this->kb_model->increase_total_view($id);

		$related = $this->db
			->select('kb_id, title, total_view')
			->from('tbl_knowledgebase')
			->where('status', 1)
			->where('for_all', 'No')
			->where('kb_category_id', (int) $article->kb_category_id)
			->where('kb_id !=', $id)
			->order_by('total_view', 'DESC')
			->order_by('kb_id', 'DESC')
			->limit(8)
			->get()
			->result();

		$categories = $this->db
			->where('type', 'kb')
			->where('status', 1)
			->order_by('sort', 'ASC')
			->order_by('kb_category_id', 'ASC')
			->get('tbl_kb_category')
			->result();

		$categoryCountsQuery = $this->db
			->select('kb_category_id, COUNT(*) AS total_articles')
			->from('tbl_knowledgebase')
			->where('status', 1)
			->where('for_all', 'No')
			->group_by('kb_category_id')
			->get()
			->result();

		$categoryCounts = array();
		foreach ($categoryCountsQuery as $row) {
			$categoryCounts[(int) $row->kb_category_id] = (int) $row->total_articles;
		}

		$data = array(
			'title' => $article->title,
			'article' => $article,
			'related' => $related,
			'categories' => $categories,
			'category_counts' => $categoryCounts,
		);

		$this->load->view('frontend/knowledgebase_article', $data);
	}

	public function kb_details($type, $id)
	{
		$data['title'] = lang('articles') . ' ' . lang('details');
		$data['all_kb_category'] = get_result('tbl_kb_category', array('type' => 'kb', 'status' => 1));
		if ($type == 'articles') {
			$this->kb_model->increase_total_view($id);
			$data['articles_info'] = $this->kb_model->get_kb_info('articles', $id, true);
		} else {
			$data['articles_by_category'] = $this->kb_model->get_kb_info('category', $id, true);
		}
		$data['subview'] = $this->load->view('frontend/kb/articles_details', $data, TRUE);
		$this->load->view('frontend/_layout_main', $data); //page load
	}

	public function kb_download($id, $fileName)
	{
		$file_info = get_row('tbl_knowledgebase', array('kb_id' => $id));
		$attachment = json_decode($file_info->attachments);
		// get array value from $attachment array
		$values = array_values((array)$attachment);
		// get file name from array value
		$file_name = $values[$fileName];
		$path = $file_name->path;
		// check file is exist or not
		if (file_exists('./' . $path)) {
			$data = file_get_contents('./' . $path); // Read the file's contents
			force_download($file_name->fileName, $data);
		} else {
			$type = 'error';
			$message = lang('operation_failed');
			set_message($type, $message);
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	function get_article_suggestion()
	{
		$search = $this->input->post("search", true);
		if ($this->input->is_ajax_request()) {
			if ($search) {
				$result = $this->kb_model->get_suggestions($search, true);
				echo json_encode($result);
				exit();
			}
		}
	}
}
