<?php 
// Retrieve parameters from request
$pro_temp_id = $_REQUEST['pro_temp_id'];
$id = $_REQUEST['id'];

// Define URLs for form actions
$submit_url = "index?page=view_product";
$show_all_page = "page=view_product";

// Retrieve previously selected filter and search values from session or set default values
$selected_filter = isset($_SESSION['selected_filter']) ? $_SESSION['selected_filter'] : 'product_name';
$search_product_name = isset($_SESSION['search_product_name']) ? $_SESSION['search_product_name'] : '';
//$search_category = isset($_SESSION['search_category']) ? $_SESSION['search_category'] : '';
//$search_part_number = isset($_SESSION['search_part_number']) ? $_SESSION['search_part_number'] : '';
$search_barcode = isset($_SESSION['search_barcode']) ? $_SESSION['search_barcode'] : '';
$search_description = isset($_SESSION['search_description']) ? $_SESSION['search_description'] : '';

?>
<form class="form-horizontal" action="<?php echo $submit_url;?>" method="post" name="search_form" id="search_form">
    <div class="card-body">
        <div class="row">
            <div class="col-lg-3 col-12">
                <label for="product_template_id" class="control-label">Item Template Name</label>
                <div class="">
                    <?php echo $gsi->getProductTempateList("product_template_id", $product_template_id, "100%", false, "form-control select2", ""); ?>
                </div>
            </div>
            <div class="col-lg-3 col-12">
                <label for="filter_dropdown" class="control-label">Filter By:</label>
                <select class="form-control " id="filter_dropdown" name="filter_dropdown">
                    <option value="product_name" <?php echo ($selected_filter === 'product_name') ? 'selected' : ''; ?>>Item Name</option>
                    <option value="category" <?php echo ($selected_filter === 'category') ? 'selected' : ''; ?>>Category</option>
                    <option value="part_number" <?php echo ($selected_filter === 'part_number') ? 'selected' : ''; ?>>Part Number</option>
                    <option value="barcode" <?php echo ($selected_filter === 'barcode') ? 'selected' : ''; ?>>Barcode</option>
                    <option value="description" <?php echo ($selected_filter === 'description') ? 'selected' : ''; ?>>Description</option>
					<option value="sales_item_code" <?php echo ($selected_filter === 'sales_item_code') ? 'selected' : ''; ?>>Sales Item Code</option>
                </select>
            </div>
            <div class="col-lg-3 col-12">
                <label for="search" class="control-label" id="search_label">Search By <?php echo ucfirst($selected_filter); ?></label>
                <input type="text" class="form-control search-input" id="search_product_name" name="search_product_name" value="<?php echo $search_product_name; ?>" placeholder="Item Name" <?php echo ($selected_filter === 'product_name') ? '' : 'style="display:none;"'; ?>>
                <div class="search-input" id="search_category" <?php echo ($selected_filter === 'category') ? '' : 'style="display:none;"'; ?>><?php echo $gsi->getCategoryList("search_category", $search_category, "100%", false, "form-control select2"); ?></div>
				<div class="search-input" id="search_part_number" <?php echo ($selected_filter === 'category') ? '' : 'style="display:none;"'; ?>><?php echo $gsi->getProductGroupList("search_part_number", $search_part_number, "100%", false, "form-control select2"); ?></div>
                <input type="text" class="form-control search-input" id="search_barcode" name="search_barcode" value="<?php echo $search_barcode; ?>" placeholder="Barcode" <?php echo ($selected_filter === 'barcode') ? '' : 'style="display:none;"'; ?>>
                <input type="text" class="form-control search-input" id="search_description" name="search_description" value="<?php echo $search_description; ?>" placeholder="Description" <?php echo ($selected_filter === 'description') ? '' : 'style="display:none;"'; ?>>
				
				<input type="text" class="form-control search-input" id="search_sales_item_code" name="search_sales_item_code" value="<?php echo $search_sales_item_code; ?>" placeholder="Sales Item Code" <?php echo ($selected_filter === 'sales_item_code') ? '' : 'style="display:none;"'; ?>>
            </div>
            <div class="col-lg-3 col-12">
                <label for="Postcode" class="control-label">&nbsp;</label><br>
                <button type="submit" id="searchingg"class="btn btn-success">Search <i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i></button>
                <button type="button" value="Show All" id="show_all_button" onclick="javascript:window.location='?<?php echo $show_all_page ?>'" class="btn btn-info">Show All</button>
            </div>
        </div>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Retrieve previously selected filter and search values from localStorage
    var selectedFilter = localStorage.getItem('selected_filter') || 'product_name';
    var searchProductName = localStorage.getItem('search_product_name') || '';
    var searchCategory = localStorage.getItem('search_category') || '';
    var searchPartNumber = localStorage.getItem('search_part_number') || '';
    var searchBarcode = localStorage.getItem('search_barcode') || '';
    var searchDescription = localStorage.getItem('search_description') || '';
    var searchSales = localStorage.getItem('search_sales_item_code') || '';

    // Set the selected filter dropdown option and search inputs based on the retrieved values
    document.getElementById('filter_dropdown').value = selectedFilter;
    updateSearchInput(selectedFilter);

    // Set search input values
    document.getElementById('search_product_name').value = searchProductName;
    document.getElementById('search_category').value = searchCategory;
    document.getElementById('search_part_number').value = searchPartNumber;
    document.getElementById('search_barcode').value = searchBarcode;
    document.getElementById('search_description').value = searchDescription;
    document.getElementById('search_sales_item_code').value = searchSales;
});

document.getElementById('filter_dropdown').addEventListener('change', function() {
    // Update selected filter value in localStorage
    localStorage.setItem('selected_filter', this.value);
    // Update search input visibility based on selected filter
    updateSearchInput(this.value);
});

document.querySelectorAll('.search-input').forEach(function(input) {
    input.addEventListener('input', function() {
        // Update search input values in localStorage
        localStorage.setItem(this.id, this.value);
    });
});

function updateSearchInput(selectedValue) {
    var searchLabel = document.getElementById('search_label');
    var filterInputs = document.querySelectorAll('.search-input');

    // Reset label text
    searchLabel.innerHTML = "Search By " + document.querySelector('option[value="' + selectedValue + '"]').text;

    // Show the input corresponding to the selected value and hide others
    filterInputs.forEach(function(input) {
        if (input.id === 'search_' + selectedValue) {
            input.style.display = 'block';
        } else { // Hide all other search inputs
            input.style.display = 'none';
        }
    });
}

document.getElementById('show_all_button').addEventListener('click', function() {
    // Clear all localStorage values
    localStorage.removeItem('selected_filter');
    localStorage.removeItem('search_product_name');
    localStorage.removeItem('search_category');
    localStorage.removeItem('search_part_number');
    localStorage.removeItem('search_barcode');
    localStorage.removeItem('search_description');
    localStorage.removeItem('search_sales_item_code');

    // Reset form inputs
    document.getElementById('filter_dropdown').value = 'product_name';
    document.querySelectorAll('.search-input').forEach(function(input) {
        input.value = '';
    });
});
</script>
