<?php
include "db.php";

if (isset($_GET['type'])) {
    $type = trim($_GET['type']);
    // Naya sub_filter param receive kiya (CASH ya BANK)
    $sub_filter = isset($_GET['sub_filter']) ? trim($_GET['sub_filter']) : ''; 
    
    $options = "<option value='' selected disabled>-- Select Name --</option>";

    if ($type == 'customer') {
        // Customers table se list uthana
        $query = "SELECT id, name FROM customers ORDER BY name ASC";
        $result = $conn->query($query);
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $options .= "<option value='" . $row['id'] . "'>" . htmlspecialchars($row['name']) . "</option>";
            }
        } else {
            options .= "<option value='' disabled>No Customers Found</option>";
        }
    } 
    elseif ($type == 'supplier') {
        // Suppliers table se list uthana
        $query = "SELECT id, name FROM suppliers ORDER BY name ASC";
        $result = $conn->query($query);
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $options .= "<option value='" . $row['id'] . "'>" . htmlspecialchars($row['name']) . "</option>";
            }
        } else {
            $options .= "<option value='' disabled>No Suppliers Found</option>";
        }
    } 
    else {
        // Agar ASSET select hua hai aur sub_filter (CASH/BANK) mojood hai
        if ($type == 'ASSET' && !empty($sub_filter)) {
            if ($sub_filter == 'CASH') {
                // Sirf Petty Cash ya Cash accounts uthane ke liye query
                $stmt = $conn->prepare("SELECT account_name, sub_type FROM chart_of_accounts WHERE (main_type = 'ASSET' OR sub_type = 'ASSET') AND (account_name LIKE ? OR sub_type LIKE ?) ORDER BY account_name ASC");
                $search_term = "%Cash%";
                $stmt->bind_param("ss", $search_term, $search_term);
            } else {
                // Sirf Bank ke accounts uthane ke liye query (Jo Cash na hon)
                $stmt = $conn->prepare("SELECT account_name, sub_type FROM chart_of_accounts WHERE (main_type = 'ASSET' OR sub_type = 'ASSET') AND (account_name NOT LIKE '%Cash%' AND sub_type NOT LIKE '%Cash%') ORDER BY account_name ASC");
            }
            $stmt->execute();
            $result = $stmt->get_result();
        } 
        else {
            // Baqi normal COA types ke liye (EXPENSE, INCOME, EQUITY etc.) jo pehle chal raha tha
            $stmt = $conn->prepare("SELECT account_name, sub_type FROM chart_of_accounts WHERE main_type = ? OR sub_type = ? ORDER BY account_name ASC");
            $stmt->bind_param("ss", $type, $type);
            $stmt->execute();
            $result = $stmt->get_result();
        }

        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $display_name = htmlspecialchars($row['account_name']) . " (" . htmlspecialchars($row['sub_type']) . ")";
                $options .= "<option value='" . htmlspecialchars($row['account_name']) . "'>" . $display_name . "</option>";
            }
        } else {
            $options .= "<option value='' disabled>No Accounts Found in COA</option>";
        }
        $stmt->close();
    }

    echo $options;
}
?>