using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.share;

namespace wispro.sp.api.AppealHandler
{
    public class SpecialPointsHandler : IDoAppealObject
    {
        public void DoAppeal(AppealObject appeal, int appealRecordId, DbContext spContext)
        {
            bool isAggree = true;

            foreach (var iv in appeal.inputFieldValues)
            {
                if (iv.InputField == null)
                {
                    iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
                }
            }

            var temField = appeal.inputFieldValues.FirstOrDefault(iv => iv.InputField.FieldName == "审核意见" && iv.Value == "同意");

            isAggree = (temField != null);
            
            if (isAggree)
            {
                var apRecord = ((spDbContext)spContext).AppealRecords.FirstOrDefault(p => p.Id == appealRecordId);

                if (apRecord != null)
                {
                    var Item = ((spDbContext)spContext).PerformanceItems.FirstOrDefault(p=>p.Id == apRecord.ItemId);

                    foreach (var iv in appeal.inputFieldValues)
                    {
                        if (iv.InputField.FieldName == "给定点数")
                        {
                            Item.BasePoint = double.Parse(iv.Value);
                        }

                        if (iv.InputField.FieldName == "绩效类型")
                        {
                            Item.Type = iv.Value;
                        }

                    }

                    Item.AgentFeedbackMemo = "特殊点数申诉";

                }
                else
                {
                    throw new ApplicationException("没找到指定的申诉记录！");
                }


            }
        }
    }
}
