<?php
include('security.php');

$connection = mysqli_connect("localhost","root","","mudidelivery");

if(isset($_POST['registerbtn']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];
    
    if($password === $cpassword)
    {
            $query = "INSERT INTO adminpanel (username,email,password) VALUES ('$username','$email','$password')";
            $query_run = mysqli_query($connection, $query);

        if($query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Admin Profile Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Admin Profile NOT Added";
            header('Location: register.php');
        }
    }
    else
    {
        $_SESSION['status'] = "Password and Confirm Password Does Not Match";
    header('Location: register.php');
    }
   }


if(isset($_POST['item']))
{
    
    $Package_Type = $_POST['Package'];
    $Origin_Service_Area = $_POST['der'];
    $Destination_Service_Area = $_POST['sarea'];
    $Quantity_Of_Product = $_POST['quality'];
    $Weight_of_Product= $_POST['weigh'];
    $Item_Type = $_POST['itype'];
    $Mode_Of_Transportation = $_POST['mode'];
    $Cognsignment = $_POST['Cognsignment'];
    $Estimated_Time_Of_Arrival = $_POST['time'];
    $Receiver_Name = $_POST['name'];
    $Receiver_Address = $_POST['address'];
    $Receiver_Email= $_POST['email'];
    $Receiver_Phone_Number = $_POST['phone'];
    $Sender_Name = $_POST['ame'];
    $Sender_Address = $_POST['saddress'];
    $Sender_Phone_Number = $_POST['sphone'];
    
    $Sender_Email = $_POST['mail'];
    $Status= $_POST['status'];
    $Message = $_POST['Message'];
    $In_Transit_At = $_POST['transit'];
    
//    if($password === $cpassword)
    {
            $query = "INSERT INTO users (Package,der,sarea,quality,weigh,itype,mode,Cognsignment,time,name,address,email,phone,ame,saddress,sphone,mail,status,Message,transit) VALUES ('$Package_Type','$Origin_Service_Area','$Destination_Service_Area','$Quantity_Of_Product','$Weight_of_Product','$Item_Type','$Mode_Of_Transportation','$Cognsignment','$Estimated_Time_Of_Arrival','$Receiver_Name','$Receiver_Address','$Receiver_Email','$Receiver_Phone_Number','$Sender_Name','$Sender_Address','$Sender_Phone_Number','$Sender_Email','$Status','$Message','$In_Transit_At')";
            $query_run = mysqli_query($connection, $query);

        if($query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Profile Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Admin Profile NOT Added";
            header('Location: register.php');
        }
    }
    
    {
    header('Location: register.php');
    }
   }







if(isset($_POST['updatebtn']))
{
    $id = $_POST['edit_id'];
    $Package_Type = $_POST['edit_Package'];
    $Origin_Service_Area = $_POST['edit_der'];
    $Destination_Service_Area = $_POST['edit_sarea'];
    $Quantity_Of_Product = $_POST['edit_quality'];
    $Weight_of_Product= $_POST['edit_weigh'];
    $Item_Type = $_POST['edit_itype'];
    $Mode_Of_Transportation = $_POST['edit_mode'];
    $Cognsignment = $_POST['edit_Cognsignment'];
    $Estimated_Time_Of_Arrival = $_POST['edit_time'];
    $Receiver_Name = $_POST['edit_name'];
    $Receiver_Address = $_POST['edit_address'];
    $Receiver_Email= $_POST['edit_email'];
    $Receiver_Phone_Number = $_POST['edit_phone'];
    $Sender_Name = $_POST['edit_ame'];
    $Sender_Address = $_POST['edit_saddress'];
    $Sender_Phone_Number = $_POST['edit_sphone'];
    
    $Sender_Email = $_POST['edit_mail'];
    $Status= $_POST['edit_status'];
    $Message = $_POST['edit_Message'];
    $In_Transit_At = $_POST['edit_transit'];
	
    
    $query = "UPDATE users SET Package='$Package_Type',der='$Origin_Service_Area',sarea='$Destination_Service_Area',quality='$Quantity_Of_Product',weigh='$Weight_of_Product',itype='$Item_Type',mode='$Mode_Of_Transportation',Cognsignment='$Cognsignment',time='$Estimated_Time_Of_Arrival',name='$Receiver_Name',address='$Receiver_Address',email='$Receiver_Email',phone='$Receiver_Phone_Number',ame='$Sender_Name',saddress='$Sender_Address',sphone='$Sender_Phone_Number',mail='$Sender_Email',status='$Status',Message='$Message',transit='$In_Transit_At'  WHERE id='$id' ";
    $query_run = mysqli_query($connection, $query);
    
    if($query_run)
    {
        $_SESSION['success'] = "Your Data is Updated";
        header('location: register.php');
    }
    else
    {
        $_SESSION['status'] = "Your Data is NOT Updated";
        header('location: register.php');    
    }
}


