<?php 
// Get path
$phpver = (float)phpversion();
if ($phpver >= 7.0) {
   $prev_path = dirname(__DIR__, 3);
 }
 
include($prev_path.'/functions/pagename.php');
include($prev_path.'/functions/'.$session);
Session::checkSession_app();
include($prev_path.'/db/db.cnf.php');
include($prev_path.'/db/database.Class.php');
$db = new database();


function test_input($data) { //filter value function
    $db = new database();
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = strtolower($data);
    $data = mysqli_real_escape_string($db->link, $data);
    return $data;
}

$evp_options_sql = "SELECT * FROM evp_options WHERE option_name = 'app_url'";
$evp_options_read = $db->select($evp_options_sql);
if ($evp_options_read) {
    $evp_options_check = mysqli_num_rows($evp_options_read);
    if ($evp_options_check > 0) {
		$evp_options = $evp_options_read->fetch_assoc();
        $get_script_url =  $evp_options['option_value'];
	}
}

$error = false;
$message = '';
$action_std = false;
$action_cat = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if(isset($_POST['start-btn']))  {
          $action_std = true;
          $action_cat = 'exicute';
          $target_id = test_input($_POST['target_id']);
          $user_id = $_SESSION['id'];
          $user_update_sql = "UPDATE evp_campaign_list SET c_status = 'processing' WHERE c_id = '$target_id' and c_user_id = '$user_id'";
          $user_insert_read = $db->update($user_update_sql);
          if ($user_insert_read) {
			$start_mail = 'true';
			
            $error = false;

	        $message = "Campaign started!";
          }else{
            $error = true;
            $message = "Database connection error!";
          }
        }elseif (isset($_POST['stop-btn']))  {
          $action_std = true;
          $action_cat = 'exicute';
          $target_id = test_input($_POST['target_id']);
          $user_id = test_input($_POST['user_id']);
          $user_update_sql = "UPDATE evp_campaign_list SET c_status = 'pending' WHERE c_id = '$target_id' and c_user_id = '$user_id'";
          $user_insert_read = $db -> update($user_update_sql);
          if ($user_insert_read) {
            $error = false;
            $message = "Campaign has been Stop";
          }else{
            $error = true;
            $message = "Database connection error!";
          }
        }elseif (isset($_POST['del-btn']))  {
          $action_std = true;
          $action_cat = 'exicute';
          $target_id = test_input($_POST['target_id']);
          $user_id = test_input($_POST['user_id']);
          $user_update_sql = "DELETE e_data.*, e_list.* 
								FROM evp_campaign_list e_data 
								LEFT JOIN evp_send_email_list e_list
								ON e_list.list_name = e_data.c_name 
								WHERE e_data.c_id = '$target_id' and
								e_data.c_user_id = '$user_id'";
          $user_insert_read = $db->update($user_update_sql);
          if ($user_insert_read) {
            $error = false;
            $message = "Campaign Deleted!";
          }else{
            $error = true;
            $message = "Database connection error!";
          }
        }else{
          header("Location: $get_script_url/".$error_404_page);
        }
        if($action_std && !empty($action_cat)){
          Session::set("action_cat", $action_cat);
          Session::set("action", $error);
          Session::set("action_message", $message);
          header("Location: $get_script_url/".$send_mail_page);
        }else{
          header("Location: $get_script_url/".$error_404_page);
        }
      }else{
        header("Location: $get_script_url/".$error_404_page);
      }
?>