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;

public partial class etwuser_addnews : System.Web.UI.Page
{
    public int userid = 0;
    public int langid = 0;
    public string picture = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookEtwuser"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        updatetimetxt.Text = DateTime.Now.ToString("yyyy-MM-dd");
        DataSet ds1 = DbHelperSQL.Query("select userid from etw_user where username='" + GetCookie["username"].ToString() + "'");
        if (ds1.Tables[0].Rows.Count > 0)
        {
            userid = Convert.ToInt32(ds1.Tables[0].Rows[0]["userid"]);
            if (!IsPostBack)
            {
                Fun.Funrecord(Request.Url.AbsoluteUri,Page.Request.UserHostAddress, userid.ToString());
                etw.etw_web weblead = new etw.etw_web();
                DataSet dsc = weblead.GetwebList(userid);
                websitetxt.DataSource = dsc.Tables[0].DefaultView;
                websitetxt.DataTextField = "website";
                websitetxt.DataValueField = "webid";
                websitetxt.DataBind();
            }
      }
        
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SaveImages();
        for (int i=0; i < websitetxt.Items.Count;i++ )
        {
            etw.etw_news lead = new etw.etw_news();
            lead.userid = userid;
            lead.title = Server.UrlEncode(titletxt.Text.Trim());
            lead.content = Server.UrlEncode(contenttxt.Text.Trim());
            lead.description = Server.UrlEncode(descriptiontxt.Value.Trim());
            lead.updatetime = updatetimetxt.Text.Trim();
            if (websitetxt.Items[i].Selected)
            {
            lead.website = websitetxt.Items[i].Text;
            }
            lead.picture = picture.Trim(";".ToCharArray());
            lead.Add();
            Fun.JsAlerT("上传成功！", "addnews.aspx"); 
        }
    }
    private void tosmallImage(string myFile, int iFile)
    {
        imagezoom(myFile, 400, 300, Server.MapPath("../newsupfile/") + myFile);
    }
    private void imagezoom(string myFile, int width, int height, string filepath)
    {
        string filepath_s = Server.MapPath("../newsupfile/s_") + myFile;
        System.Drawing.Image myImage = System.Drawing.Image.FromFile(Server.MapPath("../newsupfile/") + myFile);
        decimal MAX_WIDTH = width;
        decimal MAX_HEIGHT = height;
        decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
        int newWidth, newHeight;
        decimal originalWidth = myImage.Width;
        decimal originalHeight = myImage.Height;

        if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
        {
            decimal factor;
            if (originalWidth / originalHeight > ASPECT_RATIO)
            {
                factor = originalWidth / MAX_WIDTH;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            }
            else
            {
                factor = originalHeight / MAX_HEIGHT;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            }
        }
        else
        {
            newWidth = myImage.Width;
            newHeight = myImage.Height;
        }
        System.Drawing.Image thumbImage = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);
        thumbImage.Save(filepath_s);
        thumbImage.Dispose();
        myImage.Dispose();
    }

    private void SaveImages()
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpRequest request = HttpContext.Current.Request;
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                HttpPostedFile postedFile = files[iFile];
                string extension1 = System.IO.Path.GetExtension(File1.PostedFile.FileName);  //判断用户上传文件扩展名
                if (File1.PostedFile.FileName == "")
                {
                    picture = "nopic.gif";
                }
                else
                {
                    string tupian;
                    string filepath = File1.PostedFile.FileName;
                    int i = filepath.LastIndexOf(".");
                    string newext = filepath.Substring(i);
                    string newtime = "news"+DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss");
                    newtime = newtime.Replace("-", "").Replace("/", "");
                    tupian = newtime + iFile.ToString() + newext;
                    string serverpath = Server.MapPath("../newsupfile/") + tupian;
                    postedFile.SaveAs(serverpath);
                    picture = picture+";"+tupian;
                    tosmallImage(tupian, iFile);
                }

            }
        }
        catch (Exception ex)
        {
            //this.lb_info.Text = "An error occured!";
        }

    }
}
