using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepair.Models.Common;
using DeviceRepairAndOptimization.Biz;
using DeviceRepairAndOptimization.Common;
using DeviceRepairAndOptimization.Pages;
using DeviceRepairAndOptimization.Pages.Log;
using DeviceRepairAndOptimization.Pages.Maintenance;
using DeviceRepairAndOptimization.Pages.Users;
using System;
using System.Linq;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization
{
    public partial class frm_Launch : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private BarButtonItem CurrentMenu;

        public frm_Launch()
        {
            InitializeComponent();
            GlobalInfo._RootForm = this;
        }

        private void frm_Launch_Load(object sender, EventArgs e)
        {
            Scheduler.Instance.CallBack += InvokeChangeFormText;
            Scheduler.Instance.Run();

            this.FormClosing += (ss, ee) =>
            {
                if (XtraMessageBoxHelper.AskYesNo("确认现在退出系统吗？") != DialogResult.Yes)
                {
                    ee.Cancel = true;
                }

                UserManager.Instance.LogOut();
            };

            //获取 tips 保养计划弹窗数据
            try
            {
                // 检测是否首次登录及超过3个月未修改密码
                APIResponseData apiResponseData2 = UserManager.Instance.CheckChangePwdDate(GlobalInfo.CurrentUser.LoginCode);
                if (apiResponseData2.IsSuccess && apiResponseData2.ToBool())
                {
                PassChange:
                    if (new frmPwdModify(false).ShowDialog(this) == DialogResult.OK)
                    {
                        XtraMessageBoxHelper.Info("操作成功！");
                    }
                    else
                    {
                        goto PassChange;
                    }
                }

                btn_VerNo.Caption = "V " + Application.ProductVersion;

                if (string.IsNullOrWhiteSpace(GlobalInfo.CurrentUser.RealName) || string.IsNullOrWhiteSpace(GlobalInfo.CurrentUser.LoginCode))
                    bSubLblUser.Visibility = BarItemVisibility.Never;
                else
                    bSubLblUser.Caption = GlobalInfo.CurrentUser.LoginCode + "-" + GlobalInfo.CurrentUser.RealName;

                using (page_PlanDialog pd = new page_PlanDialog())
                {
                    if (GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE))
                    {
                        if (pd.ShowDialog(this) == DialogResult.Cancel)
                        {
                            this.Close();
                            return;
                        }
                    }

                    if (GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PLAN))
                    {
                        //进入主程序
                        pnl_Content.ShowInPanel<page_MaintenancePlan>();
                        CurrentMenu = btn_DrivePlan;
                        SetButtonDown();
                        //SetPlansScheduleText();
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message, "错误");
            }
        }

        /// <summary>
        /// 委托更新显示值
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="Value"></param>
        protected void InvokeChangeFormText(string arg, object Value)
        {
            this.Invoke(new Action(() =>
            {
                switch (arg)
                {
                    case "SchedulerPlanTips":
                        btn_PlanCount.Caption = Value + "";
                        break;
                    case "CurrentYearPlanSchedule":
                        if (Value != null)
                        {
                            if (Value is CurrentYearPlanSchedule)
                                barLb_Schedule.Caption = ((CurrentYearPlanSchedule)Value).ToString();
                            else
                                barLb_Schedule.Caption = Value + "";
                        }
                        break;
                    default:
                        break;
                }
            }));
        }

        /// <summary>
        /// 设备类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Maintenance_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_DEVICELEDGER))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                pnl_Content.ShowInPanel(new page_DriveTypeTree());
                CurrentMenu = btn_Maintenance;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Import_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 文档维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FormVersionMaintenence_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FormVersion_PM))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_FormVersion>();
                CurrentMenu = btn_FormVersionMaintenence;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 设备保养计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrivePlan_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PLAN_PM))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_MaintenancePlan>();
                CurrentMenu = btn_DrivePlan;
                SetButtonDown();
                //SetPlansScheduleText();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 设备台账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Report01_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_DEVICELEDGER))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_DriveListInfo>();
                CurrentMenu = btn_Report01;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            // pnl_Content.ShowInPanel<page_Setting>();
        }

        /// <summary>
        /// 设备保养记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MaintenanceLst_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_LOG))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_MaintenanceView>();
                CurrentMenu = barButtonItem6;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (new frmPwdModify().ShowDialog(this) == DialogResult.OK)
                {
                    XtraMessageBoxHelper.Info("操作成功！");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }


        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UserManager_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.Base_UserManager))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                CurrentMenu = btn_UserManager;
                SetButtonDown();
                frmUsers us = new frmUsers();
                us.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.Base_RoleManager))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                CurrentMenu = barButtonItem5;
                SetButtonDown();
                frmRoles view = new frmRoles();
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        void SetButtonDown()
        {

            var DownControl = ribbonControl.Items.FirstOrDefault(x => x is BarBaseButtonItem && (x as BarBaseButtonItem).Down && x != CurrentMenu);
            if (DownControl != null)
            {
                (DownControl as BarBaseButtonItem).Down = false;
            }

            if (CurrentMenu != null)
            {
                CurrentMenu.ButtonStyle = BarButtonStyle.Check;
                CurrentMenu.Down = true;
            }
        }

        /// <summary>
        /// 设备维修台账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_RepairRecord_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        /// <summary>
        /// 显示计划提醒小窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PlanCount_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                splashScreenManager1.ShowWaitForm();
                if (GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE))
                {
                    splashScreenManager1.TryCloseWait();
                    new page_PlanDialog().ShowDialog(this);
                    return;
                }

                throw new Exception($"当前账号缺少此操作的权限");
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_LOG_PM))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_MaintenanceView>();
                CurrentMenu = barButtonItem6;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 现象区分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bar_SymptomlDistinction_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FIELD_SYMPTOMLDISTINCTION))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                SetButtonDown();
                bar_SymptomlDistinction.Down = true;
                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("SYMPTOMLDISTINCTION", "现象区分");
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 维修方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bar_Maintenance_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FIELD_MAINTENANCE))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("MAINTENANCE", "维修方式");
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 设备维修台账 - （新）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bar_btn_EquipmentMaintenanceLst_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_REPAIRRECORD))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                CurrentMenu = bar_btn_EquipmentMaintenanceLst;
                SetButtonDown();
                Pages.Maintain.pageMaintainView view = new Pages.Maintain.pageMaintainView();
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 配件维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FIELD_ACCESSORIES))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("ACCESSORIES", "配件维护");
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 故障发生地
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FIELD_WHEREFAILUREOCCURRED))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("WHEREFAILUREOCCURRED", "故障发生地");
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSystemSetting_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.Base_Config))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<pageSytemSetting>();
                CurrentMenu = btnSystemSetting;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 用户登录登出日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem13_ItemClick(object sender, ItemClickEventArgs e)
        {

            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.LOGS_USERLOGIN))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<page_UserLoginLog>();
                //CurrentMenu = barButtonItem12;
                //SetButtonDown();
            }
            catch (Exception ex)
            {
                //SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// AM 的树形设备清单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_List_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_DEVICELEDGER))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                pnl_Content.ShowInPanel(new Pages.AM.DeviceTreeView.pageAmDeviceTreeView());
                CurrentMenu = Am_List;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// AM的保养计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_Plans_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PLAN_AM))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<Pages.AM.Plan.pageAmMaintenancePlanView>();
                CurrentMenu = Am_Plans;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// tag
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTagMgr_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_TAG))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.Tag.TagFormView view = new Pages.Tag.TagFormView();
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// AM 的点检表维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_Form_ItemClick(object sender, ItemClickEventArgs e)
        {

            if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_FormVersion_AM))
            {
                XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                return;
            }

            pnl_Content.ShowInPanel<Pages.AM.FormVersion.pageFormVersionView>();
            CurrentMenu = Am_Form;
            SetButtonDown();
        }

        /// <summary>
        /// AM的保养记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_Record_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_AM))
            {
                XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                return;
            }

            Pages.AM.Preserve.pageRecordView view = new Pages.AM.Preserve.pageRecordView();
            view.ShowDialog(this);
        }
    }
}