using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils.Extensions;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraLayout;
using DevExpress.XtraTab;
using MES.Entity;
using WinformGeneralDeveloperFrame.Commons;
using MES;

namespace WinformGeneralDeveloperFrame
{
    public partial class FrmBaseForm : XtraForm
    {
        public string ID = string.Empty;  // 主键
        private XtraTabControl xtraTab;
        private GridControl gridControl;
        private GridView gridView;
        private LayoutControlGroup[] controlGroups;
        private object DataType;
        public string menucode;
        public FrmBaseForm()
        {
            InitializeComponent();
        }

        public void InitFrom(XtraTabControl xtraTab, GridControl gridControl, GridView gridView, LayoutControlGroup[] controlGroups, object DataType)
        {
            initTools();
            InitFrom(xtraTab, gridControl, gridView, controlGroups);
            this.DataType = DataType;
        }
        public void InitFrom(XtraTabControl xtraTab, GridControl gridControl, GridView gridView, LayoutControlGroup[] controlGroups)
        {
            this.xtraTab = xtraTab;
            this.gridControl = gridControl;
            this.gridView = gridView;
            this.controlGroups = controlGroups;
            InitToolBtntatus(EFormStatus.eInit);
            InitEvent();
        }
        public void InitEvent()
        {
            xtraTab.SelectedPageChanged += xtraTabControl1_SelectedPageChanged;
            gridControl.MouseDoubleClick += gridControl_MouseDoubleClick;
        }
        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {

        }
        private void gridControl_MouseDoubleClick(object sender, EventArgs e)
        {
            var dr = this.gridView.GetFocusedRow();
            if (dr != null)
            {
                this.ModelDataToControl(dr);
                xtraTab.SelectedTabPageIndex = 1;
                SetControlStatus(controlGroups, true);
                InitToolBtntatus(EFormStatus.eView);
                gridControlMouseDoubleClickFunction(sender, e);
            }
        }
        public virtual void gridControlMouseDoubleClickFunction(object sender, EventArgs e)
        {

        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            xtraTab.SelectedTabPageIndex = 1;
            xtraTab.TabPages[0].PageEnabled = false;
            ClearScreen();
            DateEdit dt = (DateEdit)this.getControl("txtcreateTime");
            if (dt != null)
                dt.DateTime = DateTime.Now;
            LookUpEditBase look= (LookUpEditBase)this.getControl("txtcreatorId");
            DateEdit dt1 = (DateEdit)this.getControl("txteditTime");
            if (dt1 != null)
                dt1.DateTime = DateTime.Now;
            LookUpEditBase look1 = (LookUpEditBase)this.getControl("txteditorId");
            if (look1 != null)
                look1.EditValue = AppInfo.LoginUserInfo.id;
            if (look != null)
                look.EditValue = AppInfo.LoginUserInfo.id;
            SetControlStatus(controlGroups, false);
            InitToolBtntatus(EFormStatus.eAdd);
            AddFunction();
        }
        public virtual void AddFunction()
        {

        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            xtraTab.TabPages[0].PageEnabled = false;
            DateEdit dt =(DateEdit) this.getControl("txteditTime");
            if(dt!=null)
                 dt.DateTime=DateTime.Now;
            LookUpEditBase look = (LookUpEditBase)this.getControl("txteditorId");
            if (look != null)
                look.EditValue = AppInfo.LoginUserInfo.id;
            SetControlStatus(controlGroups, false);
            InitToolBtntatus(EFormStatus.eEdit);
            EditFunction();
        }
        public virtual void EditFunction()
        {

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            xtraTab.TabPages[0].PageEnabled = true;
            if (CheckInput()&&SaveFunction())
            {
                "保存成功！".ShowTips();
                
                InitToolBtntatus(EFormStatus.eView);
                InitgrdListDataSource();
                SetControlStatus(controlGroups, true);
            }
            
        }
        public void initTools()
        {

            List<sysDictTypeInfo> list = null;
            using (var db=new MESDB())
            {
                list = db.sysDictTypeInfo.Where(p => p.pid == 10).ToList();
            }
            //List<MenuInfo> list = BLLFactory<Menu>.Instance.GetAll().Where(p => p.IsToolBtn == true).ToList();
            int i = 1;
            int width = 0;
            foreach (sysDictTypeInfo info in list)
            {
                if (AppInfo.FunctionList.Contains(menucode+"-"+info.remark))
                {
                    SimpleButton button = new SimpleButton();
                    button.Name = "btn" + info.remark;
                    button.Text = info.name;
                    button.Location = new Point(width, 5);
                    button.PaintStyle = PaintStyles.Light;
                    button.AutoSize = true;
                    // button.ImageOptions.Image = Image.FromFile(Application.StartupPath + "\\" + info.Icon);
                    button.Click += (sender, e) =>
                    {
                        ToolFunction(sender, e);
                    };
                    this.panelControl1.AddControl(button);
                    width += button.Width;
                }
            }
        }
        public virtual void ToolFunction(object sender, EventArgs e)
        {
            string FunctionId = ((SimpleButton)sender).Name;
            switch (FunctionId)
            {
                case "btnAdd": btnAdd_Click(sender,e); break;
                case "btnEdit": btnEdit_Click(sender, e); break;
                case "btnSearch": btnSearch_Click(sender, e); break;
                case "btnCanle": btnCanel_Click(sender, e); break;
                case "btnSave": btnSave_Click(sender,e); break;
                case "btnDel": btnDel_Click(sender, e); break;

            }
        }
        public virtual void InitgrdListDataSource()
        {

        }
        public virtual bool SaveFunction()
        {
            return true;
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //InitToolBtntatus(EFormStatus.e);
            
            SearchFunction();
        }
        public virtual void SearchFunction()
        {
        }
        private void btnCanel_Click(object sender, EventArgs e)
        {
            xtraTab.TabPages[0].PageEnabled = true;
            InitToolBtntatus(EFormStatus.eCanel);
            CanelFunction();
            SetControlStatus(controlGroups, true);
        }
        public virtual void CanelFunction()
        {

        }

