using DevExpress.XtraBars;
using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepairAndOptimization.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.Maintain
{
    public partial class dlgAccessoriesEdit : ToolbarForm
    {
        public List<DeviceWarrantyRequestAccessoriesInfo> CurrentAccessories = null;
        DeviceWarrantyRequestAccessoriesInfo CurrentAccessoriesInfoModel = null;
        int m_SelectedCurrentRowIndex = 0;
        public dlgAccessoriesEdit(List<DeviceWarrantyRequestAccessoriesInfo> currentAccessories = null)
        {
            InitializeComponent();

            CurrentAccessories = currentAccessories ?? new List<DeviceWarrantyRequestAccessoriesInfo>();
            this.Load += DlgAccessoriesEdit_Load;
        }

        private void DlgAccessoriesEdit_Load(object sender, EventArgs ee)
        {
            GridViewInitialize(this.gridView1);

            /// 自增长行号
            gridView1.CustomDrawRowIndicator += (s, 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;
                }
            };

            /// 修改字段标题
            gridView1.CustomDrawColumnHeader += (s, e) =>
            {
                if (e.Column != null && e.Column.Caption == "Selection")
                    e.Info.Caption = "选择";
            };

            /// 单元格点击
            gridView1.RowCellClick += (s, e) =>
            {
                if (e.Button == MouseButtons.Right)
                {
                    e.Handled = true;
                    return;
                }

                if (e.Column.Caption == "Selection")
                {
                    if (this.m_SelectedCurrentRowIndex == e.RowHandle && this.gridView1.IsRowSelected(e.RowHandle))
                        this.gridView1.UnselectRow(e.RowHandle);
                }
            };

            /// 表格选中行更改
            gridView1.FocusedRowChanged += (s, e) =>
            {
                try
                {
                    if (e.FocusedRowHandle >= 0)
                    {
                        m_SelectedCurrentRowIndex = e.FocusedRowHandle;
                        CurrentAccessoriesInfoModel = gridView1.GetRow(e.FocusedRowHandle) as DeviceWarrantyRequestAccessoriesInfo;

                        #region 修改选定状态
                        if (gridView1.SelectedRowsCount > 0)
                        {
                            for (int i = 0; i < this.gridView1.RowCount; i++)
                            {
                                if (this.gridView1.IsRowSelected(i) && this.gridView1.FocusedRowHandle.ToString().Equals(i.ToString()) == false)
                                {
                                    this.gridView1.UnselectRow(i);
                                }
                            }
                        }

                        this.gridView1.SelectRow(e.FocusedRowHandle);
                        #endregion
                    }
                    else
                    {
                        CurrentAccessoriesInfoModel = null;
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBoxHelper.Error(ex.Message);
                }
            };

            if (CurrentAccessories == null)
                CurrentAccessories = new List<DeviceWarrantyRequestAccessoriesInfo>();
            else
            {
                gridControl1.DataSource = CurrentAccessories;
                gridView1.BestFitColumns();
            }
        }

        /// <summary>
        /// 行选择模式，且全部只读
        /// </summary>
        /// <param name="view"></param>
        void GridViewInitialize(DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            view.OptionsBehavior.Editable = false;
            view.OptionsBehavior.ReadOnly = true;

            view.OptionsSelection.EnableAppearanceFocusedCell = false;
            view.OptionsScrollAnnotations.ShowSelectedRows = DevExpress.Utils.DefaultBoolean.False;

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

            view.BestFitColumns();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AddItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                using (dlgAccessoriesLst dlg = new dlgAccessoriesLst())
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        splashScreenManager1.ShowWaitForm();
                        if (CurrentAccessories.Any(x => x.FieldID == dlg.CurrentAccessories.FieldID))
                        {
                            throw new Exception("当前配件已存在！");
                        }

                        CurrentAccessories.Add(dlg.CurrentAccessories);
                        gridControl1.DataSource = CurrentAccessories;
                        gridView1.BestFitColumns();
                        splashScreenManager1.TryCloseWait();
                    }
                }
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 删除配件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DeleteItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!CurrentAccessories.Any(x => x.FieldID == CurrentAccessoriesInfoModel.FieldID))
                {
                    throw new Exception("请选择要删除的配件行！");
                }

                CurrentAccessories.Remove(CurrentAccessoriesInfoModel);
                gridControl1.DataSource = CurrentAccessories;
                gridView1.BestFitColumns();
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 修改配件数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_EditItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!CurrentAccessories.Any(x => x.FieldID == CurrentAccessoriesInfoModel.FieldID))
                {
                    throw new Exception("请选择要修改的配件行！");
                }

                int Count = XtraInputBox.Show<int>("请输入配件的数量：", "配件添加", 1);
                if (Count > 0)
                {
                    CurrentAccessoriesInfoModel.AccessoriesCount = Count;
                    gridControl1.DataSource = CurrentAccessories;
                    gridView1.BestFitColumns();
                }
                else
                {
                    throw new Exception("配件的数量必须大于零！");
                }

                gridControl1.DataSource = CurrentAccessories;
                gridView1.BestFitColumns();
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 提交到列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SaveAccessories_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}