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 etw_f_m_etwtranslate : System.Web.UI.Page
{
    public string title = string.Empty;
    public string language = string.Empty;
    public string username = string.Empty;
    public string domain = string.Empty;
    public string url = string.Empty;
    public string address = string.Empty;
    public string kehuname = string.Empty;
    public string fanyi_content = string.Empty;
    public string filename = string.Empty;
    public string t_emails = string.Empty;
    public string transfujian = string.Empty;
    public string ischild = string.Empty;
    public string pid=string.Empty;
    public int wordsnum = 0;
    public int untranssum = 0;
    public int langid = 0;
    public int translatesum = 0;
    public int lanid = 0;
    public int cid = 0;
    public int id = 0;
    public int userid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        cid = Convert.ToInt32(Request.QueryString["chatid"].ToString());
        id = Convert.ToInt32(Request.QueryString["id"].ToString());
        HttpCookie GetCookie = Request.Cookies["cook_admin"];
        if (Request.Cookies["cook_admin"] == null)
        {
            Response.Redirect("../etw_c_m/index.aspx");
        }

        if (!IsPostBack)
        {
            etwadmin();
            untranssum = (int)DbHelperSQL.GetSingle("select count(*) from etw_translate where achivetime=0 and trans_content is not null and (language ='" + domain + "' or target ='" + domain + "' )");
            translatesum = (int)DbHelperSQL.GetSingle("select count(id) from etw_translate where achivetime!=0 and trans_content is not null and (language ='" + domain + "' or target ='" + domain + "' )");
            etwusername();
            translate();
        }
    }

    public void etwadmin()
    {
        DataSet ds = DbHelperSQL.Query("select * from etw_admin where etwadminname='" + Request.Cookies["cook_admin"]["username"] + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            username = ds.Tables[0].Rows[0]["realname"].ToString();
            pid=ds.Tables[0].Rows[0]["id"].ToString();
            langid = Convert.ToInt32(ds.Tables[0].Rows[0]["transid"].ToString());
        }

        etw.etw_language lag = new etw.etw_language();
        lag.id = langid;
        lag.GetModel();
        language = lag.language;
        domain = lag.domain;
    }
    public void etwusername()
    {
        etw.etw_onlines ols = new etw.etw_onlines();
        ols.id = cid;
        ols.GetModel();
        title = ols.title;
        int chatuserid = ols.childuserid;
        address = ols.address;
        url = ols.url;
        if (url != null && url != "")
        {
            if (ols.url.Contains("http:") == true)
            {
                url = "<a target='_blank' href='" + url + "'>" + url + "</a>";
            }
            else
            {
                url = "<a target='_blank' href='http://www." + url + "'>http://www." + url + "</a>";
            }
        }
        userid = ols.usernameid;
        kehuname = Convert.ToString(DbHelperSQL.GetSingle("select company from etw_user where id=" + userid + ""));

        etw.etw_translate tlt = new etw.etw_translate();
        tlt.id = id;
        tlt.GetModel();
        fanyi_content = tlt.source_content;
        trans_content.Value = tlt.trans_content;
        if (tlt.trans_fujian != null && tlt.trans_fujian != "")
        {
            transfujian = "已经上传了附件资料：<a target='_blank' href='download.aspx?fujian=" + tlt.trans_fujian + "' style='text-decoration:none; color:#004080'><img src='../images/downpic.gif' border='0' />下载 附件 </a>";
        }

    }


    protected void translate()
    {
        DataSet ds5 = DbHelperSQL.Query("select t.id,t.achivetime,a.realname,t.trans_content,t.source_content,t.fujian,t.trans_fujian from etw_translate t left join etw_admin a on t.personid=a.id  where chatid=" + cid + " and achivetime!=0 order by receivetime asc");
        if (ds5.Tables[0].Rows.Count > 0)
        {
            Repeater1.DataSource = ds5.Tables[0].DefaultView;
            Repeater1.DataBind();
        }


    }

    public string trans_fujian(string trnsfujian)
    {
        if (trnsfujian != null && trnsfujian != "")
        {
            trnsfujian = " <a target='_blank' href='download.aspx?fujian=" + trnsfujian + "' style='text-decoration:none; color:#004080'><img src='../images/downpic.gif' border='0' />下载 附件 </a>";
        }
        return trnsfujian;
    }
    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)
        {
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Savefujian();
        if (Request.Form["trans_content"] == "" || Request.Form["trans_content"] == null)
        {
            Response.Write("<script type='text/javascript'>alert('内容不能为空');history.back();</script>");
        }
        else
        {
            if (RadioButtonList1.SelectedValue == "0")
            {
                string trans_content = Fun.Encode(Fun.NoHTML(Request.Form["trans_content"]));
                DbHelperSQL.ExecuteSql("update etw_translate set trans_content='" + trans_content + "', personid=" + userid + ", achivetime='" + Fun.GetDateTime() + "', trans_fujian='" + filename + "' where id=" + id + "");
                etwadmin();
                DataSet dss = DbHelperSQL.Query("select * from etw_translate where id=" + id);
                wordsnum = Fun.Count_com(dss.Tables[0].Rows[0]["source_content"].ToString());
                if (dss.Tables[0].Rows.Count>0 )
                {
                    DataSet dss1 = DbHelperSQL.Query("select * from [2010_08_words] where receivetime=" +dss.Tables[0].Rows[0]["receivetime"].ToString());
                    if (dss1.Tables[0].Rows.Count>0)
                    {
                        DbHelperSQL.ExecuteSql("update [2010_08_words] set achivetime=" + Fun.GetDateTime() + ",words=" + wordsnum + ",personid=" + pid + " where receivetime=" + dss.Tables[0].Rows[0]["receivetime"].ToString());
                    }
                    else
                    {
                        DbHelperSQL.ExecuteSql("insert into [2010_08_words] (userid,receivetime,achivetime,words,chatid,language,personid)values("+ dss.Tables[0].Rows[0]["userid"].ToString() + "," + dss.Tables[0].Rows[0]["receivetime"].ToString() + "," + Fun.GetDateTime() + "," + wordsnum + "," + dss.Tables[0].Rows[0]["chatid"].ToString() + ",'" + dss.Tables[0].Rows[0]["language"].ToString() + "'," + pid + ")");
                    }                   
                }
               
                string emailstr = "你好，你收到一封ETW翻译的询盘，请<a href='http://www.etwservice.com'>登陆查收</a>";
                string[] email = t_emails.Split(new string[] { ",", ";" }, System.StringSplitOptions.RemoveEmptyEntries);
                string title = "翻译提醒";
                if (t_emails != "" && t_emails != null)
                {
                    if (ischild == "" || ischild == null || ischild == "0")
                    {
                       // Fun.sendmail(email[0], title, emailstr);
                        DbHelperSQL.ExecuteSql("update etw_onlines set readed='0' where id=" + cid + "");

                    }
                    else
                    {
                        for (int i = 0; i < email.Length; i++)
                        {
                            if (email[i] == ischild)
                            {
                                etw.chat_user ctu = new etw.chat_user();
                                ctu.id = Convert.ToInt32(ischild);
                                ctu.GetModel();
                                string receiver = ctu.username;
                                Fun.sendmail(receiver, title, emailstr);
                                DbHelperSQL.ExecuteSql("update etw_onlines set childreaded='0' where id=" + cid + "");
                            }

                        }
                    }
                }
            }
            else if (RadioButtonList1.SelectedValue == "1")
            {
                string trans_content = Fun.Encode(Fun.NoHTML(Request.Form["trans_content"]));
                if (filename != null && filename != "")
                {
                    DbHelperSQL.ExecuteSql("update etw_translate set trans_content='" + trans_content + "' , personid=" + userid + ", trans_fujian='" + filename + "' where id=" + id + "");
                }
                else
                {
                    DbHelperSQL.ExecuteSql("update etw_translate set trans_content='" + trans_content + "' , personid=" + userid + " where id=" + id + "");
                }
            }
            Response.Write("<script type=\"text/javascript\">window.location.href='untranslist.aspx'</script>");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Cookies["cook_admin"].Expires = DateTime.Now.AddDays(-5);
        Response.Write("<script type=text/javascript>parent.location.reload();</script>");
    }
}