        public virtual bool CheckInput()
        {
            return true;
        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            if ("确定删除？".ShowYesNoAndTips() == DialogResult.Yes)
            {
                if (DelFunction())
                {
                    InitToolBtntatus(EFormStatus.eDelete);
                    InitgrdListDataSource();
                    ClearScreen();
                }
            }
        }

        public virtual bool DelFunction()
        {
            return true;
        }
        public  void SetControlStatus( LayoutControlGroup[] controlGroups, bool flag)
        {
            if (controlGroups != null)
            {
                foreach (LayoutControlGroup col in controlGroups)
                {
                    SetLayoutControlReadOnly(flag, col);
                }
            }
        }

        public void InitToolBtntatus(EFormStatus status)
        {
            switch (status)
            {
                case EFormStatus.eInit:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnSearch" )
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eView:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnDel" || btn.Name == "btnEdit")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eViewTab:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eAdd:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnCanel" || btn.Name == "btnSave" )
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eDelete:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnSearch" )
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eEdit:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnCanle" || btn.Name == "btnSave")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
                case EFormStatus.eCanel:
                    {
                        foreach (var button in panelControl1.Controls)
                        {
                            if (button.GetType().Name == "SimpleButton")
                            {
                                var btn = (SimpleButton)button;
                                if (btn.Name == "btnAdd" || btn.Name == "btnDel"||btn.Name== "btnEdit")
                                {
                                    btn.Enabled = true;
                                }
                                else
                                {
                                    btn.Enabled = false;
                                }
                            }
                        }
                    }; break;
            }
        }

        public void SetLayoutControlReadOnly(bool flag, LayoutControlGroup col)
        {
            foreach (var con1 in col.Items)
            {
                if (con1.GetType().Name == "LayoutControlItem")
                {
                    LayoutControlItem con = con1 as LayoutControlItem;
                    if (con.Control == null) continue;
                    {
                        DevExpress.XtraEditors.BaseEdit d = con.Control as BaseEdit;
                        if (d != null && !string.IsNullOrEmpty(d.Name))
                        {
                            if (d.Name.Substring(3) == "creatorId" || d.Name.Substring(3) == "createTime" || d.Name.Substring(3) == "editorId" || d.Name.Substring(3) == "editTime" || d.Name.Substring(3) == "id")
                            {
                                d.ReadOnly = true;
                            }
                            else
                            {
                                d.ReadOnly = flag;
                            }
                        }
                    }
                }
            }
        }
        public  void ClearScreen()
        {
            this.ID = "";////需要设置为空，表示新增
            ClearControlValue(this);
        }
        public void ClearControlValue(System.Windows.Forms.Control ctrl)
        {
            ClearSinglelValue(ctrl);
            if (ctrl.Controls.Count > 0)
            {
                foreach (System.Windows.Forms.Control control in ctrl.Controls)
                {
                    ClearControlValue(control);
                }
            }
        }
        public  void ClearSinglelValue(System.Windows.Forms.Control ctrl)
        {
            switch (ctrl.GetType().Name)
            {
                case "TextEdit":
                case "MemoEdit":
                    (ctrl).Text = "";
                    break;
                case "LookUpEdit":
                    ((LookUpEdit)ctrl).EditValue="";
                    break;
                case "CheckEdit":
                    ((CheckEdit)ctrl).Checked = false;
                    break;

                case "ComboBoxEdit":
                    ((ComboBoxEdit)ctrl).EditValue = "";
                    break;
                case "TreeListLookUpEdit":
                    ((TreeListLookUpEdit)ctrl).EditValue = "";
                    break;
                case "CheckedComboBoxEdit":
                    ((CheckedComboBoxEdit)ctrl).SetEditValue("");
                    break;
                case "DateEdit":
                    ((DateEdit)ctrl).EditValue=null;
                    break;
            }
        }

        private void FrmBaseForm_Load(object sender, EventArgs e)
        {
            InitgrdListDataSource();
        }
    }
    /// <summary>
    /// 窗体状态
    /// </summary>
    public enum EFormStatus
    {
        eViewTab = 5,//tab切换
        eView = 0,//详情
        eAdd = 1,//新增
        eEdit = 2,//编辑
        eDelete = 3,//删除
        eCanel = 6,//取消
        eInit=7//初始化
    }
}