<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include '../includes/config.php'; 

if (isset($_GET['del'])) {  
    $ref_no = $_GET['del']; 

    try {
        $stmt = $conn->prepare("DELETE FROM purchase_lines WHERE ref_no = :ref_no");
        $stmt->bindParam(':ref_no', $ref_no, PDO::PARAM_STR);

        if ($stmt->execute()) {
            echo "<script>
                    alert('Record deleted successfully.');
                    window.location.href = '../list_purchase.php';
                  </script>";
        } else {
            throw new Exception("Failed to delete record.");
        }
    } catch (PDOException $e) {
        echo "Database error: " . $e->getMessage();
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
} else {
    echo "<script>
            alert('Invalid request.');
            window.location.href = '../list_purchase.php';
          </script>";
}
?>
