using IronIntel.Contractor.Site;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Jump : ContractorBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Title = PageTitle;
            var session = GetCurrentLoginSession();
            if (session == null)
            {
                Response.Redirect(LoginPageUrl + "?f=" + HttpUtility.UrlEncode(Request.Url.ToString()), true);
            }
            else
            {
                //byte[] buf = Encoding.UTF8.GetBytes(s);
                //return Convert.ToBase64String(tmp);

                string p = Request.Params["p"];//jt=woe;woid=3
                try
                {
                    byte[] tmp = Convert.FromBase64String(p);
                    p = Encoding.UTF8.GetString(tmp);
                }
                catch { }

                Dictionary<string, string> ps = GetParams(p);
                string jumpType = "";
                if (ps.ContainsKey("jt"))
                    jumpType = ps["jt"].ToUpper();

                switch (jumpType)
                {
                    case "WOE"://Work Order Edit
                        JumpToWorkOrder(ps);
                        break;
                }
            }
        }
    }

    /// <summary>
    /// 解析跳转相关参数
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    private Dictionary<string, string> GetParams(string s)
    {
        Dictionary<string, string> ps = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        if (!string.IsNullOrEmpty(s))
        {
            string[] kvs = s.Split(';');
            foreach (string kv in kvs)
            {
                string[] temp = kv.Split('=');
                if (temp.Length == 2)
                {
                    ps[temp[0]] = temp[1];
                }
            }
        }
        return ps;
    }

    private void JumpToWorkOrder(Dictionary<string, string> ps)
    {
        string woid = "";
        if (ps.ContainsKey("woid"))
            woid = ps["woid"];
        Response.Redirect("Maintenance/AddWorkOrder.aspx?woid=" + woid, true);
    }
}