<?php
    include_once 'opendb.php';
     $response["success"] = -1;
     $response["message"] = "Error";
     $response["client_code"] ="";
     $response["insert_id"] = 0;
     
     $CompID=0;
     if (isset($_POST["Comp_Id"]))
    {
         $CompID=$_POST["Comp_Id"]; 
    }
    if($CompID==0) {
         $response["message"] = "Error: Web Co ID invalid";
            // echo no users JSON
            die( json_encode($response));
    }
    if (isset($_POST["CustCode"]) && isset($_POST["Latitude"]) && isset($_POST["Longitude"]))
    {
	
        $CustCode=$_POST["CustCode"] ;
        $Latitude=$_POST["Latitude"] ;
        $Longitude=$_POST["Longitude"] ;
        $client_Code=$_POST["Client_Code"];
        $Accuracy=$_POST["Accuracy"];
        $Distance=$_POST["Distance"];
        $Notes=$_POST["Notes"];
        
        $CustName=$_POST["CustName"] ;
        $Address=$_POST["Address"] ;
        $Phone= $_POST["Phone"] ;
        $Area=$_POST["Area"] ;
        $TaxRegNo= $_POST["TaxRegNo"] ;
        
        
        $edittime= date('ymdHi');
        $sql = "insert into customer_visits (`comp_id`, `cust_code`, `client_code`, `visit_date`, `latitude`, `longitude`, `distance`, `accuracy`,`notes`,`cust_name`, `address`, `area`, `phone`, `tax_reg_no`) ";
        $sql.=" values($CompID,'$CustCode','$client_Code','$edittime',$Latitude,$Longitude,$Distance,$Accuracy,'$Notes','$CustName','$Address','$Area','$Phone','$TaxRegNo')";
     
        if (!$conn -> query($sql)) {
            $response["insert_id"]=$conn->insert_id;
            $conn ->close();
            die("Error : " . $mysqli -> error.$sql);
        } else {
              $response["success"] = 1;
               $response["message"] = "Update Successful";
        }

    }
    else
    {
        $response["message"] = "Error: No Parameters";
    }
    
    $conn->close();
    echo json_encode($response);
?>