<?PHP
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/

// vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");


include_once("../inc/config.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_rdv.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
check_modules();

// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);
$td = $_GET['td'];
$codeint = $_GET['ci'];
$idrdv = 0;
if (isset($_GET['rdv']))
    $idrdv = intval(@$_GET['rdv']);

$pbm_int = FALSE;
$pbm_rdv = FALSE;
$cre_int = FALSE;
$sais_daterdv = TRUE;

// Récupération des paramètres de SMI
$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
$rech = "SELECT * FROM `" . TBPPRM . "`";
$row = $db->get_row($rech, ARRAY_A);
if ($row)
    extract($row);
$db->disconnect();

// On vérifie si le code intervention existe
if ($codeint <> "") {
    $row = lit_enr_int($codeint);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        if (datetolocal($int_datefin) <> "")
            $pbm_int = TRUE;
    } else {
        // Si non, on vérifie si on est en mode ajout
        // (Auquel cas, on stocke le code temporaire)
        // Sinon => erreur
        if ($td <> "add")
            $pbm_int = TRUE;
        else
            $cre_int = TRUE;
    }
}

if ($pbm_int == FALSE) {
    if ($idrdv <> 0) {
        // Récupération des données de la fiche RDV
        $row = lit_enr_rdv_id($idrdv);
        if (is_array($row)) {
            extract($row);
            $rdv_datef = datetolocal($rdv_date);
            if (($rdv_coderat <> $codeint) OR (intval($rdv_typrat) <> 1))
                $pbm_rdv = TRUE;
        } else
            $pbm_rdv = TRUE;

    } else {
        if ($td <> "add")
            $pbm_rdv = TRUE;
    }
}

