using System;
using System.Linq;
using System.Windows.Controls;
using PetroleumViscosityTest.Models;

namespace PetroleumViscosityTest.Controls
{
    public partial class TestRecordControl : UserControl
    {
        public TestRecordControl()
        {
            InitializeComponent();
        }

        public void LoadData(ViscosityTestData data)
        {
            txtSampleID.Text = data.SampleID;
            txtSampleName.Text = data.SampleName;
            txtTestTemp.Text = data.TestTemp.ToString("F1");
            txtThermostatTime.Text = Constants.GetThermostatTime(data.TestTemp).ToString();
            txtConstant.Text = data.Constant.ToString("G5");
            txtDensity.Text = data.Density.HasValue ? data.Density.Value.ToString("F3") : "—";
            txtAvgTime.Text = data.AvgTime.ToString("F3");
            txtViscosity.Text = data.Viscosity.ToString("G4");
            txtDynamicViscosity.Text = data.DynamicViscosity > 0 ? data.DynamicViscosity.ToString("G4") : "—";
            txtValidCount.Text = $"{data.ValidTimes.Count} / {data.RawTimes.Count}";
            txtOperator.Text = data.Operator;
            txtTestDate.Text = data.TestDate.ToString("yyyy-MM-dd");
            txtRemark.Text = data.Remark;

            // 显示所有流动时间
            var timeItems = data.RawTimes.Select((t, i) => new { Display = $"第{i + 1}次: {t:F3}s" }).ToList();
            lstTimes.ItemsSource = timeItems;
        }
    }
}