<?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
 ****************************************************************************************/

header("content-type: text/javascript");

session_start();

require_once("../lib/gettext/gettext.inc");
include_once("../inc/config.inc.php");

echo "tab_comp = new Array();\n";
$i = 0;
foreach ($tab_comp_symbole as $key => $value) {
    echo "tab_comp[" . $i . "]= new Array('" . $key . "','" . $value . "','" . $tab_comp_libelle[$key] . "');\n";
    $i++;
}
?>

/**
* param (int) timeWait : Temps en ms d'attente avant de relancer une requête AJAX
* param (int) timeFactor : Facteur pour incrémenter le temps avant le lancement récursif
* param (int) timeWaitMax : Temps en ms d'attente maximum avant de relancer une requête AJAX
* param (int) timeOut : Temps en ms d'attente global maximum
* param (int) timeExec : Temps d'execution
* param (int) nbAppel : Nombre d'appel récursif effectué
* param (Bool) debug : True affiche le debug
*/
function getGenerationStatus(timeWait, timeFactor, timeWaitMax, timeOut, timeExec, nbAppel, debug) {
    nbAppel = nbAppel+1;
    timeExec = timeExec+timeWait;
    var timeWaitCurrent = timeWait*timeFactor;
    if(timeWaitCurrent>timeWaitMax) {
        timeWaitCurrent = timeWaitMax;
    }
    if(timeExec<timeOut) {
    $.ajax({
    url: "<?PHP echo $rep_appli . '/ajax/'; ?>phpExcel_generationStatus.php",
    type: "POST",
    success: function(data) {
        if (data!="Fini")
        {
            if(debug==true) {
            var stringTempDebug =
            "<p class='monospace'>"+
                "--> Numéro d'appel = "+nbAppel+
                "<br/> --> Temps d'attente avant le prochain appel = "+timeWaitCurrent/1000+" sec"+
                "<br/> --> Temps d'execution total = "+timeExec/1000+" sec"+
                "</p><p class='monospace'>Return statut : "+data+"</p>";
            $("#loading-debug").append(stringTempDebug);
            $("#loading-debug").css("display","block");
        }
    setTimeout(
        function() {
        getGenerationStatus(timeWaitCurrent, timeFactor, timeWaitMax, timeOut, timeExec, nbAppel, true);
            }
            , timeWaitCurrent
    );
    }
    else
    {
    if(debug==true) {
    var stringTempDebug =
    "<p class='monospace'>"+
        "--> Numéro d'appel = "+nbAppel+
        "<br/> --> Temps d'execution total = "+timeExec/1000+" sec"+
        "</p><p class='monospace'>Return statut : fini</p>";
    $("#loading-debug").append(stringTempDebug);
    $("#loading-debug").css("display","block");
    }
    $("#waitajax").html("");
    pop_txt('Exportation Terminée');
    //$("#loading").delay(3000).fadeOut("slow");
    }
    }
    });
    } else {
    if(debug==true) {
    var stringTempDebug =
    "<p class='monospace'>"+
        "--> Numéro d'appel = "+nbAppel+
        "<br/> --> Temps d'execution total = "+timeExec/1000+" sec"+
        $("#loading-debug").append(stringTempDebug);
        $("#loading-debug").css("display","block");
        }
        $("#waitajax").html("");
        warn_txt('Timeout ('+timeOut+' sec) atteint');
        }
        }

        function init_ajax_exp() {
        $("#fic_exp").click(function() {
        // Informe l'utilisateur que la génération du fichier est en cours
        var stringTemp = "<p>Génération du fichier en cours...<p/>";
        $("#waitajax").center();
        $("#waitajax").html('<img src="../img/wait_1.gif" alt="Patience">');
        $("#loading-debug").center();
        $("#loading-debug").html("");
        $("#loading-debug").css("display","none");

    // Chargement de l'iframe associé à l'idGenerer clické
    var oIFrm = document.getElementById('iframePhpExcel');
    var url_file = $.url().attr('file');
    var new_url_file = url_file.replace(".php","exp.php");
    oIFrm.contentWindow.document.location = new_url_file+"?<?php echo $_SERVER['QUERY_STRING']; ?>&mode=Excel5";

    // Appel de la fonction récursive pour tester le statut après une attente d'une seconde.
    var timeWait = 1000;
    var timeFactor = 2;
    var timeWaitMax = 8000;
    var timeOut = 36000000;
    var timeExec = 1000;
    var nbAppel = 0;
    var debug = false;
    setTimeout(
    function() {
    getGenerationStatus(timeWait, timeFactor, timeWaitMax, timeOut, timeExec, nbAppel, debug);
    }
    , timeWait
    );
    });
}

