<?php
    include_once '../../account/session.php';
    include_once '../../../scripts/bd.php';
    
    if(isset($_GET['action'])){
        $action=$_GET['action'];
        if($action=='add')add();
        if($action=='add_groups')add_groups();
        if($action=='show')show();
        if($action=='get')get();
        if($action=='set')set();
        if($action=='delete_vmta')delete_vmta();
        if($action=='set_active_on')set_active_on();
        if($action=='set_active_off')set_active_off();
        if($action=='get_servers')get_servers();
        if($action=='get_mainIP')get_mainIP();
   }
   
   function verify_name($id,$name,$id_server){
       $query = bd::query("SELECT * FROM server_vmta where name like '$name' && id_server='$id_server' && id!='$id'");
       if(mysql_num_rows($query)>0){
           return true;
       }
       return false;
   }

   function get_mainIP($id)
   {
    if(isset($_GET["id_server"]) AND !empty($_GET["id_server"]) AND is_numeric($_GET["id_server"])) {
      $sql = bd::query("SELECT main_ip FROM server WHERE id = '".$_GET["id_server"]."'");
      $data = mysql_fetch_array($sql);
      echo $data["main_ip"];
    }
   }

   function add(){ 
        $id_server=$_POST["id_server"];
        $name=trim($_POST["name"]);
        $block_ip=$_POST["block_ip"];
        $address_ip=$_POST["address_ip"];
        $host_name=$_POST["host_name"];
        $active=0;
        $date_add=date('Y-m-d h:i:s');
        $date_set=date('Y-m-d h:i:s');
        
        $ok=0;
        if(!verify_name('0',$name,$id_server)){
            $rep=bd::query("INSERT INTO server_vmta VALUES (NULL, '$id_server', '$name', '$block_ip', '$address_ip', '$host_name', '0', '$active', '$date_add', '$date_set');");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }

   function add_groups(){ 
       $id_server = $_POST["server1"];
       $vmtas = trim($_POST["vmta_name"]);
       $block_ip = $_POST["block_ips"];
       $ips = trim($_POST["ips"]);
       $hosts = trim($_POST["hosts"]);

       $vmtasTab = explode(chr(10),$vmtas); 
       $ipsTab = explode(chr(10),$ips); 
       $hostsTab = explode(chr(10),$hosts); 

      $active=0;
      $date_add=date('Y-m-d h:i:s');
      $date_set=date('Y-m-d h:i:s');
        

       if(count($vmtasTab) == count($ipsTab) AND count($vmtasTab) == count($hostsTab)) {
        for ($i=0; $i <= count($vmtasTab)-1; $i++) {
          $rep=bd::query("INSERT INTO server_vmta VALUES (NULL, '$id_server', '$vmtasTab[$i]', '$block_ip', '$ipsTab[$i]', '$hostsTab[$i]', '0', '$active', '$date_add', '$date_set');");
        }
        echo "OK !";
      } else {
        echo "Please check from !";
      }
   }

   function delete_vmta()
   {
      if(isset($_GET['id_vmta']) AND !empty($_GET['id_vmta'])) {
        $sql = bd::query("SELECT id FROM server_vmta WHERE id = '".$_GET['id_vmta']."'");
        if(mysql_num_rows($sql)) {
          bd::query("DELETE FROM server_vmta WHERE id = '".$_GET['id_vmta']."'");
        }
        header("Location: show.php");
      }
   }
   
   function show(){
      $list=array();
      if($_GET["id_server"]==0){
          $data= bd::query("SELECT V.id,S.name as 'server',V.name,V.block_ip,V.address_ip,V.host_name,V.active FROM server_vmta V,server S where S.id = V.id_server");
      }else{
          $data= bd::query("SELECT V.id,S.name as 'server',V.name,V.block_ip,V.address_ip,V.host_name,V.active FROM server_vmta V,server S WHERE S.id = V.id_server AND V.id_server='{$_GET["id_server"]}'");
      }
      while ($row = mysql_fetch_object($data)) {
          $list[]=$row;
      }
      echo json_encode($list);
   }
   
   function get(){ 
      $data = bd::query("SELECT * FROM server_vmta WHERE id='{$_GET["id_vmta"]}'");
      $row = mysql_fetch_array($data);
      return $row;
   }
   
    function set(){ 
       $id_server=$_POST["id_server"];
       $name=trim($_POST["name"]);
       $block_ip=$_POST["block_ip"];
       $address_ip=$_POST["address_ip"];
       $host_name=$_POST["host_name"];
       $date_set=date('Y-m-d h:i:s');
       
       $ok=0;
        if(!verify_name($_POST["id"],$name,$id_server)){
            $rep=bd::query("UPDATE server_vmta SET id_server='$id_server',name='$name', block_ip='$block_ip', address_ip='$address_ip', host_name='$host_name', date_set='$date_set' WHERE id='{$_POST["id"]}'");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }
   
   function set_active_on(){ 
       bd::query("UPDATE server_vmta SET active= '1' WHERE id='{$_GET["id"]}'");
   }
   
   function set_active_off(){ 
       bd::query("UPDATE server_vmta SET active= '0' WHERE id='{$_GET["id"]}'");
   }
   
   function get_servers(){
      $data=array();
      $query = bd::query("SELECT id,name FROM server");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   