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.Maintenance;
using DeviceRepairAndOptimization.Pages.Users;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Threading.Tasks;
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)
        {
            #region 显示代表的保养设备数量

            Task.Run(async () =>
            {
                while (true)
                {

                    int Count = await PlanManager.Instance.GetCurrentMonthPlanTipsCountAsync((s) =>
                    {
                        //获取返回值
                        try
                        {
                            APIResponseData apiResponseData = JsonConvert.DeserializeObject<APIResponseData>(s.Html);
                            if (apiResponseData.IsSuccess)
                            {
                                int C = apiResponseData.ToInt();
                                Invoke(new Action(() => { btn_PlanCount.Caption = C + ""; }));
                            }
                        }
                        catch (Exception ex)
                        {
                            Invoke(new Action(() => { btn_PlanCount.Caption = "0"; }));
                        }
                    });

                    if (Count >= 0)
                    {
                        Invoke(new Action(() => { btn_PlanCount.Caption = Count + ""; }));
                    }

                    Invoke(new Action(() =>
                    {
                        CurrentYearPlanSchedule schedule = PlanManager.Instance.GetCurrentYearPlanSchedule();
                        if (schedule != null)
                            this.barLb_Schedule.Caption = schedule.ToString();
                    }));

                    await Task.Delay(6000);
                }

            });

            #endregion

            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() == 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("BIZ_MAINTENANCE_ADD"))
                    {
                        if (pd.ShowDialog() == DialogResult.Cancel)
                        {
                            this.Close();
                            return;
                        }
                    }

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

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

                pnl_Content.ShowInPanel(new page_DriveTypeTree());
                //pnl_Content.ShowInPanel(new page_DriveTypeLst("设备类型"));
                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("BIZ_MAINTENANCEFORMREV_VIEW"))
                {
                    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("BIZ_PLAN_VIEW"))
                {
                    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("BIZ_DEVICELEDGER_VIEW"))
                {
                    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("BIZ_MAINTENANCE_LOG_VIEW"))
                {
                    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() == 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("BASE_USER_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                frmUsers us = new frmUsers();
                us.ShowDialog();
            }
            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("BASE_ROLE_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

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

        void SetButtonDown()
        {
            btn_DrivePlan.Down = false;
            btn_Maintenance.Down = false;
            barButtonItem6.Down = false;
            btn_FormVersionMaintenence.Down = false;
            btn_Report01.Down = false;
            btn_RepairRecord.Down = false;
            btnSystemSetting.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)
        {
            //try
            //{
            //    if (!GlobalInfo.HasRole("BIZ_REPAIRRECORD_VIEW"))
            //    {
            //        XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
            //        return;
            //    }

            //    pnl_Content.ShowInPanel<page_RepairRecordView>();
            //    CurrentMenu = btn_RepairRecord;
            //    SetButtonDown();
            //}
            //catch (Exception ex)
            //{
            //    SetButtonDown();
            //    XtraMessageBoxHelper.Error(ex.Message);
            //}
        }

        /// <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("BIZ_MAINTENANCE_ADD"))
                {
                    splashScreenManager1.TryCloseWait();
                    new page_PlanDialog().ShowDialog();
                    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("BIZ_MAINTENANCE_LOG_VIEW"))
                {
                    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("BIZ_FIELD_SYMPTOMLDISTINCTION_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("SymptomlDistinction", "现象区分");
                view.ShowDialog();
            }
            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("BIZ_FIELD_MAINTENANCE_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("Maintenance", "维修方式");
                view.ShowDialog();
            }
            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("BASE_ROLE_VIEW"))
                //{
                //    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                //    return;
                //}

                Pages.Maintain.pageMaintainView view = new Pages.Maintain.pageMaintainView();
                view.ShowDialog();
            }
            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("BIZ_FIELD_ACCESSORIES_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("Accessories", "配件维护");
                view.ShowDialog();
            }
            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("BIZ_FIELD_WHEREFAILUREOCCURRED_VIEW"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                Pages.CustomField.pageFieldCode view = new Pages.CustomField.pageFieldCode("WhereFailureOccurred", "故障发生地");
                view.ShowDialog();
            }
            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("BASE_CONFIG"))
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }
                pnl_Content.ShowInPanel<pageSytemSetting>();
                CurrentMenu = btnSystemSetting;
                SetButtonDown();
            }
            catch (Exception ex)
            {
                SetButtonDown();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}