using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace CitySunlight
{



    public static class Utils
    {
        public static IList<ItemInfo> ToItemInfoList(this DataTable t, string pFieldValue, string pFieldText)
        {
            IList<ItemInfo> list = new List<ItemInfo>();
            if (t == null)
            {
                return list;
            }

            foreach (DataRow row in t.Rows)
            {
                list.Add(ItemInfo.Create(row[pFieldValue].ToString(), row[pFieldText].ToString()));
            }

            return list;
        }

        public static string GetSelect(IList<ItemInfo> pInfolist, string pControlName, string pEditValue, string pFirstValue, string pFirstText)
        {
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append("<select id=\"" + pControlName + "\" name=\"" + pControlName + "\">");
            if (pFirstText.Length > 0)
            {
                sbStr.Append("<option value=\"");
                sbStr.Append(pFirstValue);
                sbStr.Append("\"");

                if (pFirstValue == pEditValue)
                {
                    sbStr.Append(" selected=\"selected\"");
                }

                sbStr.Append(">");

                sbStr.Append(pFirstText);
                sbStr.Append("</option>");
            }

            foreach (ItemInfo info in pInfolist)
            {
                sbStr.Append("<option value=\"");
                sbStr.Append(info.id);
                sbStr.Append("\"");
                if (pEditValue == info.id)
                {
                    sbStr.Append(" selected=\"selected\"");
                }
                sbStr.Append(">");
                sbStr.Append(info.name);
                sbStr.Append("</option>");
            }
            sbStr.Append("<lect>");
            return sbStr.ToString();
        }
    }
    public class ItemInfo
    {
        public string id;
        public string name = string.Empty;
        public string detail;

        public ItemInfo() { }
        public ItemInfo(string pId, string pName)
        {
            this.id = pId;
            this.name = pName;
        }

        public ItemInfo(string pId, string pName, string pDetail)
        {
            this.id = pId;
            this.name = pName;
            this.detail = pDetail;
        }

        public override string ToString()
        {
            return this.name;
        }

        public static ItemInfo Create(string pName)
        {
            return new ItemInfo(pName, pName, "");
        }

        public static ItemInfo Create(string pId, string pName)
        {
            return new ItemInfo(pId, pName, "");
        }

        public static ItemInfo Create(string pId, string pName, string pDetail)
        {
            return new ItemInfo(pId, pName, pDetail);
        }
    }
}