using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http;

namespace WebServer
{
    public class ProjectController : BaseController
    {


        // PUT api/values/5
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }

        [HttpGet]
        public string Test()
        {
            return "test";
        }

        [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 EEEntities();
                var rows = db.Projects.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.status.Contains("완료") == false).OrderByDescending(t => t.pdate).Take(50);

                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("<tr>");
                    tbody.AppendLine($"<th scope='row'>{item.pdate}</th>");
                    tbody.AppendLine($"<td>{item.name}</td>");


                    //if (item.description.Length > 10)
                    //    tbody.AppendLine($"<td>{item.description.Substring(0, 10)}...</td>");
                    //else
                    //    tbody.AppendLine($"<td>{item.description}</td>");
                    tbody.AppendLine("</tr>");
                }
            }

            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("<tr>");
                tbody.AppendLine("<th scope='row'>1</th>");
                tbody.AppendLine("<td colspan='6'>자료가 없습니다</td>");
                tbody.AppendLine("</tr>");
            }


            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 ScheduleConfirm(int? id)
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View();
            var project = (int)id;

            //데이터를 조회해서 표시를 해준다.
            var db = new EEEntities();
            var prjinfo = db.Projects.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.idx == project).FirstOrDefault();
            var schrows = db.EETGW_ProjectsSchedule.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.project == project).OrderByDescending(t => t.project).OrderByDescending(t => t.no).OrderBy(t => t.seq);

            var gets = Request.GetQueryNameValuePairs();// GetParameters(data);


            System.Text.StringBuilder tinfo = new System.Text.StringBuilder();
            //프로젝트정보를 표시합니다.
            tinfo.AppendLine("<tr>");
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.idx));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.status));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.progress));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.name));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.reqstaff));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.userManager));
            tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.orderno));
            tinfo.AppendLine("</tr>");


            var contents = result.Content.Replace("{search}", "");
            contents = contents.Replace("{tableinfo}", tinfo.ToString());

            tinfo.Clear();
            foreach (var item in schrows)
            {
                tinfo.AppendLine("<tr>");
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.no));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.seq));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.title));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.sw));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.ew));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.swa));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.ewa));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.progress));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.uid));
                tinfo.AppendLine(string.Format("<td>{0}</td>", item.memo));
                tinfo.AppendLine("</tr>");
            }
            contents = contents.Replace("{scheinfo}", tinfo.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 EEEntities();


                var rows = db.Projects.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.status.Contains("완료") == false).OrderByDescending(t => t.pdate).Take(50);

                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("<tr>");
                    tbody.AppendLine($"<th scope='row'>{item.idx}</th>");
                    tbody.AppendLine($"<td>{item.status}</td>");
                    tbody.AppendLine($"<td>{item.progress}</td>");
                    tbody.AppendLine($"<td>{item.name}</td>");
                    tbody.AppendLine($"<td>{item.reqstaff}</td>");
                    tbody.AppendLine($"<td>{item.userManager}</td>");
                    tbody.AppendLine($"<td>{item.cnt}</td>");
                    tbody.AppendLine($"<td>{item.costo}</td>");
                    tbody.AppendLine($"<td>{item.costn}</td>");
                    tbody.AppendLine($"<td>{item.costo - item.costn}</td>");
                    tbody.AppendLine($"<td>{item.orderno}</td>");
                    if (item.memo != null)
                        tbody.AppendLine($"<td>{item.memo}</td>");
                    else
                        tbody.AppendLine($"<td>&nbsp;</td>");



                    //if (item.description.Length > 10)
                    //    tbody.AppendLine($"<td>{item.description.Substring(0, 10)}...</td>");
                    //else
                    //    tbody.AppendLine($"<td>{item.description}</td>");
                    tbody.AppendLine("</tr>");
                }
            }

            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("<tr>");
                tbody.AppendLine("<th scope='row'>1</th>");
                tbody.AppendLine("<td colspan='6'>자료가 없습니다</td>");
                tbody.AppendLine("</tr>");
            }


            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 detail(int id)
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            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 EEEntities();


                var rows = db.Projects.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.status.Contains("완료") == false).OrderByDescending(t => t.pdate).Take(50);

                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("<tr>");
                    tbody.AppendLine($"<th scope='row'>{item.idx}</th>");
                    tbody.AppendLine($"<td>{item.status}</td>");
                    tbody.AppendLine($"<td>{item.progress}</td>");
                    tbody.AppendLine($"<td>{item.name}</td>");
                    tbody.AppendLine($"<td>{item.reqstaff}</td>");
                    tbody.AppendLine($"<td>{item.userManager}</td>");
                    tbody.AppendLine($"<td>{item.cnt}</td>");
                    tbody.AppendLine($"<td>{item.costo}</td>");
                    tbody.AppendLine($"<td>{item.costn}</td>");
                    tbody.AppendLine($"<td>{item.costo - item.costn}</td>");
                    tbody.AppendLine($"<td>{item.orderno}</td>");
                    if (item.memo != null)
                        tbody.AppendLine($"<td>{item.memo}</td>");
                    else
                        tbody.AppendLine($"<td>&nbsp;</td>");



                    //if (item.description.Length > 10)
                    //    tbody.AppendLine($"<td>{item.description.Substring(0, 10)}...</td>");
                    //else
                    //    tbody.AppendLine($"<td>{item.description}</td>");
                    tbody.AppendLine("</tr>");
                }
            }

            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("<tr>");
                tbody.AppendLine("<th scope='row'>1</th>");
                tbody.AppendLine("<td colspan='6'>자료가 없습니다</td>");
                tbody.AppendLine("</tr>");
            }


            var contents = result.Content.Replace("{search}", searchkey);
            contents = contents.Replace("{tabledata}", tbody.ToString());
            contents = contents.Replace("{cnt}", itemcnt.ToString());
            contents = contents.Replace("{pidx}", id.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 partlist(int id)
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            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 contents = result.Content.Replace("{search}", searchkey);


            //테이블데이터생성
            var itemcnt = 0;
            //if (searchkey.isEmpty() == false)
            {

                var db = new EEEntities();

                var prjinfo = db.Projects.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.idx == id).FirstOrDefault();
                System.Text.StringBuilder tinfo = new System.Text.StringBuilder();
                //프로젝트정보를 표시합니다.
                tinfo.AppendLine("<tr>");
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.idx));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.status));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.progress));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.name));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.reqstaff));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.userManager));
                tinfo.AppendLine(string.Format("<td>{0}</td>", prjinfo.orderno));
                tinfo.AppendLine("</tr>");

                contents = contents.Replace("{tableinfo}", tinfo.ToString());


                var rows = db.ProjectsPart.Where(t => t.Project == id).OrderBy(t=>t.no);

                itemcnt = rows.Count();
                foreach (var item in rows)
                {
                    tbody.AppendLine("<tr>");
                    tbody.AppendLine($"<th scope='row'>{item.no}</th>");
                    tbody.AppendLine($"<td>{item.ItemGroup}</td>");
                    tbody.AppendLine($"<td>{item.ItemModel}</td>");
                    tbody.AppendLine($"<td>{item.ItemUnit}</td>");
                    tbody.AppendLine($"<td>{item.ItemName}</td>");
                    tbody.AppendLine($"<td>{item.ItemSid}</td>");
                    tbody.AppendLine($"<td>{item.ItemManu}</td>");
                    tbody.AppendLine($"<td>{item.qty}</td>");
                    tbody.AppendLine($"<td>{item.qtyn}</td>");
                    tbody.AppendLine($"<td>{item.price}</td>");
                    tbody.AppendLine($"<td>{item.amt}</td>");
                    tbody.AppendLine($"<td>{item.amtn}</td>");
                    tbody.AppendLine($"<td>{item.remark}</td>");
                    tbody.AppendLine($"<td>{item.qtybuy}</td>");
                    tbody.AppendLine($"<td>{item.qtyin}</td>");
                    tbody.AppendLine($"<td>{item.bbuy}</td>");
                    tbody.AppendLine($"<td>{item.bconfirm}</td>");
                    tbody.AppendLine("</tr>");
                }
            }

            //아잍쳄이 없는경우
            if (itemcnt == 0)
            {
                tbody.AppendLine("<tr>");
                tbody.AppendLine("<th scope='row'>1</th>");
                tbody.AppendLine("<td colspan='6'>자료가 없습니다</td>");
                tbody.AppendLine("</tr>");
            }


            contents = contents.Replace("{tabledata}", tbody.ToString());
            contents = contents.Replace("{cnt}", itemcnt.ToString());
            contents = contents.Replace("{pidx}", id.ToString());


            //공용값 적용
            ApplyCommonValue(ref contents);

            //최종문자 적용
            result.Content = contents;

            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };

            return resp;
        }

    }
}
