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_fanyiadd : System.Web.UI.Page
{
    public string usernameid = string.Empty;
    public string childid = "0";
    public string translate = "0";//总账户
    public string childtranslate = "0";//子账户
    public string filename = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie ChatCookie = Request.Cookies["cookChatuser"];
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookChatuser"] == null)
        {
            if (Request.Cookies["cookEtwuser"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
               translate = Fun.GetDateTime();
               DataSet ds = DbHelperSQL.Query("select id from etw_user where username='" + GetCookie["username"] + "'");
               if (ds.Tables[0].Rows.Count > 0)
               {
                   usernameid = ds.Tables[0].Rows[0]["id"].ToString();
               }
            }
        }
        else
        {
            childtranslate = Fun.GetDateTime();
            DataSet dss = DbHelperSQL.Query("select userid,id from chat_user where username='" + ChatCookie["username"] + "'");
            if (dss.Tables[0].Rows.Count > 0)
            {
                usernameid = dss.Tables[0].Rows[0]["userid"].ToString();
                childid = dss.Tables[0].Rows[0]["id"].ToString();
            }
        }
        Fun.Funrecord(Request.Url.AbsoluteUri,Page.Request.UserHostAddress, usernameid);
    }
    private void Savefujian()
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        try
        {
            string extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();  //判断用户上传文件扩展名
            if (extension != "")
            {
                string filepath = FileUpload1.PostedFile.FileName;
                int i = filepath.LastIndexOf(".");
                string newext = filepath.Substring(i);
                string newtime = DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss");
                newtime = newtime.Replace("-", "").Replace("/", "");
                filename = newtime + newext;
                string serverpath = Server.MapPath("../etw_c_m/fujian/") + filename;
                FileUpload1.PostedFile.SaveAs(serverpath);
            }
        }
        catch (Exception ex)
        {
        }
    }
    public string onlinesid()
    {
        string onid = string.Empty;
        DataSet ds = DbHelperSQL.Query("select top 1 id from etw_onlines where yid=-1 and usernameid=" + usernameid + " order by id desc");
        if (ds.Tables[0].Rows.Count > 0)
        {
            onid = ds.Tables[0].Rows[0]["id"].ToString();
        }
        return onid;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Savefujian();
        etw.etw_onlines lead = new etw.etw_onlines();
        lead.yid = -1;
        lead.usernameid = Convert.ToInt32(usernameid);
        lead.contacter = "";
        lead.title = Fun.Encode(title.Text);
        lead.content = Fun.Encode(Fun.NoHTML(contentstr.Value));
        lead.senddate = DateTime.Now.ToString();
        lead.translate = translate;
        lead.childtranslate = childtranslate;
        lead.readed = 1;
        lead.childreaded = 1;
        lead.language ="0";
        lead.childuserid = Convert.ToInt32(childid);
        lead.biaoji = 1;
        lead.Add();

        etw.etw_translate lead_tr = new etw.etw_translate();
        lead_tr.chatid = Convert.ToInt32(onlinesid());
        lead_tr.userid = Convert.ToInt32(usernameid);
        lead_tr.source_content = Fun.Encode(Fun.NoHTML(contentstr.Value));
        lead_tr.receivetime = Fun.GetDateTime();
        lead_tr.language = "0";
        lead_tr.type = 1;
        lead_tr.fujian = filename;
        if (childid!="0")
        {
            lead_tr.ischild = 1;
        }
        lead_tr.Add();
        Response.Write("<script language=\"javascript\">window.location='fanyi_news.aspx'</" + "script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        contentstr.Value = "";
    }
}
