using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;
using System.Data.OleDb;
public partial class admini_ManageLink : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckLogin()) return;
        Utility.RegisterTypeForAjax(typeof(admini_ManageLink));
    }

    [AjaxMethod]

    public ArrayList getLink(int page,int p )
    {
        int _page=1;
        if (page > 1)
            _page = (page - 1) * p;
        else
            _page = 1;
        string sql ="";
        if (_page == 1)
            sql = "select top 10 * from links where 1=1 order by id desc";
        else
            sql = string.Format("SELECT Top 10 * FROM LINKS   WHERE ID NOT IN (SELECT TOP {0} id from links order by id desc)order by id desc", _page);
        object count = SqlHelper.GetSingle("SELECT COUNT(ID) FROM LINKS");
        OleDbDataReader dr = SqlHelper.ExecuteReader(sql);
        ArrayList list = new ArrayList();
        while (dr.Read())
        {
            ArrayList al = new ArrayList();
            al.Add(dr["id"]);
            al.Add(dr["sname"]);
            al.Add(dr["surl"]);
            al.Add(dr["sdesc"]);
            al.Add(dr["scount"]);
            al.Add(dr["soutcount"]);
            al.Add(dr["sstatus"]);
            al.Add(dr["sfirst"]);
            list.Add(al);
        }
        
        list.Add(page);
        list.Add(count);
        return list;
    }//5^1^a^s^p^x

    [AjaxMethod]

    public string del(int id)
    {
        int key = id;
            try
            {
                string sql = string.Format("delete from links where id={0}", key);
                int n = SqlHelper.ExecuteSql(sql);
                if (n > 0)
                {
                    WebClass.ReMoveCacheByKey("AllLinks");
                    return "1";
                }
                else
                    return "0";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
       
    }

    [AjaxMethod]
    public string addNewSite(int? id,string sname,string surl,string sdesc,string st,int scount,int soutcount)
    {
        string sql ="";
        bool b = st == "0" ? false : true;
        try
        {
            if (id == null)
                sql = string.Format("insert into links (sname,surl,sdesc,sstatus,scount,soutcount,sfirst) values ('{0}','{1}','{2}',{3},{4},{5},{6})", sname, surl, sdesc,b, scount, soutcount, 0);
            else
                sql = string.Format("update links set sname='{0}',surl='{1}',sdesc='{2}',scount={3},soutcount={4},sfirst=0 ,sstatus={5} where id={6}", sname, surl, sdesc ,scount, soutcount,b,id);

            int n = SqlHelper.ExecuteSql(sql);
            if (n > 0)
            {
                WebClass.ReMoveCacheByKey("AllLinks");
                return "1";
            }
            else
                return "0";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }
}
