<?php
	class Distributor_logs extends Admin_Controller{
		public function __construct(){
			parent::__construct();

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

		public function index(){

			$this->data["access_right"] = "view_right";
			$this->data["page_name"] = "distributor_logs";
			$this->data['meta_title'] = 'Distributor Logs';

			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$this->data['subview'] = $this->config->item('admin_folder').'/access_denied';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}else{
				$this->data['subview'] = $this->config->item('admin_folder').'/distributor_logs/index';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);

			}
		}


		public function ajax_load_data(){
			if(isset($_POST) && !empty($_POST)){

				$search_params = array();
				$like_params = array();

				if(isset($_POST['search']['value']) && !empty($_POST['search']['value'])){
					$like_params = array(
						"hc_distributor_logs.action_type" => $_POST['search']['value'],
						"hc_distributor_logs.action_identifier" => $_POST['search']['value'],
						"hc_distributor_logs.request" => $_POST['search']['value'],
						"hc_distributor_logs.response" => $_POST['search']['value']
					);

					$this->db->group_start();
					foreach ($like_params as $key => $value) {
						$this->db->or_like($key, $value);
					}
					$this->db->group_end();
		        }

				$this->db->select("hc_distributor_logs.*");
				$this->db->where($search_params);
				$this->db->limit($_POST['length'], $_POST['start']);
				$this->db->order_by("created_at", "desc");
				$result = $this->distributor_logs->get();

				$data = array();
	        	$no = $_POST['start'];

				foreach ($result as $key => $value) {

					$request = json_decode($value->request);
					$response = json_decode($value->response);

					
					$no++;
		            $row = array();
		            
		            $row[] = "<strong>".$no.".</strong>";
		            $row[] = $value->action_type;
		            $row[] = $this->bind_request_data($request, $value->action_identifier);
		            $row[] = $this->bind_response_data($response, $value->action_identifier);
		            $row[] = $value->created_at;
		            

		            $data[] = $row;
				}

				$output = array(
	                "draw" => $_POST['draw'],
	                "recordsTotal" => $this->distributor_logs->get_search_count($search_params, array()),
	                "recordsFiltered" => $this->distributor_logs->get_search_count($search_params, $like_params),
	                "data" => $data,
	        	);
		        //output to json format
		        echo json_encode($output);

			}
		}


		public function bind_request_data($data, $action_identifier){
			if($action_identifier == "distributor_login" || $action_identifier == "distributor_logout"){
				$html = "";
				foreach ($data->distributor_data as $key => $value) {
					if($key != "password" && $key != "expiry_date" && $key != "two_fa_enabled" && $key != "email_otp" && $key != "is_active" && $key != "created_at" && $key != "updated_at" && $key != "updated_by" && $key != "created_by"){
						$html .= $key . ': ' . $value . '<br>';
					}
				}
				return $html;
			}
		}

		public function bind_response_data($data, $action_identifier){
			if($action_identifier == "distributor_login" || $action_identifier == "distributor_logout"){
				$html = "";
				foreach ($data as $key => $value) {
					$html .= $key . ': ' . $value . '<br>';
				}
				return $html;
			}
		}

	}
?>