<?php
session_start();
require('../conn_auth.php');
include 'admin_outputfunction.php';
getAllInfo(); 
function clean($str) {
global $conn;
    $str = @trim($str);
    if (get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return $conn->real_escape_string($str);
}


switch($_REQUEST['action']){


case "Create new account":

$accnt_nmbr = $_POST['accnt_nmbr']; 
$accnt_type = $_POST['accnt_type'];
$cust_name = $_POST['cust_name'];
$email = $_POST['email'];
$phn_nmbr = $_POST['phn_nmbr'];
$sel_country = $_POST['sel_country']; 
$addrs = $_POST['addrs'];
$day_birth = $_POST['year_birth'] . "-" . $_POST['mnth_birth'] ."-" .  $_POST['day_birth'];
$cust_photo = $_FILES['cust_photo']['name'];
$uploaddir = "../upload/". $cust_photo;
$username = $_POST['username'];
$passwrd = $_POST['passwrd'];  

if(move_uploaded_file($_FILES['cust_photo']['tmp_name'], $uploaddir)){
$picxname = $cust_photo;   
}else{
$picxname = "Nill";
}
$muserdata = array(
            "fullname" => $cust_name, 
            "sexs" => "Nill", 
            "addrs" => $addrs, 
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "username" => $username, 
            "pass" => $passwrd, 
            "access_lvl" => 1,
            );
$insData = enterUserData("user_table", $muserdata, 1, 0);


$userID=  $conn->insert_id;


$mdata = array(
            "cust_name" => $cust_name, 
            "user_id" => $userID,
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "addrs" => $addrs, 
            "day_birth" => $day_birth, 
            "country" => $sel_country, 
            "acct_nmb" => $accnt_nmbr, 
            "accnt_type" => $accnt_type,
            "cust_photo" => $picxname, 
            "stat" => 'Active', 
            "date_create" => date("Y-m-d H:i:s")
            );
$insData = enterUserData("customer_info", $mdata, 1, 0);

if($insData == 1){
header("location:all-customers?msg=Successfully created"); 
}else{
header("location:new-customer?msg=Error creating"); 
}

  break;

case "Update active customer account":
 $userID= $_POST['user_id']; 
$accnt_nmbr = $_POST['accnt_nmbr']; 
$accnt_type = $_POST['accnt_type'];
$cust_name = $_POST['cust_name'];
$email = $_POST['email'];
$phn_nmbr = $_POST['phn_nmbr'];
$sel_country = $_POST['sel_country']; 
$addrs = $_POST['addrs'];
$day_birth = $_POST['day_birth'];
$cust_photo = $_FILES['cust_photo']['name'];
$uploaddir = "../upload/". $cust_photo;
$passwrd = $_POST['passwrd']; 
$profpix_name = $_POST['profpix_name']; 
$existnPhotourl = "../upload/". $profpix_name;
$cust_id = $_POST['cust_id']; 

   $mdata = array(
            "cust_name" => $cust_name, 
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "addrs" => $addrs, 
            "day_birth" => $day_birth, 
            "country" => $sel_country, 
            "acct_nmb" => $accnt_nmbr, 
            "accnt_type" => $accnt_type,
            "stat" => 'Active', 
            "date_create" => date("Y-m-d H:i:s")
            );
$updateAcctn = updateData("customer_info", $mdata, "customer_id",  $cust_id);

// Update new image 
if($cust_photo != ""){
if($profpix_name != "Nill" && $profpix_name != ""){
unlink($existnPhotourl);
}
 

if(move_uploaded_file($_FILES['cust_photo']['tmp_name'], $uploaddir)){
$picxname = $cust_photo;   
}else{
$picxname = "Nill";
}

   $mdata = array(
            "cust_photo" => $picxname, 
            );
$updatePhoto = updateData("customer_info", $mdata, "user_id",  $userID);
}


header("location:admin-edit-active_accnt?msg=success&id=" . $userID . "&custid=".  $cust_id); 
    break;

 case "Update customer account":
 $userID= $_POST['user_id']; 
$accnt_nmbr = $_POST['accnt_nmbr']; 
$accnt_type = $_POST['accnt_type'];
$cust_name = $_POST['cust_name'];
$email = $_POST['email'];
$phn_nmbr = $_POST['phn_nmbr'];
$sel_country = $_POST['sel_country']; 
$addrs = $_POST['addrs'];
$day_birth = $_POST['day_birth'];
$cust_photo = $_FILES['cust_photo']['name'];
$uploaddir = "../upload/". $cust_photo;
$passwrd = $_POST['passwrd']; 
$profpix_name = $_POST['profpix_name']; 
$existnPhotourl = "../upload/". $profpix_name;
$cust_id = $_POST['cust_id']; 


if($userID == 0){
$muserdata = array(
            "fullname" => $cust_name, 
            "sexs" => "Nill", 
            "addrs" => $addrs, 
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "username" => $accnt_nmbr, 
            "pass" => $passwrd, 
            "access_lvl" => 1,
            );
$insData = enterUserData("user_table", $muserdata, 1, 0);

$muserID =  $conn->insert_id;
   $mdata = array(
            "cust_name" => $cust_name, 
            "user_id" => $muserID,
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "addrs" => $addrs, 
            "day_birth" => $day_birth, 
            "country" => $sel_country, 
            "acct_nmb" => $accnt_nmbr, 
            "accnt_type" => $accnt_type,
            "stat" => 'Active', 
            "date_create" => date("Y-m-d H:i:s")
            );
$updateAcctn = updateData("customer_info", $mdata, "customer_id",  $cust_id);

}else{
$muserdata = array(
            "fullname" => $cust_name, 
            "addrs" => $addrs, 
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "username" => $accnt_nmbr, 
            "pass" => $passwrd, 
            );
$updateUsr = updateData("user_table", $muserdata, "user_id",  $userID);    
   $mdata = array(
            "cust_name" => $cust_name, 
            "email" => $email, 
            "phn_nmb" => $phn_nmbr, 
            "addrs" => $addrs, 
            "day_birth" => $day_birth, 
            "country" => $sel_country, 
            "acct_nmb" => $accnt_nmbr, 
            "accnt_type" => $accnt_type,
            "stat" => 'Active', 
            "date_create" => date("Y-m-d H:i:s")
            );
$updateAcctn = updateData("customer_info", $mdata, "user_id",  $userID);
}




// Update new image 
if($cust_photo != ""){
if($profpix_name != "Nill" && $profpix_name != ""){
unlink($existnPhotourl);
}
 

if(move_uploaded_file($_FILES['cust_photo']['tmp_name'], $uploaddir)){
$picxname = $cust_photo;   
}else{
$picxname = "Nill";
}
   $mdata = array(
            "cust_photo" => $picxname, 
            );
$updatePhoto = updateData("customer_info", $mdata, "customer_id",  $cust_id);
}

if($userID==0){
$redir_userID = $muserID; 
}else{
$redir_userID = $userID; 
}

header("location: admin-view-account?msg=success&id=". $redir_userID); 
    break; 

case "Delete customers":
 $id = $_GET['id'];    
 $delCust = $conn->query("delete from customer_info where user_id = '$id'") or die($conn->error); 
 $delUser = $conn->query("delete from user_table where user_id = '$id'") or die($conn->error);
   echo 1;
    break;

  case "Delete all transactions":
    $id = $_GET['id'];
    $acctnmbr = $_GET['acctnmbr']; 

    $delCust = $conn->query("delete from credit_info where transact_id = '$id'") or die($conn->error); 
    header("location:customer-transct-details?msg=success&acctnbr=". $acctnmbr); 

        break;  

   case "Delete pending trasaction":
$id = $_GET['id'];    
 $delCust = $conn->query("delete from transfer_info where trans_id  = '$id'") or die($conn->error);
header("location:view-pending-transfer?acctnbr=". $_GET['accnt']); 
        break; 

case "Delete pending customer":
   $id = $_GET['id'];    
 $delCust = $conn->query("delete from customer_info where customer_id = '$id'") or die($conn->error);  
    echo 1; 
    break;

case "Delete transfer pin":
   $delPin = $conn->query("delete from transfer_pin where tr_pin_id = '" . $_GET['id'] . "'") or die($conn->error); 
   echo 1; 
break;

case "Delete ticket":
   $delPin = $conn->query("delete from contact_us where contact_id = '" . $_GET['id'] . "'") or die($conn->error); 
   echo 1;  
    break;
  case "Mark ticket as seen":
  $updateTick = $conn->query("update contact_us set stat = 'Answered' where contact_id = '" . $_GET['id'] . "'") or die($conn->error);      
  echo 1; 
  break;  

 case "Credit user account":
$accnt_nmbr = $_POST['accnt_nmbr']; 
$trans_type = $_POST['trans_type']; 
$narratn = $_POST['narratn'];
$day_birth = $_POST['year_birth'] . "-" . $_POST['mnth_birth'] ."-" .  $_POST['day_birth'];
$trans_ref = substr(md5(rand()*time()), 0, 10);
$amt_cred = $_POST['amt_cred']; 

$selAccnt = $conn->query("select * from customer_info where acct_nmb  = '$accnt_nmbr'") or die($conn->error);
  if(!$selAccnt->num_rows){
header("location:credit-user?msg=error");
  }else{
 $rowCust =$selAccnt->fetch_assoc();    
 
if($trans_type == "Credit"){
  $descr = $narratn . "<br>with ref" . $trans_ref;
 $credamt =  $amt_cred;
 $debitamt = 0; 
}else{
 $descr = $narratn . "<br>with ref" . $trans_ref; 
$credamt =  0;
 $debitamt = $amt_cred;
}


  $mdata = array(
            "transn_ref" =>  $trans_ref, 
            "transferId" =>  0, 
            "payee_name" =>  "self", 
            "payee_accnt" => "self",
            "cust_name" => $rowCust['cust_name'],
            "accnt_nmb" => $accnt_nmbr,
            "trans_type" => $trans_type,
            "narratn" => $descr, 
            "amt_cred" => $credamt, 
            "amt_dep" => $debitamt, 
            "dat_pay" => $day_birth
            );
$insData = enterUserData("credit_info", $mdata, 1, 0);
//Udate transfer as complete
header("location:view-credit-info?msg=success&acctnbr=". $accnt_nmbr);
}
    break;
case "Create transfer pin":
   $mdata = array(
            "pin" =>  $_POST['tr_pin'], 
            "accnt" =>  $_POST['accnt_nmbr'],
            "tr_name" => $_POST['tr_name'], 
            "stage_tr" => $_POST['stage_tr'],
            "stat" => 'Active', 
            "date_cr" => date("Y-m-d H:i:s")
            );
$insData = enterUserData("transfer_pin", $mdata, 1, 0);
//Udate transfer as complete
header("location:manage-pin?msg=success");   
break;
case "Create self help":
    $mdata = array(
            "title" =>  $_POST['title'], 
            "descr" =>  $_POST['descr'],
            "stat" => 'Active', 
            "date_cr" => date("Y-m-d H:i:s")
            );
$insData = enterUserData("myself_hep", $mdata, 1, 0);
//Udate transfer as complete
header("location:self-help-log?msg=success"); 
break;
case "Set Currency":
    $accnt_nmbr = $_POST['accnt_nmbr']; 
    $cur_type = $_POST['cur_type'];
    $mdata = array(
            "accnt_nmb" =>  $accnt_nmbr,
            "curr_type" => $cur_type, 
            );
$insData = enterUserData("curr_type", $mdata, 1, 0);
header("location:change-currency?msg=success"); 
    break;
case "Create Notification Users":
    $mdata = array(
            "user_id" => $_POST['user_id'],
            "title" =>  $_POST['title'], 
            "descr" =>  $_POST['msgBody'],
            "stat" => 'Pending', 
            "date_cr" => date("Y-m-d H:i:s")
            );
$insData = enterUserData("user_notificatn", $mdata, 1, 0);
 header("location:manage-notification-log"); 
  break;

case "Update Notifications sent":
$notifyID = $_POST['notifyID']; 

$mdata = array(
            "title" =>  $_POST['title'], 
            "descr" =>  $_POST['msgBody'],
            );
$updateNotf = updateData("user_notificatn", $mdata, "notifcatn_id",  $notifyID);
header("location:edit-notification?msg=success&id=". $notifyID); 
    break;
case "Delete notification":
$id = $_GET['id']; 
$delNotfn = $conn->query("delete from user_notificatn where notifcatn_id = '$id'") or die($conn->error);         
header("location:manage-notification-log?msg=success"); 
        break;    
default: 
echo "No action has been requested.";
break; 
  }