<?php


    
//get the values from the previous Session created
    
$pending $_SESSION['pending'];

    
$nbrOfStates $pending['nbrOfStates'];
    
$titles $pending['stateTitles'];
    
$respons $pending['stateRespons'];
    
$fields $pending['stateFields'];
    
$workflowId $pending['workflowId'];
    
$stateIds $pending['stateIds'];


    if(isset(
$_POST['uploadFields']))
    {
        
$workflowId $_POST['workflowId'];
        
$names $_POST['fieldName'];
        
$count $nbrOfStates;
        
$types $_POST['fieldType'];
        
$requireds $_POST['fieldRequired'];

        for(
$i=1;$i<=$count;$i++)
        {
            for(
$j=1;$j<=count($names[$i]);$j++)
            {
                
$fieldName=$names[$i][$j];
                
$fieldType=$types[$i][$j];
                
$stateId=$stateIds[$i];
                
$required=$requireds[$i][$j];

                
$statement $LINK->prepare("INSERT INTO TicketFields (fi_steps, label, type, is_optional) VALUES (?, ?, ?, ?)");
                
$statement->bind_param('issi'$stateId$fieldName$fieldType$required);
                
$statement->execute();
                
$statement->close();
                echo 
"New Extra created successfully";
            }
            
header("Location: index.php");
        }
    }

?>
<form method="POST">
  <input type="hidden" name="workflowId"  value="<?= $workflowId ?>">
  <input type="hidden" name="nbrOfStates" value="<?= $nbrOfStates ?>">

  <?php for ($i 1$i <= $nbrOfStates$i++): ?>
    <fieldset style="margin-bottom:1em;">
      <legend>
        State #<?= $i ?>:
        “<?= htmlspecialchars($titles[$i]) ?>
        (responsible: <?= htmlspecialchars($respons[$i]) ?>)
      </legend>

      <?php
      
// for each “field” this state needs, render a pair of inputs
      
for ($j 1$j <= (int)$fields[$i]; $j++): 
      
?>
        <p>
          <label>
            Field <?= $j ?> Name:
            <input
              type="text"
              name="fieldName[<?= $i ?>][<?= $j ?>]"
              required
            >
          </label>
          &nbsp;&nbsp;
          <label>
            Type:
            <select name="fieldType[<?= $i ?>][<?= $j ?>]" required>
              <option value="">– select –</option>
              <option value="text">Text</option>
              <option value="number">Number</option>
              <option value="date">Date</option>
              <!-- etc -->
            </select>
          </label>
          <label>
            Required:
            <input type="hidden" name="fieldRequired[<?= $i ?>][<?= $j ?>]" value="0">
            <input type="checkbox" name="fieldRequired[<?= $i ?>][<?= $j ?>]" value="1">
          </label>
        </p>
      <?php endfor; ?>
    </fieldset>
  <?php endfor; ?>

  <button type="submit" name="uploadFields">Save All Fields</button>
</form>