<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $su_id = $paramValue;  
  else if ($paramName == "rowids") 
    $rowIds = explode (",", $paramValue);  
  else if ($paramName == "elerows") 
    $eleRows = explode (",", $paramValue);  
  else if ($paramName == "valrows") 
    $valRows = explode (",", $paramValue);  
}

$dal = new DAL();

for ($i=0; $i<count($rowIds); $i++) {
    
  $te_id = $rowIds[$i];
  $element = explode (":", $eleRows[$i]);
  $value   = explode (":", $valRows[$i]);
//print_r ($element[0]);
  if ($element[0] == "function" && $value[0] == -1) {
      $sql = "DELETE FROM telno WHERE id = $te_id ";
      $result = $dal->query($sql)->id;
    }

  else if ($element[0] == "function" && $value[0] == 1) {
    $colList = "supplier_id";
    $valList = "$su_id";

    for ($j=1; $j<count($element); $j++) {
      if ($element[$j] == "select") {    
        $colList .= ", telnotype";
        $valList .= ", '$value[$j]'";
      }    
      else if ($element[$j] == "input1") {
        $colList .= ", number";
        $valList .= ", '$value[$j]'";
      }    
      else if ($element[$j] == "input2") {
        $colList .= ", exten";
        $valList .= ", '$value[$j]'";
      }    
    }
    $sql = "INSERT INTO telno (" . $colList . ") " .
           "VALUES (" . $valList . ") " ;
    $result = $dal->query($sql);
  }    

  else if ($element[0] == "function" && $value[0] == 2) {
    $setList = "supplier_id=$su_id";

    for ($j=1; $j<count($element); $j++) {
      if ($element[$j] == "select") {    
        $setList .= ", telnotype='$value[$j]'";
      }    
      else if ($element[$j] == "input1") {
        $setList .= ", number='$value[$j]'";
      }    
      else if ($element[$j] == "input2") {
        $setList .= ", exten='$value[$j]'";
      }    
    }
    $sql = "UPDATE telno SET " . $setList . " " .
           "WHERE id = $te_id " ;
    $result = $dal->query($sql);
  }    

      
}  
    
echo json_encode($su_id);

?>

