<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiAppStock.Views.AppareilPiecesPage"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <StackLayout Padding="10" Spacing="10">
        <!-- Barre de recherche -->
        <SearchBar Placeholder="Rechercher une pièce..." Text="{Binding SearchText}" />

        <!-- Bouton de tri unique -->
        <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10">
            <Button Text="Trier" Clicked="OnToggleSortOrderClicked"/>
        </StackLayout>

        <!-- Liste des associations -->
        <ListView x:Name="AssociationsListView" ItemsSource="{Binding AppareilPieces}"
                  IsPullToRefreshEnabled="True"
                  RefreshCommand="{Binding LoadAssociationsCommand}"
                  ItemTapped="OnAssociationTapped"
                  HasUnevenRows="True"
                  HeightRequest="500">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="10" ColumnSpacing="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <StackLayout Grid.Column="0">
                                <Label Text="{Binding NomPiece}" FontSize="16"/>
                                <Label Text="{Binding DescriptionPiece}" FontSize="12" TextColor="Gray"/>
                            </StackLayout>
                            <Label Grid.Column="1" Text="Recommandé !" TextColor="Green" VerticalOptions="Center" IsVisible="False">
                                <Label.Triggers>
                                    <DataTrigger TargetType="Label" Binding="{Binding EstRecommandee}" Value="True">
                                        <Setter Property="IsVisible" Value="True"/>
                                    </DataTrigger>
                                </Label.Triggers>
                            </Label>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <!-- Bouton d'ajout d'une association -->
        <Button Text="Ajouter une pièce" Clicked="OnAddPieceClicked"/>
    </StackLayout>
</ContentPage>
