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 WinformGeneralDeveloperFrame;
using WinformGeneralDeveloperFrame.Commons;
using DevExpress.XtraLayout;
using MES.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity;
namespace MES.Form
{
	public partial class Frmsaledetail : FrmBaseForm
	{
        private Dictionary<string, string> fieldDictionary = new Dictionary<string, string>();
		public Frmsaledetail()
		{
			InitializeComponent();
		}
		private void Frmsaledetail_Load(object sender, EventArgs e)
        {
		
			InitFrom(xtraTabControl1,grdList,grdListView,new LayoutControlGroup[]{layoutControlGroup1},new saledetailInfo());
            InitSearchDicData();
        }
        /// <summary>
		/// 数据源初始化
		/// </summary>
		/// <returns></returns>
		private void Init()
		{
		
			


				txtproductid.Properties.DataSource = GetDataTableUtils.SqlTable("产品");     
				repositoryItemtxtproductid.DataSource= GetDataTableUtils.SqlTable("产品");  
				txtunit.Properties.DataSource = GetDataTableUtils.SqlTable("计量单位");     
				repositoryItemtxtunit.DataSource= GetDataTableUtils.SqlTable("计量单位");  
				txtwarehouse.Properties.DataSource = GetDataTableUtils.SqlTable("仓库");     
				repositoryItemtxtwarehouse.DataSource= GetDataTableUtils.SqlTable("仓库");  


		}
        /// <summary>
		/// 搜索字段
		/// </summary>
		/// <returns></returns>
        private void InitSearchDicData()
        {
				fieldDictionary.Add("交货日期","deliverdate");     
				fieldDictionary.Add("仓库","warehouse");     
				fieldDictionary.Add("产品编号","productcode");     
				fieldDictionary.Add("销售单号","salecode");     
        }

		public override void InitgrdListDataSource()
        {
			 using (var con=new MESDB())///
            {
				grdList.DataSource=con.saledetailInfo.ToList();
            }
            Init();
		}
	    /// <summary>
		/// 字段为空校验
		/// </summary>
		/// <returns></returns>
        public override bool CheckInput()
        {
			if(string.IsNullOrEmpty(txtproductid.EditValue.ToString()))
			{
				"产品不能为空".ShowWarning();
				txtproductid.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductspec.EditValue.ToString()))
			{
				"规格型号不能为空".ShowWarning();
				txtproductspec.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtsalenumber.EditValue.ToString()))
			{
				"数量不能为空".ShowWarning();
				txtsalenumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunit.EditValue.ToString()))
			{
				"计量单位不能为空".ShowWarning();
				txtunit.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunitprice.EditValue.ToString()))
			{
				"单价不能为空".ShowWarning();
				txtunitprice.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmoney.EditValue.ToString()))
			{
				"金额不能为空".ShowWarning();
				txtmoney.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtdeliverdate.EditValue.ToString()))
			{
				"交货日期不能为空".ShowWarning();
				txtdeliverdate.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtwarehouse.EditValue.ToString()))
			{
				"仓库不能为空".ShowWarning();
				txtwarehouse.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtreadynumber.EditValue.ToString()))
			{
				"已排产量不能为空".ShowWarning();
				txtreadynumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtnoreadynumber.EditValue.ToString()))
			{
				"待排产量不能为空".ShowWarning();
				txtnoreadynumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtfinishnumber.EditValue.ToString()))
			{
				"已完工量不能为空".ShowWarning();
				txtfinishnumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtoutnumber.EditValue.ToString()))
			{
				"已出货量不能为空".ShowWarning();
				txtoutnumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtreturnnumber.EditValue.ToString()))
			{
				"退货量不能为空".ShowWarning();
				txtreturnnumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtnodelivernumber.EditValue.ToString()))
			{
				"未交货数量不能为空".ShowWarning();
				txtnodelivernumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtdelivernumber.EditValue.ToString()))
			{
				"实际交货数量不能为空".ShowWarning();
				txtdelivernumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtclosenumber.EditValue.ToString()))
			{
				"已结算数量不能为空".ShowWarning();
				txtclosenumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtnoclosenumber.EditValue.ToString()))
			{
				"未结算数量不能为空".ShowWarning();
				txtnoclosenumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtmaterialcost.EditValue.ToString()))
			{
				"物料成本不能为空".ShowWarning();
				txtmaterialcost.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtrealmaterialcost.EditValue.ToString()))
			{
				"实际物料成本不能为空".ShowWarning();
				txtrealmaterialcost.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductcode.EditValue.ToString()))
			{
				"产品编号不能为空".ShowWarning();
				txtproductcode.Focus();
				return false;
			}
			//if(string.IsNullOrEmpty(txtsaleid.EditValue.ToString()))
			//{
			//	"销售主表不能为空".ShowWarning();
			//	txtsaleid.Focus();
			//	return false;
			//}
			if(string.IsNullOrEmpty(txtsalecode.EditValue.ToString()))
			{
				"销售单号不能为空".ShowWarning();
				txtsalecode.Focus();
				return false;
			}
			return true;
        }
        /// <summary>
		/// 保存
		/// </summary>
		/// <returns></returns>
		public override bool SaveFunction()
        {
            try
            {
                saledetailInfo info= (saledetailInfo)this.ControlDataToModel(new saledetailInfo());
                using (var db = new MESDB())
                {
                    db.saledetailInfo.AddOrUpdate(info);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public override bool DelFunction()
        {
            try
            {
                saledetailInfo info = (saledetailInfo)this.ControlDataToModel(new saledetailInfo());
                using (var db = new MESDB())
                {
                    db.Entry(info).State=EntityState.Deleted;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 搜索
		/// </summary>
		/// <returns></returns>
        public override void SearchFunction()
        {
            FrmSearch frm = new FrmSearch(fieldDictionary);
            if (frm.ShowDialog()==DialogResult.OK)
            {
                string sql = frm.sql;
                using (var db = new MESDB())
                {
                    if (string.IsNullOrEmpty(sql))
                    {
                        grdList.DataSource = db.saledetailInfo.SqlQuery("select * from saledetail").ToList();
                    }
                    else
                    {
                        grdList.DataSource = db.saledetailInfo.SqlQuery($"select * from saledetail where {sql}").ToList();
                    }
                }
            }
        }
	}
}