<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="768" d:DesignWidth="1024">

    <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>

            <!-- RFID连接状态样式 -->
            <Style x:Key="RFIDCounnectStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="#00C851"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Style.Triggers>
                    <!-- 成功 = 红色 -->
                    <DataTrigger Binding="{Binding RFIDConnectState}" Value="未连接">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger>

                    <!-- 失败 = 绿色 -->
                    <DataTrigger Binding="{Binding RFIDConnectState}" 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 RFIDConnectState,Mode=TwoWay}" FontSize="15" Style="{StaticResource RFIDCounnectStyle}"  Margin="0,0,8,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.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <!-- 左半部分：准备写入的标签 -->
            <Grid Grid.Column="0">
                <Border BorderThickness="1" BorderBrush="#E0E0E0" CornerRadius="4" Margin="0,10,0,0">
                    <Grid Margin="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- 面板标题 -->
                        <Grid Grid.Row="0" >
                            <WrapPanel Orientation="Horizontal" ItemHeight="50">
                                <TextBlock  Text="RFID写入列表"  FontSize="15" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <Label Content="订单号:"  FontSize="15" Foreground="Black" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <TextBox IsReadOnly="False" IsEnabled="True"  Text="{Binding MesOrderNo,Mode=TwoWay}" Width="100" Height="30" FontSize="15" Foreground="Black" Margin="0,0,0,0" />
                                <Label Content="产线号:"  FontSize="15" Foreground="Black" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <TextBox IsReadOnly="False" IsEnabled="True"  Text="{Binding MesOrderNo,Mode=TwoWay}" Width="100" Height="30" FontSize="15" Foreground="Black" Margin="0,0,0,0" />
                                <Label Content="带号:"  FontSize="15" Foreground="Black" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <TextBox IsReadOnly="False" IsEnabled="True"  Text="{Binding MesOrderNo,Mode=TwoWay}" Width="100" Height="30" FontSize="15" Foreground="Black" Margin="0,0,0,0" />
                                <Label Content="写入数量:"  FontSize="15" Foreground="Black" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <TextBox IsReadOnly="False" IsEnabled="True"  Text="{Binding MesOrderNo,Mode=TwoWay}" Width="100" Height="30" FontSize="15" Foreground="Black" Margin="0,0,0,0" />
                                <Button Content="生成编码" Width="100" Height="30" FontSize="15" Command="{Binding GenerateCodeCommand}" Background="#009999" Style="{StaticResource  BUTTON_AGREE}" Margin="10,0,0,0"/>
                                <Button Content="清除数据" Width="100" Height="30" FontSize="15" Command="{Binding ListClearCommand}" Background="#009999" Style="{StaticResource  BUTTON_AGREE}" Margin="10,0,0,0"/>
                            </WrapPanel>
                        </Grid>
                        <!-- 数据表格（绑定ViewModel中的历史记录） -->
                        <Grid  Grid.Row="1">
                            <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="0">
                                <DataGrid x:Name="PrePareWriteDataGrid"  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.ColumnHeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                            <Setter Property="Background" Value="Transparent"/>
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Binding="{Binding LineNumber}" Header="编号" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTextColumn Binding="{Binding RFIDCode}" Header="RFID编码" Width="6*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTextColumn Binding="{Binding IsWrite}" Header="状态" Width="1*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTemplateColumn Header="操作" Width="2*">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button Content="写入" Height="40" Width="100"  Command="{Binding DataContext.UpdateCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                        CommandParameter="{Binding}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </Border>
            </Grid>

            <!-- 右半部分：最近10条RFID读写记录 -->
            <Grid Grid.Column="1">
                <Border BorderThickness="1" BorderBrush="#E0E0E0" CornerRadius="4" Margin="0,10,0,0">
                    <Grid Margin="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50px"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- 面板标题 -->
                        <Grid Grid.Row="0" >
                            <WrapPanel Orientation="Horizontal" ItemHeight="50">
                                <TextBlock  Text="RFID写入记录"  FontSize="15" VerticalAlignment="Center" Margin="10,0,0,0"/>
                                <Button Content="清除列表" Width="100" Height="30" FontSize="15" Command="{Binding HistoryClearCommand}" Background="#009999" Style="{StaticResource  BUTTON_AGREE}" Margin="10,0,0,0"/>
                                <Button Content="查看历史" Width="100" Height="30" FontSize="15" Command="{Binding HistoryPageCommand}" Background="#009999" Style="{StaticResource  BUTTON_AGREE}" Margin="10,0,0,0"/>
                            </WrapPanel>
                        </Grid>
                        <!-- 数据表格（绑定ViewModel中的历史记录） -->
                        <Grid  Grid.Row="1">
                            <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="0">
                                <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.ColumnHeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                            <Setter Property="Background" Value="Transparent"/>
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Binding="{Binding LineNumber}" Header="产线号" Width="1.5*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTextColumn Binding="{Binding OriginalEPC}" Header="原始EPC" Width="3*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTextColumn Binding="{Binding RFIDCode}" Header="RFID编码" Width="3*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                        <DataGridTextColumn Binding="{Binding WriteTime}" Header="写入时间" Width="1.5*" ElementStyle="{StaticResource  DataGridTextColumnCenterSytle}" />
                                    </DataGrid.Columns>
                                </DataGrid>
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </Border>
            </Grid>
        </Grid>
    </Grid>
</UserControl>
