using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepair.Models.Common;
using DeviceRepair.Models.Enum;
using DeviceRepair.Utils;
using System;
using System.Linq;
using System.Windows.Forms;
using TsSFCDevice.Client.Biz;
using TsSFCDevice.Client.Biz.Base.Utils;
using TsSFCDevice.Client.Biz.Impl;
using TsSFCDevice.Client.Launch.Plan;
using TsSFCDevice.Client.Launch.sysConfig;

namespace TsSFCDevice.Client.Launch
{
    public partial class frm_Launch : RibbonForm
    {
        private BarButtonItem CurrentMenu;

        public frm_Launch()
        {
            InitializeComponent();
            AuthLayOut(false);
        }

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


            //btn_DrivePlan.Tag = OperationAuthConstValue.PM_PLAN;
            //btn_FormVersionMaintenence.Tag = OperationAuthConstValue.PM_CheckFORM;
            //barButtonItem6.Tag = OperationAuthConstValue.PM_PRESERVE_Watch;

            //Am_Plans.Tag = OperationAuthConstValue.AM_PLAN;
            //Am_Form.Tag = OperationAuthConstValue.AM_CheckFORM;
            //Am_Record.Tag = OperationAuthConstValue.AM_PRESERVE_Watch;

            //bar_btn_EquipmentMaintenanceLst.Tag = OperationAuthConstValue.Device_REPAIRRECORD;
            //barButtonItem7.Tag = OperationAuthConstValue.Device_REPAIRRECORD;

            //btnTagMgr.Tag = OperationAuthConstValue.Device_TAG;

            //barSubItem1.Tag = OperationAuthConstValue.Custom_FIELD;
            //bar_SymptomlDistinction.Tag = OperationAuthConstValue.Custom_FIELD_SYMPTOMLDISTINCTION;
            //bar_Maintenance.Tag = OperationAuthConstValue.Custom_FIELD_MAINTENANCEN;
            //bar_Accessories.Tag = OperationAuthConstValue.Custom_FIELD_ACCESSORIES;
            //barWherefailureoccurred.Tag = OperationAuthConstValue.Custom_FIELD_WHEREFAILUREOCCURRED;

            //btn_Report01.Tag = "BIZ_EQUIP_65";
            //btn_treeView.Tag = "BIZ_EQUIP_65";
            //btnPlanCheckJump.Tag = OperationAuthConstValue.PLAN_JUMP_CHECK;
            //btnSystemSetting.Tag = OperationAuthConstValue.Sys_Config;


            //btn_DrivePlan.Tag = "BIZ_EQUIP_26";
            //btn_FormVersionMaintenence.Tag = "BIZ_EQUIP_32";
            //barButtonItem6.Tag = "BIZ_EQUIP_39";

            //Am_Plans.Tag = "BIZ_EQUIP_43";
            //Am_Form.Tag = "BIZ_EQUIP_49";
            //Am_Record.Tag = "BIZ_EQUIP_56";

            //bar_btn_EquipmentMaintenanceLst.Tag = "BIZ_EQUIP_01";
            //barButtonItem7.Tag = "BIZ_EQUIP_01";

            //btnTagMgr.Tag = "BIZ_EQUIP_59";

            //barSubItem1.Tag = "BIZ_EQUIP_08";
            //bar_SymptomlDistinction.Tag = "BIZ_EQUIP_09";
            //bar_Maintenance.Tag = "BIZ_EQUIP_13";
            //bar_Accessories.Tag = "BIZ_EQUIP_17";
            //barWherefailureoccurred.Tag = "BIZ_EQUIP_21";

            //btn_Report01.Tag = "BIZ_EQUIP_65";
            //btn_treeView.Tag = "BIZ_EQUIP_65";
            //btnPlanCheckJump.Tag = "BIZ_EQUIP_72";
            //btnSystemSetting.Tag = "BIZ_EQUIP_71";


            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 (!Biz.Base.Utils.Utility.SystemRuntimeInfo.CurrentUser.UserName.IsNull())
                {
                    bSubLblUser.Caption = $"{Biz.Base.Utils.Utility.SystemRuntimeInfo.CurrentUser.UserName}";
                }

