﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LJH.Inventory.WebApiClient;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.SearchCondition;

namespace LJH.Inventory.UI.Controls
{
    public partial class UC不锈钢卷材规格 : UserControl
    {
        public UC不锈钢卷材规格()
        {
            InitializeComponent();
        }

        #region 公共方法
        public void Init(ProductClass pc)
        {
            Init(new List<ProductClass>() { pc });
        }

        public void Init(List<ProductClass> pcs)
        {
            this.txtThick.Items.Clear();
            this.txtWidth.Items.Clear();
            this.txtThick.Items.Add(string.Empty);
            this.txtWidth.Items.Add(string.Empty);
            var con = new ProductSearchCondition();
            con.Classes = pcs;
            List<string> ps = new ProductBLL(AppSettings.Current.ConnStr).GetAllSpecifications(con);
            if (ps != null && ps.Count > 0)
            {
                var thicks = (from p in ps
                              let thick = !string.IsNullOrEmpty(p) ? 卷材规格Helper.GetWrittenThick(p) : null
                              where thick != null
                              orderby thick ascending
                              select thick.ToString()).Distinct();
                foreach (var thick in thicks)
                {
                    txtThick.Items.Add(thick);
                }

                var widths = (from p in ps
                              let width = !string.IsNullOrEmpty(p) ? 卷材规格Helper.GetWrittenWidth(p) : null
                              where width != null
                              orderby width ascending
                              select width.ToString()).Distinct();
                foreach (var w in widths)
                {
                    txtWidth.Items.Add(w);
                }
            }
        }
        #endregion

        #region 公共属性
        public string Specification
        {
            get
            {
                decimal thick = 0;
                if (decimal.TryParse(txtThick.Text, out thick))
                {
                    return string.Format("{0}*{1}", thick.ToString("F2"), txtWidth.Text);
                }
                return string.Format("{0}*{1}", txtThick.Text, txtWidth.Text);
            }
            set
            {
                if (string.IsNullOrEmpty(Specification))
                {
                    txtThick.Text = string.Empty;
                    txtWidth.Text = string.Empty;
                }
                else
                {
                    decimal? thick = 卷材规格Helper.GetWrittenThick(value);
                    txtThick.Text = thick.HasValue ? thick.Value.ToString("F2") : string.Empty;
                    decimal? width = 卷材规格Helper.GetWrittenWidth(value);
                    txtWidth.Text = width.HasValue ? width.Value.ToString() : string.Empty;
                }
            }
        }
        #endregion
    }
}