$titre = '';
switch ($td) {
    case "add" :
        $titre = _("SAISIE RDV");
        $rdv_typrat = 1;
        $rdv_coderat = $codeint;
        break;
    case "edit" :
        $titre = _("EDITION RDV");
        break;
    case "del" :
        $sais_daterdv = FALSE;
        $titre = _("SUPPRESSION RDV");
        break;
    case "view" :
        $sais_daterdv = FALSE;
        $titre = _("VISUALISATION RDV");
        break;
    default :
        $pbm_rdv = TRUE;
        break;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("RDV Intervention"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:daterdvf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date"); ?> rendez-vous ; stok:frmok ; stbad:frmbad",
            "nom:hrdv; format:liste ; req:Y ; vmn:1 ; txt:<?PHP echo _("Heure rendez-vous"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:dureem; format:liste ; req:Y ; vmn:1 ; txt:<?PHP echo _("Durée"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:codelieu; format:liste ; req:Y ; txt:<?PHP echo _("Lieu"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:urgence; format:liste ; req:Y ; txt:<?PHP echo _("Niveau urgence"); ?> ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_datepicker() {
            $.datepicker.setDefaults({
                closeText: "<?PHP echo _("Fermer"); ?>",
                prevText: "<?PHP echo _("Précédent"); ?>",
                nextText: "<?PHP echo _("Suivant"); ?>",
                currentText: "<?PHP echo _("Aujourd'hui"); ?>",
                monthNames: ["<?PHP echo implode('","',$lng_tmoisf); ?>"],
                monthNamesShort: ["<?PHP echo implode('","',$lng_tmoism); ?>"],
                dayNames: ["<?PHP echo implode('","',$lng_tjourf); ?>"],
                dayNamesShort: ["<?PHP echo implode('","',$lng_tjourm); ?>"],
                dayNamesMin: ["<?PHP echo implode('","',$lng_tjours); ?>"],
                weekHeader: "<?PHP echo _("Sem."); ?>",
                dateFormat: "<?PHP echo $_SESSION['date_fmtshort']; ?>",
                firstDay: 1,
                isRTL: !1,
                changeMonth: true,
                changeYear: true,
                showMonthAfterYear: !1,
                yearSuffix: "",
                showButtonPanel: true,
                showOn: "both",
                buttonImageOnly: true,
                buttonImage: "../img/calendar.png",
                buttonText: "<?PHP echo _("Calendrier"); ?>"
            });
            <?PHP if ($sais_daterdv == TRUE) { ?>
            $("#daterdvf").datepicker({
                onSelect: function (datesel) {
                    $("#daterdvf").blur();
                    if (chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', false, 'dateddef,today', '', '', true) == false) {
                        var val_regul = parseInt(window.opener.$('#regul').val());
                        if (val_regul == 0) {
                            warn_txt('Erreur de saisie, contrôlez vos dates');
                            $('#' + this.id).val('');
                        }
                    }
                }
            });
            <?PHP } ?>
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?php
            switch ($td)
            {
                case "add" :
                    echo "init_valid_form('enr','rdvi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_autoraz('razdaterdvf','daterdvf','click');";
                    break;
                case "edit" :
                    echo "init_valid_form('enr','rdvi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_autoraz('razdaterdvf','daterdvf','click');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "init_valid_form('enr','rdvi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
            }
            ?>
            add_func_click('annul', 'sclose','');
            Focus_first();
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>
</head>
<body>

<?PHP
if ($pbm_rdv == FALSE) {
    ?>
    <p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
    <p align="center"><span class="titre_gras">
	<?PHP
    if ($cre_int == FALSE)
        echo _("Pour l'intervention") . " : " . $codeint;
    else
        echo _("Intervention en cours de création");
    ?>
	</span><br />
        <span class="annot_petit_it"><?PHP echo _("Tous les champs sont obligatoires"); ?></span></p>

    <form name="rdvi_maj" id="rdvi_maj" method="post" action="rdvi_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <input name="codeint" type="hidden" id="codeint" value="<?PHP echo $rdv_coderat; ?>">
        <input name="idrdv" type="hidden" id="idrdv" value="<?PHP echo $rdv_id; ?>">
        <input name="creint" type="hidden" id="creint" value="<?PHP echo $cre_int; ?>">
        <input name="today" type="hidden" id="today" value="<?PHP echo $datef; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_rdv" align="right"><?PHP echo _("Date"); ?></td>
                <td>
                    <?PHP
                    // Date RDV
                    echo '<input name="daterdvf" type="text" id="daterdvf" value="' . $rdv_datef . '" size="10" maxlength="10" readonly="1">';
                    if ($sais_daterdv == TRUE)
                        echo ' <img src="../img/raz.png" id ="razdaterdvf" border="0" align="absmiddle">';
                    echo '<div class="annot_petit_it">';
                    echo _("Au moins le") . ' ';
                    echo datetolocal($int_datedde) . ' (' . _("date de création") . ') - ' . _("sauf mode régularisation");
                    echo '</div>';
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_rdv" align="right"><?PHP echo _("Heure"); ?></td>
                <td>
                    <?PHP
                    // $tab_heures = give_tab_heures(0, 86400, ($pla_stepmin * 60));
                    $tab_heures = give_tab_heures(($pprm_firsthour * 3600), ($pprm_lasthour * 3600), ($pla_stepmin * 60));

                    cre_select('hrdv', 'hrdv', $tab_heures, substr($rdv_heure, 0, 5));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_rdv" align="right"><?PHP echo _("Durée"); ?></td>
                <td>
                    <?PHP
                    $tab_dureemin = give_tab_dureemin($pla_stepmin, 1440, $pla_stepmin);
                    cre_select('dureem', 'dureem', $tab_dureemin, intval($rdv_dureem));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_lieu" align="right"><?PHP echo _("Lieu"); ?></td>
                <td>
                    <?PHP
                    // création de la liste des Types Lieux
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBLIEU . "` ORDER BY lieu_desc";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('codelieu', 'codelieu', $rows, 'lieu_code', 'lieu_desc', '', $rdv_codelieu, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_urg" align="right"><?PHP echo _("Niveau d'urgence"); ?></td>
                <td>
                    <?PHP
                    // Création de la liste des Niveaux d'urgence
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBURG . "` ORDER BY urg_niveau";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('urgence', 'urgence', $rows, 'urg_niveau', 'urg_des', '', $rdv_urg);
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <?PHP
            switch ($td) {
                case "add" :
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _("Réinitialiser") . '">';
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _("Ajouter") . '">';
                    break;
                case "edit" :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _("Modifier") . '">';
                    break;
                case "del" :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _("Supprimer") . '">';
                    break;
            }
            echo '<input name="annul" type="button" class="bton_std" id="annul" value="' . _("Annuler") . '">';
            ?>
        </p>
    </form>
<?PHP
} else
    close_fen_auto('', '');
?>
</body>
</html>
