<?php
/*=======================================================================
 *
 * customer_service_delivery_reasons.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../include/html.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_service", "objId", "objType", "f_code_new", "f_reason_new", "f_mail_new", "f_text", "deactivateMenu"));


// Set number of delivery reasons lists
$numOfLists = 4;  // Insert parameter to expand....
$numOfReasonsPerList = 20;  // Insert parameter to expand....

if ($f_service == "") : $f_service = "0"; endif;

// Check HTTP-Parameters for the reasons
$f_reason_array = array();
$f_mail_array = array();
for ($i = 0; $i <= $numOfReasonsPerList; $i++) :
    // Reason code and text
    list($tmpCode) = getSecHttpVars("1",array("f_code_" . pad($i,2)));
    list($tmpReason) = getSecHttpVars("1",array("f_reason_" . pad($i,2)));
    if ($tmpReason != "") :
       $f_reason_array[$i] = $tmpCode . "$" . $tmpReason;
    endif;
    /*
    if ($tmpCode != "" && $tmpReason != "") :
       list($tmpMail) = getSecHttpVars("1",array("f_mail_" . pad($i,2)));
       $f_mail_array[$i] = $tmpMail;
    endif;
    */
endfor;
if ($f_reason_new != "") :
   array_push($f_reason_array, $f_code_new . "$" . $f_reason_new);
endif;
/*
if ($f_mail_new != "") :
   array_push($f_mail_array, $f_mail_new);
endif;
*/


getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("ABLIEFERGRÜNDE AUF DEM ENDGERÄT");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1"; $usrAccessArray["cs"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);


// Get global definitions of the services
$output = getLngt("Listen") . ":&nbsp;";
$output .= "<select class=\"f8np1\" name=\"f_service\" id=\"f_service\" onChange=\"serviceFinishPage('');\">";
// $output .= "  <option value=\"\" " . ($f_service == "" ? "selected" : "") . ">" . getLngt("Keine Auswahl") . "</option>";
for ($i = 0; $i < $numOfLists; $i++) :
    $output .= "  <option value=\"" . $i . "\" " . ($f_service == $i ? "selected" : "") . ">" . getLngt("Liste") . ": " . ($i + 1) . "</option>";
endfor;
$output .= "</select>";

