<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['manager_id']) && isset($_POST['version']) && isset($_POST['admin_id']) && isset($_POST['user_type']))
{
	$dis_id = $_POST['manager_id'];
	$admin_id = $_POST['admin_id'];
	$user_type = $_POST['user_type'];

	if($user_type=='Manager') 
		$select_product = mysqli_query($con,"select *,tbl_customer.status as cstatus from tbl_customer where tbl_customer.status in ('Active','Deactive') and tbl_customer.manager_id='$dis_id'");
	else
		$select_product = mysqli_query($con,"select *,tbl_customer.status as cstatus from tbl_customer where tbl_customer.status in ('Active','Deactive') and tbl_customer.manager_id in (select manager_id from tbl_manager where admin_id='$admin_id')");
	
	$customer=array();

	if(mysqli_num_rows($select_product)>0)
	{     
		while($fetch_product=mysqli_fetch_assoc($select_product))
		{
			$customer['customer_id']=$fetch_product['customer_id'];
			$customer['customer_name']=$fetch_product['customer_name'];
			$customer['phone_no']=$fetch_product['phone_no'];
			$customer['email']=$fetch_product['email'];
			$customer['address']=$fetch_product['address'];
			$customer['status']=$fetch_product['cstatus'];
			$customer['manager_id']=$fetch_product['manager_id'];

			$customer['policies']=array();

			if($user_type=='Manager') 
	 			$select_policies = mysqli_query($con,"select *from tbl_customer,tbl_insurance_company,tbl_policy_master where tbl_customer.customer_id=tbl_policy_master.customer_id and tbl_insurance_company.company_id=tbl_policy_master.company_id and tbl_customer.manager_id='$dis_id' and  tbl_customer.customer_id='".$fetch_product['customer_id']."' and tbl_policy_master.status in ('Active','Renewal Pending')");
	 		else
	 			$select_policies = mysqli_query($con,"select * from tbl_customer,tbl_insurance_company,tbl_policy_master where tbl_customer.customer_id=tbl_policy_master.customer_id and tbl_insurance_company.company_id=tbl_policy_master.company_id and tbl_customer.manager_id in (select manager_id from tbl_manager where admin_id='$admin_id') and tbl_customer.customer_id='".$fetch_product['customer_id']."'");
			if(mysqli_num_rows($select_policies)>0)
			{     
				while($fetch_policies=mysqli_fetch_assoc($select_policies))
				{
		        	$policies['policy_no']=$fetch_policies['policy_no'];
					$policies['premium_amt']=$fetch_policies['premium_amt'];
					$policies['company_name']=$fetch_policies['company_name'];
					$policies['renewal_date']=$fetch_policies['renewal_date'];
					$policies['policy_type']=$fetch_policies['policy_type'];
					$policies['policy_term']=$fetch_policies['policy_term'];
					$policies['term_value']=$fetch_policies['term_value'];
					$policies['NCB']=$fetch_policies['NCB'];
					$policies['NCB_value']=$fetch_policies['NCB_value'];
					$policies['assign_status']=$fetch_policies['status'];
					$policies['unique_id']=$fetch_policies['unique_id'];
					// $select_assign_status = mysqli_query($con,"SELECT * FROM `tbl_customer_premium` where policy_no ='".$fetch_policies['policy_no']."' and status ='Pending'");
					// if(mysqli_num_rows($select_assign_status)>0)
					// { 
					// 		$policies['assign_status']="Pending";
					// }else{
					// 		$policies['assign_status']="Complete";
					// }
					
					$customer['policies'][]=$policies;
				}
			}

		$response['customer'][]=$customer;
		}
	}
	else
	{
		$response['customer']=$customer;
	}
	


	if($user_type=='Manager') 
		$select_agent = mysqli_query($con,"select * from tbl_agent where status in ('Active','Deactive') and manager_id='$dis_id' order by agent_id");
	else
		$select_agent = mysqli_query($con,"select * from tbl_agent where status in ('Active','Deactive') and manager_id in (select manager_id from tbl_manager where admin_id='$admin_id') order by agent_id");
	$agent=array();
	if(mysqli_num_rows($select_agent)>0)
	{     
		while($fetch_agent=mysqli_fetch_assoc($select_agent))
		{
			$agent['agent_id']=$fetch_agent['agent_id'];
			$agent['manager_id']=$fetch_agent['manager_id'];
			$agent['agent_name']=$fetch_agent['agent_name'];
			$agent['agent_phone']=$fetch_agent['agent_phone'];
			$agent['agent_code']=$fetch_agent['agent_code'];
			$agent['status']=$fetch_agent['status'];
			$response['agent'][]=$agent;
		}
	}
	else
	{
		$response['agent'][]=$agent;
	}
	

	$select_company = mysqli_query($con,"select * from tbl_insurance_company where status in ('Active','Deactive') and admin_id='$admin_id' order by company_id");
	$company=array();
	if(mysqli_num_rows($select_company)>0)
	{     
		while($fetch_company=mysqli_fetch_assoc($select_company))
		{	
			
			$company['company_id']=$fetch_company['company_id'];
			$company['company_name']=$fetch_company['company_name'];
			$company['status']=$fetch_company['status'];
			$company['vehicles']=array();	
			$q5=mysqli_query($con,"Select * from tbl_vehicle_master where company_id='".$fetch_company['company_id']."'");
			if(mysqli_num_rows($q5)>0)
			{     
				while($fetch_q5=mysqli_fetch_assoc($q5))
				{
						$vehicles['vehicle_id']=$fetch_q5['vehicle_id'];
						$vehicles['vehicle_type']=$fetch_q5['vehicle_type'];
						$company['vehicles'][]=$vehicles;
				}
			}


			$response['company'][]=$company;

		}
	}
	else
	{
		$response['company'][]=$company;
	}


	$select_manager = mysqli_query($con,"select * from tbl_manager where status in ('Active','Deactive') and admin_id='$admin_id' and manage_type='Manager' order by manager_id");
	$manager=array();
	if(mysqli_num_rows($select_manager)>0)
	{     
		while($fetch_manager=mysqli_fetch_assoc($select_manager))
		{
			$manager['manager_id']=$fetch_manager['manager_id'];
			$manager['manager_name']=$fetch_manager['manager_name'];
			$manager['manager_phone']=$fetch_manager['manager_phone'];
			$manager['manager_email']=$fetch_manager['manager_email'];
			$manager['address']=$fetch_manager['address'];
			$manager['password']=$fetch_manager['password'];
			$manager['status']=$fetch_manager['status'];
			$response['manager'][]=$manager;
		}
	}
	else
	{
		$response['manager'][]=$manager;
	}
	
	$select_policy = mysqli_query($con,"select * from tbl_policy_type where status in ('Active','Deactive') and admin_id='$admin_id' order by policy_id");
	$policy=array();
	if(mysqli_num_rows($select_policy)>0)
	{     
		while($fetch_policy=mysqli_fetch_assoc($select_policy))
		{
			$policy['policy_id']=$fetch_policy['policy_id'];
			$policy['policy_name']=$fetch_policy['policy_name'];
			$policy['status']=$fetch_policy['status'];
			
			$response['policy'][]=$policy;
		}
	}
	else
	{
		$response['policy'][]=$policy;
	}

   $select_vehicle = mysqli_query($con,"SELECT tbl_vehicle_master.*,tbl_insurance_company.company_name FROM `tbl_vehicle_master`,tbl_insurance_company where tbl_insurance_company.company_id = tbl_vehicle_master.company_id and tbl_vehicle_master.admin_id = '$admin_id'");
	$vehicle=array();
	if(mysqli_num_rows($select_vehicle)>0)
	{     
		while($fetch_vehicle=mysqli_fetch_assoc($select_vehicle))
		{
			$vehicle['vehicle_id']=$fetch_vehicle['vehicle_id'];
			$vehicle['vehicle_type']=$fetch_vehicle['vehicle_type'];
			$vehicle['company_name']=$fetch_vehicle['company_name'];
			$vehicle['status']=$fetch_vehicle['status'];
			
			$response['vehicle'][]=$vehicle;
		}
	}
	else
	{
		$response['vehicle'][]=$vehicle;
	}

	$select_gst = mysqli_query($con,"select * from tbl_gst_percentage where status in ('Active','Deactive') and admin_id = '$admin_id'");
	$gst=array();
	if(mysqli_num_rows($select_gst)>0)
	{     
		while($fetch_gst=mysqli_fetch_assoc($select_gst))
		{
			$gst['gst_id']=$fetch_gst['gst_id'];
			$gst['percentage_value']=$fetch_gst['percentage_value'];
			$gst['status']=$fetch_gst['status'];
			
			$response['gst'][]=$gst;
		}
	}
	else
	{
		$response['gst'][]=$gst;
	}

 $select_commission = mysqli_query($con,"select tbl_commission_type.*,tbl_vehicle_master.vehicle_type from tbl_commission_type,tbl_vehicle_master where tbl_commission_type.admin_id='$admin_id' and tbl_vehicle_master.vehicle_id = tbl_commission_type.vehicle_id order by commission_id");
	$commission=array();
	if(mysqli_num_rows($select_commission)>0)
	{     
		while($fetch_commission=mysqli_fetch_assoc($select_commission))
		{
			$commission['commission_id']=$fetch_commission['commission_id'];
			$commission['vehicle_id']=$fetch_commission['vehicle_id'];
			$commission['vehicle_name']=$fetch_commission['vehicle_type'];
			$commission['admin_id']=$fetch_commission['admin_id'];
			$commission['type']=$fetch_commission['type'];
			$commission['percentage']=$fetch_commission['percentage'];
			$commission['percentage2']=$fetch_commission['percentage2'];
			$commission['start_date']=$fetch_commission['start_date'];
			$commission['end_date']=$fetch_commission['end_date'];

			$q11 = mysqli_query($con,"Select company_name from tbl_insurance_company where company_id = (SELECT company_id from tbl_vehicle_master where vehicle_id = '".$fetch_commission['vehicle_id']."')");
			while($fetch_q11=mysqli_fetch_assoc($q11)){
				$commission['company_name']=$fetch_q11['company_name'];
			}
			$response['commission_percentage'][]=$commission;
		}
	}
	else
	{
		$response['commission_percentage'][]=$commission;
	}


	$get_distributor=mysqli_query($con,"SELECT * FROM tbl_admin_panel where admin_id='$admin_id'");
	while($fetch_distributor=mysqli_fetch_assoc($get_distributor))
	{
		$response['next_renew_date']=$fetch_distributor['next_renew_date'];
	}

	$get_distributor=mysqli_query($con,"SELECT imei_no FROM `tbl_manager` WHERE manager_id ='$dis_id'");
	while($fetch_distributor=mysqli_fetch_assoc($get_distributor))
	{
		$response['imei']=$fetch_distributor['imei_no'];
	}

	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$response['current_date']=$cdate;
	$response['app_version']='1.3';
	$response['success']='1';
	$response['details']="Fetch Detail Successful";
}
else
{
	$response['success']='0';
	$response['details']="Error in Logging in!! Please update app from Google Play Store and try again!!";
}	
echo json_encode($response);
?>