using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using DeviceRepair.Models.Record;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TsSFCDevice.Control.Maintenance
{
    public partial class cfMaintenanceRecord : XtraUserControl
    {
        [Category("Behavior"), Description("关闭窗体时执行的操作。Exit退出程序，HideToNotify隐藏到通知栏。")]
        public DevExpress.XtraLayout.Utils.LayoutVisibility ShowFilterPanel
        {
            get
            {
                return layoutControlGroup1.Visibility;
            }
            set
            {
                layoutControlGroup1.Visibility = value;
            }
        }

        public List<MaintenanceRecordHistoryModel> Datas;
        public MaintenanceRecordHistoryModel CurrentModel;

        public delegate void DataSearchEventHandler(MaintenanceFilterModel filter);
        public event DataSearchEventHandler DataSearch;

        private MaintenanceFilterModel _fitler = new MaintenanceFilterModel();
        public MaintenanceFilterModel filterModel
        {
            get
            {
                if (_fitler == null)
                    _fitler = new MaintenanceFilterModel();

                if (ShowFilterPanel == DevExpress.XtraLayout.Utils.LayoutVisibility.Always)
                {
                    _fitler.EquipmentName = ipsFilterString.EditValue + "";
                    _fitler.StartDate = this.ipsStartDate.DateTime < new DateTime(1753, 1, 1) ? new DateTime(1753, 1, 1) : this.ipsStartDate.DateTime;
                    _fitler.EndDate = this.ipsEndDate.DateTime;
                    _fitler.Banci = (EnumMaintenanceBanciType)DeviceRepair.Utils.EnumExtend.GetComboBoxItemValue(typeof(EnumMaintenanceBanciType), ipsBanci.EditValue + "");
                    _fitler.MaintenanceType = (EnumMaintenanceType)DeviceRepair.Utils.EnumExtend.GetComboBoxItemValue(typeof(EnumMaintenanceType), ipsMaintenanceType.EditValue + "");
                }
                return _fitler;
            }
            set
            {
                _fitler = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public cfMaintenanceRecord()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cfMaintenanceRecord_Load(object sender, EventArgs e)
        {
            layoutControlGroup1.Visibility = ShowFilterPanel;
            this.ipsStartDate.EditValue = DateTime.Today.AddMonths(-1);
            this.ipsEndDate.EditValue = DateTime.Today.AddDays(1).AddMilliseconds(-1);

            // 关闭列头右键菜单
            gridView1.OptionsMenu.EnableColumnMenu = false;

            // 关闭layoutControl 右键菜单
            layoutControl1.AllowCustomization = false;

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsBehavior.ReadOnly = true;

            /* 枚举类型遍历绑定控件 */
            ipsBanci.Properties.Items.BeginUpdate();
            ipsBanci.Properties.Items.AddRange(DeviceRepair.Utils.EnumExtend.GetComboBoxItems(typeof(EnumMaintenanceBanciType)));
            ipsBanci.Properties.Items.EndUpdate();
            ipsBanci.SelectedIndex = 0;

            ipsMaintenanceType.Properties.Items.BeginUpdate();
            ipsMaintenanceType.Properties.Items.AddRange(DeviceRepair.Utils.EnumExtend.GetComboBoxItems(typeof(EnumMaintenanceType)));
            ipsMaintenanceType.Properties.Items.EndUpdate();
            ipsMaintenanceType.SelectedIndex = 0;

            foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView1.Columns)
            {
                item.OptionsColumn.AllowEdit = false;
                item.OptionsColumn.AllowGroup = DevExpress.Utils.DefaultBoolean.True;
                item.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
                item.OptionsColumn.AllowShowHide = false;
            }
        }


        /// <summary>
        /// 根据条件搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                splashScreenManager1.ShowWaitForm();
                DataSearch?.Invoke(filterModel);
                splashScreenManager1.TryCloseWait();
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 数据刷新
        /// </summary>
        public void RefreshGrid()
        {
            gridControl1.BeginUpdate();
            gridControl1.DataSource = null;
            gridControl1.DataSource = Datas;
            gridControl1.EndUpdate();
            CurrentModel = null;

            // 设置行号列宽度
            if ((Datas?.Count ?? 0) > 0)
            {
                SizeF size = this.CreateGraphics().MeasureString(Datas.Count.ToString(), this.Font);
                gridView1.IndicatorWidth = Convert.ToInt32(size.Width) + 20;
                gridView_RowCellClick(gridView1, new RowCellClickEventArgs(new DevExpress.Utils.DXMouseEventArgs(MouseButtons.Left, 1, 1, 1, 1), 0, gcEquipmentID));
            }
        }

        /// <summary>
        /// 自增长行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                e.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }

        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            try
            {
                if (e.Button != MouseButtons.Right)
                {
                    CurrentModel = gridView1.GetRow(e.RowHandle) as MaintenanceRecordHistoryModel;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}
