using DevExpress.Spreadsheet;
using DevExpress.Utils.Menu;
using DevExpress.XtraSpreadsheet.Menu;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using TsSFCDevice.Client.Launch.Common.NpoiExtend;

namespace TsSFCDevice.Client.Launch.PreserveTemplate
{
    public class Template_AM_17 : BaseTemplate
    {
        /// <summary>
        /// 设备名称
        /// </summary>
        internal readonly string EquipmentNameAddress = "[F,1]";

        /// <summary>
        /// 安装地点
        /// </summary>
        internal readonly string InstallationSiteAddress = "[R,1]";

        /// <summary>
        /// 设备编号
        /// </summary>
        internal readonly string EquipmentNumberAddress = "[F,2]";

        /// <summary>
        /// 设备型号
        /// </summary>
        internal readonly string EquipmentModelAddress = "[R,2]";

        /// <summary>
        /// 异常处理固定字符
        /// </summary>
        internal override string ExceptionFreezeString
        {
            get { return "异常备注："; }
        }

        /// <summary>
        /// 正文
        /// </summary>
        internal override SpreadsheetPopupMenu popupContent
        {
            get
            {
                if (_popupContent == null)
                {
                    _popupContent = new SpreadsheetPopupMenu()
                    {
                        Caption = "正文",
                        Items =
                        {
                            new DXMenuItem("已保养",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "√"); }),
                            new DXMenuItem("未保养",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "×", EnumMaintenanceOperationType.Write, true); }),
                            new DXMenuItem("不适用",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "N/A"); }),
                            new DXMenuItem("清空",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "", EnumMaintenanceOperationType.Clear); }),
                        }
                    };
                }
                return _popupContent;
            }
        }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        internal override SpreadsheetPopupMenu popupException
        {
            get
            {
                if (_popupException == null)
                {
                    _popupException = new SpreadsheetPopupMenu()
                    {
                        Caption = "其他异常处理",
                        Items =
                        {
                             new DXMenuItem("编辑",(s,e)=> 
                             { 
                                 WriteValue(EnumMaintenanceCellType.ExceptionDescription, ExceptionFreezeString, EnumMaintenanceOperationType.Write,true); 
                             }),
                             new DXMenuItem("清空", (a, aa) =>
                             {
                                 WriteValue(EnumMaintenanceCellType.ExceptionDescription, "", EnumMaintenanceOperationType.Clear);
                             }),
                        }
                    };
                }
                return _popupException;
            }
        }

        public Template_AM_17() : base()
        {
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="ws">Worksheet 控件对象</param>
        /// <param name="CurrentType">当前计划类型</param>
        /// <param name="EditColValue">当前编辑列</param>
        public Template_AM_17(Worksheet ws) : base(ws)
        {
            #region 设备名称
            EquipmentName.Add(SheetExtend.AddressToIndex(EquipmentNameAddress));
            #endregion

            #region 安装地点
            InstallationSite.Add(SheetExtend.AddressToIndex(InstallationSiteAddress));
            #endregion

            #region 设备编号
            EquipmentID.Add(SheetExtend.AddressToIndex(EquipmentNumberAddress));
            #endregion

            #region 设备型号
            DeviceSpecification.Add(SheetExtend.AddressToIndex(EquipmentModelAddress));
            #endregion

            int JanColIndex = SheetExtend.ColumnIndexFromLetter("O");
            int DecColIndex = SheetExtend.ColumnIndexFromLetter("Z");

            for (int rowIndex = 4; rowIndex < LastUsedRowIndex; rowIndex++)
            {
                #region 空行判断
                Cell FirstCell = worksheet.Cells[rowIndex, 0];
                if (FirstCell.Value.IsEmpty && !FirstCell.IsMerged)
                    break;
                #endregion

                for (int colIndex = JanColIndex; colIndex <= DecColIndex; colIndex++)
                {
                    Cell cell = worksheet.Cells[rowIndex, colIndex];
                    Cell PreviousCell = worksheet[rowIndex, JanColIndex - 1];
                    if (PreviousCell.IsMerged)
                    {
                        //获取主单元格位置
                        int PreRowIndex = (PreviousCell.GetMergedRanges()[0]).TopRowIndex;
                        int PreColumnIndex = (PreviousCell.GetMergedRanges()[0]).LeftColumnIndex;
                        PreviousCell = worksheet[PreRowIndex, PreColumnIndex];
                    }

                    if ((PreviousCell?.Value?.TextValue ?? "") == "保养人签字")
                    {
                        // 保养人签字
                        Operation.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = colIndex, MaintenanceType = EnumMaintenanceType.Monthly, MaintenanceTypeValue = worksheet.Cells[3, colIndex].Value.TextValue + "" });

                        // 异常备注：
                        ExceptionDescription.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = 0, MaintenanceType = EnumMaintenanceType.Monthly, MaintenanceTypeValue = worksheet.Cells[3, colIndex].Value.TextValue + "" });
                    }
                    else if ((PreviousCell?.Value?.TextValue ?? "") == "保养日期")
                    {
                        // 保养日期
                        OperationDate.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = colIndex, MaintenanceType = EnumMaintenanceType.Monthly, MaintenanceTypeValue = worksheet.Cells[3, colIndex].Value.TextValue + "" });
                    }
                    else
                    {
                        // 正文
                        Content.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = colIndex, MaintenanceType = EnumMaintenanceType.Monthly, MaintenanceTypeValue = worksheet.Cells[3, colIndex].Value.TextValue + "" });
                    }
                }
            }
        }


    }
}
