using DeviceRepair.Models.Tag;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TsSFCDevice.Control.Tag
{
    public partial class xueTagForm : DevExpress.XtraEditors.XtraUserControl
    {
        private bool _IsView = false;
        [System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("是否禁用全部控件。")]
        public bool IsView
        {
            get { return _IsView; }
            set { _IsView = value; Invalidate(); }
        }

        TagRecordInfo currentData;
        public TagRecordInfo CurrentData
        {
            get { return currentData; }
            set
            {
                currentData = value;
                if (currentData != null)
                {
                    RefreshFormData();
                }
            }
        }

        public delegate void SelectDeviceEventHandler(xueTagForm Own);
        public event SelectDeviceEventHandler SelectDeviceDialog;

        public delegate void TagDataSavedEventHandler(TagRecordInfo tag);
        public event TagDataSavedEventHandler TagDataSaved;

        public xueTagForm()
        {
            InitializeComponent();
        }

        private void xueTagForm_Load(object sender, EventArgs e)
        {
            layoutControl1.AllowCustomization = false;
            layoutControl2.AllowCustomization = false;

            InitializeControl();

            if (!IsView)
            {
                SelectDeviceDialog?.Invoke(this);
            }
            else if (CurrentData?.AutoID != 0)
            {
            }
        }

        public void HideReceipt()
        {
            simpleSeparator1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlItem23.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }

        void InitializeControl()
        {
            ipsTagNo.Enabled = false;
            ipsCreatOn.Enabled = false;
            IpsEquipmentID_C.Enabled = false;
            ipsEquipmentName.Enabled = false;
            ipsCreatorName.Enabled = false;

            ipsBrokenDamaged.Enabled = !IsView;
            ipsAccess.Enabled = !IsView;
            ipsClean.Enabled = !IsView;
            ipsInspect.Enabled = !IsView;
            ipsSourceOfContamination.Enabled = !IsView;
            ipsNotInUse.Enabled = !IsView;
            ipsNotNeeded.Enabled = !IsView;
            ipsUnsafeConditions.Enabled = !IsView;
            ipsMissingComponents.Enabled = !IsView;
            ipsDetailsOfIssue.ReadOnly = IsView;

            ipsTagNo2.Enabled = !IsView;
            ipsReceiptOn.Enabled = !IsView;
            IpsEquipmentID_R.Enabled = !IsView;
            ipsEquipmentName2.Enabled = !IsView;
            ipsReceiptName.Enabled = !IsView;
        }

        public void RefreshFormData()
        {
            try
            {
                ipsTagNo.EditValue = CurrentData.AutoID;
                ipsCreatOn.EditValue = CurrentData.CreatOn.ToString("yyyy.MM.dd");
                IpsEquipmentID_C.EditValue = CurrentData.EquipmentDisPlayID;
                ipsEquipmentName.EditValue = CurrentData.EquipmentName;
                ipsCreatorName.EditValue = CurrentData.CreatorName;

                ipsBrokenDamaged.Checked = CurrentData.BrokenDamaged;
                ipsAccess.Checked = CurrentData.Access;
                ipsClean.Checked = CurrentData.Clean;
                ipsInspect.Checked = CurrentData.Inspect;
                ipsSourceOfContamination.Checked = CurrentData.SourceOfContamination;
                ipsNotInUse.Checked = CurrentData.NotInUse;
                ipsNotNeeded.Checked = CurrentData.NotNeeded;
                ipsUnsafeConditions.Checked = CurrentData.UnsafeConditions;
                ipsMissingComponents.Checked = CurrentData.MissingComponents;
                ipsDetailsOfIssue.EditValue = CurrentData.DetailsOfIssue;

                // 已接收
                if (CurrentData.TagFormStatus == "C")
                {
                    ipsTagNo2.EditValue = CurrentData.AutoID;
                    ipsReceiptOn.EditValue = CurrentData.ReceiptOn.Value.ToString("yyyy.MM.dd");
                    IpsEquipmentID_R.EditValue = CurrentData.EquipmentDisPlayID;
                    ipsEquipmentName2.EditValue = CurrentData.EquipmentName;
                    ipsReceiptName.EditValue = CurrentData.ReceiptName;
                    ipsDetailsOfIssue.EditValue += $"{Environment.NewLine}{currentData.ReceiptDetailsOfIssue}";
                }
                else
                {
                    ipsTagNo2.EditValue = "N/A";
                    ipsReceiptOn.EditValue = "N/A";
                    IpsEquipmentID_R.EditValue = "N/A";
                    ipsEquipmentName2.EditValue = "N/A";
                    ipsReceiptName.EditValue = "N/A";
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }


        public bool CanClose()
        {
            try
            {
                TagRecordInfo tagRecord = new TagRecordInfo();
                // 选项均未选择
                if (!ipsBrokenDamaged.Checked && !ipsAccess.Checked && !ipsClean.Checked && !ipsInspect.Checked && !ipsSourceOfContamination.Checked
                    && !ipsNotInUse.Checked && !ipsNotNeeded.Checked && !ipsUnsafeConditions.Checked && !ipsMissingComponents.Checked)
                {
                    throw new Exception("请至少选择一个报修选项!");
                }

                string DetailsOfIssue = ipsDetailsOfIssue.EditValue + "";
                if (string.IsNullOrWhiteSpace(DetailsOfIssue))
                {
                    throw new Exception("详情不能为空!");
                }
                else if (DetailsOfIssue.Length > 200)
                {
                    throw new Exception("详情长度不允许超过200字!");
                }
                tagRecord.AutoID = CurrentData.AutoID;
                tagRecord.EquipmentID = CurrentData.EquipmentID;
                tagRecord.EquipmentDisPlayID = CurrentData.EquipmentDisPlayID;
                tagRecord.EquipmentName = CurrentData.EquipmentName;

                tagRecord.BrokenDamaged = ipsBrokenDamaged.Checked;
                tagRecord.Access = ipsAccess.Checked;
                tagRecord.Clean = ipsClean.Checked;
                tagRecord.Inspect = ipsInspect.Checked;
                tagRecord.SourceOfContamination = ipsSourceOfContamination.Checked;
                tagRecord.NotInUse = ipsNotInUse.Checked;
                tagRecord.NotNeeded = ipsNotNeeded.Checked;
                tagRecord.UnsafeConditions = ipsUnsafeConditions.Checked;
                tagRecord.MissingComponents = ipsMissingComponents.Checked;

                tagRecord.DetailsOfIssue = DetailsOfIssue;
                TagDataSaved?.Invoke(tagRecord);
                return true;
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
            return false;
        }

        //private void panel1_Paint(object sender, PaintEventArgs e)
        //{
        //    float[] dashValues = { 1, 1, 1, 1 };
        //    Pen blackPen = new Pen(Color.White, 5);
        //    blackPen.DashPattern = dashValues;
        //    e.Graphics.DrawLine(blackPen, new Point(e.ClipRectangle.X, 5), new Point(e.ClipRectangle.Right, 5));
        //}
    }
}
