<?php
    include_once '../../account/session.php';
    include_once '../../../scripts/bd.php';
    
    if($_SESSION["type"] == 'it') {
      die("404 not found: <a href='/app/manager/offer/show.php'>Go back</a>");
    }
    
    if(isset($_GET['action'])){
        $action=$_GET['action'];
        if($action=='add')add();
        if($action=='show')show();
        if($action=='get')get();
        if($action=='set')set();
        if($action=='set_active_on')set_active_on();
        if($action=='set_active_off')set_active_off();
   }
   

   function verify_name($id,$name){
       $query = bd::query("SELECT * FROM data_isp where name like '$name' && id!='$id'");
       if(mysql_num_rows($query)>0){
           return true;
       }
       return false;
   }
   
   function add(){ 
        $name=$_POST["name"];
        $extensions=$_POST["extensions"];
        $hard_bounce_code=addslashes($_POST["hard_bounce_code"]);
        $imap_host=addslashes($_POST["imap_host"]);
        $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)){
            $rep=bd::query("INSERT INTO data_isp VALUES (NULL, '$name', '$extensions', '$hard_bounce_code', '$imap_host', '$active', '$date_add', '$date_set');");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }
   
   function show(){ 
      $list=array();
      $data= bd::query("SELECT id,name,regex,active FROM data_isp");
      while ($row = mysql_fetch_object($data)) {
          $list[]=$row;
      }
      echo json_encode($list);
   }
   
   function get(){ 
      $data = bd::query("SELECT * FROM data_isp WHERE id='{$_GET["id_isp"]}'");
      $row = mysql_fetch_array($data);
      return $row;
   }
   
   function set(){ 
      $name=$_POST["name"];
      $extensions=$_POST["extensions"];
      $hard_bounce_code=addslashes($_POST["hard_bounce_code"]);
      $imap_host=addslashes($_POST["imap_host"]);
      $date_set=date('Y-m-d h:i:s');
      $ok=0;
      if(!verify_name($_POST["id"],$name)){
          $rep=bd::query("UPDATE data_isp SET name='$name', regex='$extensions', hard_bounce_code='$hard_bounce_code', imap_host='$imap_host', date_set='$date_set' WHERE id='{$_POST["id"]}'");
          if(!$rep){
             $ok=2; 
          }
      }else{
          $ok=1; 
      }
      echo $ok;
   }
   
   function set_active_on(){ 
       bd::query("UPDATE data_isp SET active= '1' WHERE id='{$_GET["id"]}'");
   }
   
   function set_active_off(){ 
       bd::query("UPDATE data_isp SET active= '0' WHERE id='{$_GET["id"]}'");
   }

