using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.Data;
using EKETEAM.FrameWork;

public partial class Customs_Tags : System.Web.UI.Page
{
    public string UserArea = "Application";
    public eUser user;
    public eModel model;
    protected void Page_Load(object sender, EventArgs e)
    {
        user = new eUser(eBase.getUserArea(UserArea));
        eModelInfo customModel = new eModelInfo(user);
        model = customModel.Model;

        string act = eParameters.Request("act");
        if (act == "")
        {
            #region 列表
            eList list = new eList("Tags");
            list.Fields.Add("TagID,Name");
            list.Where.Add("delTag=0");
            list.OrderBy.Add("px,addTime");
            list.Bind(Rep);
            #endregion
        }
        if (act == "load")
        {
            #region 重新加载
            eList list = new eList("Tags");
            list.Fields.Add("TagID,Name");
            list.Where.Add("delTag=0");
            list.OrderBy.Add("px,addTime");
            list.Bind(Rep);
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Rep.RenderControl(htw);
            Rep.Visible = false;
            Response.Write(sw.ToString());
            Response.End();
            #endregion
        }
        if (act == "del")
        {
            #region 删除标签
            string id = eParameters.Form("id");
            eTable etb = new eTable("Tags", user);
            etb.Where.Add("TagID='" + id + "'");
            etb.Delete();

            string sql = "update a set a.PX=b.PX from Tags a";
            sql+=" inner join (select ROW_NUMBER() over(order by px,addtime) as PX,TagID from Tags where delTag=0) as b on a.TagID=b.TagID";
            sql+=" where a.delTag=0";;
            eBase.DataBase.Execute(sql);

            sql = "update Tags set px=999999 where TagID='" + id + "'";
            eBase.DataBase.Execute(sql);
            #endregion
            eResult.Success("删除成功!");
        }
        if (act == "sort")
        {
            #region 排序
            string ids = eParameters.Form("ids");
            int i = 1;
            foreach (string id in ids.Split(",".ToCharArray()))
            {
                eBase.DataBase.Execute("update Tags set PX=" + i.ToString() + " where TagID='" + id + "'");
                i++;
            }
            #endregion
            eResult.Success("排序成功!");
        }
        if (act == "save")
        {
            #region 保存
            eTable etb;
            string id = eParameters.Form("id");
            string value = eParameters.Form("value");
            value = value.Replace("，", ",");
            if (id.Length == 0)
            {
                #region 添加
                int maxpx = Convert.ToInt32(eBase.DataBase.getValue("select isnull(max(px),0) + 1 from Tags where px<999999 and deltag=0"));
                if (value.IndexOf("	") > -1 || value.IndexOf("\n") > -1 || value.IndexOf(",") > -1)
                {
                    #region 批量添加
                    DateTime time = DateTime.Now;
                    foreach (string str in value.Replace("\r", "").Split("\n".ToCharArray()))
                    {
                        if (str.Trim().Length > 0)
                        {
                            foreach (string _str in str.Split("	".ToCharArray()))
                            {
                                if (_str.Trim().Length > 0)
                                {
                                    foreach (string key in _str.Replace("，", ",").Split(",".ToCharArray()))
                                    {
                                        if (key.Trim().Length > 0)
                                        {
                                            etb = new eTable("Tags", user);
                                            etb.Fields.Add("Name", key.Trim());
                                            etb.Fields.Add("px", maxpx.ToString());
                                            etb.Fields.Add("addTime", time.ToString());
                                            etb.Add();
                                            maxpx++;
                                            time = time.AddSeconds(1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    etb = new eTable("Tags", user);
                    etb.Fields.Add("Name", value);
                    etb.Fields.Add("px", maxpx.ToString());
                    etb.Add();
                }
                #endregion
                eResult.Success("添加成功!");
            }
            else
            {
                #region 修改
                etb = new eTable("Tags", user);
                etb.Fields.Add("Name", value);
                etb.Where.Add("TagID='" + id + "'");
                etb.Update();
                #endregion
                eResult.Success("修改成功!");
            }
            #endregion
        }
    }
}