<Window x:Class="XLAB2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Приемо-сдаточные ведомости"
        Height="860"
        Width="1500"
        MinHeight="700"
        MinWidth="1180"
        WindowState="Maximized"
        Loaded="Window_Loaded">
    <Window.Resources>
        <Style x:Key="GhostGridSplitterStyle" TargetType="GridSplitter">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="ShowsPreview" Value="True" />
            <Setter Property="Opacity" Value="1" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#D7DADF" />
                    <Setter Property="BorderBrush" Value="#BCC1C7" />
                    <Setter Property="BorderThickness" Value="1" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid Margin="12">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Menu Grid.Row="0"
              Margin="0,0,0,12">
            <MenuItem Header="Справочники">
                <MenuItem Header="Области измерений"
                          Click="SpoiDirectoryMenuItem_Click" />
                <MenuItem Header="Наименования типов СИ"
                          Click="SpnmtpDirectoryMenuItem_Click" />
            </MenuItem>
            <MenuItem Header="Подразделения"
          Click="FrpdDirectoryMenuItem_Click" />
            <MenuItem Header="Персонал"
          Click="PrsnDirectoryMenuItem_Click" />
            <MenuItem Header="Типоразмеры"
          Click="TypeSizeDirectoryMenuItem_Click" />
            <MenuItem Header="Экземпляры"
          Click="EkzDirectoryMenuItem_Click" />
            <MenuItem Header="Отчеты"
          Click="VerificationReportsMenuItem_Click" />
        </Menu>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="430" />
                <ColumnDefinition Width="12" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <GroupBox Grid.Column="0" Header="Документы">
                <Grid Margin="8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <WrapPanel Grid.Row="0"
                               Margin="0,0,0,8"
                               VerticalAlignment="Center">
                        <RadioButton Margin="0,0,16,0"
                                     GroupName="DocumentModeGroup"
                                     Content="Открытые ПСВ"
                                     IsChecked="{Binding ShowOpenDocuments, Mode=TwoWay}" />
                        <RadioButton GroupName="DocumentModeGroup"
                                     Content="Закрытые ПСВ (текущий год)"
                                     IsChecked="{Binding ShowClosedDocuments, Mode=TwoWay}" />
                    </WrapPanel>

                    <TextBlock Grid.Row="1"
                               Margin="0,0,0,4"
                               VerticalAlignment="Center"
                               Text="Поиск по номеру ПСВ или подразделению" />

                    <TextBox Grid.Row="2"
                             Margin="0,0,0,8"
                             Text="{Binding DocumentFilterText, UpdateSourceTrigger=PropertyChanged}" />

                    <ListBox Grid.Row="3"
                             ItemsSource="{Binding DocumentsView}"
                             SelectedItem="{Binding SelectedDocument, Mode=TwoWay}"
                             BorderThickness="1"
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             ScrollViewer.VerticalScrollBarVisibility="Auto">
                        <ListBox.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                <MenuItem Header="Добавить"
                                          Command="{Binding AddDocumentCommand}" />
                                <MenuItem Header="Распечатать"
                                          Command="{Binding PrintDocumentCommand}" />
                                <Separator/>
                                <MenuItem Header="Удалить"
                                          Command="{Binding DeleteDocumentCommand}" />
                            </ContextMenu>
                        </ListBox.ContextMenu>
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <EventSetter Event="PreviewMouseRightButtonDown"
                                             Handler="DocumentListItem_PreviewMouseRightButtonDown" />
                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                <Setter Property="Padding" Value="0" />
                                <Setter Property="Margin" Value="0,0,0,4" />
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Border Padding="8"
                                        BorderBrush="{StaticResource AppBorderBrush}"
                                        BorderThickness="0,0,0,1">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="18" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <Ellipse Grid.Column="0"
                                                 Width="10"
                                                 Height="10"
                                                 Margin="0,4,0,0"
                                                 HorizontalAlignment="Center"
                                                 VerticalAlignment="Top"
                                                 Stroke="#8093A4B5"
                                                 StrokeThickness="1">
                                            <Ellipse.Style>
                                                <Style TargetType="Ellipse">
                                                    <Setter Property="Visibility" Value="Collapsed" />
                                                    <Setter Property="Fill" Value="Transparent" />
                                                    <Setter Property="ToolTip" Value="{x:Null}" />
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsOpenDocumentAtTenDays}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible" />
                                                            <Setter Property="Fill" Value="{StaticResource OpenDocumentTenDaysIndicatorBrush}" />
                                                            <Setter Property="ToolTip" Value="С даты приемки прошло 10 дней." />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding IsOpenDocumentAtTwentyDays}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible" />
                                                            <Setter Property="Fill" Value="{StaticResource OpenDocumentTwentyDaysIndicatorBrush}" />
                                                            <Setter Property="ToolTip" Value="С даты приемки прошло 20 дней." />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding IsOpenDocumentOverdue}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible" />
                                                            <Setter Property="Fill" Value="{StaticResource OpenDocumentOverdueIndicatorBrush}" />
                                                            <Setter Property="ToolTip" Value="Срок ПСВ истек: дата приемки + 30 дней." />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Ellipse.Style>
                                        </Ellipse>

                                        <StackPanel Grid.Column="1">
                                            <DockPanel LastChildFill="False">
                                                <TextBlock DockPanel.Dock="Left"
                                                           FontWeight="SemiBold"
                                                           Text="{Binding DocumentNumber}" />
                                                <TextBlock DockPanel.Dock="Right"
                                                           Foreground="DimGray"
                                                           Text="{Binding AcceptedOn, StringFormat=d}" />
                                            </DockPanel>
                                            <DockPanel Margin="0,4,0,0"
                                                       LastChildFill="True">
                                                <TextBlock DockPanel.Dock="Right"
                                                           Foreground="DimGray"
                                                           Text="{Binding TimelineDisplay}" />
                                                <TextBlock Margin="0,0,12,0"
                                                           Foreground="DimGray"
                                                           Text="{Binding CustomerName}"
                                                           TextTrimming="CharacterEllipsis" />
                                            </DockPanel>
                                        </StackPanel>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <TextBlock Grid.Row="4"
                               Margin="0,8,0,0"
                               Foreground="DimGray"
                               Text="{Binding DocumentStatusText}" />
                </Grid>
            </GroupBox>

            <GridSplitter Grid.Column="1"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          ResizeDirection="Columns"
                          ResizeBehavior="PreviousAndNext"
                          Style="{StaticResource GhostGridSplitterStyle}"
                          Cursor="SizeWE" />

            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="10" />
                    <RowDefinition Height="340" />
                </Grid.RowDefinitions>

                <GroupBox Grid.Row="0" Header="Реквизиты документа">
                    <Grid Margin="8">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="110" />
                            <ColumnDefinition Width="220" />
                            <ColumnDefinition Width="110" />
                            <ColumnDefinition Width="160" />
                            <ColumnDefinition Width="110" />
                            <ColumnDefinition Width="160" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0"
                                   Grid.Column="0"
                                   Margin="0,0,8,8"
                                   VerticalAlignment="Center"
                                   Text="Номер ПСВ" />
                        <TextBox Grid.Row="0"
                                 Grid.Column="1"
                                 Margin="0,0,12,8"
                                 IsEnabled="{Binding IsDocumentHeaderEditable}"
                                 Text="{Binding DocumentNumberEditor, UpdateSourceTrigger=PropertyChanged}" />

                        <TextBlock Grid.Row="0"
                                   Grid.Column="2"
                                   Margin="0,0,8,8"
                                   VerticalAlignment="Center"
                                   Text="Дата приемки" />
                        <DatePicker Grid.Row="0"
                                    Grid.Column="3"
                                    Margin="0,0,12,8"
                                    IsEnabled="{Binding IsDocumentHeaderEditable}"
                                    SelectedDate="{Binding HeaderReceivedOn, Mode=TwoWay}"
                                    SelectedDateFormat="Short" />

                        <TextBlock Grid.Row="0"
                                   Grid.Column="4"
                                   Margin="0,0,8,8"
                                   VerticalAlignment="Center"
                                   Text="Дата выдачи" />
                        <DatePicker Grid.Row="0"
                                    Grid.Column="5"
                                    Margin="0,0,12,8"
                                    IsEnabled="{Binding IsDocumentHeaderEditable}"
                                    SelectedDate="{Binding HeaderIssuedOn, Mode=TwoWay}"
                                    SelectedDateFormat="Short" />

                        <StackPanel Grid.Row="0"
                                    Grid.Column="6"
                                    Orientation="Horizontal"
                                    HorizontalAlignment="Right">
                            <Button Width="120"
                                    Margin="0,0,0,8"
                                    IsEnabled="{Binding IsDocumentHeaderEditable}"
                                    Command="{Binding SaveDocumentHeaderCommand}"
                                    Content="Сохранить" />
                        </StackPanel>

                        <TextBlock Grid.Row="1"
                                   Grid.Column="0"
                                   Margin="0,0,8,6"
                                   VerticalAlignment="Center"
                                   Text="Подразделение" />
                        <ComboBox Grid.Row="1"
                                  Grid.Column="1"
                                  Grid.ColumnSpan="3"
                                  Margin="0,0,12,6"
                                  ItemsSource="{Binding Customers}"
                                  DisplayMemberPath="CustomerName"
                                  SelectedValuePath="CustomerId"
                                  SelectedValue="{Binding SelectedCustomerId, Mode=TwoWay}"
                                  IsEnabled="{Binding IsCustomerEditable}" />

                        <TextBlock Grid.Row="1"
                                   Grid.Column="4"
                                   Margin="0,0,8,6"
                                   VerticalAlignment="Center"
                                   Text="Приборов в ПСВ" />
                        <TextBlock Grid.Row="1"
                                   Grid.Column="5"
                                   Grid.ColumnSpan="2"
                                   Margin="0,0,0,6"
                                   VerticalAlignment="Center"
                                   FontWeight="SemiBold"
                                   Text="{Binding HeaderInstrumentCount}" />

                        <TextBlock Grid.Row="2"
                                   Grid.ColumnSpan="7"
                                   Foreground="DimGray"
                                   Text="{Binding LineStatusText}" />
                    </Grid>
                </GroupBox>

                <GroupBox Grid.Row="1" Header="Группы приборов выбранного документа">
                    <Grid Margin="8">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <Grid Margin="0,0,0,8">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="290" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBox Grid.Column="0"
                                     Text="{Binding GroupFilterText, UpdateSourceTrigger=PropertyChanged}" />
                            <TextBlock Grid.Column="1"
                                       Margin="12,0,0,0"
                                       VerticalAlignment="Center"
                                       Foreground="DimGray"
                                       Text="Поиск по типу, диапазону, госреестру или зав. №" />
                        </Grid>

                        <DataGrid Grid.Row="1"
                                  ItemsSource="{Binding DocumentGroupsView}"
                                  SelectedItem="{Binding SelectedDocumentGroup, Mode=TwoWay}"
                                  AutoGenerateColumns="False"
                                  CanUserAddRows="False"
                                  IsReadOnly="False"
                                  HeadersVisibility="Column">
                            <DataGrid.ContextMenu>
                                <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                    <MenuItem Header="Добавить по заводским номерам"
                                              Command="{Binding OpenInstrumentPickerCommand}" />
                                    <MenuItem Header="Добавить по типу"
                                              Command="{Binding OpenInstrumentTypePickerCommand}" />
                                    <Separator/>
                                    <MenuItem Header="Удалить"
                                              Command="{Binding DeleteSelectedGroupsCommand}" />
                                </ContextMenu>
                            </DataGrid.ContextMenu>
                            <DataGrid.RowStyle>
                                <Style TargetType="DataGridRow">
                                    <EventSetter Event="PreviewMouseRightButtonDown"
                                                 Handler="DocumentGroupRow_PreviewMouseRightButtonDown" />
                                </Style>
                            </DataGrid.RowStyle>
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Выбр."
                                                        Width="52">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox HorizontalAlignment="Center"
                                                      VerticalAlignment="Center"
                                                      IsChecked="{Binding IsBatchSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Header="Тип"
                                                    Width="180"
                                                    Binding="{Binding InstrumentType}" />
                                <DataGridTextColumn Header="Диапазон"
                                                    Width="170"
                                                    Binding="{Binding RangeText}" />
                                <DataGridTextColumn Header="Госреестр"
                                                    Width="120"
                                                    Binding="{Binding RegistryNumber}" />
                                <DataGridTextColumn Header="В поверке"
                                                    Width="90"
                                                    Binding="{Binding InVerificationCount}" />
                                <DataGridTextColumn Header="Поверено"
                                                    Width="90"
                                                    Binding="{Binding VerifiedCount}" />
                                <DataGridTextColumn Header="Годных"
                                                    Width="90"
                                                    Binding="{Binding GoodCount}" />
                                <DataGridTextColumn Header="Забракованых"
                                                    Width="105"
                                                    Binding="{Binding RejectedCount}" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </Grid>
                </GroupBox>

                <GridSplitter Grid.Row="2"
                              HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch"
                              ResizeDirection="Rows"
                              ResizeBehavior="PreviousAndNext"
                              Style="{StaticResource GhostGridSplitterStyle}"
                              Cursor="SizeNS" />

                <GroupBox Grid.Row="3" Header="Состав выбранной группы">
                    <Grid Margin="8">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <Grid Margin="0,0,0,8">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="140" />
                                <ColumnDefinition Width="320" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0"
                                       Margin="0,0,6,0"
                                       VerticalAlignment="Center"
                                       Text="Поиск по зав. №" />
                            <TextBox Grid.Column="1"
                                     Text="{Binding GroupDetailFilterText, UpdateSourceTrigger=PropertyChanged}" />
                            <TextBlock Grid.Column="2"
                                       Margin="12,0,0,0"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Right"
                                       Foreground="DimGray"
                                       Text="{Binding DetailTableCountText}" />
                        </Grid>

                        <DataGrid Grid.Row="1"
                                  ItemsSource="{Binding DocumentLinesView}"
                                  SelectedItem="{Binding SelectedDocumentLine, Mode=TwoWay}"
                                  AutoGenerateColumns="False"
                                  CanUserAddRows="False"
                                  IsReadOnly="True"
                                  HeadersVisibility="Column">
                            <DataGrid.ContextMenu>
                                <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                    <MenuItem Header="Клонировать поверку в выбранные строки"
                                              Command="{Binding CloneLineVerificationCommand}" />
                                    <MenuItem Header="Распечатать документ о поверке"
                                              Command="{Binding PrintVerificationDocumentCommand}" />
                                    <Separator/>
                                    <MenuItem Header="Годен"
                                              Command="{Binding MarkLinePassedCommand}" />
                                    <MenuItem Header="Забракован"
                                              Command="{Binding MarkLineRejectedCommand}" />
                                    <Separator/>
                                    <MenuItem Header="Отменить проверку"
                                              Command="{Binding ResetLineVerificationCommand}" />
                                    <MenuItem Header="Удалить"
                                              Command="{Binding DeleteSelectedLinesCommand}" />
                                </ContextMenu>
                            </DataGrid.ContextMenu>
                            <DataGrid.RowStyle>
                                <Style TargetType="DataGridRow">
                                    <EventSetter Event="MouseDoubleClick"
                                                 Handler="DocumentLineRow_MouseDoubleClick" />
                                    <EventSetter Event="PreviewMouseRightButtonDown"
                                                 Handler="DocumentLineRow_PreviewMouseRightButtonDown" />
                                </Style>
                            </DataGrid.RowStyle>
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Выбр."
                                                        Width="52">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox HorizontalAlignment="Center"
                                                      VerticalAlignment="Center"
                                                      IsChecked="{Binding IsBatchSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Header="Зав. №"
                                                    Width="120"
                                                    Binding="{Binding SerialNumber}" />
                                <DataGridTextColumn Header="Дата поверки"
                                                    Width="110"
                                                    Binding="{Binding VerificationDateDisplay}" />
                                <DataGridTextColumn Header="Поверитель"
                                                    Width="180"
                                                    Binding="{Binding VerifierName}" />
                                <DataGridTextColumn Header="Номер наклейки"
                                                    Width="150"
                                                    Binding="{Binding StickerNumber}" />
                                <DataGridTextColumn Header="Результат поверки"
                                                    Width="140"
                                                    Binding="{Binding ResultText}" />
                                <DataGridTextColumn Header="Номер документа по поверке"
                                                    Width="240"
                                                    Binding="{Binding VerificationDocumentDisplay}" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </Grid>
                </GroupBox>
            </Grid>
        </Grid>
    </Grid>
</Window>
