<?php
if (!$_POST){
  //If no POST, return an error
  return;
} elseif($_POST["contactId"]){
  require_once('library/src/isdk.php');

  $app = new iSDK;
  //if ($app->cfgCon("ss160")){
  if ($app->cfgCon("ifconnection")){

    $contactId = $_POST['contactId'];

    //Gets Total Show Cost
    $returnFields = array(
      "_TotalShowCost",
      "_ChurchName",
      "_ContactTownState",
      "_Numberofdaysofhotelneeded",
      "_Milagedistancebothways",
      "_ShowAmountindollars",
      "_TotalHotelCost0",
      "_TotalTravelCost0",
      "_ShowDiscount",
      "_Subtotal1",
      "_Subtotal2",
      "_Subtotal3",
      "_Subtotal4",
      "_Halfofshowcost",
      "_ShowNotes"
    );
    $contactResult = $app->loadCon($contactId, $returnFields);

    //Gets Contact ID and Id from Lead. Contact ID is the ID of the user in the Contacts table
    /*$returnFields = array("ContactId", "Id", "_TotalShowCost");
    $result = $app->dsLoad("Lead", $contactId, $returnFields);
    echo print_r($result);/**/


    if ($contactResult){
      //print_r($result);

      $totalShowCost = $contactResult["_TotalShowCost"];
      //echo $totalShowCost;

      //Gets Lead ID from Contact ID
      $returnFields = array("Id");
      $query = array('ContactId' => $contactId);
      //Returns an array of arrays
      $result = $app->dsQuery("Lead", 1, 0, $query,
      $returnFields);

      //Alternate method
      //$result = $app->dsFind("Lead", 1, 0, "ContactId", $contactId, $returnFields);

      print_r($contactResult);
      echo $contactResult["_TotalTravelCost0"];

      if ($result){
        $leadId = $result[0][Id];

        $fields = array(
          '_TotalShowCost' => $totalShowCost,
          "_ChurchName" => $contactResult["_ChurchName"],
          "_ContactTownState" => $contactResult["_ContactTownState"],
          "_Numberofdaysofhotelneeded" => $contactResult["_Numberofdaysofhotelneeded"],
          "_Milagedistancebothways" => $contactResult["_Milagedistancebothways"],
          "_ShowAmountindollars" => $contactResult["_ShowAmountindollars"],
          "_TotalHotelCost" => $contactResult["_TotalHotelCost0"],
          "_TotalTravelCost" => $contactResult["_TotalTravelCost0"],
          "_ShowDiscount" => $contactResult["_ShowDiscount"],
          "_Subtotal1" => $contactResult["_Subtotal1"],
          "_Subtotal2" => $contactResult["_Subtotal2"],
          "_Subtotal3" => $contactResult["_Subtotal3"],
          "_Subtotal4" => $contactResult["_Subtotal4"],
          "_Halfofshowcost" => $contactResult["_Halfofshowcost"],
          "_ShowNotes" => $contactResult["_ShowNotes"]
        );
        $id = $app->dsUpdate("Lead", $leadId, $fields);

        echo $id;

      } else {
        //Opportunity Doesn't exist
        return;
      }
    } else {
      //User doesn't exist
      return;
    }

  } else {
    //echo "no Connection";
    return;
  }

}
?>
