<UserControl x:Class="SlnMesnac.WPF.Page.IndexPage.ProductionLineUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:local="clr-namespace:SlnMesnac.WPF.ViewModel.IndexPage" xmlns:local1="clr-namespace:SlnMesnac.WPF.Converter.Generate"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="900">

    <UserControl.Resources>

        <ResourceDictionary>
            <Style x:Key="DataGridTextColumnCenterSytle" TargetType="{x:Type TextBlock}">
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="FontSize" Value="13"/>
            </Style>
            <local1:RowToIndexConverter x:Key="RowToIndexConverter" />
            <!-- 渐变画笔 -->
            <LinearGradientBrush x:Key="PrimaryGradientBrush" StartPoint="0,0" EndPoint="1,1">
                <GradientStop Color="#2C3E50" Offset="0"/>
                <GradientStop Color="#4A6282" Offset="1"/>
            </LinearGradientBrush>

            <LinearGradientBrush x:Key="CardGradientBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientStop Color="#FFFFFF" Offset="0"/>
                <GradientStop Color="#F8F9FA" Offset="1"/>
            </LinearGradientBrush>

            <!-- 字体 -->
            <FontFamily x:Key="DefaultFont">Microsoft YaHei UI</FontFamily>
            <FontFamily x:Key="NumberFont">Segoe UI</FontFamily>

            <!-- 文本样式 -->
            <Style x:Key="HeaderTextStyle" TargetType="TextBlock">
                <Setter Property="FontFamily" Value="{StaticResource DefaultFont}"/>
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="{StaticResource PrimaryGradientBrush}"/>
            </Style>

            <Style x:Key="LabelTextStyle" TargetType="TextBlock">
                <Setter Property="FontFamily" Value="{StaticResource DefaultFont}"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter Property="Foreground" Value="#7F8C8D"/>
            </Style>

            <Style x:Key="ValueTextStyle" TargetType="TextBlock">
                <Setter Property="FontFamily" Value="{StaticResource NumberFont}"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontWeight" Value="SemiBold"/>
                <Setter Property="Foreground" Value="#2C3E50"/>
            </Style>

            <Style x:Key="NumberTextStyle" TargetType="TextBlock">
                <Setter Property="FontFamily" Value="{StaticResource NumberFont}"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="#2C3E50"/>
            </Style>

            <!-- 卡片样式 -->
            <Style x:Key="LineCardStyle" TargetType="Border">
                <Setter Property="Background" Value="{StaticResource CardGradientBrush}"/>
                <Setter Property="CornerRadius" Value="12"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="BorderBrush" Value="#E0E0E0"/>
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect BlurRadius="10" ShadowDepth="2" Color="#40000000"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Margin" Value="8"/>
                <Setter Property="Padding" Value="16"/>
            </Style>


            <!-- 成功状态样式 -->
            <Style x:Key="SuccessTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="#00C851"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Style.Triggers>
                    <!-- 成功 = 红色 -->
                    <DataTrigger Binding="{Binding LastWriteState}" Value="失败">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger>

                    <!-- 失败 = 绿色 -->
                    <DataTrigger Binding="{Binding LastWriteState}" Value="成功">
                        <Setter Property="Foreground" Value="#00C851"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <!-- 生产中状态样式 -->
            <Style x:Key="ProducingTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="#00C851"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Style>

        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <!-- 主布局 -->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- 标题栏 -->
        <Border Grid.Row="0" 
             Background="{StaticResource PrimaryGradientBrush}" 
             Padding="20,12"
          >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <!-- 系统图标和标题 -->
                <StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
                    <Path Data="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z M12,20c-4.41,0-8-3.59-8-8s3.59-8,8-8s8,3.59,8,8S16.41,20,12,20z M13,7h-2v6h6v-2h-4V7z" 
                       Fill="White" 
                       Width="24" 
                       Height="24" 
                       Margin="0,0,12,0"/>
                    <TextBlock Text="输送带RFID数字化监控系统" 
                            FontSize="24" 
                            FontWeight="Bold" 
                            Foreground="White"/>

                </StackPanel>
                <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right">
                    <!--<Button
