<?php

class CASS_View_Level_Edit
{

  public static function execute()
  {
    global $wpdb;

    $level = new Level;

    //Default state of form
    $stateForm = "create";

    //Default values tramsitted
    $IDLevel = 0;

    if (isset($_GET["action"]) && $_GET["action"] != "") {
      $stateForm = $_GET["action"];
    } else if (isset($_POST["action"]) && $_POST["action"] != "") {
      $stateForm = $_POST["action"];
    }

    if (isset($_GET["IDLevel"]) && intval($_GET["IDLevel"])!= 0) {
      $IDLevel = intval($_GET["IDLevel"]);
    } else if (isset($_POST["IDLevel"]) && intval($_POST["IDLevel"]) != 0) {
      $IDLevel = intval($_POST["IDLevel"]);
    }

    //Edit mode
    if ($stateForm == "edit" && $IDLevel > 0) {
      $level->loadFromId(intval($IDLevel));
    }

    //Validation form data
    if (isset($_POST["name"]) && isset($_POST["description"]) && isset($_POST["type"])) {
      $dataValidationSuccess = true;

      //Obligatory fields
      if ($_POST["name"] == "") {
        //echo "<h3>Le nom est obligatoire.</h3>";
        $dataValidationSuccess = false;
      } else {
        $level->setName(SantizeGlobal($_POST["name"]));
      }

      if ($_POST["type"] == "") {
        //echo "<h3>Le type est obligatoire.</h3>";
        $dataValidationSuccess = false;
      } else {
        $level->setType(SantizeGlobal($_POST["type"]));
      }

      if ($_POST["level"] == "") {
        //echo "<h3>Le niveau est obligatoire.</h3>";
        $dataValidationSuccess = false;
      } else {
        $level->setLevel(SantizeGlobal($_POST["level"]));
      }

      //Non obligatory fields
      $level->setDescription(SantizeGlobal($_POST["description"]));

      if ($dataValidationSuccess) {
        switch ($stateForm) {
          case 'create':
            $level->create();
            break;

          case 'edit':
            $level->setId(intval($_GET["IDLevel"]));
            $level->update();
            break;
        }
        //Redirection
        redirectJS(admin_url() . "admin.php?page=cass-admin-level-list");
      } else {
        echo "<h3>Veuillez vérifier les données.</h3>";
      }
    }
    ?>


    <div class="wrap">

      <?php
      if ($stateForm == "create") {
        echo "<h2>Créer un nouveau niveau</h2>";
      } else if ($stateForm == "edit") {
        echo "<h2>Modifier un niveau</h2>";
      }

      ?>
      <form action="" method="post">
        <input type="hidden" id="action" name="action" value=<?php echo $stateForm ?>>
        <input type="hidden" id="IDLevel" name="IDLevel" value=<?php echo $IDLevel ?>>
        <br />
        <div class='form-element'>
          <label for="name" class='form-element'>Niveau*</label>
          <input class="cass" type="text" name="level" value="<?php echo $level->getLevel() ?>" required>
        </div>
        <br />
        <div class='form-element'>
          <label for="name" class='form-element'>Nom*</label>
          <input class="cass" type="text" name="name" value="<?php echo ReadGlobal($level->getName()) ?>" required>
        </div>
        <br />
        <div class='form-element'>
          <label for="type" class='form-element'>Type*</label>
          <input class="cass" type="text" name="type" value="<?php echo ReadGlobal($level->getType()) ?>" required>
        </div>
        <br />
        <div class='form-element'>
          <label for="description" class='form-element'>Description</label>
          <input class="cass" type="text" name="description" value="<?php echo ReadGlobal($level->getDescription()) ?>">
        </div>

        <br />
        <?php
        if ($stateForm == "create") {
          submit_button('Créer un niveau', 'primary');
        } else if ($stateForm == "edit") {
          submit_button('Sauvegarder', 'primary');
        }

        ?>
      </form>
    </div>
    <?php
  }
}