                //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;
                    default:
                        break;
                }
            }));
        }


        #region 设备维修

        /// <summary>
        /// 设备维修台账 - （新）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bar_btn_EquipmentMaintenanceLst_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.Device_REPAIRRECORD))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (Maintain.pageMaintainView view = new Maintain.pageMaintainView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        #endregion

        /// <summary>
        /// 设备类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Maintenance_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

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

        }

        #region PM 的保养

        /// <summary>
        /// 设备保养计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrivePlan_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_PLAN))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (Plan.pageDevicePlanView view = new Plan.pageDevicePlanView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

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

                using (CheckForm.pageCheckFormView view = new CheckForm.pageCheckFormView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        #endregion


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

                using (Device.pageDeviceView view = new Device.pageDeviceView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

                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)
        {

        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UserManager_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        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_PlanCount_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_CheckFORM_Add))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }


                splashScreenManager1.ShowWaitForm();
                using (page_PlanDialog view = new page_PlanDialog())
                {
                    splashScreenManager1.TryCloseWait();
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_PRESERVE_Watch))
            {
                throw new Exception($"当前账号缺少此操作的权限");
            }

            using (pageRecordView view = new pageRecordView(EnumDeviceBelong.PM))
            {
                view.ShowDialog(this);
            }
        }

        #region 自定义字段

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

                using (CustomField.pageFieldCode view = new CustomField.pageFieldCode("SYMPTOMLDISTINCTION", OperationAuthConstValue.Custom_FIELD_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 (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.Custom_FIELD_MAINTENANCEN))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (CustomField.pageFieldCode view = new CustomField.pageFieldCode("MAINTENANCE", OperationAuthConstValue.Custom_FIELD_MAINTENANCEN, "维修方式"))
                {
                    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 (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.Custom_FIELD_ACCESSORIES))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (CustomField.pageFieldCode view = new CustomField.pageFieldCode("ACCESSORIES", OperationAuthConstValue.Custom_FIELD_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 (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.Custom_FIELD_WHEREFAILUREOCCURRED))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

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

        #endregion

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

                using (pageSytemSetting view = new pageSytemSetting())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

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

        }

        /// <summary>
        /// AM 的树形设备清单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_List_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

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

                using (Plan.AM.pageDeviceAMPlanView view = new Plan.AM.pageDeviceAMPlanView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

                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 (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.Device_TAG))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (Tag.TagFormView view = new 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)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.AM_CheckFORM))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (CheckForm.pageCheckFormView view = new CheckForm.pageCheckFormView(EnumDeviceBelong.AM))
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// AM的保养记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Am_Record_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.AM_PRESERVE_Watch))
            {
                throw new Exception($"当前账号缺少此操作的权限");
            }

            using (pageRecordView view = new pageRecordView(EnumDeviceBelong.AM))
            {
                view.ShowDialog(this);
            }
        }


        #region 方法

        void AuthLayOut(bool beRefreshAuths)
        {
            if (beRefreshAuths)
            {
                #region 重新加载用户权限角色集合 Myl20210531

                //UserRepository vUserRepos = new UserRepository(false);
                //Utility.SystemRuntimeInfo.CurrentUser.Roles = vUserRepos.UserRolesGet(Utility.SystemRuntimeInfo.CurrentUser.GUID.Value.ToString());
                //Utility.SystemRuntimeInfo.CurrentUser.Auths = vUserRepos.UserAuthsGet(Utility.SystemRuntimeInfo.CurrentUser.UserCode);

                //if (Utility.SystemRuntimeInfo.CurrentUser.Roles == null || Utility.SystemRuntimeInfo.CurrentUser.Auths == null)
                //{
                //    XtraMessageBoxHelper.Warn("用户重新分配了角色或权限，重现分配后，角色或者权限为空！");
                //    ribbon.Enabled = false;
                //}

                #endregion
            }

            ribbonControl.Pages.ForEach(x => { PageControlAuthLayOut(x); });
        }

        void PageControlAuthLayOut(RibbonPage vRPage)
        {
            vRPage.Groups.ForEach(A => { PageGroupAuthLayOut(A); });

            var vVisibleGroup = vRPage.Groups.FirstOrDefault(A =>
            {
                return A.Visible == true;
            });

            if (vVisibleGroup == null)
            {
                vRPage.Visible = false;
            }
            else
            {
                vRPage.Visible = true;
            }
        }

        void PageGroupAuthLayOut(RibbonPageGroup vRPageGroup)
        {
            bool bShow = false;
            if (vRPageGroup.ItemLinks.Count > 0)
            {
                foreach (var item in vRPageGroup.ItemLinks)
                {
                    if (item is BarButtonItemLink)
                    {
                        #region BarButtonItemLink

                        var vBar = (BarButtonItemLink)item;
                        if (vBar.Item != null)
                        {
                            if (vBar.Item.Tag != null)
                            {
                                if (Utility.SystemRuntimeInfo.AuthValidate(vBar.Item.Tag.ToString()))
                                {
                                    if (vBar.Item.ButtonStyle != BarButtonStyle.DropDown)
                                    {
                                        vBar.Item.Visibility = BarItemVisibility.Always;
                                    }
                                    else
                                    {
                                        PopMenuBarButtonItemAuthLayOut(vBar.Item);
                                    }
                                }
                                else
                                {
                                    if (vBar.Item.ButtonStyle != BarButtonStyle.DropDown)
                                    {
                                        vBar.Item.Visibility = BarItemVisibility.Never;
                                    }
                                    else
                                    {
                                        PopMenuBarButtonItemAuthLayOut(vBar.Item);
                                    }
                                }
                            }
                            else
                            {
                                vBar.Item.Visibility = BarItemVisibility.Always;
                            }
                        }
                        else
                        {
                            vBar.Item.Visibility = BarItemVisibility.Always;
                        }
                        if (vBar.Item.Visibility == BarItemVisibility.Always)
                        {
                            bShow = true;
                        }
                        #endregion
                    }
                    else if (item is BarSubItemLink)
                    {
                        var vBar = (BarSubItemLink)item;

                        BarItemVisibility vBarVisibility = BarItemVisibility.Never;
                        foreach (BarButtonItemLink iBar in vBar.VisibleLinks)
                        {
                            if (Utility.SystemRuntimeInfo.AuthValidate(iBar.Item.Tag.ToString()))
                            {
                                vBar.Item.Visibility = BarItemVisibility.Always;
                                vBarVisibility = BarItemVisibility.Always;
                            }
                            else
                            {
                                vBar.Item.Visibility = BarItemVisibility.Never;
                            }
                        }
                        vBar.Visible = vBarVisibility == BarItemVisibility.Always;
                    }

                    var vVisibleGroup = vRPageGroup.ItemLinks.FirstOrDefault(
                   A =>
                   {
                       if (A is BarButtonItemLink)
                       {
                           return ((BarButtonItemLink)A).Item.Visibility == BarItemVisibility.Always;
                       }
                       else if (A is BarSubItemLink)
                       {
                           return ((BarSubItemLink)A).Item.Visibility == BarItemVisibility.Always;
                       }
                       else
                       {
                           return true;
                       }
                   }
               );
                    if (vVisibleGroup == null)
                    {
                        vRPageGroup.Visible = false;
                    }
                    else
                    {
                        vRPageGroup.Visible = true;
                    }
                }
            }
            else
            {
                vRPageGroup.Visible = true;
            }
        }

        void PopMenuBarButtonSubItemAuthLayOut(BarSubItem barItem, ref bool bShow)
        {
            if ((barItem.ItemLinks?.Count() ?? 0) == 0)
            {
                barItem.Visibility = BarItemVisibility.Always;
            }
            else
            {
                foreach (var item in barItem.ItemLinks)
                {
                    if (item is BarButtonItemLink)
                    {
                        #region BarButtonItemLink
                        var vPopBarItem = item as BarButtonItemLink;
                        if (vPopBarItem.Item != null)
                        {
                            if (vPopBarItem.Item.Tag != null)
                            {
                                if (Utility.SystemRuntimeInfo.AuthValidate(vPopBarItem.Item.Tag.ToString()))
                                {
                                    bShow = true;
                                    vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                                }
                                else
                                {
                                    if (Utility.SystemRuntimeInfo.AuthStartWithValidate(vPopBarItem.Item.Tag.ToString()))
                                    {
                                        bShow = true;
                                        vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                                    }
                                    else
                                    {
                                        vPopBarItem.Item.Visibility = BarItemVisibility.Never;
                                    }
                                }
                            }
                            else
                            {
                                bShow = true;
                                vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                            }
                        }
                        #endregion
                    }
                    else if (item is BarSubItemLink)
                    {
                        var vPopSubBarItem = item as BarSubItemLink;
                        if (vPopSubBarItem != null)
                        {
                            var vSubBarItem = vPopSubBarItem.Item as BarSubItem;
                            var vShowFlag = false;
                            PopMenuBarButtonSubItemAuthLayOut(vSubBarItem, ref vShowFlag);
                            if (vShowFlag)
                            {
                                bShow = true;
                            }
                        }
                    }
                }
                if (bShow)
                {
                    barItem.Visibility = BarItemVisibility.Always;
                }
                else
                {
                    barItem.Visibility = BarItemVisibility.Never;
                }
            }
        }

        void PopMenuBarButtonItemAuthLayOut(BarButtonItem barItem)
        {
            bool bShow = false;
            var pm = barItem.DropDownControl as PopupMenu;
            if (pm == null)
            {
                barItem.Visibility = BarItemVisibility.Always;
            }
            else
            {
                foreach (var item in pm.ItemLinks)
                {
                    if (item is BarButtonItemLink)
                    {
                        #region BarButtonItemLink
                        var vPopBarItem = item as BarButtonItemLink;
                        if (vPopBarItem.Item != null)
                        {
                            if (vPopBarItem.Item.Tag != null)
                            {
                                if (Utility.SystemRuntimeInfo.AuthValidate(vPopBarItem.Item.Tag.ToString()))
                                {
                                    bShow = true;
                                    vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                                }
                                else
                                {
                                    if (Utility.SystemRuntimeInfo.AuthStartWithValidate(vPopBarItem.Item.Tag.ToString()))
                                    {
                                        bShow = true;
                                        vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                                    }
                                    else
                                    {
                                        vPopBarItem.Item.Visibility = BarItemVisibility.Never;
                                    }
                                }
                            }
                            else
                            {
                                bShow = true;
                                vPopBarItem.Item.Visibility = BarItemVisibility.Always;
                            }
                        }
                        #endregion
                    }
                    else if (item is BarSubItemLink)
                    {
                        var vPopSubBarItem = item as BarSubItemLink;
                        if (vPopSubBarItem != null)
                        {
                            var vSubBarItem = vPopSubBarItem.Item as BarSubItem;
                            var vShowFlag = false;
                            PopMenuBarButtonSubItemAuthLayOut(vSubBarItem, ref vShowFlag);
                            if (vShowFlag)
                            {
                                bShow = true;
                            }
                        }
                    }
                }
                if (bShow)
                {
                    barItem.Visibility = BarItemVisibility.Always;
                }
                else
                {
                    barItem.Visibility = BarItemVisibility.Never;
                }
            }
        }
        #endregion

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

                using (Maintain.pageDeviceMaintenanceFormView view = new Maintain.pageDeviceMaintenanceFormView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 清理缓存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void batBtnRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            Utility.SystemRuntimeInfo.CurrentUsersCaches = null;
            Utility.SystemRuntimeInfo.CurrentDeviceCaches = null;
            Utility.SystemRuntimeInfo.CurrentDevRootCaches = null;
        }

        /// <summary>
        /// 树形视图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_treeView_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_Device_KH)
                    && !Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_Device_OEM)
                    )
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (Device.pageDeviceTreeView view = new Device.pageDeviceTreeView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 跳过当前日期生成设备校验计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlanCheckJump_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PLAN_JUMP_CHECK))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                using (pageJumpCheckView view = new pageJumpCheckView())
                {
                    view.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void barButtonItem8_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            try
            {
                var resRtn = PlanRepository.Instance.Get_EquipmentPlanIsComplete("198", 1);
                if (!resRtn.IsSuccess)
                {
                    throw new Exception(resRtn.Message);
                }

                if (!resRtn.Message.IsNull())
                {
                    XtraMessageBoxHelper.Warn(resRtn.Message);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}