<UserControl x:Class="DMS.WPF.Views.MqttServerDetailView"
             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:viewmodels="clr-namespace:DMS.WPF.ViewModels"
             xmlns:models="clr-namespace:DMS.Core.Models;assembly=DMS.Core"
             xmlns:iNKORE="clr-namespace:iNKORE.UI.WPF.Modern.Controls;assembly=iNKORE.UI.WPF.Modern"
             xmlns:extensions="clr-namespace:DMS.Extensions"
             xmlns:enums="clr-namespace:DMS.Core.Enums"
             xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- MQTT Server Details -->
        <StackPanel Grid.Row="0" Margin="10">
            <TextBlock Text="MQTT 服务器详情" Style="{StaticResource SubtitleTextBlockStyle}" Margin="0,0,0,10"/>
            <Grid Margin="0,0,0,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Text="名称:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding CurrentMqtt.ServerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>

                <TextBlock Grid.Row="0" Grid.Column="2" Text="主机:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding CurrentMqtt.ServerUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Text="端口:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding CurrentMqtt.Port, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>

                <TextBlock Grid.Row="1" Grid.Column="2" Text="客户端ID:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="1" Grid.Column="3" Text="{Binding CurrentMqtt.ClientId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

                <TextBlock Grid.Row="2" Grid.Column="0" Text="用户名:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding CurrentMqtt.Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>

                <TextBlock Grid.Row="2" Grid.Column="2" Text="密码:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="2" Grid.Column="3" Text="{Binding CurrentMqtt.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

                <TextBlock Grid.Row="3" Grid.Column="0" Text="发布主题:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding CurrentMqtt.PublishTopic, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>

                <TextBlock Grid.Row="3" Grid.Column="2" Text="订阅主题:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="3" Grid.Column="3" Text="{Binding CurrentMqtt.SubscribeTopic, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

                <TextBlock Grid.Row="4" Grid.Column="0" Text="平台:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <!-- <ComboBox Grid.Row="4" Grid.Column="1" SelectedValue="{Binding CurrentMqtt.MqttPlatform, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" -->
                <!--           Margin="0,0,10,0" -->
                <!--           ItemsSource="{Binding Source={extensions:EnumBindingSourceExtension {x:Type enums:MqttPlatform}}}" -->
                <!--           DisplayMemberPath="Description" -->
                <!--           SelectedValuePath="Value"/> -->
                
                <TextBlock Grid.Row="5" Grid.Column="0" Text="消息头:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding CurrentMqtt.MessageHeader, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
                
                <TextBlock Grid.Row="5" Grid.Column="2" Text="消息内容:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="5" Grid.Column="3" Text="{Binding CurrentMqtt.MessageContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                
                <TextBlock Grid.Row="6" Grid.Column="0" Text="消息尾:" Margin="0,0,5,0" VerticalAlignment="Center"/>
                <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding CurrentMqtt.MessageFooter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
            </Grid>
            <Button Content="保存更改" Command="{Binding SaveChangesCommand}" HorizontalAlignment="Right" Margin="0,10,0,0"/>
        </StackPanel>

        <!-- Associated Variables -->
        <Grid Grid.Row="1" Margin="10,0,10,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="关联变量" Style="{StaticResource SubtitleTextBlockStyle}" Margin="0,0,0,10"/>
            <DataGrid Grid.Row="1" x:Name="AssociatedVariablesDataGrid"
                             ItemsSource="{Binding AssociatedVariables}"
                             AutoGenerateColumns="False"
                             CanUserAddRows="False"
                             CanUserDeleteRows="False"
                             IsReadOnly="True"
                             SelectionMode="Extended">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="变量名称" Binding="{Binding Variable.Name}"/>
                    <DataGridTextColumn Header="MQTT发送名称" Binding="{Binding MqttAlias}"/>
                    <DataGridTextColumn Header="地址" Binding="{Binding Variable.S7Address}"/>
                    <DataGridTextColumn Header="数据类型" Binding="{Binding Variable.SignalType}"/>
                    <DataGridTextColumn Header="当前值" Binding="{Binding Variable.DataValue}"/>
                    <DataGridTextColumn Header="显示值" Binding="{Binding Variable.DisplayValue}"/>
                    <DataGridTextColumn Header="更新时间" Binding="{Binding Variable.UpdatedAt, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

        <!-- Actions for Associated Variables -->
        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="10">
            <Button Content="添加变量" Command="{Binding AddVariablesCommand}" Margin="0,0,10,0"/>
            <Button Content="移除选中变量" Command="{Binding RemoveVariablesCommand}" CommandParameter="{Binding ElementName=AssociatedVariablesDataGrid, Path=SelectedItems}"/>
        </StackPanel>
    </Grid>
</UserControl>