<Window x:Class="XLAB.PlanningWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Планирование"
        Height="860"
        Width="1540"
        MinHeight="720"
        MinWidth="1240"
        Loaded="Window_Loaded"
        WindowStartupLocation="CenterOwner">
    <Grid Margin="12">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <GroupBox Grid.Row="0"
                  Header="Параметры плана-графика">
            <Grid Margin="8">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <DockPanel Grid.Row="0">
                    <StackPanel DockPanel.Dock="Right"
                                Orientation="Horizontal">
                    </StackPanel>

                    <WrapPanel>
                        <StackPanel Margin="0,0,16,8">
                            <TextBlock Margin="0,0,0,4"
                                       Text="Год" />
                            <ComboBox Width="120"
                                      ItemsSource="{Binding YearItems}"
                                      SelectedItem="{Binding SelectedYear}" />
                        </StackPanel>

                        <StackPanel Margin="0,0,16,8">
                            <TextBlock Margin="0,0,0,4"
                                       Text="Организация-владелец" />
                            <ComboBox Width="360"
                                      ItemsSource="{Binding OwnerFilterItems}"
                                      SelectedValue="{Binding SelectedOwnerFilterId}"
                                      SelectedValuePath="Id"
                                      DisplayMemberPath="Name"
                                      IsTextSearchEnabled="True" />
                        </StackPanel>

                        <StackPanel Margin="0,0,16,8">
                            <TextBlock Margin="0,0,0,4"
                                       Text="Поиск" />
                            <TextBox Width="360"
                                     Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" />
                        </StackPanel>
                    </WrapPanel>
                </DockPanel>

                <TextBlock Grid.Row="1"
                           Foreground="DimGray"
                           Text="Явный план берётся из EKZMCP.DTMKPLO; при отсутствии записи строка рассчитывается по EKZMK и периоду поверки." />
            </Grid>
        </GroupBox>

        <GroupBox Grid.Row="1"
                  Margin="0,12,0,0"
                  Header="План-график поверки">
            <DataGrid ItemsSource="{Binding PlanItems}"
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                      AutoGenerateColumns="False"
                      CanUserAddRows="False"
                      IsReadOnly="True"
                      HeadersVisibility="Column">
                <DataGrid.ContextMenu>
                    <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="Добавить прибор"
                                  Command="{Binding AddInstrumentCommand}" />
                        <MenuItem Header="Изменить прибор"
                                  Command="{Binding EditInstrumentCommand}" />
                        <Separator />
                        <MenuItem Header="Добавить план"
                                  Command="{Binding AddPlanCommand}" />
                        <MenuItem Header="Изменить план"
                                  Command="{Binding EditPlanCommand}" />
                        <MenuItem Header="Удалить план"
                                  Command="{Binding DeletePlanCommand}" />
                    </ContextMenu>
                </DataGrid.ContextMenu>
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <EventSetter Event="PreviewMouseRightButtonDown"
                                     Handler="DataGridRow_PreviewMouseRightButtonDown" />
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="План"
                                        Width="95"
                                        Binding="{Binding PlannedOn, StringFormat=d}" />
                    <DataGridTextColumn Header="Тип записи"
                                        Width="95"
                                        Binding="{Binding RecordKindText}" />
                    <DataGridTextColumn Header="Источник"
                                        Width="210"
                                        Binding="{Binding PlanSource}" />
                    <DataGridTextColumn Header="Основание периода"
                                        Width="170"
                                        Binding="{Binding PeriodSource}" />
                    <DataGridTextColumn Header="Период"
                                        Width="90"
                                        Binding="{Binding PeriodDisplay}" />
                    <DataGridTextColumn Header="Последняя поверка"
                                        Width="105"
                                        Binding="{Binding LastVerificationOn, StringFormat=d}" />
                    <DataGridTextColumn Header="Организация-владелец"
                                        Width="210"
                                        Binding="{Binding OwnerOrganizationName}" />
                    <DataGridTextColumn Header="Область измерений"
                                        Width="160"
                                        Binding="{Binding MeasurementAreaName}" />
                    <DataGridTextColumn Header="Наименование"
                                        Width="210"
                                        Binding="{Binding InstrumentName}" />
                    <DataGridTextColumn Header="Тип"
                                        Width="170"
                                        Binding="{Binding TypeName}" />
                    <DataGridTextColumn Header="Диапазон"
                                        Width="220"
                                        Binding="{Binding RangeText}" />
                    <DataGridTextColumn Header="№ Госреестра"
                                        Width="110"
                                        Binding="{Binding RegistryNumber}" />
                    <DataGridTextColumn Header="Заводской номер"
                                        Width="130"
                                        Binding="{Binding SerialNumber}" />
                    <DataGridTextColumn Header="Инвентарный номер"
                                        Width="130"
                                        Binding="{Binding InventoryNumber}" />
                    <DataGridTextColumn Header="Сохранение"
                                        Width="110"
                                        Binding="{Binding PersistenceText}" />
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>

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

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