<?php
$addon_path="../../";


include $addon_path."includes/config.php";
include $addon_path."includes/database.php";
include $addon_path."includes/keyword.php";
include $addon_path."includes/function.php";
include $addon_path."includes/session.php";




?>
<html>
<head>
<title><?php echo $sys_browser_title; ?></title>
<LINK REL="SHORTCUT ICON" HREF="images/cloud.ico" >
<meta http-equiv=content-type content="text/html; charset=GB2312">




<LINK href="<?php print $addon_path."theme/desktop/".$cssPath1; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath2; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath3; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath4; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath5; ?>" rel=stylesheet media="(min-width: 641px)">

<LINK href="<?php print $addon_path."theme/mobile/".$cssPath1; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath2; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath3; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath4; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath5; ?>" rel=stylesheet media="(max-width: 640px)">

<LINK href="<?php print $addon_path.$fontPath; ?>" rel=stylesheet>
	
	

<link href="plugin/menu_assets/styles.css" rel="stylesheet" type="text/css">
    
<script language="javascript" src="<?php print $addon_path.$jsPath1; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath2; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath3; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath4; ?>"></script>
</head>
<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onload="mon_on_load_init()">


<script language='javascript'>
	
var offset_drag_x;	
var offset_drag_y;		

mon_dnd_event=false;

function findHighestZIndex(elem)
{
  var elems = document.getElementsByTagName(elem);
  var highest = 0;
  for (var i = 0; i < elems.length; i++)
  {
    var zindex=document.defaultView.getComputedStyle(elems[i],null).getPropertyValue("z-index");
    if ((zindex > highest) && (zindex != 'auto'))
    {
      highest = zindex;
    }
  }
  return highest;
}


function mouseX (e) {
  if (e.pageX) {
    return e.pageX;
  }
  if (e.clientX) {
    return e.clientX + (document.documentElement.scrollLeft ?
      document.documentElement.scrollLeft :
      document.body.scrollLeft);
  }
  return null;
}

function mouseY (e) {
  if (e.pageY) {
    return e.pageY;
  }
  if (e.clientY) {
    return e.clientY + (document.documentElement.scrollTop ?
      document.documentElement.scrollTop :
      document.body.scrollTop);
  }
  return null;
}



function allowDrop(ev)
{
	ev.preventDefault();
	
}

function drag(ev)
{
	
	ev.dataTransfer.setData("drag_object",ev.target.id);
	
	var mouse_x = mouseX(ev);
	var mouse_y = mouseY(ev);
	
	
        
	offset_drag_x = mouseX(ev) - ev.target.style.left.replace(/[^-\d\.]/g, '');
        offset_drag_y = mouseY(ev) - ev.target.style.top.replace(/[^-\d\.]/g, '');
	
	
	
	
	//offset_drag_x = ev.target.style.left - mouse_x;
	//offset_drag_y = ev.target.style.top-mouse_y;
	
	//alert(mouse_x + "" + ev.target.style.left );
	
	
	
	mon_dnd_event=true;
	
	
}



function drop(ev)
{
	
	if (mon_dnd_event) {
			
		
                //if (ev.target.id=='floor_object') {
                
		ev.preventDefault();
		var data = ev.dataTransfer.getData("drag_object");
		//ev.target.appendChild(document.getElementById(data));
                
                document.getElementById('floor_object').appendChild(document.getElementById(data));
                
                document.getElementById(data).style.zIndex = findHighestZIndex(document.getElementById('floor_object'))*1 + 10;
		//ev.target.insertBefore(document.getElementById(data), ev.target.firstChild);
		mon_dnd_event=false;
		
		
		
		document.getElementById(data).style.left= mouseX(ev) - offset_drag_x;
		document.getElementById(data).style.top = mouseY(ev) - offset_drag_y;
		
		
                
               // document.getElementById(document.getElementById(data).id+"_content").innerHTML= mouseX(ev) + ", " +  mouseY(ev);
                
                
		eval("document.form_floor_plan."+document.getElementById(data).id+"_value_x.value=mouseX(ev) - offset_drag_x; ");
                eval("document.form_floor_plan."+document.getElementById(data).id+"_value_y.value=mouseY(ev) - offset_drag_y; ");
                
                
                
                
                //}
		
		
	}
		
}


	
function dropBefore(ev)
{
    
    /*
	if (mon_dnd_event) {
		ev.preventDefault();
		var data = ev.dataTransfer.getData("drag_object");
		ev.target.parentNode.insertBefore(document.getElementById(data), ev.target);
		//alert(ev.target.parentNode.id);
		mon_dnd_event=false;
	}
	
	*/
}



function mon_obj_width_minus(v_i)
{
    var new_width = document.getElementById("object_"+v_i+"_content").style.width.replace(/[^-\d\.]/g, '')*1 - 4;
    document.getElementById("object_"+v_i+"_content").style.width = new_width
    eval("document.form_floor_plan.object_"+v_i+"_value_width.value = new_width; ");
                
                
}

