<?php
	class Frontend_Controller extends CI_Controller {

		public $data = array();
		public $log_path = '';

		public function __construct(){
			parent::__construct();

			$this->load->helper('form');
			$this->load->library('form_validation');
			$this->load->helper('security');
			$this->load->library('session');
			$this->load->helper('cookie');
			$this->load->helper('url');

			header("Strict-Transport-Security:max-age=63072000");
			header("X-Frame-Options: SAMEORIGIN");
			//header("X-Content-Type-Options: nosniff");
			header("X-XSS-Protection: 1");
			header("Cache-control: max-age=2592000");
			header("Pragma: no-cache");
			header("Referrer-Policy: no-referrer-when-downgrade");
			header("Feature-Policy: geolocation 'self'");
			//header("Permissions-Policy: geolocation 'self'");
			header("Expect-CT: max-age=86400");
			header("X-Permitted-Cross-Domain-Policies: 'master-only' | 'none'");

			$this->data['user_data'] = array();
			if($this->loggedin()){
				$email = $this->session->userdata('tfi_email');
				$data = $this->get_distributor_data($email);
				if($data['status'] && isset($data['data']) && !empty($data['data'])){
					$this->data['user_data'] = $data['data'];
				}
			}

			$tfi_display_view = $this->session->userdata('tfi_display_view');
			if(!$tfi_display_view){
				$tfi_display_view = "list";
			}

			$this->session->set_userdata(array("tfi_display_view" => $tfi_display_view));
			$this->data['tfi_display_view'] = $tfi_display_view;

		}

		public function get_distributor_data($email){
			$this->load->model("Hc_distributors", "distributors");

			$distributor_details = $this->distributors->get_by(array("email" => $email, "is_active" => 1, "Date(expiry_date) > " => Date("Y-m-d")), TRUE);

			if(empty($distributor_details)){
				$returnArr['status'] = false;
				$returnArr['message'] = "The distributor account is inactive or expired.";

				return $returnArr;
			}else{
				$returnArr['status'] = true;
				$returnArr['message'] = "Success";
				$returnArr['data'] = $distributor_details;

				return $returnArr;
			}
		}

		public function loggedin(){
			return (bool) $this->session->userdata('tfi_looged_in');
		}

		public function save_distributor_logs($data){

			$this->load->model("hc_distributor_logs", "distributor_logs");

			$data["created_by"] = 0;
			$data["created_at"] = Date("Y-m-d H:i:s");

			$log_id = $this->distributor_logs->save($data, NULL);

			return $log_id;
		}
	}
?>