// Display delivery reasons
$outputReasons = "";
$outputText = "";
if ($f_service != "" && $objType != "" && $objId != "") :

   // Set special database fieldname regarding the generic data container structure
   $specialDbFieldNameReasons = "service_delivery_reasons";
   $specialDbFieldNameMailState = "service_delivery_reasons_mail_state";
   $specialDbFieldNameText = "service_delivery_reasons_text";

   // Save reasons
   if ($f_act == "saveCustomerServicesDeliveryReasons") :

      // **** Reasons ****
      // Load current reasons of the customer
      $gdcContentReasons = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameReasons . "'");
      $currArray = getKeyValueArrayFromString($gdcContentReasons);    // Convert key-value-string to array
      $currArray[$f_service] = implode("§", $f_reason_array);         // Implode new reasons of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfLists; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentReasons = implode("|", $newContentArray);
      // Update or insert reasons
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameReasons))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentReasons),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameReasons . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameReasons, "gdc_content", $gdcContentReasons));
      endif;

      // **** Mail state for reasons ****
      /*
      // Load current mail states of the customer
      $gdcContentMails = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
      $currArray = getKeyValueArrayFromString($gdcContentMails);    // Convert key-value-string to array
      $currArray[$f_service] = implode("§", $f_mail_array);             // Implode new reasons of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfLists; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentReasons = implode("|", $newContentArray);
      // Update or insert reasons
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameMailState))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentReasons),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameMailState, "gdc_content", $gdcContentReasons));
      endif;
      */

      // **** Text ****
      /*
      $gdcContentTexts = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
      $currArray = getKeyValueArrayFromString($gdcContentTexts);      // Convert key-value-string to array
      $currArray[$f_service] = $f_text;                               // Implode new text of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfLists; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentTexts = implode("|", $newContentArray);
      // Update or insert text
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameText))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentTexts),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameText, "gdc_content", $gdcContentTexts));
      endif;
      */
   endif;


   // **** Prepare output reasons ****
   // (Re-)Load current reasons and mail states of the customer
   $gdcContentReasons = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameReasons . "'");
   $gdcContentMails = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
   $i = "";  // Init
   $outputReasons .= "<table>\n";
   $outputReasons .= "<tr>\n";
   $outputReasons .= "<td>" . getLngt("Lfd.") . "&nbsp;</td>";
   $outputReasons .= "<td>" . getLngt("Code") . "&nbsp;</td>";
   $outputReasons .= "<td>" . getLngt("Text") . "&nbsp;</td>";
   // $outputReasons .= "<td>" . getLngt("Mail-Status") . "</td>";
   $outputReasons .= "</tr>\n";
   if ($gdcContentReasons != "") :
      // Split content to get the reasons of the current selected service
      // E.g.: "1=R1§R2§R3|2=R1§R2§R3§R4§R5|3=R1..."  => x(0) = R1§R2§R3, x(1) = R1,R2,R3,R4,R5, ...
      // [=> 1=R1,R2,R3 => List "1" is mapped to the reasons]
      $f_reason_array = getKeyValueArrayFromString($gdcContentReasons);
      $f_reason_string = $f_reason_array[$f_service];  // Take the reasons (array) of the current service
      // $f_mail_array = getKeyValueArrayFromString($gdcContentMails);
      // $f_mail_string = $f_mail_array[$f_service];  // Take the mail states (array) of the reasons of the current service
      if ($f_reason_string != "") :
         $f_reason_array = explode("§", $f_reason_string);
         $f_reason_array_len = count($f_reason_array);
         // $f_mail_array = explode("§", $f_mail_string);
         // $f_f_mail_array_len = count($f_mail_array);
         if ($f_reason_array_len > 0) :
            for ($i = 0; $i < $f_reason_array_len; $i++) :
                $tmpVal = explode("$", $f_reason_array[$i]);
                $reasonCode = $tmpVal[0];
                $reasonText = $tmpVal[1];
                $outputReasons .= "<tr>\n";
                $outputReasons .= "<td>" . ($i + 1) . ".&nbsp;</td>";
                $outputReasons .= "<td>";
                $outputReasons .= "<input class=\"f10np1\" type=\"text\" name=\"f_code_" . pad($i,2) . "\" value=\"" . $reasonCode . "\" size=\"5\">";
                $outputReasons .= "</td>";
                $outputReasons .= "<td>";
                $outputReasons .= "<input class=\"f10np1\" type=\"text\" name=\"f_reason_" . pad($i,2) . "\" value=\"" . $reasonText . "\" size=\"100\">";
                $outputReasons .= "</td>";
                /*
                $outputReasons .= "<td>";
                $outputReasons .= "<select class=\"f8np1\" name=\"f_mail_" . pad($i,2) . "\">";
                $outputReasons .= "  <option value=\"\" " . ($f_mail_array[$i] == "" ? "selected" : "") . ">" . getLngt("Keine Mail") . "</option>";
                $outputReasons .= "  <option value=\"1\" " . ($f_mail_array[$i] == "1" ? "selected" : "") . ">" . getLngt("Mail bei JA") . "</option>";
                $outputReasons .= "  <option value=\"2\" " . ($f_mail_array[$i] == "2" ? "selected" : "") . ">" . getLngt("Mail bei NEIN") . "</option>";
                $outputReasons .= "</select>";
                $outputReasons .= "</td>";
                */
                $outputReasons .= "</tr>\n";
            endfor;
         endif;
      endif;
   endif;
   if ($i == "") : $i = 1; else : $i++; endif;
   $outputReasons .= "<tr>\n";
   $outputReasons .= "<td>" . $i . ".&nbsp;</td>\n";
   $outputReasons .= "<td>\n";
   $outputReasons .= "<input class=\"f10np1\" type=\"text\" name=\"f_code_new\" value=\"\" size=\"5\">";
   $outputReasons .= "</td>\n";
   $outputReasons .= "<td>\n";
   $outputReasons .= "<input class=\"f10np1\" type=\"text\" name=\"f_reason_new\" value=\"\" size=\"100\">";
   $outputReasons .= "</td>\n";
   /*
   $outputReasons .= "<td>";
   $outputReasons .= "<select class=\"f8np1\" name=\"f_mail_new\">";
   $outputReasons .= "  <option value=\"\" selected>" . getLngt("Keine Mail") . "</option>";
   $outputReasons .= "  <option value=\"1\">" . getLngt("Mail bei JA") . "</option>";
   $outputReasons .= "  <option value=\"2\">" . getLngt("Mail bei NEIN") . "</option>";
   $outputReasons .= "</select>";
   */
   $outputReasons .= "</tr>\n";
   $outputReasons .= "</table>\n";
   
   
   // **** Prepare output text ****
   // (Re-)Load current text of the customer
   $gdcContentText = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
   $f_text_array = getKeyValueArrayFromString($gdcContentText);
   $f_text = $f_text_array[$f_service];  // Take the text of the current service
   $outputText .= "<table>";
   $outputText .= "  <tr>";
   $outputText .= "    <td>";
   $outputText .= "      <textarea name=\"f_text\" cols=\"100\" rows=\"10\">" . $f_text . "</textarea>";
   $outputText .= "    </td>";
   $outputText .= "  </tr>";
   $outputText .= "</table>";
endif;


// Only for output
if ($objId != "" && ($objType == "cs" || $objType == "cr")) :
   $cmpId = getFieldValueFromId("customer", "cs_id", $objId, "cmp_id");
   $tmpFields = getFieldsValueFromId("company","cmp_id",$cmpId,array("cmp_comp","cmp_comp2"));
   $title = $tmpFields[0] . " " . $tmpFields[1];
endif;
?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>


function serviceFinishPage(f_act) {
  document.forms[0].f_act.value = f_act;
  document.forms[0].submit();
};
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

<form action="customer_service_delivery_reasons.php" method="post">
  <input type="hidden" name="f_act" value="">  
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">  
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
  <input type="hidden" name="objType" value="<?php echo $objType ?>">  
  <input type="hidden" name="objId" value="<?php echo $objId ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <?php if ($title != "") : ?>
    <div class="f12bp1_blue">
      <?php echo $title ?>
    </div>
    <?php echo htmlDivLineSpacer("30px"); ?>
  <?php endif; ?>

  <div>
    <div class="f12bp1_blue"><?php echo $pageTitel ?></div>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <div class="f10np1"><?php echo $output ?></div>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <div class="f10np1"><?php echo $outputReasons ?></div>
  </div>
  <?php echo htmlDivLineSpacer("30px"); ?>
  
  <?php if (false && $f_service != "") : ?>
    <div>
      <div class="f10np1"><?php echo $outputText ?></div>
    </div>
    <?php echo htmlDivLineSpacer("30px"); ?>
  <?php endif; ?>

  <?php if ($f_service != "") : ?>
    <div>
      <?php echo defineButtonType10(getLngt("Speichern"), "action_save", "serviceFinishPage('saveCustomerServicesDeliveryReasons');", "150", "left", "3"); ?>
      <?php echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "150", "left", "3"); ?>
    </div>
    <?php echo htmlDivLineSpacer("5px", "", "left"); ?>
  <?php endif; ?>
</form>
</div>
</body>

</html>