<?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 sponsor where name like '$name' && id!='$id'");
       if(mysql_num_rows($query)>0){
           return true;
       }
       return false;
   }
   
   function add(){ 
        $name=$_POST["name"];
        $affiliate_id=$_POST["affiliate_id"];
        $affiliate_manager=$_POST["affiliate_manager"];
        $platform=$_POST["platform"];
        $login_page=$_POST["login_page"];
        $login=$_POST["login"];
        $password=$_POST["password"];
        $api_key=$_POST["api_key"];
		$api_host=$_POST["api_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 sponsor VALUES (NULL, '$name', '$login_page', '$login', '$password', '$platform', '$affiliate_id', '$affiliate_manager', '$api_key','$active', '$date_add', '$date_set','$api_host');");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }
   
   function show(){ 
      $list=array();
      $data= bd::query("SELECT id,name,affiliate_id,affiliate_manager,platform,active FROM sponsor");
      while ($row = mysql_fetch_object($data)) {
          $list[]=$row;
      }
      echo json_encode($list);
   }
   
   function get(){ 
      $data = bd::query("SELECT * FROM sponsor WHERE id='{$_GET["id_sponsor"]}'");
      $row = mysql_fetch_array($data);
      return $row;
   }
   
   function set(){ 
      $name=$_POST["name"];
      $affiliate_id=$_POST["affiliate_id"];
      $affiliate_manager=$_POST["affiliate_manager"];
      $platform=$_POST["platform"];
      $login_page=$_POST["login_page"];
      $login=$_POST["login"];
      $password=$_POST["password"];
      $api_key=$_POST["api_key"];
	  $api_host=$_POST["api_host"];
      $date_set=date('Y-m-d h:i:s');
      $ok=0;
      if(!verify_name($_POST["id"],$name)){
          $rep=bd::query("UPDATE sponsor SET name='$name', login_page='$login_page', login='$login', password='$password', platform='$platform', affiliate_id='$affiliate_id', affiliate_manager='$affiliate_manager', api_key='$api_key', api_host='$api_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 sponsor SET active= '1' WHERE id='{$_GET["id"]}'");
   }
   
   function set_active_off(){ 
       bd::query("UPDATE sponsor SET active= '0' WHERE id='{$_GET["id"]}'");
   }