if(isset($_POST['account_blockbtn']))
{
    $id = $_POST['edit_id'];
    $first_name = $_POST['edit_first'];
    $last_name = $_POST['edit_lastname'];
    $mail= $_POST['edit_email'];
    $accountblock= $_POST['block'];
    
    
    
    
    $query = "UPDATE users SET first_name='$first_name',last_name='$last_name',mail='$mail', account_block='$accountblock'  WHERE id='$id' ";
    $query_run = mysqli_query($connection, $query);
    
    if($query_run)
    {
        $_SESSION['success'] = "Your Data is Updated";
        header('location: register.php');
    }
    else
    {
        $_SESSION['status'] = "Your Data is NOT Updated";
        header('location: register.php');    
    }
}




if(isset($_POST['delete_btn']))
{
    $id = $_POST['delete_id'];
    
    $query = "DELETE FROM users WHERE id='$id'";
    $query_run = mysqli_query($connection, $query);
    
    if($query_run)
    {
        $_SESSION['success'] = "Your Data is Deleted";
        header('location: register.php');
    }
    else
    {
        $_SESSION['status'] = "Your Data is NOT Deleted";
        header('location: register.php');
    }
}






if(isset($_POST['login_btn']))
{
    $email_login = $_POST['email']; 
    $password_login = $_POST['password'];
    
    $query = "SELECT * FROM adminpanel WHERE email='$email_login' AND password='$password_login'";
    $query_run = mysqli_query($connection, $query);
    
    if(mysqli_fetch_array( $query_run))
    {
        $_SESSION['username'] = $email_login;
        header('Location: index.php');
    }
    else
    {
        $_SESSION['status'] = 'Email id / Password is Invalid';
        header('Location: login.php');  
    }
}



if(isset($_POST['logout_btn']))
{
    session_destory();
    unset($_SESSION['username']);
}




if(isset($_POST['deposit_historytbtn']))
{
    
    $User_ID = $_POST['edit_id'];
    $Transaction_ID = $_POST['edit_trans'];
    $Date_and_Time = $_POST['edit_date'];
    $Amount_Deposited = $_POST['edit_amount'];
    $Status = $_POST['edit_status'];
    
    
   
            $query = "INSERT INTO deposit_history (User_ID,Transaction_ID,Date_and_Time,Amount_Deposited,Status) VALUES ('$User_ID','$Transaction_ID','$Date_and_Time','$Amount_Deposited','$Status')";
            $query_run = mysqli_query($connection, $query);

        if($query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Deposit History Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Deposit History NOT Added";
            header('Location: register.php');
        }
    }
//    else
//    {
//        $_SESSION['status'] = "Password and Confirm Password Does Not Match";
//    header('Location: register.php');
//    }
  
if(isset($_POST['tradebtn']))
{
    
    $User_ID = $_POST['edit_id'];
    $Transaction_ID = $_POST['edit_trans'];
    $Date_and_Time = $_POST['edit_date'];
    $Profit = $_POST['edit_amount'];
    $Status = $_POST['edit_status'];
    
    
   
            $query = "INSERT INTO trade (User_ID,Transaction_ID,Date_and_Time,Profit,Status) VALUES ('$User_ID','$Transaction_ID','$Date_and_Time','$Profit','$Status')";
            $query_run = mysqli_query($connection, $query);

        if($query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Trade History Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Trade History NOT Added";
            header('Location: register.php');
        }
    }
//    else
//    {
//        $_SESSION['status'] = "Password and Confirm Password Does Not Match";
//    header('Location: register.php');
//    }
  




?>