using CsharpHttpHelper;
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using DeviceRepairAndOptimization.Common;
using System;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.Common
{
    public partial class pageSignature : XtraForm
    {
        /// <summary>
        /// 操作人
        /// </summary>
        public string OperateBy
        {
            get
            {
                return bTxtUserCode.Text.Trim();
            }
        }

        /// <summary>
        /// 操作人密码
        /// </summary>
        public string OperateByPwd
        {
            get
            {
                return bTxtPassword.Text.Trim();
            }
        }

        /// <summary>
        /// 备注
        /// </summary>
        public string ConfirmNotes
        {
            get
            {
                return bTxtNotes.Text.Trim();
            }
        }

        /// <summary>
        /// 待认证的权限字符串
        /// </summary>
        public string AuthString
        {
            get; set;
        }

        private UserConfirmModel m_ConfirmModel;
        public UserConfirmModel ConfirmModel
        {
            get
            {
                return m_ConfirmModel;
            }
            set
            {
                m_ConfirmModel = value;
            }
        }

        /// <summary>
        /// 验证内容描述
        /// </summary>
        public string ConfirmContent
        {
            get;
            set;
        }

        public bool? NeedNotes { get; set; }
        public bool? CanNa { get; set; }

        public int NoteMaxLen { get; set; } = 200;

        public ApiOperationRtn OperationResult;

        public pageSignature()
        {
            InitializeComponent();
        }

        private void pageSignature_Load(object sender, EventArgs e)
        {
            try
            {
                Text = $"用户确认-{ConfirmContent}";
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(OperateBy))
                {
                    XtraMessageBoxHelper.Error("请录入账号！");
                    bTxtUserCode.Focus();
                    return;
                }

                if (OperateBy.Length > 50)
                {
                    XtraMessageBoxHelper.Error("输入的账号长度不正确！");
                    bTxtUserCode.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(OperateByPwd))
                {
                    XtraMessageBoxHelper.Error("请录入密码！");
                    bTxtPassword.Focus();
                    return;
                }

                if (OperateByPwd.Length > 200)
                {
                    XtraMessageBoxHelper.Error("输入的密码长度不正确！");
                    bTxtUserCode.Focus();
                    return;
                }

                if (NeedNotes ?? false)
                {
                    if (string.IsNullOrEmpty(ConfirmNotes))
                    {
                        XtraMessageBoxHelper.Error("备注必须录入，请录入备注！");
                        bTxtNotes.Focus();
                        return;
                    }

                    if (!CanNa ?? true)
                    {
                        if (ConfirmNotes.Equals("NA", StringComparison.CurrentCultureIgnoreCase) || ConfirmNotes.Equals("N/A", StringComparison.CurrentCultureIgnoreCase))
                        {
                            XtraMessageBoxHelper.Error("备注必须录入且不允许为N/A或NA，请录入备注！");
                            bTxtNotes.Focus();
                            return;
                        }
                    }

                    if (ConfirmNotes.Length > NoteMaxLen)
                    {
                        XtraMessageBoxHelper.Error($"备注必须录入且长度不允许大于{NoteMaxLen}，请检查！");
                        bTxtNotes.Focus();
                        return;
                    }

                    APIResponseData apiResponseData = Biz.UserManager.Instance.UserConfirm(new DeviceRepair.Models.History.UserConfirmHistory
                    {
                        bSuccess = false,
                        cContent = ConfirmContent,
                        cModel = ConfirmModel.Description(),
                        ConfirmAuth = AuthString,
                        UserCode = OperateBy,
                        PassWord = HttpHelper.ToMD5(OperateByPwd),
                        Note = ConfirmNotes
                    });

                    if (!apiResponseData.IsSuccess)
                        throw new Exception(apiResponseData.Message);

                    OperationResult = apiResponseData.ToDeserializeObject<ApiOperationRtn>();
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}