@model Disco.Web.Models.Device.ShowModel
@{
    Authorization.Require(Claims.Device.ShowDetails);

    var canConfig = Authorization.Has(Claims.Device.Properties.Details);

    if (canConfig)
    {
        Html.BundleDeferred("~/ClientScripts/Modules/Disco-PropertyChangeHelpers");
    }

    var processors = Model.Device.DeviceDetails.Processors();
    var physicalMemory = Model.Device.DeviceDetails.PhysicalMemory();
    var diskDrives = Model.Device.DeviceDetails.DiskDrives();
    var networkAdapters = Model.Device.DeviceDetails.NetworkAdapters();
    var lanMacAddress = Model.Device.DeviceDetails.LanMacAddress();
    var wlanMacAddress = Model.Device.DeviceDetails.WLanMacAddress();
    var bios = Model.Device.DeviceDetails.Bios();
    var baseBoard = Model.Device.DeviceDetails.BaseBoard();
    var computerSystem = Model.Device.DeviceDetails.ComputerSystem();
    var batteries = Model.Device.DeviceDetails.Batteries();
    var mdmHardwareData = Model.Device.DeviceDetails.MdmHardwareData();
}
<div id="DeviceDetailTab-Details" class="DevicePart">
    <div id="DeviceDetailTab-DetailsContainer">
        <table class="tableData">
            <tbody>
                @if (bios != null)
                {
                    <tr class="device_detail_bios">
                        <th>BIOS</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Manufacturer</th>
                                        <th>Serial Number</th>
                                        <th>Version</th>
                                        <th>System Version</th>
                                        <th>Release Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var b in bios)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@b.Manufacturer</span></td>
                                            <td><span data-clipboard>@b.SerialNumber</span></td>
                                            <td><span data-clipboard>@b.SMBIOSBIOSVersion @(b.SMBIOSMajorVersion.GetValueOrDefault(0)).@b.SMBIOSMinorVersion</span></td>
                                            <td><span data-clipboard>@(b.SystemBiosMajorVersion).@b.SystemBiosMinorVersion</span></td>
                                            <td>
                                                @if (b.ReleaseDate.HasValue)
                                                {
                                                    <span data-clipboard>@b.ReleaseDate.Value.ToString("yyyy-MM-dd")</span>
                                                }
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (baseBoard != null)
                {
                    <tr class="device_detail_base_board">
                        <th>Base Board</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Manufacturer</th>
                                        <th>Model</th>
                                        <th>Product</th>
                                        <th>Part Number</th>
                                        <th>SKU</th>
                                        <th>Serial Number</th>
                                        <th>Config Options</th>
                                        <th>Version</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var b in baseBoard)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@b.Manufacturer</span></td>
                                            <td><span data-clipboard>@b.Model</span></td>
                                            <td><span data-clipboard>@b.Product</span></td>
                                            <td><span data-clipboard>@b.PartNumber</span></td>
                                            <td><span data-clipboard>@b.SKU</span></td>
                                            <td><span data-clipboard>@b.SerialNumber</span></td>
                                            <td>
                                                @if (b.ConfigOptions != null)
                                                {
                                                    <span data-clipboard>@string.Join("; ", b.ConfigOptions)</span>
                                                }
                                            </td>
                                            <td><span data-clipboard>@b.Version</span></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (computerSystem != null)
                {
                    <tr class="device_detail_computer_system">
                        <th>System</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Description</th>
                                        <th>Form Factor</th>
                                        <th>Type</th>
                                        <th>Primary Owner</th>
                                        <th>Chassis SKU</th>
                                        <th>System SKU</th>
                                        <th>OEM Reference</th>
                                        <th>Time Zone</th>
                                        <th>Roles</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var c in computerSystem)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@c.Description</span></td>
                                            <td><span data-clipboard>@c.PCSystemType</span></td>
                                            <td><span data-clipboard>@c.SystemType</span></td>
                                            <td><span data-clipboard>@c.PrimaryOwnerName @c.PrimaryOwnerContact</span></td>
                                            <td><span data-clipboard>@c.ChassisSKUNumber</span></td>
                                            <td><span data-clipboard>@c.SystemSKUNumber</span></td>
                                            <td>
                                                @if (c.OEMStringArray != null)
                                                {
                                                    <span data-clipboard>@string.Join("; ", c.OEMStringArray)</span>
                                                }
                                            </td>
                                            <td>
                                                @if (c.CurrentTimeZone.HasValue)
                                                {
                                                    <span data-clipboard>@((c.CurrentTimeZone.Value / 60).ToString(@"00\:"))@(Math.Abs(c.CurrentTimeZone.Value % 60).ToString("00"))</span>
                                                }
                                            </td>
                                            <td>
                                                @if (c.Roles != null)
                                                {
                                                    <span data-clipboard>@string.Join("; ", c.Roles)</span>
                                                }
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (processors != null)
                {
                    <tr class="device_detail_processors">
                        <th>Processors</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Description</th>
                                        <th>Architecture</th>
                                        <th>Max Clock Speed</th>
                                        <th>Cores</th>
                                        <th>Logical Processors</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var processor in processors)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@processor.Name</span></td>
                                            <td><span data-clipboard>@processor.Description</span></td>
                                            <td><span data-clipboard>@processor.Architecture</span></td>
                                            <td><span data-clipboard>@processor.MaxClockSpeedFriendly()</span></td>
                                            <td><span data-clipboard>@processor.NumberOfCores.GetValueOrDefault(0).ToString("N0")</span></td>
                                            <td><span data-clipboard>@processor.NumberOfLogicalProcessors.GetValueOrDefault(0).ToString("N0")</span></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (physicalMemory != null)
                {
                    <tr class="device_detail_memory">
                        <th>Memory</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Location</th>
                                        <th>Manufacturer</th>
                                        <th>Part Number</th>
                                        <th>Serial Number</th>
                                        <th>Capacity</th>
                                        <th>Clock Speed</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var memory in physicalMemory)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@memory.DeviceLocator</span></td>
                                            <td><span data-clipboard>@memory.Manufacturer</span></td>
                                            <td><span data-clipboard>@memory.PartNumber</span></td>
                                            <td><span data-clipboard>@memory.SerialNumber</span></td>
                                            <td><span data-clipboard>@memory.CapacityFriendly()</span></td>
                                            <td><span data-clipboard>@memory.ConfiguredClockSpeed</span></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (networkAdapters != null)
                {
                    <tr class="device_detail_network_adapters">
                        <th>Network Adapters</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Connection</th>
                                        <th>Manufacturer</th>
                                        <th>Product</th>
                                        <th>Speed</th>
                                        <th>MAC Address</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var adapter in networkAdapters)
                                    {
                                        <tr>
                                            <td>
                                                <span data-clipboard>@(adapter.NetConnectionID ?? "N/A")</span> @if (adapter.IsWlanAdapter)
                                                {<i class="fa fa-wifi"></i>}
                                            </td>
                                            <td><span data-clipboard>@adapter.Manufacturer</span></td>
                                            <td><span data-clipboard>@adapter.ProductName</span></td>
                                            <td><span data-clipboard>@adapter.SpeedFriendly()</span></td>
                                            <td><span data-clipboard>@adapter.MACAddress</span></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                else
                {
                    if (lanMacAddress != null)
                    {
                        <tr class="device_detail_lan_mac_address">
                            <th>LAN MAC Address</th>
                            <td class="pad code"><span data-clipboard>@lanMacAddress</span></td>
                        </tr>
                    }
                    if (wlanMacAddress != null)
                    {
                        <tr class="device_detail_wlan_mac_address">
                            <th>WLAN MAC Address</th>
                            <td class="pad code"><span data-clipboard>@wlanMacAddress</span></td>
                        </tr>
                    }
                }
                @if (diskDrives != null)
                {
                    <tr class="device_detail_disk_drives">
                        <th>Disk Drives</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Manufacturer</th>
                                        <th>Model</th>
                                        <th>Serial Number</th>
                                        <th>Firmware</th>
                                        <th>Type</th>
                                        <th>Interface</th>
                                        <th>Size</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var disk in diskDrives)
                                    {
                                        <tr>
                                            <td><span data-clipboard>@disk.Manufacturer</span></td>
                                            <td><span data-clipboard>@disk.Model</span></td>
                                            <td><span data-clipboard>@disk.SerialNumber</span></td>
                                            <td><span data-clipboard>@disk.FirmwareRevision</span></td>
                                            <td><span data-clipboard>@disk.MediaType</span></td>
                                            <td><span data-clipboard>@disk.InterfaceType</span></td>
                                            <td><span data-clipboard>@disk.SizeFriendly()</span></td>
                                        </tr>
                                        if (disk.Partitions != null)
                                        {
                                            // calculate stretched offsets
                                            var minPartitionSize = (double)disk.Size * 0.1;
                                            var diskSizeAdjusted = disk.Partitions.Sum(p => Math.Max(minPartitionSize, p.Size.GetValueOrDefault(0)));
                                            var diskAdjustedOffet = (double)0;
                                            <tr>
                                                <td colspan="7">
                                                    <div class="partition">
                                                        @foreach (var partition in disk.Partitions)
                                                        {
                                                            var logicalDisk = partition.LogicalDisk;

                                                            var offsetPercentage = Math.Round(((partition.StartingOffset.GetValueOrDefault(0) + diskAdjustedOffet) / diskSizeAdjusted) * 100, 3);
                                                            var widthPercentage = Math.Round((Math.Max(minPartitionSize, partition.Size.GetValueOrDefault(0)) / diskSizeAdjusted) * 100, 3);
                                                            var freeSpacePercentage = 0d;
                                                            if (partition.Size < minPartitionSize)
                                                            {
                                                                diskAdjustedOffet += minPartitionSize - partition.Size.GetValueOrDefault(0);
                                                            }

                                                            var partitionTitle = partition.Type;
                                                            var tags = new List<string>();
                                                            if (partition.BootPartition.GetValueOrDefault(false))
                                                            {
                                                                tags.Add("Boot");
                                                            }
                                                            if (partition.PrimaryParition.GetValueOrDefault(false))
                                                            {
                                                                tags.Add("Primary");
                                                            }
                                                            if (logicalDisk != null)
                                                            {
                                                                partitionTitle = string.Format("{0} {1}", logicalDisk.DeviceID, logicalDisk.VolumeName);
                                                                tags.Add(logicalDisk.SizeFriendly());
                                                                tags.Add(logicalDisk.FreeSpaceFriendly() + " Free");
                                                                tags.Add(logicalDisk.FileSystem);
                                                                tags.Add(logicalDisk.DriveType);
                                                                freeSpacePercentage = Math.Round(((double)logicalDisk.FreeSpace.GetValueOrDefault(0) / logicalDisk.Size.GetValueOrDefault(0)) * 100, 3);
                                                            }
                                                            else
                                                            {
                                                                tags.Add(partition.SizeFriendly());
                                                            }
                                                            var tag = string.Join(" | ", tags);
                                                            <span title="@(partitionTitle) &#xA@(tag)" style="left: @(offsetPercentage)%; width: @(widthPercentage)%">
                                                                @if (freeSpacePercentage > 0.5)
                                                                {
                                                                    <span class="freespace" style="left: @(100 - freeSpacePercentage)%; width: @(freeSpacePercentage)%"></span>
                                                                }
                                                                <span class="details">
                                                                    <strong>@partitionTitle</strong>
                                                                    <br />
                                                                    @tag
                                                                </span>
                                                            </span>
                                                        }
                                                    </div>
                                                </td>
                                            </tr>
                                        }
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                @if (!string.IsNullOrEmpty(mdmHardwareData))
                {
                    <tr class="device_detail_mdm_hardware_data">
                        <th>MDM Hardware Data</th>
                        <td class="pad">
                            <div>
                                <button class="button small" data-action="copy">Copy</button>
                                <button class="button small" data-action="show">Show</button>
                            </div>
                            <code class="hidden" data-clipboard>@mdmHardwareData</code>
                            <script>
                                $(function () {
                                    const $container = $('tr.device_detail_mdm_hardware_data');
                                    if (!navigator.clipboard) {
                                        $container.find('button[data-action="copy"]').remove();
                                    } else {
                                        $container.find('button[data-action="copy"]').on('click', function () {
                                            navigator.clipboard.writeText($container.find('code').text());
                                        });
                                    }
                                    $container.find('button[data-action="show"]').on('click', function () {
                                        $container.find('code').css('display', 'block').removeClass('hidden');
                                        $(this).remove();
                                    });
                                });
                            </script>
                        </td>
                    </tr>
                }
                <tr class="device_detail_ac_adapter">
                    <th>AC Adapter</th>
                    <td class="pad">
                        @if (canConfig)
                        {
                            @Html.TextBox("DeviceDetail_ACAdapter", Model.Device.DeviceDetails.ACAdapter()) @AjaxHelpers.AjaxSave() @AjaxHelpers.AjaxLoader()
                            <script>
                            $(function () {
                                document.DiscoFunctions.PropertyChangeHelper($('#DeviceDetail_ACAdapter'), 'Unknown', '@Url.Action(MVC.API.Device.UpdateDetailACAdapter(Model.Device.SerialNumber, null))', 'DetailACAdapter');
                            });
                            </script>
                        }
                        else
                        {
                            @(Model.Device.DeviceDetails.ACAdapter() ?? "Unknown")
                        }
                    </td>
                </tr>
                <tr class="device_detail_battery">
                    <th>Battery</th>
                    <td class="pad">
                        @if (canConfig)
                        {
                            @Html.TextBox("DeviceDetail_Battery", Model.Device.DeviceDetails.Battery()) @AjaxHelpers.AjaxSave() @AjaxHelpers.AjaxLoader()
                            <script>
                            $(function () {
                                document.DiscoFunctions.PropertyChangeHelper($('#DeviceDetail_Battery'), 'Unknown', '@Url.Action(MVC.API.Device.UpdateDetailBattery(Model.Device.SerialNumber, null))', 'DetailBattery');
                            });
                            </script>
                        }
                        else
                        {
                            @(Model.Device.DeviceDetails.Battery() ?? "Unknown")
                        }
                    </td>
                </tr>
                @if (batteries != null)
                {
                    <tr class="device_detail_batteries">
                        <th>&nbsp;</th>
                        <td>
                            <table class="tableData">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Description</th>
                                        <th>Availability</th>
                                        <th>Chemistry</th>
                                        <th>Design Voltage</th>
                                        <th>Design Capacity</th>
                                        <th>Capacity</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var battery in batteries)
                                    {
                                        <tr>
                                            <td>@battery.Name</td>
                                            <td>@battery.Description</td>
                                            <td>@battery.Availability</td>
                                            <td>@battery.Chemistry</td>
                                            <td>@battery.DesignVoltage.GetValueOrDefault(0).ToString("N0") mV</td>
                                            <td>@battery.DesignCapacity.GetValueOrDefault(0).ToString("N0") mWh</td>
                                            <td>@battery.FullChargeCapacity.GetValueOrDefault(0).ToString("N0") mWh</td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
                <tr class="device_detail_keyboard">
                    <th>Keyboard</th>
                    <td class="pad">
                        @if (canConfig)
                        {
                            @Html.TextBox("DeviceDetail_Keyboard", Model.Device.DeviceDetails.Keyboard()) @AjaxHelpers.AjaxSave() @AjaxHelpers.AjaxLoader()
                            <script>
                            $(function () {
                                document.DiscoFunctions.PropertyChangeHelper($('#DeviceDetail_Keyboard'), 'Unknown', '@Url.Action(MVC.API.Device.UpdateDetailKeyboard(Model.Device.SerialNumber, null))', 'DetailKeyboard');
                            });
                            </script>
                        }
                        else
                        {
                            @(Model.Device.DeviceDetails.Keyboard() ?? "Unknown")
                        }
                    </td>
                </tr>
                @if (Model.Device.LastEnrolDate.HasValue)
                {
                    <tr class="device_detail_update">
                        <td colspan="2"><em>Details collected @CommonHelpers.FriendlyDate(Model.Device.LastEnrolDate) at time of last enrolment</em></td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <script>
        $('#DeviceDetailTabItems').append('<li><a href="#DeviceDetailTab-Details">Details</a></li>');
    </script>
</div>
