<?php
global $wpdb;
$table_reg = $wpdb->prefix.'frregsvg';
$table_map = $wpdb->prefix.'frregset';
echo "<div class=wrap>";
echo "<div class='icon32' id='icon-edit'><br /></div>
    <h2>";
	_e('Regions settings', 'fr_reg_wp');
echo "</h2>";	
echo "<table class=\"form-table\">
			<tbody><tr>
			<th scope='row'><label for='regions'>";_e('Edit a region', 'fr_reg_wp'); echo "</label></th>
			<td>";	
echo "<select name='regions' onchange=\"document.location.href='".admin_url('/options-general.php?page=frreg', __FILE__)."'+this.options[this.selectedIndex].value;\">";
					echo "<option value=''>";_e('Select a region', 'fr_reg_wp'); echo "</option>";
					$resultats = $wpdb->get_results("SELECT * FROM $table_reg ORDER BY nom") ;
					foreach ($resultats as $post) {
					echo "<option value='&reg=$post->reg'"; if(isset($_GET['reg']) && $_GET['reg']==$post->reg){ echo " SELECTED"; } echo ">$post->nom</option>";
					}
					echo "</select></td></tr></table><p class=\"description\">"; _e('To edit a region\'s settings, select it from the drop-down menu above.', 'fr_reg_wp'); echo "</p><hr>";
	
	
	
if(isset($_POST['update_map'])){
extract($_POST);
$maj = $wpdb->query( "UPDATE $table_reg SET color = '#$xcolor', url = '$xurl', target = '$xtarget' WHERE reg='$xreg'" );
if($maj>0) {
echo "<div class='updated notice'><p>$xnom has been successfully updated !</p></div>";
}else{
echo "<div class='error notice'><p>There has been an error.</p></div>";
}
}
	
	
	
if(isset($_GET['reg']) && $_GET['reg']!="")
{
$resultat = $wpdb->get_row("SELECT * FROM $table_reg WHERE reg = ".$_GET['reg']."" ) ;
$reg =  $resultat->reg;
$nom = $resultat->nom;
$color = $resultat->color;
$url = $resultat->url;
$target = $resultat->target;
	echo "<form method=\"post\" action=\"".$_SERVER['REQUEST_URI']."\">";
	echo "<div class='postbox ' id='postexcerpt'><h3>";
	_e('Edit this region', 'fr_reg_wp');
	echo "</h3>
            <div class='inside'>
			<table class=\"form-table\">
			<tbody><tr>
			<th scope='row'><label for='xnom'>";_e('Region name', 'fr_reg_wp'); echo "</label></th>
			<td><input name='xnom' id='xnom' value=\"$nom\" class=\"regular-text\" type=\"text\"></td>
			</tr>
			<tr>
			<th scope='row'><label for='xcolor'>";_e('Region color', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xcolor' id='xcolor' class='color' value='$color'></td>
			</tr>
			<tr>
			<th scope='row'><label for='xurl'>";_e('Region link', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xurl' id='xurl' value=\"$url\">
			<p class=\"description\">";_e('To disable this Region on your map, leave this field blank', 'fr_reg_wp'); echo "</p>
			</td>
			</tr>
			<th scope='row'><label for='xtarget'>";_e('Link target', 'fr_reg_wp'); echo "</label></th>
			<td><select name='xtarget' id='xtarget'>
				<option value='_self'"; if($target=="_self"){ echo " SELECTED"; } echo ">_self</option>
				<option value='_blank'"; if($target=="_blank"){ echo " SELECTED"; } echo ">_blank</option>
				<option value='_parent'"; if($target=="_parent"){ echo " SELECTED"; } echo ">_parent</option>
				<option value='_top'"; if($target=="_top"){ echo " SELECTED"; } echo ">_top</option>
				</select>
				<p class=\"description\">";_e('_self: same window | _blank: new window', 'fr_reg_wp'); echo "</p>
				</td>
			</tr>";
                    
                    
        echo "<tr><td>
		<input type='hidden' name='xreg' value='$reg'>
		</td><td><input type='submit' name='update_map' value='"; _e('Update', 'fr_reg_wp'); echo "' class='button button-primary' /></td></tr></table>
		</div></form>";
	
	
	
	
	
echo "</div>";

  
}
echo "<div class=wrap>";
echo "<div class='icon32' id='icon-edit'><br /></div>
    <h2>";
	_e('Map settings', 'fr_reg_wp');
echo "</h2>";

if(isset($_POST['update_map_set'])){
extract($_POST);
$maj2 = $wpdb->query( "UPDATE $table_map SET mapoff = '#$xmapoff', maphover_fill = '#$xmaphover_fill', mapstroke = '#$xmapstroke', mapstroke_width = '$xmapstroke_width', aff_dom = '$xaff_dom', aff_reg = '$xaff_reg' WHERE idp='$xidp'" );
if($maj2>0) {
echo "<div class='updated notice'><p>Map parameters has been successfully updated !</p></div>";
}else{
echo "<div class='error notice'><p>There has been an error.</p></div>";
}
}
if(isset($_POST['update_map_color'])){
extract($_POST);
$maj3 = $wpdb->query( "UPDATE $table_reg SET color = '#$xmapfill'" );
$maj4 = $wpdb->query( "UPDATE $table_map SET mapfill = '#$xmapfill' WHERE idp='$xidp'" );
if($maj3>0) {
echo "<div class='updated notice'><p>Map parameters has been successfully updated !</p></div>";
}else{
echo "<div class='error notice'><p>There has been an error.</p></div>";
}
}



