<Window x:Class="RevitEstimate.PlumbingModule.PlumbingEstimateWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Advanced MEP Plumbing Estimation &amp; BIM Extraction Module" Height="800" Width="1300" WindowStartupLocation="CenterScreen">
    <Grid Margin="10">
        <TabControl x:Name="MainTabControl">
            <!-- TAB 1 -->
            <TabItem Header="1. Tanks &amp; Heights Setup">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,15">
                        <TextBlock Text="Where is your Water Tank? (Top Source Level): " FontWeight="Bold" VerticalAlignment="Center"/>
                        <ComboBox x:Name="CmbTankLevel" Width="220" DisplayMemberPath="Name" Margin="10,0,0,0"/>
                    </StackPanel>
                    <TextBlock Grid.Row="1" Text="Default Fixture Heights from FFL (Meters):" FontWeight="Bold" Margin="0,0,0,5"/>
                    <DataGrid x:Name="GridFixturesConfig" Grid.Row="2" AutoGenerateColumns="False" CanUserAddRows="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Fixture Name" Binding="{Binding FixtureName}" Width="250" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Height from FFL (m)" Binding="{Binding HeightFromFFL, StringFormat=N2}" Width="150"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>

            <!-- TAB 2 -->
            <TabItem Header="2. Flat-wise Grouped Room Matrix">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Rooms grouped by Flats. Wet zones (including Powder Room) auto-filled:" FontWeight="Bold" Margin="0,0,0,10"/>
                    <DataGrid x:Name="GridRoomMatrix" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" AlternatingRowBackground="#FFF5F5F5">
                        <DataGrid.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" Margin="5,10,5,5">
                                            <CheckBox Content="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="#FF003366" 
                                                      IsChecked="{Binding Items[0].IsFlatEnabled, Mode=OneWay}" 
                                                      Click="FlatToggle_Click" VerticalAlignment="Center"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </DataGrid.GroupStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Room Tag &amp; Name" Binding="{Binding RoomTag}" Width="190" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Zone Type" Binding="{Binding ZoneType}" Width="120" IsReadOnly="True"/>
                            <DataGridCheckBoxColumn Header="Active Room" Binding="{Binding IsRoomActive, UpdateSourceTrigger=PropertyChanged}" Width="90"/>
                            <DataGridTextColumn Header="Wash Basin" Binding="{Binding WashBasinQty}" Width="85"/>
                            <DataGridTextColumn Header="Eur. WC" Binding="{Binding WaterClosetQty}" Width="80"/>
                            <DataGridTextColumn Header="Health Faucet" Binding="{Binding HealthFaucetQty}" Width="95"/>
                            <DataGridTextColumn Header="Geyser" Binding="{Binding GeyserQty}" Width="75"/>
                            <DataGridTextColumn Header="Kitchen Sink" Binding="{Binding KitchenSinkQty}" Width="95"/>
                            <DataGridTextColumn Header="Shower" Binding="{Binding ShowerQty}" Width="75"/>
                            <DataGridTextColumn Header="Bib Cock" Binding="{Binding WashingMachineQty}" Width="85"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>

            <!-- TAB 3 -->
            <TabItem Header="3. Pipe Materials &amp; Diameters Setup">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Configure Material and Diameters for Main, Branch, and Distribution across MEP Systems:" FontWeight="Bold" Margin="0,0,0,10"/>
                    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
                        <StackPanel Margin="0,0,10,0">
                            <!-- Water Supply -->
                            <GroupBox Header="1. Water Supply (Fixtures, Taps, Showers)" FontWeight="Bold" Margin="0,0,0,10" Padding="10">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"/>
                                        <ColumnDefinition Width="200"/>
                                        <ColumnDefinition Width="200"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Pipe Application" FontWeight="Bold"/>
                                    <TextBlock Grid.Row="0" Grid.Column="1" Text="Material Selection" FontWeight="Bold"/>
                                    <TextBlock Grid.Row="0" Grid.Column="2" Text="Diameter (Size)" FontWeight="Bold"/>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Main Vertical (Riser):" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWS_MainMat" Grid.Row="1" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWS_MainDia" Grid.Row="1" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Branch Network:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWS_BranchMat" Grid.Row="2" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWS_BranchDia" Grid.Row="2" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Distribution / Fixture:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWS_DistMat" Grid.Row="3" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWS_DistDia" Grid.Row="3" Grid.Column="2" Margin="5"/>
                                </Grid>
                            </GroupBox>
                            <!-- Sanitary -->
                            <GroupBox Header="2. Sanitary Soil (WC, Solid Waste)" FontWeight="Bold" Margin="0,0,0,10" Padding="10">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"/>
                                        <ColumnDefinition Width="200"/>
                                        <ColumnDefinition Width="200"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Main Vertical (Stack):" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbSan_MainMat" Grid.Row="0" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbSan_MainDia" Grid.Row="0" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Branch Network:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbSan_BranchMat" Grid.Row="1" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbSan_BranchDia" Grid.Row="1" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Fixture Connection:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbSan_DistMat" Grid.Row="2" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbSan_DistDia" Grid.Row="2" Grid.Column="2" Margin="5"/>
                                </Grid>
                            </GroupBox>
                            <!-- Waste Water -->
                            <GroupBox Header="3. Waste Water (Basin, Kitchen, Shower)" FontWeight="Bold" Margin="0,0,0,10" Padding="10">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"/>
                                        <ColumnDefinition Width="200"/>
                                        <ColumnDefinition Width="200"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Main Vertical (Stack):" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWW_MainMat" Grid.Row="0" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWW_MainDia" Grid.Row="0" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Branch Network:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWW_BranchMat" Grid.Row="1" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWW_BranchDia" Grid.Row="1" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Fixture Connection:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbWW_DistMat" Grid.Row="2" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbWW_DistDia" Grid.Row="2" Grid.Column="2" Margin="5"/>
                                </Grid>
                            </GroupBox>
                            <!-- Rain Water -->
                            <GroupBox Header="4. Rain Water (Roof Drainage, Balcony)" FontWeight="Bold" Margin="0,0,0,10" Padding="10">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"/>
                                        <ColumnDefinition Width="200"/>
                                        <ColumnDefinition Width="200"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Main Vertical (Stack):" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbRW_MainMat" Grid.Row="0" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbRW_MainDia" Grid.Row="0" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Branch Network:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbRW_BranchMat" Grid.Row="1" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbRW_BranchDia" Grid.Row="1" Grid.Column="2" Margin="5"/>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Collection Point:" VerticalAlignment="Center" FontWeight="Normal"/>
                                    <ComboBox x:Name="CmbRW_DistMat" Grid.Row="2" Grid.Column="1" Margin="5"/>
                                    <ComboBox x:Name="CmbRW_DistDia" Grid.Row="2" Grid.Column="2" Margin="5"/>
                                </Grid>
                            </GroupBox>
                        </StackPanel>
                    </ScrollViewer>
                    <Button Grid.Row="2" Content="Calculate &amp; Open DSR Estimate (Tab 5)" Height="45" Background="#FF007ACC" Foreground="White" FontWeight="Bold" Margin="0,15,0,0" Click="BtnGenerateReport_Click"/>
                </Grid>
            </TabItem>

            <!-- TAB 4: ACTUAL BIM MODEL QUANTITIES -->
            <TabItem Header="4. Actual BIM Model Quantities">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Quantities extracted directly from Revit Plumbing Elements (Pipes, Fixtures &amp; Equipment):" FontWeight="Bold" FontSize="14" Margin="0,0,0,10"/>
                    <DataGrid x:Name="GridBimQuantities" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" AlternatingRowBackground="#FFF9F9F9">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="150" FontWeight="Bold"/>
                            <DataGridTextColumn Header="Family Name" Binding="{Binding FamilyName}" Width="220"/>
                            <DataGridTextColumn Header="Type Name" Binding="{Binding TypeName}" Width="220"/>
                            <DataGridTextColumn Header="System Type" Binding="{Binding SystemType}" Width="150"/>
                            <DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="120"/>
                            <DataGridTextColumn Header="Quantity / Length" Binding="{Binding Quantity, StringFormat=N2}" Width="120" FontWeight="Bold" Foreground="#FF003366"/>
                            <DataGridTextColumn Header="Unit" Binding="{Binding Unit}" Width="80"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>

            <!-- TAB 5: DSR ESTIMATE -->
            <TabItem Header="5. DSR Estimate">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Detailed CPWD DSR / NSR Estimate (Auto-calculated):" FontWeight="Bold" FontSize="14" Margin="0,0,0,10"/>

                    <DataGrid x:Name="GridDsrEstimate" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" AlternatingRowBackground="#FFF9F9F9">
                        <DataGrid.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="14" Foreground="#FF800000" Margin="5,15,5,5"/>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </DataGrid.GroupStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Sr. No." Binding="{Binding SrNo}" Width="50"/>
                            <DataGridTextColumn Header="DSR Code" Binding="{Binding DsrCode}" Width="90"/>
                            <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="350">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="TextWrapping" Value="Wrap"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Logic / Remark" Binding="{Binding Remark}" Width="200" Foreground="#FF555555" FontStyle="Italic"/>
                            <DataGridTextColumn Header="Unit" Binding="{Binding Unit}" Width="60"/>
                            <DataGridTextColumn Header="Qty" Binding="{Binding Qty, StringFormat=N2}" Width="60"/>
                            <DataGridTextColumn Header="Rate (₹)" Binding="{Binding Rate, StringFormat=N2}" Width="90"/>
                            <DataGridTextColumn Header="Amount (₹)" Binding="{Binding Amount, StringFormat=N2}" Width="110" FontWeight="Bold"/>
                        </DataGrid.Columns>
                    </DataGrid>

                    <Grid Grid.Row="2" Margin="0,15,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" Content="📁 Export to Excel / Choice Folder" Height="40" Width="240" Background="#FF28A745" Foreground="White" FontWeight="Bold" Click="BtnExportDsrExcel_Click"/>
                        <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                            <TextBlock Text="Total Estimated Amount (Rs): " FontWeight="Bold" FontSize="16" VerticalAlignment="Center"/>
                            <TextBlock x:Name="TxtTotalAmount" Text="0.00" FontWeight="Bold" FontSize="18" Foreground="#FF007ACC" Margin="10,0,0,0" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Grid>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>