using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Services;
using TABS.MailCarrier.WebSDK;
using TABS.MailCarrier.WebSDK.Mail;

public partial class Mail_PermitSend_CallAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]
    public static int Confirm(string clid, string comment, string delegateName)
    {
        int retcode = 0; // 0-성공, 1-이미 처리된 메일입니다, 2-라이선스 오류, 3-DB 오류
        SqlConnection conn = new SqlConnection(GlobalConfig.ConnString);
        SqlTransaction tran = null;
        PermitSendEx ps = new PermitSendEx(WebUser.CurrentUser.DomainID);
        try
        {
            Guid id = new Guid(clid);
            conn.Open();
            tran = conn.BeginTransaction();
            retcode = ps.PsConfirmListConfirm(conn, tran, id, comment, WebUser.CurrentUser.Name);
            tran.Commit();

            if (retcode == 0)
            {
                ps.SendPermitMail(conn, id, comment, delegateName);
            }
        }
        catch (SqlException)
        {
            if (tran != null) tran.Rollback();
            retcode = 3;
        }
        finally
        {
            conn.Close();
        }
        return retcode;
    }

    [WebMethod]
    public static int Reject(string clid, string comment, string delegateName)
    {
        int retcode = 0; // 0-성공, 1-이미 처리된 메일입니다, 2-라이선스 오류, 3-DB 오류
        SqlConnection conn = new SqlConnection(GlobalConfig.ConnString);
        SqlTransaction tran = null;
        PermitSendEx ps = new PermitSendEx(WebUser.CurrentUser.DomainID);
        try
        {
            Guid id = new Guid(clid);
            conn.Open();
            tran = conn.BeginTransaction();
            retcode = ps.PsConfirmListReject(conn, tran, id, comment, WebUser.CurrentUser.Name);
            tran.Commit();

            if (retcode == 0)
            {
                ps.SendRejectMail(conn, id, comment, delegateName);
            }
        }
        catch (SqlException e)
        {
            if (tran != null) tran.Rollback();
            retcode = 3;
        }
        finally
        {
            conn.Close();
        }
        return retcode;
    }

    [WebMethod]
    public static int Cancel(string clid)
    {
        int retcode = 0; // 0-성공, 1-이미 처리된 메일입니다, 2-라이선스 오류, 3-DB 오류
        SqlConnection conn = new SqlConnection(GlobalConfig.ConnString);
        SqlTransaction tran = null;
        PermitSendEx ps = new PermitSendEx(WebUser.CurrentUser.DomainID);
        try
        {
            Guid id = new Guid(clid);
            conn.Open();
            tran = conn.BeginTransaction();
            retcode = ps.PsConfirmListCancel(conn, tran, id);
            tran.Commit();
        }
        catch (SqlException e)
        {
            if (tran != null) tran.Rollback();
            retcode = 3;
        }
        finally
        {
            conn.Close();
        }
        return retcode;
    }

    [WebMethod]
    public static string GetComment(string clid)
    {
        string comment = "";
        SqlConnection conn = new SqlConnection(GlobalConfig.ConnString);
        PermitSendEx ps = new PermitSendEx(WebUser.CurrentUser.DomainID);
        try
        {
            conn.Open();
            DataSet ds = ps.PsConfirmListSelectById(conn, new Guid(clid));
            if (ds.Tables[0].Rows.Count > 0)
            {
                comment = WebUtil.GetFormattedPlainText((string)ds.Tables[0].Rows[0]["cl_comment"], true);
            }
        }
        finally
        {
            conn.Close();
        }
        return comment;
    }
}