<?php
    session_start();

    require("../includes/database_connect.php");


    if($_POST['type'] == 'username'){
        $username = $_POST['username'];
        $id = $_POST['id'];
        $sql = "UPDATE admin SET username='".$username."' where id='".$id."'";
    
        if ($conn->query($sql)) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = $username." updated successfully";
        } else {
            $_SESSION['error'] = "Error updating record: " . $conn->error;
        }
    }

    if($_POST['type'] == 'reset_password'){
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $id = $_POST['id'];

        if($password == $confirm_password){
            $hashed_password = sha1($password);

            $sql = "UPDATE admin SET password='".$hashed_password."' where id='".$id."'";
            if ($conn->query($sql)) {
                $_SESSION['success'] = $username." updated successfully";
            } else {
                $_SESSION['error'] = "Error updating record: " . $conn->error;
            }
        }else{
            $_SESSION['error'] = "Error : Password not same with confirm password ";
        }
    }   

    header('location: ../profile.php');

?>