using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Xml;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Text.RegularExpressions;

namespace DSWeb.FeeCodes
{
    public partial class InvoicenoAdapter : System.Web.UI.Page
    {
        private string strMark;
        private string strPos;
        private int iPos = 0;

        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);
                }
                //
                XmlDocument docs = GetDoc();
                Response.ContentType = "text/xml";
                Response.Write(docs.OuterXml.ToString());
            }
        }

        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))
            {
                T_ALL_DA T_ALL_DA = new T_ALL_DA();
                string strSql = "SELECT B.INVOICENUM FROM ch_fee_invoiceitems as B INNER JOIN ch_fee_invoicebooks as A ON B.BOOKID = A.GID WHERE (A.ISDELETE=0 or A.ISDELETE is null) and (A.ISLOCK=0 or A.ISLOCK is null) and (B.ISDELETE=0 or B.ISDELETE is null) and (B.ISLOCK=0 or B.ISLOCK is null) and (B.ISMAKEOUT=0 or B.ISMAKEOUT is null) order by B.INVOICENUM";
                DataTable sourceTable = T_ALL_DA.GetExcuteSql(strSql).Tables[0];
                if (sourceTable.Rows.Count > 0)
                {
                    int iCount = 0;
                    for (int j = 0; j < sourceTable.Rows.Count; j++)
                    {
                        for (int i = 0; i < sourceTable.Columns.Count; i++)
                        {
                            if (!sourceTable.Rows[j][i].ToString().Trim().Equals(""))
                            {
                                resultBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", sourceTable.Rows[j][i].ToString().Trim(), sourceTable.Rows[j][i].ToString().Trim());
                                iCount++;
                            }
                        }
                    }
                }
            }
            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;
        }
    }
}