function mon_obj_width_plus(v_i)
{
    
    var new_width =  document.getElementById("object_"+v_i+"_content").style.width.replace(/[^-\d\.]/g, '')*1 + 4;
    document.getElementById("object_"+v_i+"_content").style.width = new_width
    eval("document.form_floor_plan.object_"+v_i+"_value_width.value = new_width; ");
}



function mon_obj_height_minus(v_i)
{
    
    
    var new_height = document.getElementById("object_"+v_i+"_content").style.height.replace(/[^-\d\.]/g, '')*1 - 4;
    document.getElementById("object_"+v_i+"_content").style.height =new_height;
    eval("document.form_floor_plan.object_"+v_i+"_value_height.value = new_height; ");
}

function mon_obj_height_plus(v_i)
{
    var new_height = document.getElementById("object_"+v_i+"_content").style.height.replace(/[^-\d\.]/g, '')*1 + 4;
    document.getElementById("object_"+v_i+"_content").style.height =new_height;
    eval("document.form_floor_plan.object_"+v_i+"_value_height.value = new_height; ");
}


function mon_obj_to_circle(v_i)
{
    document.getElementById("object_"+v_i+"_content").style.borderRadius = "200px";
    eval("document.form_floor_plan.object_"+v_i+"_shape.value = 'CIRCLE'; ");
}


function mon_obj_to_square(v_i)
{
    document.getElementById("object_"+v_i+"_content").style.borderRadius = "2px";
    eval("document.form_floor_plan.object_"+v_i+"_shape.value = 'SQUARE'; ");
}




function isCollide(a, b) {
    return !(
        ((a.y + a.height) < (b.y)) ||
        (a.y > (b.y + b.height)) ||
        ((a.x + a.width) < b.x) ||
        (a.x > (b.x + b.width))
    );
}

</script>

<style>
.mon_table_object{
	
	margin:0px;
	border:0px dashed #ff0000;
	color:#ffffff;
	position:absolute;
	float:left;
	
        
	
}
	
        
        
.mon_table_object_content{
	
	
	border:0px solid #000000;
	width:50px;
        height:50px;
	padding:5px;
	color:#000;
	background:#ECF0F1;
	text-align: center;
	border-radius:5px;
        cursor:move;
        box-shadow: 0px 0px 1px 1px #ccc;
        border-radius:2px;
	
}



.mon_table_object_setting_width{
    border:0px solid #c0c0c0;
    min-width:50px;
    color:#bdc3c7;
}


.mon_table_object_setting_height{
    
    
    border:0px solid #c0c0c0;
    min-height:50px;
    color:#bdc3c7;
}



.mon_floor_plan
{
	
	border:0px solid #000000;
	width:1000px;
	height:500px;
}
	