function init_export() {
$("#fic_exp").click(function() {
var url_courante = $(location).attr('href');
var tab_url = url_courante.split("?");
var url_file = tab_url[0];
var url_query = tab_url[1];
new_url = url_file.replace(".php","exp.php")+'?'+url_query;
if (url_query != '')
{
new_url = new_url + '&';
}
new_url += 'mode=Excel5';
init_new_window(new_url,'expint',1,1,'yes');
});
}
<?php
$tab_titre_col = [];
$tab_titre_col_rech = [];
switch ($_SESSION["typ_filtres"]) {
    case "int" :
        $tab_titre_col = $int_titre_col;
        $tab_titre_col_rech = $int_titre_col_rech;
        break;
    case "cli" :
        $tab_titre_col = $cli_titre_col;
        $tab_titre_col_rech = $cli_titre_col_rech;
        break;
    default :
        break;
}

foreach ($tab_titre_col as $key => $value) {
    if ($tab_titre_col_rech[$key] == 1) {
        ?>
        function init_next_comp_<?php echo $value; ?>()
        {
        $("#prm_rech_<?php echo $value; ?>").click(function() {
        var val_bton = $("#prm_rech_<?php echo $value; ?>").val();
        var val_pop = $("#lib_rech_<?php echo $value; ?>").html();
        var idx = -1;
        for (var i=0; i<tab_comp.length; i++)
		{
			if (tab_comp[i][1] == val_bton)
			{
				idx = i;
			}
		}
		idx++;
		if (idx >= tab_comp.length)
		{
			idx=0;
		}
		$("#prm_rech_<?php echo $value; ?>").val(tab_comp[idx][1]);
		$("#lib_rech_<?php echo $value; ?>").html(tab_comp[idx][2]+'<div class="annot_petit_it">(cliquez pour changer)</div>');
	});
}

function valid_ajax_rech_<?php echo $value; ?>() {
	$("#valid_rech_<?php echo $value; ?>").click(function() {
				var val_comp = $("#prm_rech_<?php echo $value; ?>").val();
				var val_input = $("#<?php echo $value; ?>").val();
				var prm_rech = '';
				for (var i=0; i<tab_comp.length; i++)
				{
					if (tab_comp[i][1] == val_comp)
					{
						prm_rech = tab_comp[i][0];
					}
				}
				if (prm_rech != 'null')
				{
					$("#waitajax").center();
					$("#waitajax").html('<img src="../img/wait_1.gif" alt="Patience">');
					$.ajax({
						url: "<?PHP echo $rep_appli . '/ajax/'; ?>update_session_tri_col.php",
						data: "cmd=valid&chp=<?php echo $value; ?>&comp="+prm_rech+"&val="+val_input,
						type: "GET",
						success: function(data) {
								if (data == "ok")
								{
									$("#waitajax").html("");
									$("#rech_col_<?php echo $value; ?>").css("visibility","hidden");
									$("#ico_rech_<?php echo $value; ?>").removeClass("ico_rech_col_std").addClass("ico_rech_col_sel"); 
                                    var val_bton = $("#lst_valid").val();
                                    var val_tl = $("#tl").val();
                                    var val_ta = '';
                                    if ($("#lst_ta").length > 0)
                                    {
                                        val_ta = $("#lst_ta").val();
                                    }
                                    $("#lst_valid").val(val_bton);
                                    $("#tl").val(val_tl);
                                    if (val_ta != '')
                                    {
                                        $("#ta").val(val_ta);
                                    }
                                    $('#page').val('1');
                                    $('#formr').submit();
								}
								else
								{
									alert(data);
								}
						}
					});
				}
				else
				{
					warn_txt('Il manque le critère de comparaison');
				}
	});
}

function cancel_ajax_rech_<?php echo $value; ?>() {
	$("#cancel_rech_<?php echo $value; ?>").click(function() {
				$("#waitajax").center();
				$("#waitajax").html('<img src="../img/wait_1.gif" alt="Patience">');
		$.ajax({
			url: "<?PHP echo $rep_appli . '/ajax/'; ?>update_session_tri_col.php",
			data: "cmd=close&chp=<?php echo $value; ?>",
			type: "GET",
			success: function(data) {
					if (data.substr(0,2) == "ok")
					{
						var tab_result = data.split("_");
						var tab_tmp = new Array();;
						for (var i=0; i<tab_comp.length; i++)
						{
							if (tab_comp[i][0] == tab_result[1])
							{
								tab_tmp = tab_comp[i];
							}
						}
						$("#prm_rech_<?php echo $value; ?>").val(tab_tmp[1]);
						$("#<?php echo $value; ?>").val(tab_result[2]);
						$("#waitajax").html("");
						$("#rech_col_<?php echo $value; ?>").css("visibility","hidden");
					}
					else
					{
						alert(data);
					}
			}
		});
	});
}

function reset_ajax_rech_<?php echo $value; ?>() {
	$("#reset_rech_<?php echo $value; ?>").click(function() {
				$("#waitajax").center();
				$("#waitajax").html('<img src="../img/wait_1.gif" alt="Patience">');
		$.ajax({
			url: "<?PHP echo $rep_appli . '/ajax/'; ?>update_session_tri_col.php",
			data: "cmd=reset&chp=<?php echo $value; ?>",
			type: "GET",
			success: function(data) {
					if (data == "ok")
					{
						$("#prm_rech_<?php echo $value; ?>").val(' ');
						$("#lib_rech_<?php echo $value; ?>").html(tab_comp[0][2]+'<div class="annot_petit_it">(cliquez pour changer)</div>');
						$("#<?php echo $value; ?>").val('');
						$("#waitajax").html("");
                        var val_bton = $("#lst_valid").val();
                        var val_tl = $("#tl").val();
                        var val_ta = '';
                        if ($("#lst_ta").length > 0)
                        {
                            val_ta = $("#lst_ta").val();
                        }
                        $("#lst_valid").val(val_bton);
                        $("#tl").val(val_tl);
                        if (val_ta != '')
                        {
                            $("#ta").val(val_ta);
                        }
                        $('#page').val('1');
                        $('#formr').submit();
					}
					else
					{
						alert(data);
					}
			}
		});
	});
}

function open_form_rech_col_<?php echo $value; ?>() {
	$("#ico_rech_<?php echo $value; ?>").click(function() {
			$("#rech_col_<?php echo $value; ?>").css("visibility","visible");
	});
}

<?php
	}
}
?>

