using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Xml;
using DSWeb.Models;
using DSWeb.EntityDA;

namespace DSWeb.FeeCodes
{
    public partial class ATTNAdapter : System.Web.UI.Page
    {
        private string strMark;
        private string strPos;
        private int iPos = 0;
        public string strTO = "";
        public string strYARD = "";
        public string strPortName = "";
        UnicodeEncoding unicode = new UnicodeEncoding();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strPost = Request.Url.ToString();

                if (Request.QueryString["mask"] != null)
                {
                    strMark = Request.QueryString["mask"].ToString();
                }
                if (Request.QueryString["pos"] != null)
                {
                    strPos = Request.QueryString["pos"].ToString();
                    iPos = int.Parse(strPos);
                }
                if (Request.QueryString["strTO"] != null)
                {
                    strTO = unicode.GetString(unicode.GetBytes(Request.QueryString["strTO"].ToString()));
                }
                if (Request.QueryString["strYARD"] != null)
                {
                    strYARD = unicode.GetString(unicode.GetBytes(Request.QueryString["strYARD"].ToString()));
                }
                if (Request.QueryString["strSHOWNAME"] != null)
                {
                    string strTELS = unicode.GetString(unicode.GetBytes(Request.QueryString["strSHOWNAME"].ToString()));
                    Response.Write(getTELS(strTELS));
                    return;
                }
                if (Request.QueryString["strADDR"] != null)
                {
                    string strADDR = unicode.GetString(unicode.GetBytes(Request.QueryString["strADDR"].ToString()));
                    Response.Write(getADDR(strADDR));
                    return;
                }


                XmlDocument docs = GetDoc();
                Response.ContentType = "text/xml";
                Response.Write(docs.OuterXml.ToString());
            }
        }

        public string getADDR(string strADDR)
        {
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            DataSet ds = T_ALL_DA.GetAllSQL("select * from info_client where SHORTNAME='" + strYARD + "' and ISSTOP=0");
            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["ADDR"].ToString().Trim() == "" && dr["TEL"].ToString().Trim() == "")
                    {
                        strPortName = "";
                    }
                    else if (dr["ADDR"].ToString().Trim() != "" && dr["TEL"].ToString().Trim() == "")
                    {
                        strPortName = dr["ADDR"].ToString().Trim();
                    }
                    else if (dr["ADDR"].ToString().Trim() == "" && dr["TEL"].ToString().Trim() != "")
                    {
                        strPortName = "&1&" + dr["TEL"].ToString().Trim();
                    }
                    else if (dr["ADDR"].ToString().Trim() != "" && dr["TEL"].ToString().Trim() != "")
                    {
                        strPortName = dr["ADDR"].ToString().Trim() + "&2&" + dr["TEL"].ToString().Trim();
                    }
                    else
                    {
                        strPortName = "";
                    }
                }
            }
            else
            {
                strPortName = "";
            }
            return strPortName;
        }

        public string getTELS(string strSHOWNAME)
        {
            string sSQL = "";
            if (strYARD.Trim() != "")
            {
                sSQL = "select * from info_client_contact where ISSTOP=0 and ISDELETED=0 and SHOWNAME='" + strSHOWNAME + "' and LINKID = (select top 1 GID from info_client where SHORTNAME='" + strYARD + "' and ISSTOP=0)";
            }
            else if (strTO.Trim() != "")
            {
                sSQL = "select * from info_client_contact where ISSTOP=0 and ISDELETED=0 and SHOWNAME='" + strSHOWNAME + "' and LINKID = (select top 1 GID from info_client where DESCRIPTION='" + strTO + "' and ISSTOP=0)";
            }
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            DataSet ds = T_ALL_DA.GetAllSQL(sSQL);

            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    strPortName = dr["TEL"].ToString().Trim() + "&&" + dr["FAX"].ToString().Trim();
                }
            }
            else
            {
                strPortName = "";
            }
            return strPortName;
        }

        public string BuildXML()
        {
            StringBuilder resultBuilder = new StringBuilder();

            resultBuilder.Append("<?xml version=\"1.0\" ?>");
            resultBuilder.AppendFormat("<complete{0}>", iPos == 0 ? string.Empty : " add=\"true\"");
            if (!string.IsNullOrEmpty(strMark))
            {
                string sSQL = "";
                if (strYARD.Trim() != "")
                {
                    sSQL = "select * from info_client_contact where ISSTOP=0 and ISDELETED=0 and LINKID = (select top 1 GID from info_client where SHORTNAME='" + strYARD + "' and ISSTOP=0) order by SHOWNAME";
                }
                else if (strTO.Trim() != "")
                {
                    sSQL = "select * from info_client_contact where ISSTOP=0 and ISDELETED=0 and LINKID = (select top 1 GID from info_client where DESCRIPTION='" + strTO + "' and ISSTOP=0) order by SHOWNAME";
                }
                T_ALL_DA T_ALL_DA = new T_ALL_DA();
                DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
                if (ds != null)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        if (dr["GID"].ToString().Trim() != "" || dr["SHOWNAME"].ToString().Trim() != "")
                        {
                            //string js = dr["CODENAME"].ToString().Trim() + " | " + dr["SHOWNAME"].ToString().Trim();
                            resultBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", dr["GID"].ToString().Trim(), dr["SHOWNAME"].ToString().Trim());
                        }
                    }
                }
            }
            resultBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", "", "");
            resultBuilder.Append("</complete>");
            return resultBuilder.ToString().Replace("&", "&amp;");
        }

        public XmlDocument GetDoc()
        {
            XmlDocument xmldoc = new XmlDocument();
            string content = BuildXML();
            try
            {
                xmldoc.LoadXml(content);
            }
            catch (Exception)
            {
            }
            return xmldoc;
        }
    }
}