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_ads_add : 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("|ads|");

        if (!IsPostBack)
        {
            regExpression reg = new regExpression();
            pdateRegExpree.ValidationExpression = reg.dateTime;
            pdateRegExpree.ErrorMessage = reg.dateTimeError;
            eDateRegExpree.ValidationExpression = reg.dateTime;
            eDateRegExpree.ErrorMessage = reg.dateTimeError;
            httpRegExpree.ValidationExpression = reg.urlHttp;
            httpRegExpree.ErrorMessage = reg.urlHttpError;

            DateTime nStr = DateTime.Now;
            pDate.Text = nStr.ToString("yyyy-MM-dd HH:mm:ss");
            eDate.Text = nStr.AddYears(1).ToString("yyyy-MM-dd HH:mm:ss");

            c.bindingDropDownList("all", true, 0, "ads", adClass);

            this.adImg.Items.Add(new ListItem("图片：" + xml.read("xabara.config", "UploadImgType", 180), "Img"));
            this.adImg.Items.Add(new ListItem("Flash：" + xml.read("xabara.config", "UploadFlashType", 180), "Flash"));
            this.adImg.Items.Add(new ListItem("广告代码", "Code"));
        }
    }

    protected void addAD_Click(object sender, EventArgs e)
    {
        string imgFile = adImg.Text.Trim();
        string uploadFileStr = string.Empty;
        if (!imgFile.Equals("Code"))
        {
            uploadFileStr = f.upload(imgUpload, imgFile, false);
        }
        string url = adHttp.Text;
        string newFile = string.Empty;

        if (uploadFileStr.Equals("上传成功"))   //上传文件判断
        {
            newFile = Session["NewFile"].ToString().Trim();
        }

        string strDim = "@classID,@adW,@adH,@adImg,@adHttp,@adCode,@starDate,@stopDate,@postDate,@postIP,@adminID";
        string[] strValue = new string[11];

        strValue[0] = adClass.Text;
        strValue[1] = adW.Text;
        strValue[2] = adH.Text;
        strValue[3] = newFile;
        strValue[4] = url;
        if (string.IsNullOrEmpty(adCode.Text as string))
        {
            if (imgFile.Equals("Img"))
            {
                if (!string.IsNullOrEmpty(url as string))
                {
                    strValue[5] = "<a href=\"" + url + "\" target=\"_blank\"><img src=\"http://" + xml.read("xabara.config", "webDomains", 180) + f.filePath(newFile) + "\" style=\"boder:0px;width:" + adW.Text + "px;height:" + adH.Text + "px\" /></a>";
                }
                else
                {
                    strValue[5] = "<img src=\"" + f.filePath(newFile) + "\"  style=\"boder:0px;width:" + adW.Text + "px;height:" + adH.Text + "px\" />";
                }
            }
            else
            {
                strValue[5] = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" height=\"" + adH.Text + "\" width=\"" + adW.Text + "\"><param name=\"quality\" value=\"high\" /><param name=\"movie\" value=\"" + f.uploadFolder + newFile.Substring(5, 8) + "/" + newFile + "\" /><embed height=\"" + adH.Text + "\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" quality=\"high\" src=\"" + f.filePath(newFile) + "\" type=\"application/x-shockwave-flash\" width=\"" + adW.Text + "\"></embed></object>";
            }
        }
        else
        {
            strValue[5] = adCode.Text;
        }
        strValue[6] = pDate.Text;
        strValue[7] = eDate.Text;
        strValue[8] = DateTime.Now.ToString();
        strValue[9] = new ipAddress().address();
        strValue[10] = a.uid;

        if (!string.IsNullOrEmpty(newFile as string) || !string.IsNullOrEmpty(adCode.Text as string))
        {
            sql.log(a.uid, "admin", "发布广告");
            js.div(false, "", "text", sql.add("ads", strDim, strValue), 350, 150, "default.aspx", "goto");
        }
        else
        {
            js.div(false, "", "text", "1. 请上传广告图片，" + uploadFileStr + "<br />2. 直接粘贴广告代码", 450, 0, "", "");
        }
    }

    protected void adClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        long cid = xabara.isNum(adClass.Text);

        if (cid > 0)
        {
            DataTable dt = sql.getDataTable("select * from NewsTree where Tid=" + adClass.Text);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    string[] className = dt.Rows[0]["TreeName"].ToString().Trim().Split(new char[] { '_' });
                    string[] adSize = className[1].Split(new char[] { 'X' });
                    adW.Text = adSize[0];
                    adH.Text = adSize[1];
                    adSelect.Text = "";
                }
                catch
                {
                    adSelect.Text = "提醒：分类是否选择正确，广告分类命名统一采用 广告位置名称_960X90 格式，便于尺寸框自动填入！";
                    adW.Text = "0";
                    adH.Text = "0";
                }
            }
            else
            {
                adSelect.Text = "提醒：分类是否选择正确，广告分类命名统一采用 广告位置名称_960X90 格式，便于尺寸框自动填入！";
                adW.Text = "0";
                adH.Text = "0";
            }
            dt.Dispose();
        }
        else
        {
            adSelect.Text = "提醒：分类是否选择正确，广告分类命名统一采用 广告位置名称_960X90 格式，便于尺寸框自动填入！";
            adW.Text = "0";
            adH.Text = "0";
        }
    }
}