<?php
//echo($_SERVER["REQUEST_METHOD"]);
$desserts = [
  ["name" => "ice cream", "id" => 1 ],
  ["name" => "apple pie", "id" => 2 ],
  ["name" => "cheese cake", "id" => 3 ]
];

//var_dump($desserts);
//initialize vars for post and set them to default values
$first_name = "";
$last_name = "";
$pizza_toppings = array();

$validation_errs = array();

if($_SERVER["REQUEST_METHOD"] == "POST"){

  //var_dump($_POST);
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];

/*
  if(isset($_POST['pizza_toppings'])){
    $pizza_toppings = $_POST['pizza_toppings'];
  }
*/
//Use the conditional operator to set $pizza_toppings
//$pizza_toppings =(isset($_POST['pizza_toppings'])? $_POST['pizza_toppings'] : $pizza_toppings);
  $pizza_toppings = $_POST['pizza_toppings'] ?? $pizza_toppings;
//use the null coalescing operator to set $pizza_toppings
  $dessert_choice = $_POST['dessert_choice'];
//VALIDATE DATA
  if(empty($first_name)){
    $validation_errs["first_name"] = wrap_err_msg("First name is empty");
  }
  if(empty($last_name)){
    $validation_errs["last_name"] = wrap_err_msg("Last name is empty");
  }
  if(empty($pizza_toppings)){
    $validation_errs["pizza_toppings"] = wrap_err_msg("You must choose a  topping.");
  }
  if(!$dessert_choice > 0){
    $validation_errs['dessert_choice'] = wrap_err_msg("Please choose a dessert.");
  }

  if(empty($validation_errs)){
    die("Thank you for valid data!");
  }
}

function wrap_err_msg($msg){
return "<span class ='validation'>$msg</span>";
}

function createSelectOptions($selected_id = 0){
  global $desserts;
  $html = "";
  foreach ($desserts as $d) {
    if($d['id'] == $selected_id){
      $html .= "<option selected value=\"{$d['id']}\">{$d['name']}</option>";
    }else{
      $html .= "<option value=\"{$d['id']}\">{$d['name']}</option>";
    }
    

  }
  return $html;
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <title>Posting Forms</title>
    <style>
    .validation{
      color:red;
    }
    </style>
  </head>
  <body>
  <h1>Posting Forms</h1>
  <br>

  <form method="POST" action="simple-form.php" >
      FIRST NAME:
      <br> 
      <input type="text" name="first_name" value="<?php echo($first_name); ?>" />
      <?php
      echo($validation_errs['first_name'] ?? "");
      ?>
      <br>
      LAST NAME:
      <br>
      <input type="text" name="last_name" value="<?php echo($last_name); ?>"/>
      <?php echo($validation_errs['last_name'] ?? ""); ?>
      <br>
      <br>
       WHAT DO YOU LIKE ON YOUR PIZZA?
       <?php echo($validation_errs['pizza_toppings'] ?? ""); ?>
      <br>
      <input type="checkbox" name="pizza_toppings[]" value="sausage" <?php echo(in_array("sausage", $pizza_toppings) ? "checked" : ""); ?> /> Sausage
      <br>
      <input type="checkbox" name="pizza_toppings[]" value="pepperoni" <?php echo(in_array("pepperoni", $pizza_toppings) ? "checked" : ""); ?> /> Pepperoni
      <br>
      <input type="checkbox" name="pizza_toppings[]" value="mushrooms" <?php echo(in_array("mushrooms", $pizza_toppings) ? "checked" : ""); ?>/> Mushrooms
      <br>
      Choose your dessert:
      <select name="dessert_choice">
      <option value="0">Choose One...</option>;

      <?php
        echo(createSelectOptions($dessert_choice));
      ?>
      </select>
      <?php echo($validation_errs['dessert_choice'] ?? ""); ?>
      <br>
      <input type="submit" name="btn_submit" value="Submit" />
  </form>

  </body>
</html>


