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 Frmdeliversaledetail : FrmBaseForm
	{
        private Dictionary<string, string> fieldDictionary = new Dictionary<string, string>();
		public Frmdeliversaledetail()
		{
			InitializeComponent();
		}
		private void Frmdeliversaledetail_Load(object sender, EventArgs e)
        {
		
			InitFrom(xtraTabControl1,grdList,grdListView,new LayoutControlGroup[]{layoutControlGroup1},new deliversaledetailInfo());
            InitSearchDicData();
        }
        /// <summary>
		/// 数据源初始化
		/// </summary>
		/// <returns></returns>
		private void Init()
		{
		
			


				txtwarehouse.Properties.DataSource = GetDataTableUtils.SqlTable("仓库");     
				repositoryItemtxtwarehouse.DataSource= GetDataTableUtils.SqlTable("仓库");  


		}
        /// <summary>
		/// 搜索字段
		/// </summary>
		/// <returns></returns>
        private void InitSearchDicData()
        {
				fieldDictionary.Add("销售单号","salecode");     
				fieldDictionary.Add("出库单号","deliversalecode");     
				fieldDictionary.Add("备注","remark");     
        }

		public override void InitgrdListDataSource()
        {
			 using (var con=new MESDB())///
            {
				grdList.DataSource=con.deliversaledetailInfo.ToList();
            }
            Init();
		}
	    /// <summary>
		/// 字段为空校验
		/// </summary>
		/// <returns></returns>
        public override bool CheckInput()
        {
			if(string.IsNullOrEmpty(txtdeliversaleid.EditValue.ToString()))
			{
				"出货单不能为空".ShowWarning();
				txtdeliversaleid.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtsalecode.EditValue.ToString()))
			{
				"销售单号不能为空".ShowWarning();
				txtsalecode.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductname.EditValue.ToString()))
			{
				"产品名称不能为空".ShowWarning();
				txtproductname.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductcode.EditValue.ToString()))
			{
				"产品编号不能为空".ShowWarning();
				txtproductcode.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtproductspec.EditValue.ToString()))
			{
				"规格型号不能为空".ShowWarning();
				txtproductspec.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtwarehouse.EditValue.ToString()))
			{
				"仓库不能为空".ShowWarning();
				txtwarehouse.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtnumber.EditValue.ToString()))
			{
				"出货数量不能为空".ShowWarning();
				txtnumber.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtunit.EditValue.ToString()))
			{
				"计量单位不能为空".ShowWarning();
				txtunit.Focus();
				return false;
			}
			if(string.IsNullOrEmpty(txtdeliversalecode.EditValue.ToString()))
			{
				"出库单号不能为空".ShowWarning();
				txtdeliversalecode.Focus();
				return false;
			}
			return true;
        }
        /// <summary>
		/// 保存
		/// </summary>
		/// <returns></returns>
		public override bool SaveFunction()
        {
            try
            {
                deliversaledetailInfo info= (deliversaledetailInfo)this.ControlDataToModel(new deliversaledetailInfo());
                using (var db = new MESDB())
                {
                    db.deliversaledetailInfo.AddOrUpdate(info);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public override bool DelFunction()
        {
            try
            {
                deliversaledetailInfo info = (deliversaledetailInfo)this.ControlDataToModel(new deliversaledetailInfo());
                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.deliversaledetailInfo.SqlQuery("select * from deliversaledetail").ToList();
                    }
                    else
                    {
                        grdList.DataSource = db.deliversaledetailInfo.SqlQuery($"select * from deliversaledetail where {sql}").ToList();
                    }
                }
            }
        }
	}
}