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

namespace DeviceRepairAndOptimization.Pages.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 xueTagForm()
        {
            InitializeComponent();
        }

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

            InitializeControl();
        }

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

            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.Enabled = !IsView;

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

        private void panelControl1_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));
        }

        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.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);
            }
        }
    }
}
