<?php

class CASS_View_Season_Edit
{

    public static function execute()
    {
        global $wpdb;

        $season = new Season();

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

        //Default values trasmitted
        $IDSeason = 0;

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

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

        //Edit mode
        if ($stateForm == "edit" && $IDSeason > 0) {
            $season->loadFromId($IDSeason);
        }

        //Validating data
        if (isset($_POST["name"]) && isset($_POST["dateSeasonStart"]) && isset($_POST["dateSeasonEnd"])) {
            $dataValidationSuccess = true;

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

            if ($_POST["dateSeasonStart"] == "") {
                //echo "<h3>La date de début de saison est obligatoire.</h3>";
                $dataValidationSuccess = false;
            } else {
                $season->setSeasonStart($_POST["dateSeasonStart"]);
            }

            if ($_POST["dateSeasonEnd"] == "") {
                //echo "<h3>La date de fin de saison est obligatoire.</h3>";
                $dataValidationSuccess = false;
            } else {
                $season->setSeasonEnd($_POST["dateSeasonEnd"]);
            }

            if(strtotime($_POST["dateSeasonStart"]) > strtotime($_POST["dateSeasonEnd"])) {
                echo "<h3>La date de début de saison doit être inférieure à la date de fin de saison.</h3>";
                $dataValidationSuccess = false;
            }

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

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

        ?>

        <div class="wrap">

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

            ?>
            <form action="" method="post">
                <input type="hidden" id="action" name="action" value=<?php echo $stateForm ?>>
                <input type="hidden" id="IDSeason" name="IDSeason" value=<?php echo $IDSeason ?>>
                <br />
                <div class="form-element">
                    <label class="form-element">Nom*</label>
                    <input class="cass" type="text" name="name" value="<?php echo ReadGlobal($season->getName()) ?>" required>
                </div>
                <br />
                <div class="form-element">
                    <label class="form-element">Début de saison*</label>
                    <input class="cass" type="date" name="dateSeasonStart" value="<?php echo $season->getSeasonStart() ?>" required>
                </div>
                <br />
                <div class="form-element">
                    <label class="form-element">Fin de saison*</label>
                    <input class="cass" type="date" name="dateSeasonEnd" value="<?php echo $season->getSeasonStart() ?>" required>
                </div>
                <br />
                <?php
                if ($stateForm == "create") {
                    submit_button('Créer la saison', 'primary');
                } else if ($stateForm == "edit") {
                    submit_button('Sauvegarder', 'primary');
                }
                ?>
            </form>
        </div>
        <?php
    }
}