<?php
include ("header.php");


function sinistre_new_mod($id, $id_axa) { 
	global $link, $CLIENTS, $CONTRATS, $GLOBAL_TITRE;

	$color_table="#FFFFFF";
	$color_left="#FAFAFA";
	$color_right="#FFFFFF";
	$expert_autre_inline=false;
	
	if ($id=='' && $id_axa==''){
		
		$id_type="";
		$id_contrat="";
		$date_sinistre="";
		$date_declaration="";
		$date_cloture="";
		$date_reglement="";
		$date_relance="";
		$franchise="0";
		$montant="0";
		$gere="0";
		$corporel="0";
		$responsabilite="";
		$expert="";
		$expert_autre="";
		$clos="0";
		$information="";
		$scan="0";
		
		$titre_page="Ajout d'un sinitre";
		$info="";
		$rel_visible="none";	
		
		$id="";
		$id_axa="";
		}
		
	else {
		if ($id_axa!="") $result=mysql_query("SELECT * FROM sinistres WHERE id_axa='$id_axa'", $link);
		else $result=mysql_query("SELECT * FROM sinistres WHERE id='$id'", $link);
		if (mysql_num_rows($result)==0) {echo '<center><br />Aucun enregistrement ne correspond à cet identifiant !<br /><br /></center>'; return false;}
		$row=mysql_fetch_array($result); 
		foreach ($row as $key=>$value) ${$key}=html($value);
		
		if (!in_array($expert, $CONTRATS["sinistre_expert"])) { // woooho super !
			$expert_autre=$expert;
			if (substr($expert_autre, 0, 20)=="[Entreprise Service]") $expert="[Entreprise Service] Autre";
			else {
			           if (substr($expert_autre, 0, 5)=="[BCA]") $expert="BCA";
                      	           else {
                      	                     if (substr($expert_autre, 0, 15)=="[Garage Agree] ") $expert="[Garage Agree] Autre";
                      	                     else $expert="Autre";
                      	            	}
                      	      }
			$expert_autre_inline=true;
			
			}
		else $expert_autre="";

		$titre_page="Modification du sinistre $id_axa";
		$info="&nbsp;&nbsp;&nbsp;Ajouté le ".datetime_short($date_add);
		if ($date_mod!="0000-00-00 00:00:00") $info.="<br>&nbsp;&nbsp;&nbsp;Modifié le ".datetime_short($date_mod);
		}
	
	$GLOBAL_TITRE.=strip_tags($titre_page);
	
	echo '
	
	
	<table cellpadding="2" cellspacing="0" bgcolor="000000" width="100%">
	<form method="post" action="sinistre.php" name="form1">
    <tr> 
       <td valign="top" class="white" height="22">
		<b>&raquo;&nbsp;</b><a class="white" href="sinistre.php?req=liste"><b>Sinistres</b></a><b>&nbsp;&raquo;&nbsp;'.$titre_page.'</b>
	</td></tr>
	<tr> 
       <td valign="top" class="white" height="22" bgcolor="#999999">
		'.$info.'
	</td></tr>
	
	<tr>
    <td valign="top" width="100%" bgcolor="ffffff">
	
	
	
	<table cellpadding="5" cellspacing="1" border="0" bgcolor="'.$color_table.'">
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Id</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="id_axa" size="20" maxlength="250" value="'.$id_axa.'"></td>
	</tr>

	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Id contrat</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="id_contrat" size="20" maxlength="250" value="'.$id_contrat.'"
		 onchange="document.getElementById(\'i_contrat\').src=\'iframes.php?req=validate_contrat&id=\'+this.value">
		 <div style="display:inline; width:400px; height:16px" name="id_contrat_div" id="id_contrat_div"></div>
		 <iframe id="i_contrat" name="i_contrat" frameborder="0" scrolling="no" height="0" width="0" src="iframes.php?req=validate_contrat&id='.$id_contrat.'"></iframe>
		 </td>
	</tr>
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Type</b></td>
		<td><select name="id_type">';
		$result=mysql_query("SELECT * FROM sinistres_type", $link);
		while ($row=mysql_fetch_array($result)){
			echo '<option value="'.$row["id"].'"';
			if ($row["id"]==$id_type) echo 'SELECTED';
			echo '>'.$row["nom"];
			}
		echo '
			</select></td>
	</tr>
	
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Date du sinistre</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" id="date_sinistre" name="date_sinistre" size="8" maxlength="8" value="'.date_sql2txt($date_sinistre).'"> JJMMAAAA</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Date de déclaration</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" id="date_declaration" name="date_declaration" size="8" maxlength="8" value="'.date_sql2txt($date_declaration).'"> JJMMAAAA</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Date de clôture</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" id="date_cloture" name="date_cloture" size="8" maxlength="8" value="'.date_sql2txt($date_cloture).'"> JJMMAAAA</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Date de règlement</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" id="date_reglement" name="date_reglement" size="8" maxlength="8" value="'.date_sql2txt($date_reglement).'"> JJMMAAAA</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Montant de la franchise</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="franchise" size="5" maxlength="20" value="'.$franchise.'">€</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Montant</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="montant" size="5" maxlength="20" value="'.$montant.'">€</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Géré par</b></td>
		<td bgcolor="'.$color_right.'"><input type="radio" name="gere" id="gere_0" value="0" '.(($gere=="0")?"CHECKED":"").'><label for="gere_0">Agence</label><br />
		<input type="radio" name="gere" id="gere_1" value="1" '.(($gere=="1")?"CHECKED":"").'><label for="gere_1">Compagnie</label></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Corporel</b></td>
		<td bgcolor="'.$color_right.'"><input type="radio" name="corporel" id="corporel_0" value="0" '.(($corporel=="0")?"CHECKED":"").'><label for="corporel_0">Non</label><br />
		<input type="radio" name="corporel" id="corporel_1" value="1" '.(($corporel=="1")?"CHECKED":"").'><label for="corporel_1">Oui</label></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Responsabilité client</b></td>
		<td bgcolor="'.$color_right.'"><select name="responsabilite">
			<option value="100%" '.(($responsabilite=="100%")?"SELECTED":"").'>100%
			<option value="50%" '.(($responsabilite=="50%")?"SELECTED":"").'>50%
			<option value="0%" '.(($responsabilite=="0%")?"SELECTED":"").'>0%
			<option value="NS" '.(($responsabilite=="NS")?"SELECTED":"").'>NS
			</select></td>
	</tr>

	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Expert</b></td>
		<td bgcolor="'.$color_right.'"><select maxlength="70" name="expert"
		onchange="if (this.selectedIndex==11 || this.selectedIndex==15 || this.selectedIndex==16)
		document.getElementById(\'expert_autre\').style.display=\'inline\'; else document.getElementById(\'expert_autre\').style.display=\'none\';">';
		
		foreach($CONTRATS["sinistre_expert"] as $experts)
			echo '<option value="'.$experts.'" '.(($expert==$experts)?"SELECTED":"").'>'.$experts;
		echo '
			</select><br/><br/>
			<input type="text" name="expert_autre" id="expert_autre" value="'.html($expert_autre).'" style="display:'.((!$expert_autre_inline)?"none":"inline").';" size="35" maxlength="70"></td>
	</tr>

	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Infos</b></td>
		<td bgcolor="'.$color_right.'"><textarea name="information" cols="49" rows="6" style="overflow:auto;">'.$information.'</textarea></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>A relancer le</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" id="date_relance" name="date_relance" size="8" maxlength="8" value="'.date_sql2txt($date_relance).'"> JJMMAAAA</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Sinistre clos</b></td>
		<td bgcolor="'.$color_right.'"><input type="checkbox" name="clos" value="1" '.(($clos=="1")?"CHECKED":"").'></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>scan</b></td>
		<td bgcolor="'.$color_right.'"><input type="checkbox" name="scan" value="1" '.(($scan=="1")?"CHECKED":"").'></td>
	</tr>
	
	</table>
	
	</td></tr>
    <tr>
	<td align="center"><br/>
	<input type="hidden" name="req" value="save">
	<input type="hidden" name="id" value="'.$id.'">
	<input type="submit" value="Enregistrer"><br /><br /></td></tr>
	</form></table>
	';
	}



function sinistre_save() {
    global $link, $SS_user_id, $GLOBAL_TITRE;

	foreach ($_POST as $key=>$value) {
		if (is_array($value)) {
			foreach ($value as $akey=>$avalue) {
				if (!is_array($avalue)) $_POST[$key][$akey]=var_post_post($avalue);
				else $_POST[$key][$akey]=$avalue;
				}
			}
		else $_POST[$key]=var_post_post($value);
		}
	
	$_POST["date_sinistre"]=date_txt2sql($_POST["date_sinistre"]);
	$_POST["date_declaration"]=date_txt2sql($_POST["date_declaration"]);
	$_POST["date_cloture"]=date_txt2sql($_POST["date_cloture"]);
	$_POST["date_reglement"]=date_txt2sql($_POST["date_reglement"]);
	$_POST["date_relance"]=date_txt2sql($_POST["date_relance"]);
	if (!isset($_POST["clos"])) $_POST["clos"]="0";
	if (!isset($_POST["scan"])) $_POST["scan"]="0";
	
	if ($_POST["expert"]=="[Entreprise Service] Autre") $_POST["expert"]="[Entreprise Service] ".$_POST["expert_autre"];
	if ($_POST["expert"]=="BCA") $_POST["expert"]="[BCA] ".$_POST["expert_autre"];
	if ($_POST["expert"]=="[Garage Agrée] Autre") $_POST["expert"]="[Garage Agree] ".$_POST["expert_autre"];
	If ($_POST["expert"]=="Autre") $_POST["expert"]=$_POST["expert_autre"];
	unset($_POST["expert_autre"]);
	
	
	
	$error=0;
	echo '<center>';	
	
	if ($_POST["id_axa"]=="") {
        $error=1;
        echo '<b>Le champ identifiant doit être saisi !</b><br><br>';
    	}
	
	$sql="SELECT id FROM sinistres WHERE id_axa='".mysql_escape_string($_POST["id_axa"])."'";
	$test_res=mysql_query($sql, $link);
	if (mysql_num_rows($test_res)!=0){
		$test_id=mysql_result($test_res, 0, "id");
		if ($_POST["id"]!=""){
			if ($test_id!=$_POST["id"]) {
				$error=1;
				echo 'Ce sinistre est déjà inscrit !';
				}
			}
		else {
			$error=1;
			echo 'Ce sinistre est déjà inscrit !';
			}
		}
	
			
	if ($error!=1){
	 	$sql_more="";
		
		foreach ($_POST as $key=>$value){
			if ($key!="req" && $key!="id") $sql_more.=",".$key."='".$value."'";
			}
		
		
		if ($_POST["id"]=="") {
			$sql_action="INSERT INTO";
			$sql_more="date_add=NOW(), id_utilisateur_add='".$SS_user_id."' ".$sql_more;
			}
		else {
			$sql_action="UPDATE";
			$sql_more="date_mod=NOW(), id_utilisateur_mod='".$SS_user_id."'  ".$sql_more." WHERE id='".$_POST["id"]."'";
			}
			
		
		$sql=$sql_action." sinistres SET ".$sql_more;
		mysql_query($sql, $link);echo mysql_error();
		if ($_POST["id"]=="") $_POST["id"]=mysql_insert_id();
		echo '
			<br><font class="normal">Merci !</font><br>
			<font class="normal">Les informations ont été enregistrées !
			<br><font class="normal">La fiche <a class="normal" href="sinistre.php?req=mod&id='.$_POST["id"].'">#'.$_POST["id_axa"].'</a> est à jour</font>
			<br><br><font class="normal"><a class="normal" href="sinistre.php?req=addone">Ajouter un sinistre</a></font><br>
			<font class="normal"><a class="normal" href="sinistre.php?req=liste">Retour à la liste</a></font><br></center><br />';
			$GLOBAL_TITRE.="Enregistrement #".$_POST["id_axa"].' effectué';
		}
	else echo '<br/><input type="button" onClick="history.go(-1)" value="Retour"></center>';
	}


	
function sinistre_liste($id_axa="", $page=0) {
    global $link, $GLOBAL_TITRE;
	$GLOBAL_TITRE.="Liste";
	$per_page=20;
	$start=$page*$per_page;
	
	$sql_more="";
	if ($id_axa!="") $sql_more.=" AND id_contrat='".mysql_escape_string($id_axa)."'";
	if (isset($_GET["clos"]) && $_GET["clos"]==="1") $sql_more.=" AND clos='1'";
	if (isset($_GET["clos"]) && $_GET["clos"]==="0") $sql_more.=" AND clos='0'";
	if (isset($_GET["clos"]) && $_GET["clos"]==="-1") $sql_more.=" AND clos='0' AND date_relance!='0000-00-00'";
	if (isset($_GET["gere"]) && $_GET["gere"]==="1") $sql_more.=" AND gere='1'";
	if (isset($_GET["gere"]) && $_GET["gere"]==="0") $sql_more.=" AND gere='0'";
	if (isset($_GET["scan"]) && $_GET["scan"]==="1") $sql_more.=" AND scan='1'";
	if (isset($_GET["scan"]) && $_GET["scan"]==="0") $sql_more.=" AND scan='0'";
	
		
	echo '<center>
	<table border=0 cellpadding="3" cellspacing="1" bgcolor="#eeeeee" width="100%">
		
	<tr class="white">
	<td bgcolor="#000000"><b># sinistre</b></td>
	<td bgcolor="#000000" align="left" width="150"><b># Contrat</b></td>
	<td bgcolor="#000000" align="left" width="150"><b>Client</b></td>
	<td bgcolor="#000000" align="center"><b>Type Sinistre</b></td>
	<td bgcolor="#000000" align="center"><b>Date&nbsp;Sinistre</b></td>
	<td bgcolor="#000000" align="center"><b>scan</b></td>
	<td bgcolor="#000000" align="center"><b>Actions</b></td>
	
	</tr>';
	$nb_res=mysql_result(mysql_query("SELECT count(id) FROM sinistres WHERE 1 ".$sql_more."", $link),0);
	
	$query="SELECT id, id_axa, id_contrat, id_type, date_sinistre, clos, date_relance, scan FROM sinistres WHERE 1 ".$sql_more." ORDER by date_sinistre DESC LIMIT ".$start.", ".$per_page."";
	$result=mysql_query($query, $link);echo mysql_error();
	while ($row=mysql_fetch_array($result)){
		
		$id=$row['id'];	
		$id_axa=$row['id_axa'];
		$id_contrat=$row['id_contrat'];
		$id_type=$row['id_type'];
		$date_sinistre=$row["date_sinistre"];
		$date_relance=$row["date_relance"];
		$id_client=contrat_id_client(contrat_id($id_contrat));
		$row_client=mysql_fetch_array(mysql_query("SELECT id_axa, nom, prenom FROM clients WHERE id='".$id_client."'", $link));
		$client_nom=$row_client["nom"];
		$client_prenom=$row_client["prenom"];
		$client_id_axa=$row_client["id_axa"];
		$scan=$row["scan"];
		if($scan==0){
			$img ="../images/case_vide.png";
			}else{
			$img ="../images/check_vert.png";}
		if ($date_sinistre=="0000-00-00") $date_sinistre="";
		if ($row["clos"]=="1") $color="#F3F3F3";
		else $color="#FFFFFF";
		echo '<tr bgcolor="'.$color.'">
	
	


	<td valign="center"><a href="sinistre.php?req=mod&id='.$id.'"><img src="../images/admin/sinistre.gif" align="absmiddle" border="0" style="margin-right:3px;">'.$id_axa.'</a></td>
		<td align="left" valign="center">
			<a target="_blank" href="contrat.php?req=mod&id_axa='.$id_contrat.'"><img src="../images/admin/contrat.gif" style="margin-right:3px;" align="absmiddle" border="0"><span style=" font-size:9px;font-family:tahoma;">'.$id_contrat.'</span></a><br />
			&nbsp;<span style=" font-size:9px;font-family:tahoma;">'.contrat_type_nom(contrat_id_type(contrat_id($id_contrat))).'</span>
			</td>
		<td align="left" valign="top">
			<a title="Voir le client : '.html($client_nom." ".$client_prenom).' (#'.$client_id_axa.')" target="_blank" href="client.php?req=mod&id='.$id_client.'"><img align="absmiddle" src="../images/admin/client.gif" border="0"><span style="font-size:9px;font-family:tahoma;">'.$client_id_axa.'</span></a><br />
			&nbsp;<span style=" font-size:9px;font-family:tahoma;">'.html($client_nom." ".$client_prenom).'</span>
			</td>
		<td align="center" valign="center"><span style=" font-size:9px;font-family:tahoma;">'.sinistre_type_nom($id_type).'</span></td>
		<td align="center" valign="center">'.dateshort($date_sinistre).'</td>
		<td align="center" valign="center"><img src='.$img.'></td>
				
		
		<td align="center" valign="center">
		<a title="Modifier" href="sinistre.php?req=mod&id='.$id.'"><img src="../images/admin/mod.gif" align="absmiddle" border="0"></a>
		<a title="Supprimer" href="#" onclick="if(window.confirm(\'Etes vous sûr de vouloir supprimer le sinistre #'.$id_axa.' ?\')) location.href=\'sinistre.php?req=del&id='.$id.'\'; else return false;"><img src="../images/admin/del.gif" align="absmiddle" border="0"></a></td>		
		
	
			</tr>';
			
		}
	
	echo '</table>
	<br />
	<table border="0" cellpadding="4" cellspacing="1"><tr>
	';
	
	$nb_page=ceil($nb_res/$per_page);
	$page_visu=$page+1;
	
	$get_more="&gere=".((isset($_GET["gere"]))?$_GET["gere"]:"")."&clos=".((isset($_GET["clos"]))?$_GET["clos"]:"");
	
	if ($nb_page>1){
		echo '<td align="left" width="80">';
		if ($page>0) echo '<a href="sinistre.php?req=liste&page='.($page-1).''.$get_more.'">< Précédent</a>';
		echo '</td>';
		
		for ($i=1; $i<=$nb_page; $i++){
			if ($i<=3) echo '<td><a href="sinistre.php?req=liste&page='.($i-1).''.$get_more.'">'.(($i==$page_visu)?"<b>".$i."</b>":$i).'</a></td>';
			elseif ($i==($page_visu-2) && $i<=($page_visu+2)) echo '<td>...</td>';
			elseif ($i>=($page_visu-2) && $i<=($page_visu+2)) 	echo '<td><a href="sinistre.php?req=liste&page='.($i-1).''.$get_more.'">'.(($i==$page_visu)?"<b>".$i."</b>":$i).'</a></td>';
			elseif ($i==($nb_page-3))echo '<td>...</td>';
			elseif ($i>=($nb_page-3)) echo '<td><a href="sinistre.php?req=liste&page='.($i-1).''.$get_more.'">'.(($i==$page_visu)?"<b>".$i."</b>":$i).'</a></td>';
			}
		
		echo '<td align="right" width="80">';
		if ($page_visu<$nb_page) echo '<a href="sinistre.php?req=liste&page='.($page+1).''.$get_more.'">Suivant ></a>';
		echo '</td>';
		}
	echo '</tr></table></center>';
	}


function sinistre_del($id) {
    global $link;
	if (($id=="")||($id=="0")) exit();
   	mysql_query("DELETE FROM sinistres WHERE id='$id'", $link);
	
	sinistre_liste();
	}

if (!isset($req)) $req="";
switch($req) {

        case "liste":
			if (!isset($id_axa)) $id_axa="";
			if (!isset($page)) $page="0";
            sinistre_liste($id_axa, $page);
            break;
	
        case "addone":
            sinistre_new_mod(0, 0);
            break;
		
        case "mod":
			if (!isset($id)) $id="";
            if (!isset($id_axa)) $id_axa="";
            sinistre_new_mod($id, $id_axa);
            break;
						
        case "save":
			sinistre_save();
            break;

        case "del":
            sinistre_del($id);
            break;

      default:
            sinistre_liste();
            break;
	}

include ("footer.php");
?>