//idp 	mapfill 	mapoff 	maphover_fill 	mapstroke 	mapstroke_width 	mapOriginWidth 	mapOriginHeight 	mapWidth 	mapHeight 	aff_dom 	
$resultat2 = $wpdb->get_row("SELECT * FROM $table_map" ) ;
$mapfill =  $resultat2->mapfill;
$mapoff = $resultat2->mapoff;
$maphover_fill = $resultat2->maphover_fill;
$mapstroke = $resultat2->mapstroke;
$mapstroke_width = $resultat2->mapstroke_width;
$mapWidth = $resultat2->mapWidth;
$aff_dom = $resultat2->aff_dom;
$aff_reg = $resultat2->aff_reg;
	
	echo "<div class='postbox ' id='postexcerpt'><h3>";
	_e('Edit the map', 'Cmap_plugin');
	echo "</h3>";
            
			
			echo "<div class='inside'><form method=\"post\" action=\"".$_SERVER['REQUEST_URI']."\"><table class=\"form-table\">";
			echo "<tr>
			<th scope='row'><label for='xmaphover_fill'>";_e('Rollover color', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xmaphover_fill' id='xmaphover_fill' class='color' value='$maphover_fill'>
			<p class=\"description\">";_e('Color of the departments at the mouse hover.', 'fr_reg_wp'); echo "</p>
			</td>
			</tr>
			<tr>
			<th scope='row'><label for='xmapoff'>";_e('Disabled color', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xmapoff' id='xmapoff' class='color' value='$mapoff'>
			<p class=\"description\">";_e('Color of disabled departments (to disable a department, leave its \'URL\' field blank, it will be considered as inactive.', 'fr_reg_wp'); echo "</p>
			</td>
			</tr>
			<tr>
			<th scope='row'><label for='xmapstroke'>";_e('Map stroke color', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xmapstroke' id='xmapstroke' class='color' value='$mapstroke'>
			<p class=\"description\">";_e('Departments outlines color.', 'fr_reg_wp'); echo "</p>
			</td>
			</tr>
			<tr>
			<th scope='row'><label for='xmapstroke_width'>";_e('Stroke width', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xmapstroke_width' id='xmapstroke_width' value=\"$mapstroke_width\">
			<p class=\"description\">";_e('Thickness of departments outlines, ex.: 0.6, 1.22, 4 ...', 'fr_reg_wp'); echo "</p>
			</td>
			</tr>
			<th scope='row'><label for='xaff_dom'>";_e('Display D.O.M.', 'fr_reg_wp'); echo "</label></th>
			<td><select name='xaff_dom' id='xaff_dom'>
				<option value='true'"; if($aff_dom=="true"){ echo " SELECTED"; } echo ">Yes</option>
				<option value='false'"; if($aff_dom=="false"){ echo " SELECTED"; } echo ">No</option>
				</select></td>
			</tr>
			<th scope='row'><label for='xaff_reg'>";_e('Display map outlines', 'fr_reg_wp'); echo "</label></th>
			<td><select name='xaff_reg' id='xaff_reg'>
				<option value='true'"; if($aff_reg=="true"){ echo " SELECTED"; } echo ">Yes</option>
				<option value='false'"; if($aff_reg=="false"){ echo " SELECTED"; } echo ">No</option>
				</select></td>
			</tr>";
                    
                    
        echo "<tr><td>
		<input type='hidden' name='xidp' value='1'>
		</td><td><input type='submit' name='update_map_set' value='"; _e('Update', 'fr_reg_wp'); echo "' class='button button-primary' /></td></tr></table>
		</form></div><hr>";
		
		echo "<div class='inside'><form method=\"post\" action=\"".$_SERVER['REQUEST_URI']."\">";
			echo "<table class=\"form-table\">
			<tbody>
			<tr>
			<th scope='row'><label for='xmapfill'>";_e('Reset color', 'fr_reg_wp'); echo "</label></th>
			<td><input type='text' name='xmapfill' id='xmapfill' class='color' value='$mapfill'>
			<p class=\"description\">";_e('Set the background color of the entire map by default. <br> Be careful, the colors previously saved by region will be overwritten!', 'fr_reg_wp'); echo "</p>
			</td>
			</tr><tr><td>
		<input type='hidden' name='xidp' value='1'>
		</td><td><input type='submit' name='update_map_color' value='"; _e('Reset color', 'fr_reg_wp'); echo "' class='button button-primary' /></td></tr>
		</table></form></div><hr>";

echo "</div>";
echo "<div class=wrap>";
echo "<div class='icon32' id='icon-edit'><br /></div>
    <h2>";
	_e('Integration / Support', 'fr_reg_wp');
echo "</h2>";
echo "<h3>Shortcode</h3><p>"; _e('Use <strong>[frreg]</strong> shortcode  in your pages or articles to display your map.', 'fr_reg_wp'); echo "</p>";
echo "<h3>Support</h3><p><a href=\"https://blog.comersis.com/articles/wordpress/\" target='_blank'>"; _e('Comersis Wordpress maps User Manual', 'fr_reg_wp'); echo "</a></p>";
echo "</div>";
wp_enqueue_script('my-map', plugins_url('js/jscolor.js', __FILE__));
?>