<?php

$dbHost = 'localhost';
$dbUser = 'safehunter_admindb';
$dbPass = 'dcm-1269';
$dbName = 'safehunter_asfaleies';
// $dbHost = 'localhost';
// $dbUser = 'root';
// $dbPass = '';
// $dbName = 'asfaleies';
// $_POST['KodPel']='572440';
// $_POST['Pinakida'] = 'ΑΜ54725';
$con = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbName);
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8 ");
$q = "SELECT * FROM asf_pelates WHERE KodPel=".$_POST['KodPel']." AND Pinakida='".$_POST['Pinakida']."' AND Im_mexri='".$_POST['Im_mexri']."'";
$q2 = "SELECT * FROM asf_details WHERE KodPel=".$_POST['KodPel']." AND Pinakida='".$_POST['Pinakida']."' AND Im_mexri='".$_POST['Im_mexri']."'";
$q3 = "SELECT * FROM asf_details2 WHERE KodPel=".$_POST['KodPel']." AND Pinakida='".$_POST['Pinakida']."' AND Im_mexri='".$_POST['Im_mexri']."'";

$res1 = mysql_query($q) or die(mysql_error());
$res2 = mysql_query($q2) or die(mysql_error());
$res3 = mysql_query($q3) or die(mysql_error());
$pelates = "<table border=\"1\" id=\"tbl_pelaths\" class=\"table table-striped table-bordered bootstrap-datatable datatable responsive dataTable\">
	<thead>
	<th>ID</th>
	<th>Επωνυμία</th>
	<th>Διεύθυνση</th>
	<th>Πόλη</th>
	<th>Ταχυδρομικός Κώδικας</th>
	<th>ΑΦΜ</th>
	<th>Τηλ.Εργασίας</th>
	<th>Τηλ.Επικοινωνίας</th>
	<th>FAX</th>
	<th>Ημερομηνία Γέννησης</th>
	<th>Τίτλος</th>
	<th>Αριθμός Ταυτότητας</th>
	</thead>
	<tbody>";

$details = "<table border=1 id=\"tbl_details\" class=\"table table-striped table-bordered bootstrap-datatable datatable responsive dataTable\">
	<thead>
	<th>Κλάδος</th>
	<th>Κωδ.Κάλυψης</th>
	<th>Κάλυψη</th>
	<th>Κεφάλαιο</th>
	<th>Καθαρά</th>
	<th>key0</th>
	</thead>
	<tbody>";

$details2 = "<table border=1 id=\"tbl_details2\" class=\"table table-striped table-bordered bootstrap-datatable datatable responsive dataTable\">
	<thead>
	<th>ΕΛΟΑ</th>
	<th>ΕΛΙΜ</th>
	<th>ΕΛΚΑ</th>
	<th>ΕΛΧΟΑ</th>
	<th>ΕΛΑ</th>
	<th>ΕΛΛΣ</th>
	<th>ΕΕ</th>
	<th>ΕΛΠΑ</th>
	<th>Επιβ/νση ΛΗΛΙ</th>
	<th>Επιβ/νση ΛΔΙΠ</th>
	<th>Επιβ/νση ΛΠΙΝ</th>
	<th>Επιβ/νση ΛΤΟΥΡ</th>
	<th>Ολικά</th>
	<th>key0 </th>  

	</thead>
	<tbody>";

