using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace TsSFCDeivceClient.Model.DeviceWarrantyRequest
{
    /// <summary>
    /// 设备维修申请单
    ///</summary>
    public class DeviceWarrantyRequestForm
    {
        /// <summary>
        /// 主键编号 
        ///</summary>
        public int AutoID { get; set; }
        /// <summary>
        /// 唯一编号 
        ///</summary>
        public Guid GUID { get; set; }
        /// <summary>
        /// 设备表主键编号 
        ///</summary>
        public int EquipmentPK { get; set; }
        /// <summary>
        /// 设备编号 
        ///</summary>
        public string EquipmentID { get; set; }
        /// <summary>
        /// 设备名称 
        ///</summary>
        public string EquipmentName { get; set; }
        /// <summary>
        /// 发生地点 
        ///</summary>
        public int Location { get; set; }
        /// <summary>
        /// 发生地点 
        ///</summary>
        public string LocationName { get; set; }
        /// <summary>
        /// 是否有在途生产单 
        ///</summary>
        public bool InProduction { get; set; }
        public string InProductionText { get { return InProduction ? "是" : "否"; } }
        /// <summary>
        /// 批次号 
        ///</summary>
        public string Batch { get; set; }
        /// <summary>
        /// 故障现象
        /// </summary>
        public string FaultSymptoms { get; set; }
        /// <summary>
        /// 设备设施部
        /// </summary>
        public string ReceivingDep { get; set; }
        /// <summary>
        /// 是否停机状态
        /// </summary>
        public bool IsDown { get; set; }

        public string IsDownText { get { return IsDown ? "停机" : "非停机"; } }

        /// <summary>
        /// 表单编号（版本）
        /// </summary>
        public int FormVer { get; set; }
        /// <summary>
        /// 申请时间 
        ///</summary>
        public DateTime? CreatOn { get; set; }
        /// <summary>
        /// 申请人 
        ///</summary>
        public int? CreatBy { get; set; }

        public string CreatorName { get; set; }

        /// <summary>
        /// 申请部门 
        ///</summary>
        public int? CreatDept { get; set; }
        /// <summary>
        /// 申请部门名称
        /// </summary>
        public string CreatDeptName { get; set; }
        /// <summary>
        /// 修改时间 
        ///</summary>
        public DateTime? ModifyOn { get; set; }
        /// <summary>
        /// 修改人 
        ///</summary>
        public int? ModifyBy { get; set; }

        /// <summary>
        /// 修改人姓名
        /// </summary>
        public string ModifyByName { get; set; }

        public DeviceWarrantyRequestMaintaionInfo MaintaionItems { get; set; }

        public List<DeviceWarrantyEvaluatorInfo> EvaluatorItems { get; set; }

        [JsonIgnore]
        public DateTime? EvaluatorTimePE { get { return EvaluatorItems.FirstOrDefault(x => x.EvaluatorCode == "PE")?.CreatOn; } }
        [JsonIgnore]
        public DateTime? EvaluatorTimeQE { get { return EvaluatorItems.FirstOrDefault(x => x.EvaluatorCode == "QE")?.CreatOn; } }

        [JsonIgnore]
        public string EvaluatorPE { get { return EvaluatorItems.FirstOrDefault(x => x.EvaluatorCode == "PE")?.CreatorName; } }
        [JsonIgnore]
        public string EvaluatorQE { get { return EvaluatorItems.FirstOrDefault(x => x.EvaluatorCode == "QE")?.CreatorName; } }


        public DateTime? RestorationConfirmationOn { get; set; }

        public int RestorationConfirmationBy { get; set; }

        public string RestorationConfirmationOnName { get; set; }


        public DeviceWarrantyRequestFormStatus FormStatus
        {
            get
            {
                if (MaintaionItems == null || MaintaionItems.SubmitBy == 0)
                {
                    return DeviceWarrantyRequestFormStatus.AwaitingRepair;
                }
                else if (MaintaionItems.SubmitBy > 0)
                {
                    if (RestorationConfirmationBy > 0)
                    {
                        return DeviceWarrantyRequestFormStatus.BeComplate;
                    }

                    return DeviceWarrantyRequestFormStatus.AwaitingApproval;
                }
                
                return DeviceWarrantyRequestFormStatus.AwaitingRepair;
            }
        }

        public string FormStatusText
        {
            get
            {
                return ToDescription(FormStatus);
            }
        }

        public string toJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        /// <summary>
        /// 获取枚举类型的描述
        /// </summary>
        /// <param name="enumeration"></param>
        /// <returns></returns>
        public string ToDescription(DeviceWarrantyRequestFormStatus enumeration)
        {
            Type type = enumeration.GetType();
            MemberInfo[] memInfo = type.GetMember(enumeration.ToString());
            if (null != memInfo && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (null != attrs && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return enumeration.ToString();
        }
    }
}
