using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using iBemsDataService.Model;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Collections.Generic;
using iBemsDataService.Util;

namespace iBemsDataService.Report
{
    public partial class PatrolReport : DevExpress.XtraReports.UI.XtraReport
    {
        private iBemsEntities db = new iBemsEntities();

        private int siteId;
        private DateTime startDate;
        private DateTime endDate;

        public PatrolReport()
        {
            InitializeComponent();
        }

        public PatrolReport(int siteId, DateTime startDate, DateTime endDate) : this()
        {
            this.siteId = siteId;
            this.startDate = startDate;
            this.endDate = endDate;
        }

        protected override void OnBeforePrint(System.Drawing.Printing.PrintEventArgs e)
        {
            base.OnBeforePrint(e);

            //xrLabelDate.Text = String.Format("순찰일자 : {0}", DateTime.Now.ToString("yyyy-MM-dd"));
            xrLabelDate.Text = String.Format("> 순찰일자 : {0}", startDate.ToString("yyyy-MM-dd"));

            List<CmPatrolHistory> pHList =
                (from x in db.CmPatrolHistory
                 where x.SiteId == siteId &&
                 x.startDate >= startDate && x.endDate != null && // 2016 09 20 hcLee 추가
                 x.startDate <= endDate
                 orderby x.startDate descending, x.endDate descending
                 select x).ToList<CmPatrolHistory>();
            /*
            List<PatrolHistoryDataSource> patrolHistoryList =
                (from x in db.CmPatrolHistory
                where x.SiteId == siteId &&
                x.startDate >= startDate && x.endDate != null && // 2016 09 20 hcLee 추가
                x.startDate <= endDate
                orderby x.startDate descending, x.endDate descending
                select new PatrolHistoryDataSource
                {
                    PatrolSheduleName = x.CmPatrolSchedule.Name,
                    PatrolTeam = x.CmPatrolSchedule.CmPatrolPlan.CmPatrolGroup.Name,
                    PatrolCourse = x.CmPatrolSchedule.CmPatrolPlan.CmPatrolCourse.Name,
                    StartDate = x.startDate,
                    EndDate = (System.DateTime)x.endDate,
                    //Result = x.CmPatrolType.Name,
                    Result = String.Format("{0}/{1}/{2}", x.resultPosCnt, x.resultNormalCnt, x.resultAbnormalCnt),
                    //resultPosCnt = (int)x.resultPosCnt,
                    //resultNormalCnt = (int)x.resultNormalCnt,
                    //resultAbnormalCnt = (int) x.resultAbnormalCnt,

                    ResultDesc = x.resultDesc
                }).ToList<PatrolHistoryDataSource>(); */
            List<PatrolHistoryDataSource> patrolHistoryList = new List<PatrolHistoryDataSource>();
            foreach (CmPatrolHistory x in pHList)
            {
                PatrolHistoryDataSource pdata = new PatrolHistoryDataSource();
                pdata.PatrolSheduleName = x.CmPatrolSchedule.Name;
                pdata.PatrolTeam = x.CmPatrolSchedule.CmPatrolPlan.CmPatrolGroup.Name;
                pdata.PatrolCourse = x.CmPatrolSchedule.CmPatrolPlan.CmPatrolCourse.Name;
                pdata.StartDate = x.startDate;
                pdata.EndDate = (System.DateTime)x.endDate;
                pdata.Result = String.Format("{0}/{1}/{2}", x.resultPosCnt, x.resultNormalCnt, x.resultAbnormalCnt);
                pdata.ResultDesc = x.resultDesc;
                patrolHistoryList.Add(pdata);
            }

            String resultDescs = String.Empty;
            int listCount = patrolHistoryList.Count;
            for (int i = 0; i < listCount; i++)
            {
                patrolHistoryList[i].No = patrolHistoryList.Count - i;
                resultDescs += String.Format("{0} :  {1}\n", patrolHistoryList[i].PatrolSheduleName, patrolHistoryList[i].ResultDesc);
            }

            XRTable tablePatrolHistory = ReportTableHandler.CreateTableFromListWithColumnWidthPatrol(
                patrolHistoryList,
                //new float[] { 72.39f, 150f, 120f, 120f, 140f, 140f, 96.61f },
                new float[] { 69.02f, 143.03f, 114.41f, 114.41f, 133.49f, 133.49f, 92.15f },
                new String[] { "ResultDesc" });

            tablePatrolHistory.LocationF = new Point(20, 0);

            xrRichTextResultDesc.Text = resultDescs;

            if (tablePatrolHistory.Rows.Count > 0)
            {
                DetailPatrolReport.Controls.Clear();
                DetailPatrolReport.Controls.Add(tablePatrolHistory);
            }
        }

        public class PatrolHistoryDataSource
        {
            public int No { get; set; }
            public String PatrolSheduleName { get; set; }
            public String PatrolTeam { get; set; }
            public String PatrolCourse { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public String Result { get; set; }
            //public int resultPosCnt { get; set; }
            //public int resultNormalCnt { get; set; }
            //public int resultAbnormalCnt { get; set; }
            public String ResultDesc { get; set; }
        }
    }
}