using System;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using FCOMMON;

namespace Project.Web
{
    public partial class MachineBridge
    {
        #region MailForm API (메일양식)

        /// <summary>
        /// 메일양식 목록 조회
        /// </summary>
        public string MailForm_GetList()
        {
            try
            {
                var sql = @"SELECT idx, gcode, cate, title, tolist, bcc, cc, subject, tail, body,
                                   selfTo, selfCC, selfBCC, wuid, wdate, exceptmail, exceptmailcc
                           FROM MailForm WITH (nolock)
                           WHERE gcode = @gcode
                           ORDER BY cate, title";

                var cs = Properties.Settings.Default.gwcs;
                using (var cn = new SqlConnection(cs))
                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.AddWithValue("@gcode", info.Login.gcode);
                    using (var da = new SqlDataAdapter(cmd))
                    {
                        var dt = new DataTable();
                        da.Fill(dt);
                        return JsonConvert.SerializeObject(new { Success = true, Data = dt });
                    }
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Success = false, Message = ex.Message });
            }
        }

        /// <summary>
        /// 메일양식 상세 조회
        /// </summary>
        public string MailForm_GetDetail(int idx)
        {
            try
            {
                var sql = @"SELECT idx, gcode, cate, title, tolist, bcc, cc, subject, tail, body,
                                   selfTo, selfCC, selfBCC, wuid, wdate, exceptmail, exceptmailcc
                           FROM MailForm WITH (nolock)
                           WHERE idx = @idx";

                var cs = Properties.Settings.Default.gwcs;
                using (var cn = new SqlConnection(cs))
                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.AddWithValue("@idx", idx);
                    using (var da = new SqlDataAdapter(cmd))
                    {
                        var dt = new DataTable();
                        da.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            return JsonConvert.SerializeObject(new { Success = true, Data = dt.Rows[0] });
                        }
                        return JsonConvert.SerializeObject(new { Success = false, Message = "데이터를 찾을 수 없습니다." });
                    }
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Success = false, Message = ex.Message });
            }
        }

        /// <summary>
        /// 메일양식 추가
        /// </summary>
        public string MailForm_Add(string cate, string title, string tolist, string bcc, string cc,
            string subject, string tail, string body, bool selfTo, bool selfCC, bool selfBCC,
            string exceptmail, string exceptmailcc)
        {
            try
            {
                var sql = @"INSERT INTO MailForm (gcode, cate, title, tolist, bcc, cc, subject, tail, body,
                                                  selfTo, selfCC, selfBCC, wuid, wdate, exceptmail, exceptmailcc)
                           VALUES (@gcode, @cate, @title, @tolist, @bcc, @cc, @subject, @tail, @body,
                                   @selfTo, @selfCC, @selfBCC, @wuid, GETDATE(), @exceptmail, @exceptmailcc);
                           SELECT SCOPE_IDENTITY();";

                var cs = Properties.Settings.Default.gwcs;
                using (var cn = new SqlConnection(cs))
                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.AddWithValue("@gcode", info.Login.gcode);
                    cmd.Parameters.AddWithValue("@cate", cate ?? "");
                    cmd.Parameters.AddWithValue("@title", title ?? "");
                    cmd.Parameters.AddWithValue("@tolist", tolist ?? "");
                    cmd.Parameters.AddWithValue("@bcc", bcc ?? "");
                    cmd.Parameters.AddWithValue("@cc", cc ?? "");
                    cmd.Parameters.AddWithValue("@subject", subject ?? "");
                    cmd.Parameters.AddWithValue("@tail", tail ?? "");
                    cmd.Parameters.AddWithValue("@body", body ?? "");
                    cmd.Parameters.AddWithValue("@selfTo", selfTo);
                    cmd.Parameters.AddWithValue("@selfCC", selfCC);
                    cmd.Parameters.AddWithValue("@selfBCC", selfBCC);
                    cmd.Parameters.AddWithValue("@wuid", info.Login.no);
                    cmd.Parameters.AddWithValue("@exceptmail", exceptmail ?? "");
                    cmd.Parameters.AddWithValue("@exceptmailcc", exceptmailcc ?? "");

                    cn.Open();
                    var newIdx = cmd.ExecuteScalar();
                    return JsonConvert.SerializeObject(new { Success = true, Message = "등록되었습니다.", idx = newIdx });
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Success = false, Message = ex.Message });
            }
        }

        /// <summary>
        /// 메일양식 수정
        /// </summary>
        public string MailForm_Edit(int idx, string cate, string title, string tolist, string bcc, string cc,
            string subject, string tail, string body, bool selfTo, bool selfCC, bool selfBCC,
            string exceptmail, string exceptmailcc)
        {
            try
            {
                var sql = @"UPDATE MailForm SET
                               cate = @cate, title = @title, tolist = @tolist, bcc = @bcc, cc = @cc,
                               subject = @subject, tail = @tail, body = @body,
                               selfTo = @selfTo, selfCC = @selfCC, selfBCC = @selfBCC,
                               wuid = @wuid, wdate = GETDATE(), exceptmail = @exceptmail, exceptmailcc = @exceptmailcc
                           WHERE idx = @idx";

                var cs = Properties.Settings.Default.gwcs;
                using (var cn = new SqlConnection(cs))
                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.AddWithValue("@idx", idx);
                    cmd.Parameters.AddWithValue("@cate", cate ?? "");
                    cmd.Parameters.AddWithValue("@title", title ?? "");
                    cmd.Parameters.AddWithValue("@tolist", tolist ?? "");
                    cmd.Parameters.AddWithValue("@bcc", bcc ?? "");
                    cmd.Parameters.AddWithValue("@cc", cc ?? "");
                    cmd.Parameters.AddWithValue("@subject", subject ?? "");
                    cmd.Parameters.AddWithValue("@tail", tail ?? "");
                    cmd.Parameters.AddWithValue("@body", body ?? "");
                    cmd.Parameters.AddWithValue("@selfTo", selfTo);
                    cmd.Parameters.AddWithValue("@selfCC", selfCC);
                    cmd.Parameters.AddWithValue("@selfBCC", selfBCC);
                    cmd.Parameters.AddWithValue("@wuid", info.Login.no);
                    cmd.Parameters.AddWithValue("@exceptmail", exceptmail ?? "");
                    cmd.Parameters.AddWithValue("@exceptmailcc", exceptmailcc ?? "");

                    cn.Open();
                    cmd.ExecuteNonQuery();
                    return JsonConvert.SerializeObject(new { Success = true, Message = "수정되었습니다." });
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Success = false, Message = ex.Message });
            }
        }

        /// <summary>
        /// 메일양식 삭제
        /// </summary>
        public string MailForm_Delete(int idx)
        {
            try
            {
                var sql = "DELETE FROM MailForm WHERE idx = @idx";

                var cs = Properties.Settings.Default.gwcs;
                using (var cn = new SqlConnection(cs))
                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.AddWithValue("@idx", idx);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    return JsonConvert.SerializeObject(new { Success = true, Message = "삭제되었습니다." });
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(new { Success = false, Message = ex.Message });
            }
        }

        #endregion
    }
}
