using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TABS.MailCarrier.WebSDK.Mail;
using TABS.MailCarrier.WebSDK.User;

public partial class Mail_PermitSend_ConfirmerList : System.Web.UI.Page
{
    protected string _mode = "";
    protected string _dgParam = "";
    private Guid _approverId = Guid.Empty;          // 승인권자 ID
    private bool _imDelegate = false;               // 대리자인 경우 true

    protected override void InitializeCulture()
    {
        string defLang = WebUser.CurrentUser.UiLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(defLang);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(defLang);
        base.InitializeCulture();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        WebUtil.SetMcCommonCss2(Page.Header);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = AppSetting.GetTitleInfo(Request).Title;

        _mode = WebParam.GetStrParamValue("mode");
        _approverId = WebParam.GetGuidParamValue("delegate");
        // 페이지 제목 출력
        string headerText = (_mode == "" || _mode == "ListWait") ? Resources.Mail.MailToApprove : Resources.Mail.MailToApproveAll;
        if (_approverId != Guid.Empty)
        {
            DomainUser approver = new DomainUser(_approverId);
            headerText += string.Format(" [{0}]", approver.Name);
        }
        MsgGridView.Columns[0].HeaderText = headerText;

        if (!IsPostBack)
        {
            MsgGridView.PageIndex = WebParam.GetIntParamValue("page");            
            if (_approverId == Guid.Empty) _approverId = WebUser.CurrentUser.DomainUserID;
            if (_approverId != WebUser.CurrentUser.DomainUserID)
            {
                // 대리인 검증
                bool delegateMatched = false;
                PermitSendEx ps = new PermitSendEx(WebUser.CurrentUser.DomainID);
                PermitSendEx.PsApproverInfoList apvInfoList = ps.GetApproverInfoByDelegate(WebUser.CurrentUser.DomainUserID);
                foreach (PermitSendEx.PsApproverInfo apvInfo in apvInfoList)
                {
                    if (apvInfo.DomainUserID == _approverId)
                    {
                        delegateMatched = true; break;
                    }
                }
                if (delegateMatched)
                {                    
                    _imDelegate = true;
                    _dgParam = string.Format("delegate={0}", _approverId);
                }
                else
                {
                    Response.Redirect("~/Error.aspx?code=0");
                    return;
                }
            }
            ViewState["_approverId"] = _approverId;
            ViewState["_imDelegate"] = _imDelegate;
            ViewState["_dgParam"] = _dgParam;
        }
        else
        {
            _approverId = (Guid)ViewState["_approverId"];
            _imDelegate = (bool)ViewState["_imDelegate"];
            _dgParam = (string)ViewState["_dgParam"];
        }
    }

    protected void MsgGridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        if ((gv.ShowHeader == true && gv.Rows.Count > 0) || (gv.ShowHeaderWhenEmpty == true))
        {
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    protected void MailApvDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["approverId"] = _approverId.ToString();
    }

    #region MsgGridView Methods

    protected string GetClickEventFunction(Guid clid)
    {
        return string.Format("ViewMessage('{0}', {1})", clid, MsgGridView.PageIndex);
    }

    protected string GetTo(string toList)
    {
        return Server.HtmlEncode(toList);
    }

    protected string GetDate(DateTime date)
    {
        return WebUser.CurrentUser.ToLocalTime(date).ToString("g");
    }

    protected string GetStatus(int flag, object confirmDate, int toCount, Guid clid)
    {
        string status = "";
        bool comment = false;
        switch (flag)
        {
            case 0:
                status = string.Format("{0}({1})", GetLocalResourceObject("Status01").ToString(), toCount);  // 대기
                break;
            case 1:
                status = string.Format(GetLocalResourceObject("Status02").ToString(), WebUser.CurrentUser.ToLocalTime((DateTime)confirmDate)); // 승인
                comment = true;
                break;
            case 2:
                status = string.Format(GetLocalResourceObject("Status03").ToString(), WebUser.CurrentUser.ToLocalTime((DateTime)confirmDate)); // 거부
                comment = true;
                break;
            case 3:
                status = string.Format(GetLocalResourceObject("Status04").ToString(), WebUser.CurrentUser.ToLocalTime((DateTime)confirmDate)); // 자동승인
                comment = true;
                break;
            case 4:
                status = string.Format(GetLocalResourceObject("Status05").ToString(), WebUser.CurrentUser.ToLocalTime((DateTime)confirmDate)); // 발신자에 의한 취소됨
                break;
            case 5:
                status = string.Format(GetLocalResourceObject("Status06").ToString(), WebUser.CurrentUser.ToLocalTime((DateTime)confirmDate)); // 자동거부
                comment = true;
                break;
        }
        if (comment)
        {
            status = string.Format("<a href='#' onclick=\"OpenComment(event, '{0}');return false;\">{1}</a>", clid, Server.HtmlEncode(status));
        }
        return status;
    }

    #endregion
}