<?php
header('Content-Type: application/json');
date_default_timezone_set("Asia/Kolkata");

include('DB.php');

if (isset($_POST['service_followup_update'])) {
	
	$bdd = new DB();
	//print_r($_POST);
	$remark=$_POST['remark'];
	$nfd=$_POST['nfd'];
	$service_status_date=date('Y-m-d', strtotime($_POST['service_status_date']));
	$status=$_POST['status'];
	$user_date=$_POST['nfd1'];
	$hidden_service_id=$_POST['hidden_service_id'];
	$service_type=$_POST['service_type'];
	$service_type_master_id=$_POST['service_type_master'];
	$service_no=$_POST['service_no'];
	$hidden_lfd=$_POST['hidden_lfd'];	
	$hidden_service_followup_id=$_POST['hidden_service_followup_id'];
	$hidden_user_id=$_POST['hidden_user_id'];
	$change_mobile_no=$_POST['change_mobile_no'];
	$primary_ph_no = $_POST['primary_ph_no'];
	$secondary_ph_no = $_POST['secondary_ph_no'];
	$reason = $_POST['lost_reason_master'];
	
	$td=date('Y-m-d');
	
	//$next_feedback_date=date('Y-m-d', strtotime($td. " + 3 days"));
	
	if($nfd!=-1){
		$nfd=date('Y-m-d', strtotime($td. " + $nfd days"));
	}else{
		$nfd=date('Y-m-d', strtotime($user_date));
	}
	
	$service_date=date('Y-m-d', strtotime($service_status_date. " + 30 days"));
	
	if($change_mobile_no==1){
		$values=array();
		$bdd->execute("UPDATE service SET secondary_ph_no = '$secondary_ph_no' WHERE id = $hidden_service_id",$values);
	}
	
	if($status=="pending"){
		
		$values=array();
		$bdd->execute("UPDATE `service_followup` SET `next_followup_date`='$td', `followup_status` = 'taken',`user_id`=$hidden_user_id WHERE `service_followup`.`id` =$hidden_service_followup_id",$values);

		$values=array();
		$bdd->lastId("INSERT INTO `service_followup` (`service_id`,`last_followup_date`,`next_followup_date`,`service_date`,`service_type`,`service_no`,`remark`,`user_id`) VALUES ($hidden_service_id,'$service_status_date','$nfd','$service_date','$service_type',$service_no,'$remark',$hidden_user_id)",$values);
		
		$status = "success";
		$message = "New Followup Created";
		
	}else if($status=="service_done"){

		$values=array();
		$bdd->execute("UPDATE `service_followup` SET `next_followup_date`='$td',`service_done_date`='$service_status_date',`service_status`='service_done', `followup_status` = 'taken',`user_id`=$hidden_user_id WHERE `service_followup`.`id` =$hidden_service_followup_id",$values);
		
		
		$values=array($hidden_service_id);
		$pending_service_followup=$bdd->getone("SELECT id FROM `service_followup` WHERE service_id=? and service_status='pending' LIMIT 1",$values);
		
		if(empty($pending_service_followup)){
			$values=array($service_type_master_id);
			$service_type_row=$bdd->getone("SELECT * FROM `service_type` WHERE id=?",$values);
			
			$values=array();
			$bdd->lastId("INSERT INTO `service_followup` (`service_id`,`last_followup_date`,`next_followup_date`,`service_type`,`service_no`,`service_date`,`remark`,`user_id`) VALUES ($hidden_service_id,'$td','$nfd','{$service_type_row['name']}',{$service_type_row['service_no']},'$service_date','$remark',$hidden_user_id)",$values);
			
			$status = "success";
			$message = "New Followup Created";
		}else{
			$status = "success";
			$message = "Followup Updated";
		}
		
		
		
	}else if($status=="lost"){

		$values=array();
		$bdd->execute("UPDATE `service_followup` SET `next_followup_date`='$td',`service_done_date`='$service_status_date',`service_status`='lost',lost_date='$service_status_date',reason='$reason', `followup_status` = 'taken',`user_id`=$hidden_user_id WHERE `service_followup`.`id` =$hidden_service_followup_id",$values);
		
		$status = "success";
		$message = "Followup Updated";
		/*$values=array($hidden_service_id);
		$pending_service_followup=$bdd->getone("SELECT id FROM `service_followup` WHERE service_id=? and service_status='pending' LIMIT 1",$values);
		
		if(empty($pending_service_followup)){
			$values=array($service_type_master_id);
			$service_type_row=$bdd->getone("SELECT * FROM `service_type` WHERE id=?",$values);
			
			$values=array();
			$bdd->lastId("INSERT INTO `service_followup` (`service_id`,`last_followup_date`,`next_followup_date`,`service_type`,`service_no`,`service_date`,`remark`,`user_id`) VALUES ($hidden_service_id,'$td','$nfd','{$service_type_row['name']}',{$service_type_row['service_no']},'$service_date','$remark',$hidden_user_id)",$values);
			
			$status = "success";
			$message = "New Followup Created";
		}else{
			$status = "success";
			$message = "Followup Updated";
		}*/
		
		
		
	}else{
		$status = "error";
		$message = "This part is still in development mode. Only pending status is working";
	}
	
	$bdd->disconnect();
	
	
    echo json_encode([
        'status'  => $status,
        'message' => $message,
    ]);
	
    exit;
}