<Window x:Class="XLAB2.EkzDirectoryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Экземпляры"
        Height="900"
        Width="1540"
        MinHeight="760"
        MinWidth="1260"
        Loaded="Window_Loaded"
        WindowStartupLocation="CenterOwner">
    <Grid Margin="12">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="2.2*" />
            <RowDefinition Height="1.6*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0"
              Margin="0,0,0,12">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <DockPanel Grid.Row="0">
                <Button DockPanel.Dock="Right"
                        Width="110"
                        Margin="12,0,0,0"
                        Command="{Binding RefreshCommand}"
                        Content="Обновить" />
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="0,0,8,0"
                               VerticalAlignment="Center"
                               Text="Поиск" />
                    <TextBox Width="360"
                             Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
            </DockPanel>

            <StackPanel Grid.Row="1"
                        Margin="0,8,0,0"
                        Orientation="Horizontal">
                <TextBlock Margin="0,0,8,0"
                           VerticalAlignment="Center"
                           Text="Организация-владелец" />
                <ComboBox Width="420"
                          ItemsSource="{Binding OwnerFilterItems}"
                          SelectedValue="{Binding SelectedOwnerFilterId}"
                          SelectedValuePath="Id"
                          DisplayMemberPath="Name"
                          IsTextSearchEnabled="True" />
            </StackPanel>
        </Grid>

        <GroupBox Grid.Row="1"
                  Header="Экземпляры (EKZ)">
            <DataGrid ItemsSource="{Binding EkzItems}"
                      SelectedItem="{Binding SelectedEkz, Mode=TwoWay}"
                      AutoGenerateColumns="False"
                      CanUserAddRows="False"
                      IsReadOnly="True"
                      HeadersVisibility="Column">
                <DataGrid.ContextMenu>
                    <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="Добавить"
                                  Command="{Binding AddEkzCommand}" />
                        <MenuItem Header="Изменить"
                                  Command="{Binding EditEkzCommand}" />
                        <MenuItem Header="Удалить"
                                  Command="{Binding DeleteEkzCommand}" />
                    </ContextMenu>
                </DataGrid.ContextMenu>
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <EventSetter Event="PreviewMouseRightButtonDown"
                                     Handler="DataGridRow_PreviewMouseRightButtonDown" />
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Организация-владелец"
                                        Width="220"
                                        Binding="{Binding OwnerOrganizationName}" />
                    <DataGridTextColumn Header="Область измерений"
                                        Width="160"
                                        Binding="{Binding MeasurementAreaName}" />
                    <DataGridTextColumn Header="Наименование"
                                        Width="220"
                                        Binding="{Binding InstrumentName}" />
                    <DataGridTextColumn Header="Тип"
                                        Width="180"
                                        Binding="{Binding TypeName}" />
                    <DataGridTextColumn Header="Диапазон"
                                        Width="220"
                                        Binding="{Binding RangeText}" />
                    <DataGridTextColumn Header="Х-ка точности"
                                        Width="150"
                                        Binding="{Binding AccuracyText}" />
                    <DataGridTextColumn Header="№ Госреестра"
                                        Width="120"
                                        Binding="{Binding RegistryNumber}" />
                    <DataGridTextColumn Header="Заводской номер"
                                        Width="140"
                                        Binding="{Binding SerialNumber}" />
                    <DataGridTextColumn Header="Инвентарный номер"
                                        Width="140"
                                        Binding="{Binding InventoryNumber}" />
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>

        <GroupBox Grid.Row="2"
                  Margin="0,12,0,0"
                  Header="МК выбранного экземпляра (EKZMK)">
            <DataGrid ItemsSource="{Binding EkzMkItems}"
                      AutoGenerateColumns="False"
                      CanUserAddRows="False"
                      IsReadOnly="True"
                      HeadersVisibility="Column">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Документ"
                                        Width="220"
                                        Binding="{Binding DocumentNumber}" />
                    <DataGridTextColumn Header="Документ по поверке"
                                        Width="180"
                                        Binding="{Binding VerificationDocumentDisplay}" />
                    <DataGridTextColumn Header="Вид МК"
                                        Width="120"
                                        Binding="{Binding VerificationTypeName}" />
                    <DataGridTextColumn Header="Организация"
                                        Width="220"
                                        Binding="{Binding VerificationOrganizationName}" />
                    <DataGridTextColumn Header="Период, мес."
                                        Width="95"
                                        Binding="{Binding PeriodMonths}" />
                    <DataGridTextColumn Header="Принят"
                                        Width="95"
                                        Binding="{Binding AcceptedOn, StringFormat=d}" />
                    <DataGridTextColumn Header="План"
                                        Width="95"
                                        Binding="{Binding PlannedOn, StringFormat=d}" />
                    <DataGridTextColumn Header="Выполнен"
                                        Width="95"
                                        Binding="{Binding PerformedOn, StringFormat=d}" />
                    <DataGridTextColumn Header="Выдан"
                                        Width="95"
                                        Binding="{Binding IssuedOn, StringFormat=d}" />
                    <DataGridTextColumn Header="Результат"
                                        Width="95"
                                        Binding="{Binding ResultText}" />
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>

        <TextBlock Grid.Row="3"
                   Margin="0,8,0,0"
                   Foreground="DimGray"
                   Text="{Binding StatusText}" />

        <StackPanel Grid.Row="4"
                    Margin="0,12,0,0"
                    Orientation="Horizontal"
                    HorizontalAlignment="Right">
            <Button Width="90"
                    IsCancel="True"
                    Content="Закрыть" />
        </StackPanel>
    </Grid>
</Window>
