using Microsoft.Owin;
using System;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace Project
{
    public class JobreportController : BaseController
    {
        // PUT api/values/5
        public void Put(int id, [FromBody] string value)
        {
        }
        // DELETE api/values/5
        public void Delete(int id)
        {
        }
        [HttpPost]
        public string Add(FormCollection tbpdate)
        {
            var vals = Request.GetQueryNameValuePairs();
            return string.Empty;
        }
     
        //[HttpPost]
        //public string Edit([FromBody] string value)
        //{
        //    var vals = Request.GetQueryNameValuePairs();
        //    var req = Request.GetRequestContext();
        //    return string.Empty;
        //}
        [HttpPost]
        public string Edit(FormCollection value)
        {
            var vals = Request.GetQueryNameValuePairs();
            var req = Request.GetRequestContext();
            return string.Empty;
        }
        [HttpGet]
        public HttpResponseMessage Edit(int id)
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View("/jobreport/edit");
            var gets = Request.GetQueryNameValuePairs();// GetParameters(data);
            var key_search = gets.Where(t => t.Key == "search").FirstOrDefault();
            var model = GetGlobalModel();
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);
            //기본값을 찾아서 없애줘야한다
            var searchkey = string.Empty;
            if (key_search.Key != null && key_search.Value.isEmpty() == false) searchkey = key_search.Value.Trim();
            var tbody = new System.Text.StringBuilder();
            //테이블데이터생성
            var db = new dsMSSQLTableAdapters.vJobReportForUserTableAdapter();//. EEEntitiesJobreport();
            var sd = DateTime.Now.ToString("yyyy-MM-01");
            var ed = DateTime.Now.ToShortDateString();
            var rows = db.GetData(FCOMMON.info.Login.gcode, id).FirstOrDefault();//.vJobReportForUser.AsNoTracking().Where(t => t.gcode == FCOMMON.info.Login.gcode && t.idx == id).FirstOrDefault();
            var contents = result.Content;
            if (rows == null)
            {
                //아이템이 없는 메시지를 표시한다
            }
            else
            {
                //치환작업을 진행한다
                contents = contents.Replace("{pdate}", rows.pdate);
                contents = contents.Replace("{status}", rows.status);
                contents = contents.Replace("{name}", rows.name);
                contents = contents.Replace("{package}", rows.package);
                contents = contents.Replace("{process}", rows.process);
                contents = contents.Replace("{type}", rows.type);
                contents = contents.Replace("{userProcess}", rows.userProcess);
                contents = contents.Replace("{projectName}", rows.projectName);
                contents = contents.Replace("{hrs}", rows.hrs.ToString());
                contents = contents.Replace("{ot}", rows.ot.ToString());
                contents = contents.Replace("{requestpart}", rows.requestpart);
                contents = contents.Replace("{description}", rows.description);
            }
            //공용값 적용
            ApplyCommonValue(ref contents);
            //최종문자 적용
            result.Content = contents;
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };
            return resp;
        }
        [HttpGet]
        public HttpResponseMessage Add()
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View("/jobreport/add");
            var gets = Request.GetQueryNameValuePairs();// GetParameters(data);
            var key_search = gets.Where(t => t.Key == "search").FirstOrDefault();
            var model = GetGlobalModel();
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);
            //기본값을 찾아서 없애줘야한다
            var searchkey = string.Empty;
            if (key_search.Key != null && key_search.Value.isEmpty() == false) searchkey = key_search.Value.Trim();
            var tbody = new System.Text.StringBuilder();
            //테이블데이터생성
            var itemcnt = 0;
            //if (searchkey.isEmpty() == false)
            {
                var db = new dsMSSQLTableAdapters.vJobReportForUserTableAdapter();// EEEntitiesJobreport();
                var sd = DateTime.Now.ToString("yyyy-MM-01");
                var ed = DateTime.Now.ToShortDateString();
                var rows = db.GetByDate(FCOMMON.info.Login.gcode, FCOMMON.info.Login.no, sd, ed);
                    //vJobReportForUser.AsNoTracking().Where(t => t.gcode == FCOMMON.info.Login.gcode && t.id == FCOMMON.info.Login.no && t.pdate.CompareTo(sd) >= 0 && t.pdate.CompareTo(ed) <= 1).OrderByDescending(t => t.pdate);
                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("
");
                    tbody.AppendLine($"| {item.pdate.Substring(5)}");
                    tbody.AppendLine($" | {item.ww}");
                    tbody.AppendLine($" | {item.name}");
                    if (item.status == "진행 중" || item.status.EndsWith("%"))
                        tbody.AppendLine($" | {item.status}");
                    else
                        tbody.AppendLine($" | {item.status}");
                    tbody.AppendLine($" | {item.type}");
                    tbody.AppendLine($" | {item.projectName}");
                    tbody.AppendLine($" | {item.hrs}");
                    tbody.AppendLine($" | {item.ot}");
                    tbody.AppendLine(" | ");
                    tbody.AppendLine(item.description);
                    tbody.AppendLine("");
                    tbody.AppendLine(" | 
");
                }
            }
            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("");
                tbody.AppendLine("| 1");
                tbody.AppendLine(" | 자료가 없습니다");
                tbody.AppendLine(" | 
");
            }
            var contents = result.Content.Replace("{search}", searchkey);
            contents = contents.Replace("{tabledata}", tbody.ToString());
            contents = contents.Replace("{cnt}", itemcnt.ToString());
            //공용값 적용
            ApplyCommonValue(ref contents);
            //최종문자 적용
            result.Content = contents;
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };
            return resp;
        }
        [HttpGet]
        public HttpResponseMessage Find()
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View();
            var gets = Request.GetQueryNameValuePairs();// GetParameters(data);
            var key_search = gets.Where(t => t.Key == "search").FirstOrDefault();
            var model = GetGlobalModel();
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);
            //기본값을 찾아서 없애줘야한다
            var searchkey = string.Empty;
            if (key_search.Key != null && key_search.Value.isEmpty() == false) searchkey = key_search.Value.Trim();
            var tbody = new System.Text.StringBuilder();
            //테이블데이터생성
            var itemcnt = 0;
            if (searchkey.isEmpty() == false)
            {
                var db = new dsMSSQLTableAdapters.vJobReportForUserTableAdapter();// EEEntitiesJobreport();
                var sd = DateTime.Now.ToShortDateString();
                var rows = db.GetByToday(FCOMMON.info.Login.gcode, sd);//.vJobReportForUser.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.pdate.CompareTo(sd) == 0).OrderBy(t => t.name);
                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("");
                    tbody.AppendLine($"| {item.pdate}");
                    tbody.AppendLine($" | {item.status}");
                    tbody.AppendLine($" | {item.name}");
                    tbody.AppendLine($" | {item.projectName}");
                    tbody.AppendLine($" | {item.hrs}");
                    tbody.AppendLine($" | {item.ot}");
                    tbody.AppendLine($" | {item.description}");
                    if (item.description.Length > 10)
                        tbody.AppendLine($" | {item.description.Substring(0, 10)}...");
                    else
                        tbody.AppendLine($" | {item.description}");
                    tbody.AppendLine(" | 
");
                }
            }
            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("");
                tbody.AppendLine("| 1");
                tbody.AppendLine(" | 자료가 없습니다");
                tbody.AppendLine(" | 
");
            }
            var contents = result.Content.Replace("{search}", searchkey);
            contents = contents.Replace("{tabledata}", tbody.ToString());
            contents = contents.Replace("{cnt}", itemcnt.ToString());
            //공용값 적용
            ApplyCommonValue(ref contents);
            //최종문자 적용
            result.Content = contents;
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };
            return resp;
        }
        [HttpGet]
        public HttpResponseMessage Index()
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View();
            var gets = Request.GetQueryNameValuePairs();// GetParameters(data);
            var key_search = gets.Where(t => t.Key == "search").FirstOrDefault();
            var model = GetGlobalModel();
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);
            //기본값을 찾아서 없애줘야한다
            var searchkey = string.Empty;
            if (key_search.Key != null && key_search.Value.isEmpty() == false) searchkey = key_search.Value.Trim();
            var tbody = new System.Text.StringBuilder();
            //테이블데이터생성
            var itemcnt = 0;
            //if (searchkey.isEmpty() == false)
            {
                var db = new dsMSSQLTableAdapters.vJobReportForUserTableAdapter();// EEEntitiesJobreport();
                var sd = DateTime.Now.ToShortDateString();
                var ed = DateTime.Now.ToShortDateString();
                var rows = db.GetByDate(FCOMMON.info.Login.gcode, FCOMMON.info.Login.no, sd, ed);
                //.vJobReportForUser.AsNoTracking().Where(t => t.gcode == FCOMMON.info.Login.gcode && t.id == FCOMMON.info.Login.no && t.pdate.CompareTo(sd) >= 0 && t.pdate.CompareTo(ed) <= 1).OrderByDescending(t => t.pdate);
                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("");
                    tbody.AppendLine($"| {item.pdate.Substring(5)}");
                    tbody.AppendLine($" | {item.ww}");
                    tbody.AppendLine($" | {item.name}");
                    if (item.status == "진행 중" || item.status.EndsWith("%"))
                        tbody.AppendLine($" | {item.status}");
                    else
                        tbody.AppendLine($" | {item.status}");
                    tbody.AppendLine($" | {item.type}");
                    tbody.AppendLine($" | {item.projectName}");
                    tbody.AppendLine($" | {item.hrs}");
                    tbody.AppendLine($" | {item.ot}");
                    tbody.AppendLine(" | ");
                    tbody.AppendLine(item.description);
                    tbody.AppendLine("");
                    tbody.AppendLine(" | 
");
                }
            }
            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("");
                tbody.AppendLine("| 1");
                tbody.AppendLine(" | 자료가 없습니다");
                tbody.AppendLine(" | 
");
            }
            var contents = result.Content.Replace("{search}", searchkey);
            contents = contents.Replace("{tabledata}", tbody.ToString());
            contents = contents.Replace("{cnt}", itemcnt.ToString());
            //공용값 적용
            ApplyCommonValue(ref contents);
            //최종문자 적용
            result.Content = contents;
            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };
            return resp;
        }
    }
}