if(isset($_POST['type']) && $_POST['type']=='view'){
	

	while($row=mysql_fetch_array($res1)){
		// echo "<script>console.log('preformat: ".strtotime($row['Im_apo']).", format: ".date('d-m-Y', strtotime($row['Im_apo']))."')</script>";
		$pelates.="<tr><td>".$row['KodPel']."</td>
		<td>".$row['Eponumia_pel']."</td>
		<td>".$row['Dieuthunsi']."</td>
		<td>".$row['Poli']."</td>
		<td>".$row['TK']."</td>
		<td>".$row['AFM']."</td>
		<td>".$row['Til_Ergasias']."</td>
		<td>".$row['Til_Epikoin']."</td>
		<td>".$row['Fax']."</td>
		<td>".$row['Im_gennisis']."</td>
		<td>".$row['Titlos']."</td>
		<td>".$row['AT']."</td></tr><thead>
		<th>Τίτλος ΔΟΥ</th>
		<th>Πατρώνυμο</th>
		<th>Πράκτορας</th>
		<th>Αρ.Πρωτοκόλου</th>
		<th>Κωδ.Παραγώγου</th>
		<th>Bonus Malus</th>
		<th>Απαλαγή</th>
		<th>Ομαδική Έκπτωση</th>
		<th>Έκπτωση Ιδιότητας</th>
		<th>Επασφάλιστρα</th>
		<th>Υποπράκτορας</th>
		<th>Κλάδος</th></thead><tr>
		<td>".$row['Titlos_DOY']."</td>
		<td>".$row['Patronumo']."</td>
		<td>".$row['Praktoras']."</td>
		<td>".$row['Ar_Protokolou']."</td>
		<td>".$row['Kod_paragogou']."</td>
		<td>".$row['Bonus_Malus']."</td>
		<td>".$row['Apalagi']."</td>
		<td>".$row['Omadiki_Ekptosi']."</td>
		<td>".$row['Ekptosi_idiotitas']."</td>
		<td>".$row['Epasfalistra']."</td>
		<td>".$row['Ypopraktoras']."</td>
		<td>".$row['Klados']."</td></tr><thead>
		<th>Κωδ.Πακέτου</th>
		<th>Έκπτωση με μείωση Προμήθειας</th>
		<th>Αρ.Πρότασης</th>
		<th>Ασφαλιζόμενος</th>
		<th>ΑΦΜ Ασφαλιζομένου</th>
		<th>Τίτλος Συμβολαίου</th>
		<th>Κατηγορία</th>
		<th>Νο.Συμβολαίου</th>
		<th>Κλειδί Χρέωσης</th>
		<th>No.Ανανέωσης</th>
		<th>No.ΠρΠρ</th>
		<th>Ημ.Έκδοσης</th></thead><tr>
		<td>".$row['Kod_Paketou']."</td>
		<td>".$row['Ekptosi_me_meiosi_Promitheias']."</td>
		<td>".$row['ArProtasis']."</td>
		<td>".$row['Asfalizomenos']."</td>
		<td>".$row['AFM_asfalizomenou']."</td>
		<td>".$row['Titlos_sumbolaiou']."</td>
		<td>".$row['Katigoria']."</td>
		<td>".$row['No_Sumb']."</td>
		<td>".$row['Kleidi_Xreosis']."</td>
		<td>".$row['No_Anan']."</td>
		<td>".$row['No_PrPr']."</td>
		<td>".$row['Im_ekdosis']."</td></tr><thead>
		<th>Ημερ.Από</th>
		<th>Ημερ.Έως</th>
		<th>Τρόπος Πληρωμής</th>
		<th>Ημ.ΠρΠρ.Από</th>
		<th>Ημ.ΠρΠρ.Μέχρι</th>
		<th>Ζώνη</th>
		<th>Πινακίδα</th>
		<th>Μάρκα</th>
		<th>Χρήση</th>
		<th>Ίπποι</th>
		<th>Θέσεις</th>
		<th>Έτος Κατασκευής</th></thead><tr>
		<td>".$row['Im_apo']."</td>
		<td>".$row['Im_mexri']."</td>
		<td>".$row['Tropos_Pliromis']."</td>
		<td>".$row['PrPr_Apo']."</td>
		<td>".$row['PrPr_Eos']."</td>
		<td>".$row['Zoni']."</td>
		<td>".$row['Pinakida']."</td>
		<td>".$row['Marka']."</td>
		<td>".$row['Xrisi']."</td>
		<td>".$row['Ippoi']."</td>
		<td>".$row['Theseis']."</td>
		<td>".$row['Etos_katask']."</td></tr><thead>
		<th>BM</th>
		<th>Καθαρά Ασφ.</th>
		<th>Μικτά Ασφ.</th>
		<th>Προμήθειες Συνεργάτη</th>
		<th>Άκυρο</th>
		<th>key0</th>
		<th>Διατραπεζικός Κωδ.Πληρωμής</th></thead><tr>
		<td>".$row['BM']."</td>
		<td>".$row['Kathara_asf']."</td>
		<td>".$row['Mikta_asfalistra']."</td>
		<td>".$row['Promitheies_sunergati']."</td>
		<td>".$row['Akuro']."</td>
		<td>".$row['key0']."</td>
		<td>".$row['Diatrapezikos_kodikos_pliromis']."</td>
		</tr>";
	}
	$pelates.="</table>";
	if(mysql_num_rows($res2)>0){
		while($row=mysql_fetch_array($res2)){
			$details.="<tr><td>".$row['Klados']."</td>
			<td>".$row['Kod_Kal']."</td>
			<td>".$row['Kalupsi']."</td>
			<td>".$row['Kefalaio']."</td>
			<td>".$row['Kathara']."</td>
			<td>".$row['key0']."</td></tr>";
		}
	} else {
		$details = '';
	}
	$details.='</table>';
	if(mysql_num_rows($res3)>0){
		while($row=mysql_fetch_array($res3)){
			$details2.="<tr><td>".$row['ELOA']."</td>
			<td>".$row['ELIM']."</td>
			<td>".$row['ELKA']."</td>
			<td>".$row['ELXOA']."</td>
			<td>".$row['ELA']."</td>
			<td>".$row['ELLS']."</td>
			<td>".$row['EE']."</td>
			<td>".$row['ELPA']."</td>
			<td>".$row['Epibnsi_LILI']."</td>
			<td>".$row['Epibnsi_LDIP']."</td>
			<td>".$row['Epibnsi_LPIN']."</td>
			<td>".$row['Epibnsi_LTOYR']."</td>
			<td>".$row['Olika']."</td>
			<td>".$row['key0']."</td>
			</tr>";
		}
	} else {
		$details2='';
	}
	$details2.='</table>';
	echo $pelates.$details.$details2;
}
elseif(isset($_POST['type']) && $_POST['type']=='edit'){
	$pelates="<form method='POST' action='save_edit.php'>".$pelates;
	while($row=mysql_fetch_array($res1)){
		// echo "<script>console.log('preformat: ".strtotime($row['Im_apo']).", format: ".date('d-m-Y', strtotime($row['Im_apo']))."')</script>";
		$pelates.="<tr><td>".$row['KodPel']."<input type='hidden' name='KodPel' value='".$row['KodPel']."' /></td>
		<td><input type='text' name='Eponumia_pel' value='".$row['Eponumia_pel']."' /></td>
		<td><input type='text' name='Dieuthunsi' value='".$row['Dieuthunsi']."' /></td>
		<td><input type='text' name='Poli' value='".$row['Poli']."' /></td>
		<td><input type='text' name='TK' value='".$row['TK']."' /></td>
		<td><input type='text' name='AFM' value='".$row['AFM']."' /></td>
		<td><input type='text' name='Til_Ergasias' value='".$row['Til_Ergasias']."' /></td>
		<td><input type='text' name='Til_Epikoin' value='".$row['Til_Epikoin']."' /></td>
		<td><input type='text' name='Fax' value='".$row['Fax']."' /></td>
		<td><input type='text' name='Im_gennisis' value='".$row['Im_gennisis']."' /></td>
		<td><input type='text' name='Titlos' value='".$row['Titlos']."' /></td>
		<td><input type='text' name='AT' value='".$row['AT']."' /></td>
		</tr><thead>
		<th>Τίτλος ΔΟΥ</th>
		<th>Πατρώνυμο</th>
		<th>Πράκτορας</th>
		<th>Αρ.Πρωτοκόλου</th>
		<th>Κωδ.Παραγώγου</th>
		<th>Bonus Malus</th>
		<th>Απαλαγή</th>
		<th>Ομαδική Έκπτωση</th>
		<th>Έκπτωση Ιδιότητας</th>
		<th>Επασφάλιστρα</th>
		<th>Υποπράκτορας</th>
		<th>Κλάδος</th></thead><tr>
		<td><input type='text' name='Titlos_DOY' value='".$row['Titlos_DOY']."' /></td>
		<td><input type='text' name='Patronumo' value='".$row['Patronumo']."' /></td>
		<td><input type='text' name='Praktoras' value='".$row['Praktoras']."' /></td>
		<td><input type='text' name='Ar_Protokolou' value='".$row['Ar_Protokolou']."' /></td>
		<td><input type='text' name='Kod_paragogou' value='".$row['Kod_paragogou']."' /></td>
		<td><input type='text' name='Bonus_Malus' value='".$row['Bonus_Malus']."' /></td>
		<td><input type='text' name='Apalagi' value='".$row['Apalagi']."' /></td>
		<td><input type='text' name='Omadiki_Ekptosi' value='".$row['Omadiki_Ekptosi']."' /></td>
		<td><input type='text' name='Ekptosi_idiotitas' value='".$row['Ekptosi_idiotitas']."' /></td>
		<td><input type='text' name='Epasfalistra' value='".$row['Epasfalistra']."' /></td>
		<td><input type='text' name='Ypopraktoras' value='".$row['Ypopraktoras']."' /></td>
		<td><input type='text' name='Klados' value='".$row['Klados']."' /></td>
		</tr><thead>
		<th>Κωδ.Πακέτου</th>
		<th>Έκπτωση με μείωση Προμήθειας</th>
		<th>Αρ.Πρότασης</th>
		<th>Ασφαλιζόμενος</th>
		<th>ΑΦΜ Ασφαλιζομένου</th>
		<th>Τίτλος Συμβολαίου</th>
		<th>Κατηγορία</th>
		<th>Νο.Συμβολαίου</th>
		<th>Κλειδί Χρέωσης</th>
		<th>No.Ανανέωσης</th>
		<th>No.ΠρΠρ</th>
		<th>Ημ.Έκδοσης</th></thead><tr>
		<td><input type='text' name='Kod_Paketou' value='".$row['Kod_Paketou']."' /></td>
		<td><input type='text' name='Ekptosi_me_meiosi_Promitheias' value='".$row['Ekptosi_me_meiosi_Promitheias']."' /></td>
		<td><input type='text' name='ArProtasis' value='".$row['ArProtasis']."' /></td>
		<td><input type='text' name='Asfalizomenos' value='".$row['Asfalizomenos']."' /></td>
		<td><input type='text' name='AFM_asfalizomenou' value='".$row['AFM_asfalizomenou']."' /></td>
		<td><input type='text' name='Titlos_sumbolaiou' value='".$row['Titlos_sumbolaiou']."' /></td>
		<td><input type='text' name='Katigoria' value='".$row['Katigoria']."' /></td>
		<td><input type='text' name='No_Sumb' value='".$row['No_Sumb']."' /></td>
		<td><input type='text' name='Kleidi_Xreosis' value='".$row['Kleidi_Xreosis']."' /></td>
		<td><input type='text' name='No_Anan' value='".$row['No_Anan']."' /></td>
		<td><input type='text' name='No_PrPr' value='".$row['No_PrPr']."' /></td>
		<td><input type='text' name='Im_ekdosis' value='".$row['Im_ekdosis']."' /></td>
		</tr><thead>
		<th>Ημερ.Από</th>
		<th>Ημερ.Έως</th>
		<th>Τρόπος Πληρωμής</th>
		<th>Ημ.ΠρΠρ.Από</th>
		<th>Ημ.ΠρΠρ.Μέχρι</th>
		<th>Ζώνη</th>
		<th>Πινακίδα</th>
		<th>Μάρκα</th>
		<th>Χρήση</th>
		<th>Ίπποι</th>
		<th>Θέσεις</th>
		<th>Έτος Κατασκευής</th></thead><tr>
		<td><input type='text' name='Im_apo' value='".$row['Im_apo']."' /></td>
		<td><input type='text' name='Im_mexri' value='".$row['Im_mexri']."' /><input type='hidden' name='exp_date' value='".$row['Im_mexri']."' /></td>
		<td><input type='text' name='Tropos_Pliromis' value='".$row['Tropos_Pliromis']."' /></td>
		<td><input type='text' name='PrPr_Apo' value='".$row['PrPr_Apo']."' /></td>
		<td><input type='text' name='PrPr_Eos' value='".$row['PrPr_Eos']."' /></td>
		<td><input type='text' name='Zoni' value='".$row['Zoni']."' /></td>
		<td><input type='text' name='Pinakida' value='".$row['Pinakida']."' /><input type='hidden' name='arkuk' value='".$row['Pinakida']."' /></td>
		<td><input type='text' name='Marka' value='".$row['Marka']."' /></td>
		<td><input type='text' name='Xrisi' value='".$row['Xrisi']."' /></td>
		<td><input type='text' name='Ippoi' value='".$row['Ippoi']."' /></td>
		<td><input type='text' name='Theseis' value='".$row['Theseis']."' /></td>
		<td><input type='text' name='Etos_katask' value='".$row['Etos_katask']."' /></td>
		</tr><thead>
		<th>BM</th>
		<th>Καθαρά Ασφ.</th>
		<th>Μικτά Ασφ.</th>
		<th>Προμήθειες Συνεργάτη</th>
		<th>Άκυρο</th>
		<th>key0</th>
		<th>Διατραπεζικός Κωδ.Πληρωμής</th></thead><tr>
		<td><input type='text' name='BM' value='".$row['BM']."' /></td>
		<td><input type='text' name='Kathara_asf' value='".$row['Kathara_asf']."' /></td>
		<td><input type='text' name='Mikta_asfalistra' value='".$row['Mikta_asfalistra']."' /></td>
		<td><input type='text' name='Promitheies_sunergati' value='".$row['Promitheies_sunergati']."' /></td>
		<td><input type='text' name='Akuro' value='".$row['Akuro']."' /></td>
		<td><input type='text' name='key0' value='".$row['key0']."' /></td>
		<td><input type='text' name='Diatrapezikos_kodikos_pliromis' value='".$row['Diatrapezikos_kodikos_pliromis']."' /></td>
		</tr>";
	}
	$pelates.="</table>";
	$y=0;
	if(mysql_num_rows($res2)>0){
		while($row=mysql_fetch_array($res2)){
			$details.="<tr><td><input type='text' name='".$y."-Klados' value='".$row['Klados']."' /></td>
			<td><input type='text' name='".$y."-Kod_Kal' value='".$row['Kod_Kal']."' /></td>
			<td><input type='text' name='".$y."-Kalupsi' value='".$row['Kalupsi']."' /><input type='hidden' name='".$y."-Kalipsi' value='".$row['Kalupsi']."' /></td>
			<td><input type='text' name='".$y."-Kefalaio' value='".$row['Kefalaio']."' /></td>
			<td><input type='text' name='".$y."-Kathara' value='".$row['Kathara']."' /></td>
			<td><input type='text' name='".$y."-key0' value='".$row['key0']."' /></td></tr>";
			$y++;
		}
		$details.='</table>';
	} else {
		$details = '';
	}
	
	if(mysql_num_rows($res3)>0){
		while($row=mysql_fetch_array($res3)){
			$details2.="<tr><td><input type='text' name='ELOA' value='".$row['ELOA']."' /></td>
			<td><input type='text' name='ELIM' value='".$row['ELIM']."' /></td>
			<td><input type='text' name='ELKA' value='".$row['ELKA']."' /></td>
			<td><input type='text' name='ELXOA' value='".$row['ELXOA']."' /></td>
			<td><input type='text' name='ELA' value='".$row['ELA']."' /></td>
			<td><input type='text' name='ELLS' value='".$row['ELLS']."' /></td>
			<td><input type='text' name='EE' value='".$row['EE']."' /></td>
			<td><input type='text' name='ELPA' value='".$row['ELPA']."' /></td>
			<td><input type='text' name='Epibnsi_LILI' value='".$row['Epibnsi_LILI']."' /></td>
			<td><input type='text' name='Epibnsi_LDIP' value='".$row['Epibnsi_LDIP']."' /></td>
			<td><input type='text' name='Epibnsi_LPIN' value='".$row['Epibnsi_LPIN']."' /></td>
			<td><input type='text' name='Epibnsi_LTOYR' value='".$row['Epibnsi_LTOYR']."' /></td>
			<td><input type='text' name='Olika' value='".$row['Olika']."' /></td>
			<td><input type='text' name='key0' value='".$row['key0']."' /></td>
			</tr>";
		}
	} else {
		$details2 = '';
	}
	$save="</table><input type='submit' name='Save_changes' value='Save' /></form>";
	echo $pelates.$details.$details2.$save;
} elseif(isset($_POST['type']) && $_POST['type']=='delete'){
	$error=0;
	$tbl = "asf_pelates";
	$q = "DELETE FROM ".$tbl." WHERE KodPel='".$_POST['KodPel']."' AND Pinakida='".$_POST['Pinakida']."' AND Im_mexri='".$_POST['Im_mexri']."'";
	if(!mysql_query($q)){
		$error++;
	}

	$tbl = 'asf_details';
	if(!mysql_query($q)){
		$error++;	
	}

	$tbl='asf_details2';
	if(!mysql_query($q)){
		$error++;
	}
	if($error==0){
		echo "Success!";
	} elseif($error>0 && $error<3){
		echo "Partial-Success || ".mysql_error();
	} else {
		echo "Failed || ".mysql_error();
	}
}

?>