function valid_rech_compl() {
$("#valid_compl").click(function() {
$("#rech_etendue").hide();
$("#valid").trigger('click');
});
}

function init_lst_reload()
{
$('#lst_reload').click(function(){
location.reload();
});
}

function init_lst_valid()
{
$('#lst_valid').click(function(){
var $search = $('#search').val();
$('#search').val($search.trim());
$('#page').val('1');
$('#lst_valid').attr("disabled", "disabled");
$('#formr').submit();
});

$("#formr").keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
var $search = $('#search').val();
$('#search').val($search.trim());
$('#page').val('1');
$('#lst_valid').attr("disabled", "disabled");
$('#formr').submit();
}
});
}

function init_lst_validcompl()
{
    $('#valid_compl').click(function(){
        $('#page').val('1');
        $('#formr').submit();
    });
}

function clear_simple_rech() {
    $("#cancel_rech").click(function() {
        var val_bton = $("#lst_valid").val();
        var val_tl = $("#tl").val();
        var val_ta = '';
        if ($("#lst_ta").length > 0)
        {
            val_ta = $("#lst_ta").val();
        }
        clear_form_elements('formr');
        $("#lst_valid").val(val_bton);
        $("#tl").val(val_tl);
        if (val_ta != '')
        {
            $("#ta").val(val_ta);
        }
        $('#page').val('1');
        $('#formr').submit();
    });
}

function clear_all_rech(url) {
$("#cancel_rech").click(function() {
clear_form_elements('formr');
<?php
foreach ($tab_titre_col as $key => $value) {
    if ($tab_titre_col_rech[$key] == 1) {
        ?>
        $("#prm_rech_<?php echo $value; ?>").val(' ');
        $("#lib_rech_<?php echo $value; ?>").html(tab_comp[0][2]+'<div class="annot_petit_it">(cliquez pour changer)</div>');
        $("#<?php echo $value; ?>").val('');
    <?php
    }
}
?>
$.ajax({
url: "<?PHP echo $rep_appli . '/ajax/'; ?>update_session_tri_col.php",
data: "cmd=reset&chp=all",
type: "GET",
success: function(data) {
if (data == "ok")
{
$(location).attr('href',url);
/* $("#valid").trigger('click'); */
}
else
{
alert(data);
}
}
});
});
}

function clear_all_rech_lst(url) {
    if ($("#reset_rech_lst").length >0)
    {
        $("#reset_rech_lst").click(function() {
            $.ajax({
                url: "<?PHP echo $rep_appli . '/ajax/'; ?>update_session_tri_col.php",
                data: "cmd=reset&chp=all",
                type: "GET",
                success: function(data) {
                        if (data == "ok")
                        {
                            if (url != '') {
                                $(location).attr('href',url);
                            }
                        }
                        else
                        {
                            alert(data);
                        }
                    }
            });
        });
    }
}

function init_all_rech_compl() {
    init_export();
    $('#rech_etendue').hide();
    $('#rech_more').click(function(){
        $('#rech_etendue').show();
    });
    $('#rech_less').click(function(){
        $('#rech_etendue').hide();
    });
    $('#close_rech_etendue').click(function(){
        $('#rech_etendue').hide();
    });
<?php
foreach ($tab_titre_col as $key => $value) {
    if ($tab_titre_col_rech[$key] == 1) {
        ?>
        init_next_comp_<?php echo $value; ?>();
        valid_ajax_rech_<?php echo $value; ?>();
        cancel_ajax_rech_<?php echo $value; ?>();
        reset_ajax_rech_<?php echo $value; ?>();
        open_form_rech_col_<?php echo $value; ?>();
    <?php
    }
}
?>

<?PHP
switch ($_SESSION["typ_filtres"]) {
    case "int" :
        ?>
        valid_rech_compl();
        <?PHP
        break;
    case "cli" :
        break;
    default :
        break;
}
?>
clear_all_rech('<?php echo $_SESSION["lnk_lst"]; ?>');
clear_all_rech_lst('<?php echo $_SESSION["lnk_lst"]; ?>');
}