using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DeviceRepair.Utils;
using DeviceRepair.Models.Plan;
using TsSFCDevice.Client.Biz.Impl;
using DevExpress.XtraGrid.Views.Grid;
using TsSFCDevice.Client.Biz.Base.Utils;

namespace TsSFCDevice.Client.Launch.sysConfig
{
    public partial class pageJumpCheckView : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        EquipmentJumpPlanCheckInfo m_CurrentModel = null;
        EquipmentJumpPlanCheckRepository m_Cmd;
        IList<EquipmentJumpPlanCheckInfo> Datas;

        /// <summary>
        /// 校验日期
        /// </summary>
        private DateTime m_CheckDate
        {
            get { return (baripsDate.EditValue + "").IsNull() ? DateTime.Today : Convert.ToDateTime(baripsDate.EditValue); }
        }

        /// <summary>
        /// 设备编号
        /// </summary>
        private string m_EquipmentID
        {
            get { return baripsEquipmentID.EditValue + ""; }
        }


        public pageJumpCheckView()
        {
            InitializeComponent();
            if (m_Cmd == null)
                m_Cmd = new EquipmentJumpPlanCheckRepository();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pageJumpCheckView_Load(object sender, EventArgs e)
        {
            // 关闭列头右键菜单
            gvControl.OptionsMenu.EnableColumnMenu = false;
            gvControl.OptionsBehavior.Editable = false;
            gvControl.OptionsBehavior.ReadOnly = true;
            gvControl.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvControl.OptionsScrollAnnotations.ShowSelectedRows = DevExpress.Utils.DefaultBoolean.False;

            gvControl.CustomDrawRowIndicator += GvControl_CustomDrawRowIndicator;
            gvControl.RowCellClick += GvControl_RowCellClick;
        }

        /// <summary>
        /// 单元格点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GvControl_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            m_CurrentModel = gvControl.GetRow(e.RowHandle) as EquipmentJumpPlanCheckInfo;
            barRemove.Enabled = m_CurrentModel != null;
        }

        /// <summary>
        /// 自增长行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GvControl_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.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 barSearch_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitializeGridData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barNews_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PLAN_JUMP_CHECK_Add))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (var view = new pageJumpCheckEdit())
                {
                    if (view.ShowDialog(this) == DialogResult.OK)
                    {
                        if (view.CurrentDeviceInfo != null && view.CheckDate.HasValue)
                        {
                            var result = m_Cmd.DataNews(view.CurrentDeviceInfo.EquipmentID, view.CheckDate.Value);
                            if (!result.IsSuccess)
                                throw new Exception(result.Message);
                            InitializeGridData();
                            XtraMessageBoxHelper.Info("操作成功！");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barRemove_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PLAN_JUMP_CHECK_DELETE))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                if (m_CurrentModel == null)
                {
                    throw new ArgumentNullException($"请先选择操作的数据！");

                }

                if (XtraMessageBoxHelper.AskYesNo($"是否确认执行删除操作！") == DialogResult.Yes)
                {
                    var result = m_Cmd.DataRemove(m_CurrentModel.AutoID);
                    if (!result.IsSuccess)
                        throw new Exception(result.Message);
                    InitializeGridData();
                    XtraMessageBoxHelper.Info("操作成功！");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        public void InitializeGridData()
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PLAN_JUMP_CHECK_Watch))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                splashScreenManager1.ShowWaitForm();
                Datas = m_Cmd.GetDatas(m_EquipmentID, m_CheckDate);

                if (Datas != null && Datas.Count > 0)
                {
                    foreach (var item in Datas)
                    {
                        var us = Utility.SystemRuntimeInfo.CurrentUsersCaches.FirstOrDefault(x => x.Id == item.CreateBy);
                        if (us != null)
                        {
                            item.CreatebyName = us.UserName;
                        }
                    }
                }

                gcControl.DataSource = Datas?.ToList();
                m_CurrentModel = null;
                barRemove.Enabled = false;

                int Counter = Datas?.Count ?? 0;
                if (Counter > 0)
                {
                    SizeF size = this.CreateGraphics().MeasureString(Datas.Count.ToString(), this.Font);
                    gvControl.IndicatorWidth = Convert.ToInt32(size.Width) + 20;
                    gvControl.FocusedRowHandle = 0;
                    GvControl_RowCellClick(gvControl, new RowCellClickEventArgs(new DevExpress.Utils.DXMouseEventArgs(MouseButtons.Left, 1, 1, 1, 1), 0, gcEquipmentID));
                }

                splashScreenManager1.CloseWaitForm();
            }
            catch (Exception ex)
            {
                splashScreenManager1.CloseWaitForm();
                throw new Exception(ex.Message);
            }
        }
    }
}