﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.SearchCondition;
using LJH.Inventory.BLL;
using LJH.GeneralLibrary.Core.UI;

namespace LJH.Inventory.UI.Forms.Inventory
{
    public partial class Frm增加送货单项 : Form
    {
        public Frm增加送货单项()
        {
            InitializeComponent();
        }

        #region 私有变量
        private bool IsAdding = false;
        private List<Product> _Products = null;
        #endregion

        #region 公共属性
        public StackOutItem StackOutItem { get; set; }

        public event EventHandler<ItemAddedEventArgs> ItemAdded;

        public event EventHandler<ItemUpdatedEventArgs> ItemUpdated;
        #endregion

        #region 私有方法
        private void ShowOrderItem()
        {
            var p = StackOutItem.Product;
            txtName.Text = p.Name;
            txt规格.Text = p.Specification;
            txt材质.Text = p.Model;
            txtPrice.DecimalValue = StackOutItem.Price;
            txtCount.DecimalValue = StackOutItem.Count;
            txt单位.Text = p.Unit;
            txtAmount.DecimalValue = StackOutItem.CalAmount();
            txtMemo.Text = StackOutItem.Memo;
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("没有指定产品名称");
                return false;
            }
            if (txtCount.DecimalValue <= 0)
            {
                MessageBox.Show("没有指定数量");
                return false;
            }
            if (string.IsNullOrEmpty(txt单位.Text))
            {
                MessageBox.Show("没有指定单位");
                return false;
            }
            return true;
        }

        private Product SaveProduct()
        {
            Product p = null;
            var ps = new ProductBLL(AppSettings.Current.ConnStr).GetItems(new ProductSearchCondition() { Classes = new List<ItemClass>() { ItemClass.商品 }, Name = txtName.Text }).QueryObjects;
            if (ps != null && ps.Count > 0)
            {
                p = ps.FirstOrDefault(it => 
                    it.Name == txtName.Text && 
                    it.Specification == txt规格.Text && 
                    it.Model == txt材质.Text && 
                    it.GetProperty(ItemNote.颜色) == txt颜色.Text && 
                    it.Unit == txt单位.Text && 
                    it.IsService == true);
            }
            if (p == null)
            {
                p = new Product()
                {
                    ClassID = ItemClass.商品,
                    Name = txtName.Text.Trim(),
                    Specification = txt规格.Text.Trim(),
                    Model = txt材质.Text,
                    Unit = txt单位.Text,
                    InventoryUnit = txt单位.Text,
                    IsService = true
                };
                p.SetProperty(ItemNote.颜色, txt颜色.Text);
                var ret = new ProductBLL(AppSettings.Current.ConnStr).Add(p);
                if (ret.Result != GeneralLibrary.Core.DAL.ResultCode.Successful)
                {
                    MessageBox.Show("保存产品信息失败");
                    return null;
                }
                p = ret.Value;
            }
            return p;
        }
        #endregion

        #region 事件处理方法
        private void FrmAddOrderItem_Load(object sender, EventArgs e)
        {
            IsAdding = StackOutItem == null; //这一行放在前面！！！！
            _Products = new ProductBLL(AppSettings.Current.ConnStr).GetItems(new ProductSearchCondition() { Classes = new List<ItemClass>() { ItemClass.商品 } }).QueryObjects;
            InitName();
            InitUnit();
            if (StackOutItem != null) ShowOrderItem();
            txtPrice.TextChanged += txtPrice_TextChanged;
            txtCount.TextChanged += txtCount_TextChanged;
        }

        private void InitUnit()
        {
            this.txt单位.Items.Clear();
            if (_Products != null && _Products.Count > 0)
            {
                var items = (from p in _Products
                             orderby p.Unit ascending
                             select p.Unit).Distinct().ToList();
                foreach (var p in items)
                {
                    if (!string.IsNullOrEmpty(p)) this.txt单位.Items.Add(p);
                }
            }
        }

        private void InitName()
        {
            this.txtName.Items.Clear();
            if (_Products != null && _Products.Count > 0)
            {
                var items = (from p in _Products
                             orderby p.Name ascending
                             select p.Name).Distinct().ToList();
                foreach (var p in items)
                {
                    if (!string.IsNullOrEmpty(p)) this.txtName.Items.Add(p);
                }
            }
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            txtAmount.DecimalValue = txtPrice.DecimalValue * txtCount.DecimalValue;
        }

        private void txtCount_TextChanged(object sender, EventArgs e)
        {
            txtAmount.DecimalValue = txtPrice.DecimalValue * txtCount.DecimalValue;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;
            var p = SaveProduct();
            if (p == null) return;
            if (StackOutItem == null) StackOutItem = new StackOutItem() { ID = Guid.NewGuid(), AddDate = DateTime.Now };
            StackOutItem.ProductID = p.ID;
            StackOutItem.Product = p;
            StackOutItem.Count = txtCount.DecimalValue;
            StackOutItem.Price = txtPrice.DecimalValue;
            StackOutItem.Memo = txtMemo.Text;
            if (IsAdding)
            {
                if (ItemAdded != null) this.ItemAdded(this, new ItemAddedEventArgs(StackOutItem));
                StackOutItem = null;
                IsAdding = true;
                txtName.Text = string.Empty;
                txt规格.Text = string.Empty;
                txt材质.Text = string.Empty;
                txtCount.Text = string.Empty;
                txtPrice.Text = string.Empty;
                txt单位.Text = string.Empty;
                txtName.Focus();
            }
            else
            {
                if (ItemUpdated != null) this.ItemUpdated(this, new ItemUpdatedEventArgs(StackOutItem));
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
