<?php
if (!isset($_GET["req"])) {?>

<html>
<head>
<title>Impression de courrier</title>
<link rel="stylesheet" href="../style/axa.css">
</head>

 <frameset rows="25%, 75%">
      <frame id="print_top" name="print_top" src="print.php?req=top&id=<?php echo $_GET["id"];?>&idr=<?php echo $_GET["idr"];?>">
      <frame id="print_print" name="print_print" src="print.php?req=print&id=<?php echo $_GET["id"];?>&idr=<?php echo $_GET["idr"];?>">
  </frameset>
</html><?php }
else {
	include("../master.php");
	if ($_GET["req"]!="print") 
		echo '<html>
			<head>
			<link rel="stylesheet" href="../style/axa.css">
			</head>
			<body>
			';
			
	switch($_GET["req"]){
		
		case "top":
			$relance=contrat_relance_get($_GET["id"]);
			echo '<center>
			<form method="get" target="print_print" action="print.php">Document à imprimer :<br />
			<select name="idr" id="idr">
			<option value="0" >'.((contrat_is_resiliation($_GET["id"]))?"Résiliation":"Contrat").' '.((isset($relance[0]))?"envoyé le ".dateshort($relance[0]):"");
			
			if (contrat_is_incomplet_id($_GET["id"]) && !contrat_is_resilie(contrat_id_contrat($_GET["id"]))){
				for ($i=1; $i<=sizeof($relance); $i++){
					if ($i<4) echo '<option value="'.$i.'" '.(($_GET["idr"]==$i)?"SELECTED":"").'>Relance '.$i.' '.((isset($relance[$i]))?"envoyée le ".dateshort($relance[$i]):"");
					}
				}
					
			
			echo '</select><br />
			<input type="hidden" name="id" value="'.$_GET["id"].'">
			<input type="hidden" name="req" value="print">
			<input type="submit" value="Charger ce document" >
			</form>
			
			
			<br />
			<iframe id="iframe_save" src="" frameborder="0" width="0" height="0"></iframe>
			<input type="button" value="Imprimer" onclick="parent.print_print.print_me(); document.getElementById(\'iframe_save\').src=\'print.php?req=save&id='.$id.'&idr=\'+ parent.print_print.document.getElementById(\'idr\').value;">
			</center>
			';
			break;
		
		case "save":
			$relance=contrat_relance_get($_GET["id"]);
			$relance[$_GET["idr"]]=date("Y-m-d H:i:s");
			contrat_relance_set($_GET["id"], $relance);
			break;
		
		case "print":
			echo '<html>
			<head>
			<style>
			table, body, div, span {
			font-family:  Eras Light ITC;
				font-size:14px;
				color:black;
				line-height:16px;
				text-align:justify;
				}
			</style>
			</head>
			<script>
				drag_on=false;
				paste_on=false;
			</script>
			<body><div contentEditable="true"
				ondragleave="drag_on=false;"
				onclick="this.focus()"
				ondragstart="drag_on=true" 
				ondrop="if(drag_on) drag_on=false; else event.returnValue=false;" 
				onPaste="if(!paste_on){event.returnValue=false; paste_on=true; if (window.clipboardData.getData(\'Text\')!=null) {execCommand(\'Paste\', false, window.clipboardData.getData(\'Text\'));} paste_on=false;}" 
						>
			';
			$result=mysql_query("SELECT * FROM contrats WHERE id='$id'", $link);
			$row=mysql_fetch_array($result);
			foreach ($row as $key=>$value) ${$key}=html($value);
			
			
			if ($id_contrat!="0") {
				$id_axa=contrat_id_axa($id_contrat);
				$id_client=contrat_id_client($id_contrat);
				$categorie=contrat_type_nom(contrat_id_type($id_contrat));
				$courrier="contrat_remplacement";
				}
			else {
				$categorie=contrat_type_nom(contrat_id_type($id));
				if ($id_type=="16" && ($idr=="" || $idr=="0")) $courrier="contrat_nouveau_juridique"; // 16 => juridique
				else $courrier="contrat_nouveau";
				}
				
	
			$result=mysql_query("SELECT id_axa, civilite, nom, prenom, adresse1, adresse2, adresse3, code_postal, ville FROM clients WHERE id='".$id_client."'", $link);
			while ($row=mysql_fetch_array($result)){
				if ($row["civilite"]=="") $row["civilite"]=3;
				$id_axa_client=$row["id_axa"];
				$titre=$CLIENTS["civilite"][$row["civilite"]];
				$nom=$row["nom"];
				$prenom=$row["prenom"];
				$adresse1=$row["adresse1"];
				$adresse2=$row["adresse2"];
				$adresse3=$row["adresse3"];
				$code_postal=$row["code_postal"];
				$ville=$row["ville"];
				}
			
			$pieces="";
			$need_contrat=false;
			$need_prime=false;
			$nothing=true;
			
			$relance=contrat_relance_get($_GET["id"]);
			$date_relance=array();
			foreach ($relance as $key=>$value){
				$date_relance[$key]=dateshort($value);
				}
			$result=mysql_query("SELECT p.id, p.nom, cp.fournie FROM contrats_pieces cp, pieces p WHERE cp.id_contrat='".$id."' AND p.id=cp.id_piece AND fournie='0'", $link);
			while ($row=mysql_fetch_array($result)){
				if ($idr=="" || $idr=="0"){
					$nothing=false;
						if ($row["id"]==1) $need_contrat=true; 
					elseif ($row["id"]==2) $need_prime=true;
					else $pieces.="<b>".$row["nom"]."</b><br />";
					}
				else {
					if ($row["id"]==2){
						if ($montant!="0")
							$pieces.="<b>RÈGLEMENT DE ".$montant." €</b><br />";
						}
					else $pieces.="<b>".$row["nom"]."</b><br />";
					}
				}
			if ($idr==3) $date_suspension=date("d/m/Y", mktime()+24*3600*15);
			
			if (!$need_contrat) $contrat=false; else $contrat=true;
			if (!$need_prime) $montant="0";
			
			
			include("../courriers/courrier_modele.php");
			echo '<div style="width:550px; text-align:justify; position:absolute;left:45px; top:400px;">';
			
			$resiliation=contrat_resiliation_get($_GET["id"]);
			if (!empty($resiliation)) include("../courriers/contrat_resiliation.php");
			elseif ($idr=="" || $idr=="0") include("../courriers/".$courrier.".php");
			else include("../courriers/relance_".$idr.".php");
			echo '</div>
			</div>
			<script>
			function print_me(){
				this.focus();
				window.print();
				}
			</script>
			<input type="hidden" id="idr" value="'.$idr.'">';
			break;
		
		default : break;
		}
	echo '
		</body>
		</html>';
	}

?>