using DevExpress.XtraEditors;
using DeviceRepairAndOptimization.Data;
using DeviceRepairAndOptimization.Models;
using System;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages
{
    public partial class page_Setting : FormBase
    {
        private UserSettingModel CurrentSetting;
        public page_Setting()
        {
            InitializeComponent();
        }

        void DataLoads()
        {
            UserSettingModel us = SettingMaintenance.Instance.GetUserSettingModel(GlobalInfo.CurrentUser.AutoID);

            if (us == null)
            {
                us = new UserSettingModel();
                us.AutoID = 0;
                us.PlanAlarmCycle = 30;
                us.UserID = GlobalInfo.CurrentUser.AutoID;
                SettingMaintenance.Instance.SaveUserSettingModelAsync(us);
            }

            this.input_PlanAlarmCycle.Value = us.PlanAlarmCycle.Value;
            CurrentSetting = us;
        }

        /// <summary>
        /// 保存到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                int resultInt = SettingMaintenance.Instance.SaveUserSettingModel(CurrentSetting);
                if (resultInt > 0)
                {
                    XtraMessageBox.Show("操作成功！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void page_Setting_Load(object sender, EventArgs e)
        {
            DataLoads();
            input_PlanAlarmCycle.ValueChanged += (s, en) =>
            {
                NumericUpDown control = (NumericUpDown)s;
                CurrentSetting.PlanAlarmCycle = (int)control.Value;
            };
        }

    }
}