Content="查询" Width="200" Visibility="Hidden" FontSize="20" Command="{Binding GetOrderInfoCommand}" CommandParameter="{Binding Name,ElementName=Search}"
 Background="#009999" Margin="0 0 100 0"/>-->
                    <TextBlock Text="上次写入状态:" FontSize="15" Foreground="White" Margin="0,0,8,0"/>
                    <TextBlock Text="{Binding LastWriteState,Mode=TwoWay}" FontSize="15" Style="{StaticResource SuccessTextStyle}"  Margin="0,0,8,0"/>
                    <!--<TextBlock Text="成功" FontSize="15" Style="{StaticResource SuccessTextStyle}"  Margin="0,0,8,0"/>-->
                </StackPanel>


                <!-- 当前时间 -->
                <StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">

                    <TextBlock Text="当前时间：" 
                            Foreground="White" 
                            FontSize="15" 
                            Margin="0,0,8,0"/>
                    <TextBlock x:Name="CurrentTimeText" 
                            Text="{Binding CurrentDateTime, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}"
                            Foreground="White" 
                            FontSize="15" 
                            FontWeight="Bold"/>
                </StackPanel>
            </Grid>
        </Border>

        <!-- 主要内容区域 -->
        <!--<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" Padding="20">-->
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="2*"/>
            </Grid.RowDefinitions>


            <!-- 系统概览 -->
            <!-- 上半部分：订单信息 + RFID实时信息 -->
            <TabControl ItemsSource="{Binding Deviceinfo}">
                <TabControl.ItemContainerStyle>
                    <Style TargetType="TabItem">
                        <!-- 切换标签时保留Content，不销毁重建 -->
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

                    </Style>
                </TabControl.ItemContainerStyle>
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding LineName}" Padding="5 2"/>
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <!-- 6号线标签页 -->
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <Grid Margin="10">
                            <!-- 网格布局：分为左右两列，分别放订单信息和RFID信息 -->
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <!-- 当前生产订单信息面板 -->
                            <Border Grid.Column="0" BorderThickness="1" BorderBrush="#E0E0E0" 
 CornerRadius="4" Margin="0,0,5,0">
                                <Grid Margin="10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                    </Grid.RowDefinitions>

                                    <!-- 面板标题 -->
                                    <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,10">
                                        <TextBlock Text="当前生产订单信息" FontWeight="Bold" FontSize="15"/>
                                    </StackPanel>


                                    <!-- 订单编号 -->
                                    <Grid Grid.Row="1">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="订单编号:"  FontSize="15"/>
                                        <TextBlock Text="{Binding OrderNo,Mode=TwoWay}" Grid.Column="1" FontSize="15"/>
                                    </Grid>
                                    <!-- 产线名称 -->
                                    <Grid Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="产线名称:"  FontSize="15"/>
                                        <TextBlock Text="{Binding LineName,Mode=TwoWay}"  Grid.Column="1" FontSize="15"/>
                                    </Grid>
                                    <!-- 产线编号 -->
                                    <Grid Grid.Row="3">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="产线编号:"  FontSize="15"/>
                                        <TextBlock Text="{Binding LineNo,Mode=TwoWay}"  Grid.Column="1" FontSize="15"/>
                                    </Grid>

                                    <!-- 产品类型 -->
                                    <Grid Grid.Row="4">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="产品类型:" FontSize="15"/>
                                        <TextBlock Text="{Binding ProductType,Mode=TwoWay}"  Grid.Column="1" FontSize="15"/>
                                    </Grid>

                                    <Grid Grid.Row="5">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="下一序列号:"  FontSize="15"/>
                                        <TextBlock Text="{Binding SerialNo,Mode=TwoWay}" Grid.Column="1" FontSize="15"/>
                                    </Grid>


                                    <!-- 产品类型 -->
                                    <Grid Grid.Row="6">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="带号:" FontSize="15"/>
                                        <TextBlock Text="{Binding NextProductNo,Mode=TwoWay}"  Grid.Column="1" FontSize="15"/>
                                    </Grid>
                                </Grid>
                            </Border>

                            <!-- 实时RFID读写信息面板 -->
                            <Border Grid.Column="1" BorderThickness="1" BorderBrush="#E0E0E0" 
 CornerRadius="4" Margin="5,0,0,0">
                                <Grid Margin="10">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                    </Grid.RowDefinitions>

                                    <!-- 面板标题 -->
                                    <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,10" Grid.ColumnSpan="2">
                                        <TextBlock Text="实时RFID读写信息" FontWeight="Bold" FontSize="15"/>
                                    </StackPanel>
                                    <!-- 设备状态 -->
                                    <Grid Grid.Row="1" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="设备状态:" FontSize="15"/>
                                        <TextBlock Text="{Binding IsOnline,Mode=TwoWay}" FontSize="15" Grid.Column="1"/>
                                    </Grid>
                                    <!-- RFID编码 -->
                                    <Grid Grid.Row="2" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="RFID编码:" FontSize="15"/>
                                        <TextBlock Text="{Binding RfidASCII,Mode=TwoWay}" FontSize="15"  Grid.Column="1"/>
                                    </Grid>

                                    <!-- 原始EPC -->
                                    <Grid Grid.Row="3" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="原始EPC:" FontSize="15"/>
                                        <TextBlock Text="{Binding RfidEPC,Mode=TwoWay}" FontSize="15"  Grid.Column="1"/>
                                    </Grid>

                                    <!-- 读取时间 -->
                                    <Grid Grid.Row="4" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="读取时间:"  FontSize="15"/>
                                        <TextBlock Text="{Binding ReadTime,Mode=TwoWay}" FontSize="15"  Grid.Column="1"/>
                                    </Grid>

                                    <!-- 写入时间 -->
                                    <Grid Grid.Row="5" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="写入时间:" FontSize="15"/>
                                        <TextBlock Text="{Binding WriteTime,Mode=TwoWay}" FontSize="15"  Grid.Column="1"/>
                                    </Grid>

                                    <!-- 写入状态 -->
                                    <Grid Grid.Row="6" Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="写入状态:" FontSize="15"/>
                                        <TextBlock Text="{Binding WriteStatus,Mode=TwoWay}" FontSize="15" Style="{StaticResource SuccessTextStyle}" Grid.Column="1"/>
                                    </Grid>

                                </Grid>
                            </Border>

                        </Grid>
                    </DataTemplate>
                </TabControl.ContentTemplate>


            </TabControl>

            <!-- 下半部分：最近10条RFID读写记录 -->
            <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" Padding="0">

                <Border BorderThickness="1" BorderBrush="#E0E0E0" 
                CornerRadius="4" Margin="0,10,0,0">
                    <Grid Margin="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30px"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- 面板标题 -->
                        <TextBlock Grid.Row="0" Text="最近10条RFID读写记录"  FontSize="15"
                           VerticalAlignment="Center" Margin="10,0,0,0"/>

                        <!-- 数据表格（绑定ViewModel中的历史记录） -->
                        <Grid  Grid.Row="1">
                            <DataGrid x:Name="senorlistDataGrid"  Grid.Row="0" ItemsSource="{Binding RFIDHistoryRecords}" Background="#00000000" 
                                ColumnHeaderHeight="35"   Height="{Binding Path=ActualHeight, ElementName=ScanPanel}" 
                                RowHeight="50" AutoGenerateColumns="False" RowHeaderWidth="0"  FontSize="15"
                                GridLinesVisibility="None" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                                ScrollViewer.VerticalScrollBarVisibility="Auto"  BorderThickness="0" CanUserAddRows="False" SelectionMode="Single" IsReadOnly="True" VerticalScrollBarVisibility="Auto"
                                Foreground="Black"  >

                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding OrderNumber}" Header="订单号" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding LineNumber}" Header="产线号" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding ProductType}" Header="产品类型" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding OriginalEPC}" Header="原始EPC" Width="2*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding RFIDCode}" Header="RFID编码" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding ReadTime}" Header="读取时间" Width="1.5*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTextColumn Binding="{Binding WriteTime}" Header="写入时间" Width="1.5*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    <DataGridTemplateColumn Header="写入状态" Width="1*">
                                        <DataGridTemplateColumn.CellStyle>
                                            <Style TargetType="DataGridCell">
                                                <Setter Property="HorizontalAlignment" Value="Center"/>
                                                <Setter Property="VerticalAlignment" Value="Center"/>
                                                <Setter Property="Background" Value="Transparent"/>
                                                <Setter Property="Foreground" Value="Black"/>
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="DataGridCell">
                                                            <Border Background="{TemplateBinding Background}"
                                BorderThickness="0"
                                Padding="5">
                                                                <ContentPresenter VerticalAlignment="Center" 
                                              HorizontalAlignment="Center"/>
                                                            </Border>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                                <Style.Triggers>
                                                    <!-- 成功：绿色文字 -->
                                                    <DataTrigger Binding="{Binding WriteStatus}" Value="成功">
                                                        <Setter Property="Foreground" Value="#FF008000"/>
                                                    </DataTrigger>
                                                    <!-- 失败：红色文字 -->
                                                    <DataTrigger Binding="{Binding WriteStatus}" Value="失败">
                                                        <Setter Property="Foreground" Value="#FFFF0000"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </DataGridTemplateColumn.CellStyle>
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding WriteStatus}" 
                       HorizontalAlignment="Center" 
                       VerticalAlignment="Center"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>


                                </DataGrid.Columns>
                            </DataGrid>
                        </Grid>
                    </Grid>
                </Border>
            </ScrollViewer>
        </Grid>
    </Grid>
</UserControl>