</style>
<?php

 
echo "<form name='form_floor_plan' action='' method='POST'>";
 //command button:
    echo "<div style='text-align:left;position:absolute;background: rgba(189, 195, 199,0.3);border-radius:5px;padding:5px 1% 5px 1%;margin:5px 2% 5px 2%;;width:94%;'>";
    
    
    		echo "Floor: <select name='floor' class='dropdown dropdown-auto' onchange=\"window.location='?floor='+this.value\">";
	    		
			
			
			$query_floor = "select * from t_".$key_table_prefix."_floor_plan
			 			order by flr_warehouse_code,flr_code  ";
			$result_floor = mysqli_query($dbconn,$query_floor);
			
			while($line_floor = mysqli_fetch_array($result_floor,MYSQLI_ASSOC))
			{
			    
			    if($_REQUEST["floor"]=="")
			    {
				   $_REQUEST["floor"]=$line_floor["flr_code"];
			    }
			    
			    if($line_floor["flr_code"]==$_REQUEST["floor"])
			    {
				    echo "<option value='".$line_floor["flr_code"]."' selected >".$line_floor["flr_name"]."</option>";
				    
				   
			    }
			    else{
				    echo "<option value='".$line_floor["flr_code"]."' >".$line_floor["flr_name"]."</option>";
				   
			    }
			    
			    
			}
			
			
    		echo "</select>";
    
    
        
           
            
            
            
        echo "<input type=button class='button button-blue' value='Save Layout' style='float:right;' onclick=\"document.form_floor_plan.submit();\" >";
    echo "</div>"; 
  echo "<div class='mon_floor_plan' style='relative;' id='floor_object' ondrop=\"drop(event)\" ondragover=\"allowDrop(event)\" >";
    
    
    
    
    
    $obj=0;
    $query_location="select * from t_".$key_table_prefix."_storage_location where loc_floor_code = '".$_REQUEST["floor"]."' ";
    $result_location= mysqli_query($dbconn,$query_location);
    while($line_location = mysqli_fetch_array($result_location,MYSQLI_ASSOC))
    {
        
        
        $i=$line_location["loc_id"];
        $obj++;
        
        
        
        
        if ($_SERVER[REQUEST_METHOD] == 'POST')
        {
            
		  
            $query_update="update t_".$key_table_prefix."_storage_location
                        set loc_x = '".($_REQUEST["object_".$i."_value_x"]+0)."' ,
                        loc_y = '".($_REQUEST["object_".$i."_value_y"]+0)."' ,
                        
                        loc_width = '".($_REQUEST["object_".$i."_value_width"]+0)."' ,
                        loc_height = '".($_REQUEST["object_".$i."_value_height"]+0)."' ,
                        
                        loc_shape = '".$_REQUEST["object_".$i."_shape"]."' 
                        where loc_id = '".$line_location["loc_id"]."' ";
            $result_update = mysqli_query($dbconn,$query_update);
		  
		  
		  
            
        }
        else{
            
            
            $_REQUEST["object_".$i."_value_x"]=$line_location["loc_x"];
            $_REQUEST["object_".$i."_value_y"]=$line_location["loc_y"];
            $_REQUEST["object_".$i."_value_height"]=$line_location["loc_height"];
            $_REQUEST["object_".$i."_value_width"]=$line_location["loc_width"];
            $_REQUEST["object_".$i."_shape"]=$line_location["loc_shape"];
            
            
        }
        
        
        
        
       
        
        
        if($_REQUEST["object_".$i."_value_x"]=="")
        {
            $_REQUEST["object_".$i."_value_x"]=100+$obj*10;
        }
        
        
        if($_REQUEST["object_".$i."_value_y"]=="")
        {
            $_REQUEST["object_".$i."_value_y"]=100+$obj*10;
        }
        
        
        
        if($_REQUEST["object_".$i."_value_width"]=="")
        {
            $_REQUEST["object_".$i."_value_width"]=50;
        }
         if($_REQUEST["object_".$i."_value_height"]=="")
        {
            $_REQUEST["object_".$i."_value_height"]=50;
        }
        
        
        
        if($_REQUEST["object_".$i."_shape"]=="")
        {
            $_REQUEST["object_".$i."_shape"]="SQUARE";
        }
        
        
        
        echo "<div id='object_".$i."' style='left:".$_REQUEST["object_".$i."_value_x"]."px;top:".$_REQUEST["object_".$i."_value_y"]."px;' draggable='true'
            ondragstart='drag(event);' ondrop=\"dropBefore(event)\" class='mon_table_object'
            onmouseover=\"\"  onmouseout=\"\"
            >";

        echo "<table border=0 cellspacing=0 cellpadding=0  align=left >";
            echo "<tr>";
                echo "<td valign=top align=left >";
                    //main content area:
                    echo "<div id='object_".$i."_content' class='mon_table_object_content'
                            style='width:".($_REQUEST["object_".$i."_value_width"])."px;height:".($_REQUEST["object_".$i."_value_height"])."px;font-size:8px;";
                            
                            if($_REQUEST["object_".$i."_shape"]=="CIRCLE")
                            {
                                echo "border-radius:200px;";
                            }
                            else{
                                
                                echo "border-radius:2px;";
                            }
                            echo "'>";
                        echo "".$line_location["loc_name"]."";
                    echo "</div>";
                    
                    
                    echo "<input type=hidden name='object_".$i."_shape' value='".$_REQUEST["object_".$i."_shape"]."' >";
                    
                    echo "<input type=hidden name='object_".$i."_value_x' value='".$_REQUEST["object_".$i."_value_x"]."' >";
                    echo "<input type=hidden name='object_".$i."_value_y' value='".$_REQUEST["object_".$i."_value_y"]."' >";
                    
                    echo "<input type=hidden name='object_".$i."_value_width' value='".$_REQUEST["object_".$i."_value_width"]."' >";
                    echo "<input type=hidden name='object_".$i."_value_height' value='".$_REQUEST["object_".$i."_value_height"]."' >";
                    
                    
                    
                    
                echo "</td>";
                
                echo "<td class='mon_table_object_setting_height' valign=top >";
                    //height control
                        echo "<span class='fa fa-arrows-v fa-fw'></span><br>";
                        echo "<span class='fa fa-minus-square fa-fw' onclick=\"mon_obj_height_minus(".$i.")\" ></span><br>";
                        echo "<span class='fa fa-plus-square fa-fw'  onclick=\"mon_obj_height_plus(".$i.")\"></span>";
                echo "</td>";
            echo "</tr>";
            
            echo "<tr>";
                echo "<td class='mon_table_object_setting_width' align=left >";
                    //width control
                        echo "<span class='fa fa-arrows-h fa-fw'></span>";
                        echo "<span class='fa fa-minus-square fa-fw' onclick=\"mon_obj_width_minus(".$i.")\" ></span>";
                        echo "<span class='fa fa-plus-square fa-fw' onclick=\"mon_obj_width_plus(".$i.")\"></span>";
                        
                        echo "<br>";
                        echo "<span class='fa fa-circle fa-1x' style='margin:1px;' onclick=\"mon_obj_to_circle(".$i.")\" ></span>";
                        echo "<span class='fa fa-square fa-1x' style='margin:1px;'onclick=\"mon_obj_to_square(".$i.")\"></span>";
                echo "</td>";
                
                echo "<td>";
                    //empty
                echo "</td>";
            echo "</tr>";
            
            
        echo "</table>";
        
        
            
            
            
        
        
        
           
            
           
        
        echo "</div>";
    }
        
        
        
        
        
        
    echo "</div>";
    
                            

echo "</form>";

?>








</body>
</html>