using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraEditors;
using TsSFCDevice.Client.Launch.Common;
using DeviceRepair.Models;
using DeviceRepair.Utils;
using DevExpress.XtraEditors.DXErrorProvider;

namespace TsSFCDevice.Client.Launch.sysConfig
{
    public partial class pageJumpCheckEdit : ToolbarForm
    {
        public DeviceInformationInfo CurrentDeviceInfo = null;

        public DateTime? CheckDate { get { return (ipsCheckDate.EditValue + "").IsNull() ? null : (DateTime?)Convert.ToDateTime(ipsCheckDate.EditValue); } }

        public pageJumpCheckEdit()
        {
            InitializeComponent();
        }

        private void ipsEquipmentID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Clear)
            {
                CurrentDeviceInfo = null;
                ((sender as ButtonEdit)).EditValue = "";
            }
            else
            {
                using (pageDeivceView view = new pageDeivceView())
                {
                    view.Text = "设备选择";
                    if (view.ShowDialog(this) == DialogResult.OK)
                    {
                        CurrentDeviceInfo = view.CurrentDeviceInfo;
                        ipsEquipmentID.EditValue = $"{view.CurrentDeviceInfo.EquipmentName}({view.CurrentDeviceInfo.EquipmentID})";
                    }
                }
            }
        }

        private void btnCommit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                InitializeValidationProvider();
                if (dxValidationProvider1.Validate())
                {
                    if (CurrentDeviceInfo == null)
                        throw new ArgumentException("校验的设备不能为空！");

                    if (!CheckDate.HasValue)
                        throw new ArgumentException("校验时间不能为空！");

                    if (CheckDate.Value < new DateTime(2000, 01, 01))
                        throw new ArgumentException("校验时间最小为2000年01月01日！");

                    if (CheckDate.Value > new DateTime(2999, 12, 31))
                        throw new ArgumentException("校验时间最大为2999年12月31日！");

                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 初始化验证控件
        /// </summary>
        private void InitializeValidationProvider()
        {
            try
            {
                dxValidationProvider1.RemoveControlError(ipsEquipmentID);
                dxValidationProvider1.RemoveControlError(ipsCheckDate);

                dxValidationProvider1 = new DXValidationProvider();
                //实例化一个必填规则，错误提示为：该字段不能为空
                ConditionValidationRule required = new ConditionValidationRule("RequiredRule", ConditionOperator.IsNotBlank) { ErrorText = $"字段不能为空" };

                required.ErrorText = "请先选择设备";
                dxValidationProvider1.SetValidationRule(ipsEquipmentID, required);
                dxValidationProvider1.SetIconAlignment(ipsEquipmentID, ErrorIconAlignment.MiddleLeft);

                required.ErrorText = "请选择校验日期";
                dxValidationProvider1.SetValidationRule(ipsCheckDate, required);
                dxValidationProvider1.SetIconAlignment(ipsCheckDate, ErrorIconAlignment.MiddleLeft);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}