<?php
include("dbconfig.php");
if(isset($_POST['createFullName'])){
    mysqli_query($db,"INSERT INTO billlogin(Username,Password,Name) VALUES('$_POST[Username]','$_POST[Password]','$_POST[createFullName]')");
    echo "<script>alert('User Added!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewUsers.php";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}

if(isset($_POST['StaffName'])){
    $staffid=$_POST['StaffId'];
    $name=$_POST['StaffName'];
mysqli_query($db,"UPDATE staffdetails SET Name='$name' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}
if(isset($_POST['StaffGroup'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET StaffGroup='$_POST[StaffGroup]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}
if(isset($_POST['StaffSpec'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Specification='$_POST[StaffSpec]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}
if(isset($_POST['StaffFHN'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET FatherHusband='$_POST[StaffFHN]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}

if(isset($_POST['StaffContact'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Contact='$_POST[StaffContact]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}
if(isset($_POST['StaffEmContact'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET EmContact='$_POST[StaffEmContact]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";
 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}
if(isset($_POST['StaffEmail'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Email='$_POST[StaffEmail]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}
if(isset($_POST['Staffdob'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET dob='$_POST[Staffdob]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}

if(isset($_POST['StaffGender'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Gender='$_POST[StaffGender]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}
if(isset($_POST['StaffBlood'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET ='$_POST[StaffBlood]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}
if(isset($_POST['StaffEducation'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Education='$_POST[StaffEducation]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}
if(isset($_POST['StaffAddress'])){
    $staffid=$_POST['StaffId'];
mysqli_query($db,"UPDATE staffdetails SET Address='$_POST[StaffAddress]' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";  
}
if(isset($_FILES['StaffPhoto'])){
    $staffid=$_POST['StaffId'];
    $fileName=$_FILES['StaffPhoto']['name'];
                        $fileTmpName=$_FILES['StaffPhoto']['tmp_name'];
                        $fileSize=$_FILES['StaffPhoto']['size'];
                        $fileError=$_FILES['StaffPhoto']['error'];
                        $fileType=$_FILES['StaffPhoto']['type'];

                        $fileExt=explode('.',$fileName);
                        $fileActualExt=strtolower(end($fileExt));
                        $allowed=array('jpg','jpeg','png','JPG','JPEG','PNG');

                        if(in_array($fileActualExt, $allowed))
                        {
                            if($fileError ===0){
                                if($fileSize<=3000000){
                                    $PfileNameNew=uniqid('',true).".".$fileActualExt;

    
                                    $fileDestination = '../../assets/StaffPhotos/'.$PfileNameNew;
                                    move_uploaded_file($fileTmpName, $fileDestination);  
                                }
                                else{
                                    echo "Your File Size is large";
                                }

                            }else{
                                    echo "Error Uploading File";
                                }
                            
                        }




mysqli_query($db,"UPDATE staffdetails SET Photo='$PfileNameNew' WHERE StaffId='$staffid'");
echo "<script>alert('Data Updated!')</script>";

 $url="https://vidyaranyahospital.in/Billing/Admin/default/ViewMoreStaff.php?StaffId=$staffid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
}

// 
/*search Bills*/
if ((isset($_POST['searchcontact'])) || (isset($_POST['searchPatientId']))) {

    $patientId = $_POST['searchPatientId'];
    $contact = $_POST['searchcontact'];

    // If Patient ID is provided
    if ($patientId != "") {
        // Query to fetch Patient details
        $patientQuery = mysqli_query($db, "SELECT * FROM patientdetails WHERE PatId='$patientId'");
        
        // Query to fetch OPD details
        $opdQuery = mysqli_query($db, "SELECT * FROM opdpatientregister WHERE PatientId='$patientId'");
        
        // Query to fetch Advance details
        $advanceQuery = mysqli_query($db, "SELECT * FROM advance WHERE PatId='$patientId'");

        if (mysqli_num_rows($patientQuery) != 0) {
            // Redirect to Patient Details page
            $url = "/Billing/Admin/default/BillList.php?PatientID=$patientId";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } elseif (mysqli_num_rows($opdQuery) != 0) {
            // Redirect to OPD Bill List
            $url = "/Billing/Admin/default/opdtransbill.php?PatientID=$patientId";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } elseif (mysqli_num_rows($advanceQuery) != 0) {
            // Redirect to Advance Bill List
            $url = "/Billing/Admin/default/BillList.php?PatientID=$patientId"; // Change URL if necessary
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } else {
            echo "<script>alert('No records found for the given Patient ID!')</script>";
            $url = "/Billing/Admin/default/SearchBill.php";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        }
    }

    // If contact number is provided
    elseif ($contact != "") {
        // Query to fetch Patient details by contact number
        $patientContactQuery = mysqli_query($db, "SELECT * FROM patientdetails WHERE Contact='$contact'");
        
        // Query to fetch OPD details by contact number
        $opdContactQuery = mysqli_query($db, "SELECT * FROM opdpatientregister WHERE Contact='$contact'");
        
        // Query to fetch Advance details by contact number
        $advanceContactQuery = mysqli_query($db, "SELECT * FROM advance WHERE Contact='$contact'"); // Adjust as necessary

        if (mysqli_num_rows($patientContactQuery) != 0) {
            while ($row = $patientContactQuery->fetch_assoc()) {
                $patientId = $row['PatId'];
            }
            // Redirect to Patient Details page
            $url = "/Billing/Admin/default/BillList.php?PatientID=$patientId";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } elseif (mysqli_num_rows($opdContactQuery) != 0) {
            while ($row = $opdContactQuery->fetch_assoc()) {
                $patientId = $row['PatientId'];
            }
            // Redirect to OPD Bill List
            $url = "/Billing/Admin/default/opdtransbill.php?PatientID=$patientId";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } elseif (mysqli_num_rows($advanceContactQuery) != 0) {
            while ($row = $advanceContactQuery->fetch_assoc()) {
                $patientId = $row['PatId'];
            }
            // Redirect to Advance Bill List
            $url = "/Billing/Admin/default/BillList.php?PatientID=$patientId"; // Change URL if necessary
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        } else {
            echo "<script>alert('Incorrect Phone Number or the entered number is not registered!')</script>";
            $url = "/Billing/Admin/default/SearchBill.php";
            echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />";
        }
    }
}





?>
