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 ${NameSpace}.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity;
namespace ${NameSpace}.Form
{
	public partial class Frm${tableName} : FrmBaseForm
	{
        private Dictionary<string, string> fieldDictionary = new Dictionary<string, string>();
		public Frm${tableName}()
		{
			InitializeComponent();
		}
		private void Frm${tableName}_Load(object sender, EventArgs e)
        {
			InitFrom(xtraTabControl1,grdList,grdListView,new LayoutControlGroup[]{layoutControlGroup1},new ${tableName}Info());
            InitSearchDicData();
        }
        /// <summary>
		/// 数据源初始化
		/// </summary>
		/// <returns></returns>
		private void Init()
		{
#foreach($info in $EntityList)		
#if(${info.controlType}=="LookUpEdit"||${info.controlType}=="TreeListLookUpEdit"||${info.controlType}=="CheckedComboBoxEdit"||${info.controlType}=="ComboBoxEdit")
#if(${info.controlType}=="LookUpEdit")
#if(${info.isEdit})
				${info.controlName}.Properties.DataSource = GetDataTableUtils.SqlTable("${info.DataTableName}");     
#end 	
				repositoryItem${info.controlName}.DataSource= GetDataTableUtils.SqlTable("${info.DataTableName}");  
#elseif(${info.controlType}=="TreeListLookUpEdit")
#if(${info.isEdit})
				${info.controlName}.Properties.DataSource = GetDataTableUtils.SqlTable("${info.DataTableName}");     
#end 	
				repositoryItemTreeList${info.controlName}.DataSource= GetDataTableUtils.SqlTable("${info.DataTableName}");  
#elseif(${info.controlType}=="CheckedComboBoxEdit")
                repositoryItem${info.controlName}.DataSource= GetDataTableUtils.SqlTable("${info.DataTableName}");
#if(${info.isEdit})
				${info.controlName}.Properties.DataSource = GetDataTableUtils.SqlTable("${info.DataTableName}");     
#end 	
#else
#if(${info.isEdit})
				foreach (DataRow info in GetDataTableUtils.SqlTable("${info.DataTableName}").Rows)
                {
                    ${info.controlName}.Properties.Items.Add(info.ItemArray[0].ToString());
                }     
#end 	
#end
#end  
#end
		}
        /// <summary>
		/// 搜索字段
		/// </summary>
		/// <returns></returns>
        private void InitSearchDicData()
        {
#foreach($info in $EntityList)		
#if(${info.isSearch})
				fieldDictionary.Add("${info.controlLabelName}","${info.dataBaseFieldName}");     
#end 	
#end
        }

		public override void InitgrdListDataSource()
        {
			 using (var con=new ${NameSpace}DB())///
            {
				grdList.DataSource=con.${tableName}Info.ToList();
            }
            Init();
		}
	    /// <summary>
		/// 字段为空校验
		/// </summary>
		/// <returns></returns>
        public override bool CheckInput()
        {
#foreach($info in $EntityList)		
#if(!${info.isKey}&&${info.isCheck})
			if(string.IsNullOrEmpty(${info.controlName}.EditValue.ToString()))
			{
				"${info.controlLabelName}不能为空".ShowWarning();
				${info.controlName}.Focus();
				return false;
			}
#end			
#end  
			return true;
        }
        /// <summary>
		/// 保存
		/// </summary>
		/// <returns></returns>
		public override bool SaveFunction()
        {
            try
            {
                ${tableName}Info info= (${tableName}Info)this.ControlDataToModel(new ${tableName}Info());
                using (var db = new ${NameSpace}DB())
                {
                    db.${tableName}Info.AddOrUpdate(info);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ex.Message.ShowError();
                return false;
            }
            return true;
        }
        /// <summary>
		/// 删除
		/// </summary>
		/// <returns></returns>
		public override bool DelFunction()
        {
            try
            {
                ${tableName}Info info = (${tableName}Info)this.ControlDataToModel(new ${tableName}Info());
                using (var db = new ${NameSpace}DB())
                {
                    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 ${NameSpace}DB())
                {
                    if (string.IsNullOrEmpty(sql))
                    {
                        grdList.DataSource = db.${tableName}Info.SqlQuery("select * from ${tableName}").ToList();
                    }
                    else
                    {
                        grdList.DataSource = db.${tableName}Info.SqlQuery($"select * from ${tableName} where {sql}").ToList();
                    }
                }
            }
        }
	}
}