<?php

class CASS_View_Race_User_Edit
{

  public static function execute()
  {
    global $wpdb;

    $race_user = new Race_User;

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

    //Manage GET and POST constants parameters retreive of structure form
    $Action="";
    $IDRace=0;
    $IDRace_user=0;
    if(isset($_GET["action"]) && $_GET["action"]!="") {
      $Action = $_GET["action"];
    } else if(isset($_POST["action"]) && $_POST["action"]!="") {
      $Action = $_POST["action"];
    }

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

    //Edit mode
    if ($Action == "edit" && $IDRace_user > 0) {
      $stateForm="edit";
      $race_user->loadFromId($IDRace_user);
    }

    //Validation form data
    $message = "";
    if(isset($_POST["IDWPUser"]) && isset($_POST["state"])) {
      $dataValidationSuccess = true;

      if (intval($_POST["IDWPUser"])==0) {
        $dataValidationSuccess = false;
      } else if (Race_User::participiantAlreadyInCourse($IDRace,intval($_POST["IDWPUser"])) > 0 && ($stateForm == "create")) {
        $message = "Le participant est déjà inscrit à cette course.";
        $dataValidationSuccess = false;
      }
       
      
      if (intval($_POST["state"])==0) {
        $dataValidationSuccess = false;
      }
      
    if($dataValidationSuccess) { 
      //Retreive value of post and validating data
      if(intval($_POST["IDWPUser"])!=0) {
        $race_user->setIDWPUser(intval($_POST["IDWPUser"]));
      }

      if(intval($_POST["state"])!=0) {
        $race_user->setState(intval($_POST["state"]));
      }

      if($_POST["commentInscription"]!="") {
        $race_user->setCommentInscription(SantizeGlobal($_POST["commentInscription"]));
      }

      if($_POST["commentUnscribe"]!="") {
        $race_user->setCommentUnscribe(SantizeGlobal($_POST["commentUnscribe"]));
      }

      switch ($stateForm) {
        case 'create':   
            $race_user = new Race_User;
            $race_user->setIDWPUser(intval($_POST["IDWPUser"]));
            $race_user->setState(intval($_POST["state"]));
            $race_user->setCommentInscription(SantizeGlobal($_POST["commentInscription"]));
            $race_user->setCommentUnscribe(SantizeGlobal($_POST["commentUnscribe"]));
            $race_user->setIDRace(intval($IDRace));
            $race_user->create();
              break;

            case 'edit':
              $race_user = new Race_User;
              $race_user->loadFromId($_POST["IDRace_user"]);
              $race_user->setIDWPUser(intval($_POST["IDWPUser"]));
              $race_user->setState(intval($_POST["state"]));
              $race_user->setCommentInscription(SantizeGlobal($_POST["commentInscription"]));
              $race_user->setCommentUnscribe(SantizeGlobal($_POST["commentUnscribe"]));
              $race_user->setIDRace(intval($IDRace));
              $race_user->update();
              break;
      }

      //Redirection
     redirectJS(admin_url()."admin.php?page=cass-admin-race-user-list&IDRace=".$IDRace);
    } else {
      echo "<h3>Veuillez vérifier les données.</h3>";
      echo "<p>".$message."</p>";
    }
  }

    ?>

    <div class="wrap">

      <?php
     /*
      $race = new Race;
      $race->loadFromId(intval($_POST["IDRace"]));
      echo "OK";
      if ($stateForm == "create") {
        echo "<h2>Ajouter un nouveau participant pour la course : " .$race->getName(). "</h2>";
      } else if ($stateForm == "edit") {
        echo "<h2>Modifier un participant pour la course : " .$race->getName(). "</h2>";
      }*/

      ?>
      <form action="" method="post">
        <input type="hidden" id="action" name="action" value=<?php echo $Action ?>>
        <input type="hidden" id="IDRace_user" name="IDRace_user" value=<?php echo $IDRace_user ?>>
        <input type="hidden" id="stateForm" name="stateForm" value=<?php echo $stateForm ?>>
        <input type="hidden" id="IDRace" name="IDRace" value=<?php echo $IDRace?>>
        <br />
        <div class="form-element">
          <label class="form-element">Participant*</label>
          <select id="IDWPUser" name="IDWPUser" value="<?php echo $race_user->getIDWPUser() ?>" required>
            <option value="">Sélectionner un participant (membre)</option>";
            <?php
            $args = array(
              'role' => 'CASS_MEMBER',
              'orderby' => 'user_nicename',
              'order' => 'ASC'
            );
            $users = get_users($args);
            foreach ($users as $user) {
              $selected = "";
              if ($user->id == $race_user->getIDWPUser()) {

                $selected = "selected";
              }
              echo "<option value=" . $user->id . " " . $selected . " >" . $user->user_lastname . " " . $user->user_firstname . "</option>";
            }
            ?>
          </select>


        </div>
        <br />
        <div class="form-element">
          <label class="form-element">Etat*</label>
          <select name="state" id="state" required>
            <option value="">Sélectionner un état</option>;
            <?php
            $selRegistered = "";
            $selValidated = "";
            $selCancelled = "";
            $selExcused = "";

            switch (intval($race_user->getState())) {
              case 1:
                $selRegistered = "selected";
                break;
              case 2:
                $selValidated = "selected";
                break;
              case 3:
                $selCancelled = "selected";
                break;
              case 4:
                $selExcused = "selected";
                break;
            }

            echo "<option value='1' " . $selRegistered . ">Inscrit</option>";
            echo "<option value='2' " . $selValidated . ">Validé</option>";
            echo "<option value='3' " . $selCancelled . ">Désinscrit</option>";
            echo "<option value='4' " . $selExcused . ">Excusé</option>";
            ?>
          </select>
        </div>
        <br />
        <div class="form-element">
          <label class="form-element">Com. inscription</label>
            <textarea id="commentInscription" name="commentInscription" rows="12" cols="50"><?php echo ReadGlobal($race_user->getCommentInscription()) ?></textarea>
       
        </div>
        <br />
        <div class="form-element">
          <label class="form-element">Com. desinscription</label>
          <textarea id="commentUnscribe" name="commentUnscribe" rows="12" cols="50"><?php echo ReadGlobal($race_user->getCommentUnscribe()) ?></textarea>
       
         </div>
        <br />
        <?php
        if ($stateForm == "create") {
          submit_button('Ajouter le participant', 'primary');
        } else if ($stateForm == "edit") {
          submit_button('Sauvegarder', 'primary');
        }

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