using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class WebXabara_Link_edit : System.Web.UI.Page
{
    admin a = new admin();
    sqlServer sql = new sqlServer();
    classify c = new classify();
    fileDocument f = new fileDocument();
    javasrcipt js = new javasrcipt();

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check("|link|");

        if (!IsPostBack)
        {
            c.bindingDropDownList("all", true, 0, "link", linkList);

            regExpression reg = new regExpression();
            RegHttp.ValidationExpression = reg.urlHttp;
            RegHttp.ErrorMessage = reg.urlHttpError;

            DataTable dt = sql.getDataTable("select * from linkXabara where lID=" + xabara.isNum(Request["id"]).ToString());
            if (dt.Rows.Count > 0)
            {
                string oldImg = dt.Rows[0]["uploadFile"].ToString().Trim();
                upImg.Value = oldImg;
                if (!string.IsNullOrEmpty(oldImg as string))
                {
                    imgHtm.Text = "<br /><img src='" + f.uploadFolder + oldImg.Substring(5, 8) + "/" + oldImg + "' />";
                }
                this.linkList.Items.FindByValue(dt.Rows[0]["classID"].ToString().Trim()).Selected = true;
                linkName.Text = dt.Rows[0]["linkName"].ToString().Trim();
                linkHttp.Text = dt.Rows[0]["linkURL"].ToString().Trim();
            }
            dt.Dispose();
        }
    }

    protected void editLink_Click(object sender, EventArgs e)
    {
        string[] strValue = new string[7];

        string uploadFileStr = f.upload(imgUpload, "Img", false);   //原图片

        string newFile = upImg.Value;
        if (uploadFileStr.Equals("上传成功"))
        {
            f.del(newFile.Substring(5, 8) + "/" + newFile);
            newFile = Session["NewFile"].ToString().Trim();
        }

        if (sql.getDataTable("select * from linkXabara where lid<>" + xabara.isNum(Request["id"]).ToString() + " and linkURL='" + linkHttp.Text.Trim() + "'").Rows.Count > 0)
        {
            js.div(false, "", "text", "已存在该用户的友情链接！", 350, 150, "default.aspx", "goto");
        }
        else
        {
            string strDim = "@classID,@uploadFile,@linkName,@linkURL,@AdminID,@postDate,@postIP";
            strValue[0] = linkList.Text.Trim();
            strValue[1] = newFile;
            strValue[2] = linkName.Text.Trim();
            strValue[3] = linkHttp.Text.Trim();
            strValue[4] = a.uid;
            strValue[5] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            strValue[6] = new ipAddress().address();

            sql.log(a.uid, "admin", "修改链接");

            js.div(false, "", "text", sql.update("linkXabara", strDim, strValue, " lid=" + xabara.isNum(Request["id"]).ToString()), 350, 150, "default.aspx?cid=" + Request.QueryString["cid"] + "&page=" + Request.QueryString["page"], "goto");
        }
    }
}