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_recyclerdetail : System.Web.UI.Page
{
    public string page = string.Empty;
    public string id = string.Empty;
    public string number = string.Empty;
    public etw.etw_onlines lead = new etw.etw_onlines();
    public string usernameid = string.Empty;
    public string filename = string.Empty;
    public string SQLstr = string.Empty;
    public string SQLstr1 = string.Empty;
    public string username = string.Empty;
    public int iszizhuanghu = 0;
    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
            {
                username = GetCookie["username"];
            }
        }
        else
        {
            username = ChatCookie["username"];
			iszizhuanghu = 1;
        }
        if (Request.QueryString["page"] != null && Request.QueryString["id"] != null && Request.QueryString["number"] != null)
        {
            page = Request.QueryString["page"].ToString();
            id = Request.QueryString["id"].ToString();
            number = Request.QueryString["number"].ToString();
            if (Convert.ToInt32(number) == 0 && Convert.ToInt32(page) > 0)
            {
                number = "10";
            }
            DataSet ds = DbHelperSQL.Query("select id from etw_user where username='" + username + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                usernameid = ds.Tables[0].Rows[0]["id"].ToString();
                SQLstr = "select top 1 id from etw_onlines where id>" + id + "  and isdelete=1  and usernameid=" + usernameid + " and id not in (select onlines_id  from etw_repeat where usernameid=" + usernameid + ")  order by id ";
                SQLstr1 = "select top 1 id from etw_onlines where id<" + id + " and isdelete=1  and usernameid=" + usernameid + " and id not in (select onlines_id  from etw_repeat where usernameid=" + usernameid + ")  order by id desc";
            }
            else
            {
                ds = DbHelperSQL.Query("select userid,id from chat_user where username='" + username + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    usernameid = ds.Tables[0].Rows[0]["userid"].ToString();
                    SQLstr = "select top 1 id from etw_onlines where id>" + id + "  and isdelete=1 and childuserid=" + ds.Tables[0].Rows[0]["id"].ToString() + " and id not in (select onlines_id  from etw_repeat where usernameid=" + usernameid + ")   order by id ";
                    SQLstr1 = "select top 1 id from etw_onlines where id<" + id + " and isdelete=1 and childuserid=" + ds.Tables[0].Rows[0]["id"].ToString() + " and id not in (select onlines_id  from etw_repeat where usernameid=" + usernameid + ")  order by id desc";
                }
            }
            Fun.Funrecord(Request.Url.AbsoluteUri,Page.Request.UserHostAddress, usernameid);
            lead.id = Convert.ToInt32(id);
            lead.GetModel();
            //DataSet ds1 = DbHelperSQL.Query("select id from etw_translate where chatid=" + id + "");
            //if (ds1.Tables[0].Rows.Count > 0)
            //{
            LinkButton2.Visible = false;
            //}
            if (iszizhuanghu == 1)
            {
                if (lead.childreaded == 0)
                {
                    lead.Upchildreaded();
                }
            }
            else
            {
                if (lead.readed == 0)
                {
                    lead.Upreaded();
                }
            }
            if (!IsPostBack)
            {
                DataSet dss = DbHelperSQL.Query("select source_content,fujian,receivetime,trans_content,achivetime,trans_fujian,type from etw_translate where chatid=" + id + " order by receivetime asc");
                if (dss.Tables[0].Rows.Count > 0)
                {
                    Repeater1.DataSource = dss.Tables[0].DefaultView;
                    Repeater1.DataBind();
                }
            }
        }
    }
    public string Cutren(string str)
    {
        string content = string.Empty;
        DataSet dss = DbHelperSQL.Query("select id from etw_onlines where usernameid=" + usernameid + " and email='" + lead.email + "'");
        if (dss.Tables[0].Rows.Count < 2)
        {
            content = "<span style=\"color:red\">-- 这是新邮件。</span>";
        }
        if (str != "")
        {
            DataSet ds = DbHelperSQL.Query("select id,realname from chat_user where id=" + str + "");
            if (ds.Tables[0].Rows.Count > 0)
            {
                content += " ---外贸员:" + ds.Tables[0].Rows[0]["realname"].ToString();
            }
        }
        return content;
    }
    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 Button1_Click(object sender, EventArgs e)
    {
        Savefujian();
        etw.etw_translate lead_tr = new etw.etw_translate();
        lead_tr.chatid = Convert.ToInt32(id);
        lead_tr.userid = Convert.ToInt32(usernameid);
        lead_tr.language = "0";
        lead_tr.target = "cn";
        lead_tr.source_content = Fun.Encode(Fun.NoHTML(contentstr.Value));
        lead_tr.receivetime = Fun.GetDateTime();
        lead_tr.type = 2;
        lead_tr.fujian = filename;
        lead_tr.ischild = lead.childuserid;
        lead_tr.newAdd();
        Response.Write("<script language=\"javascript\">window.location='recyclerdetail.aspx?id=" + id + "&page=" + page + "&number=" + number + "'</" + "script>");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        etw.etw_translate lead_tr = new etw.etw_translate();
        lead_tr.chatid = Convert.ToInt32(id);
        lead_tr.userid = Convert.ToInt32(usernameid);
        lead_tr.language = "0";
        lead_tr.target = "cn";
        lead_tr.source_content = Fun.Encode(Fun.NoHTML(contentstr.Value));
        lead_tr.receivetime = Fun.GetDateTime();
        lead_tr.type = 1;
        lead_tr.ischild = lead.childuserid;
        lead_tr.newAdd();
        Response.Write("<script language=\"javascript\">window.location='recyclerdetail.aspx?id=" + id + "&page=" + page + "&number=" + number + "'</" + "script>");
    }
    public string Cuttips(string str)
    {
        if (str != "")
        {
            str = "ETW:";
        }
        return str;
    }
    public string Cutkuo(string str)
    {
        string kuozhan = string.Empty;
        if (str != "")
        {
            DataSet ds = DbHelperSQL.Query("select * from etw_language where language='" + str + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                kuozhan = ds.Tables[0].Rows[0]["kuozhan"].ToString();
            }
        }
        return kuozhan;
    }
    public string CutAE()
    {
        string str = string.Empty;
        string kuozh = Cutkuo(lead.language.ToString()).ToString();
        if (kuozh == "ae")
        {
            str = "<a href=\"#\" class=\"LinkETW\" onclick=\"showforae('trancon','ae',this);return false;\">阿拉伯</a>";
        }
        return str;
    }
    public string CutFUjian(string str, string tips)
    {
        string constr = string.Empty;
        if (str != "")
        {
            constr = "<a href=\"Downloadfile.aspx?filename=" + str + "\"><img src=\"images/downpic.gif\" />" + tips + "</a>";
        }
        return constr;
    }
    public string CutUP()
    {
        int totalOrders = Convert.ToInt32(number) - 1;
        int pagecount = 0;
        if (totalOrders % 20 == 0)
        {
            pagecount = totalOrders / 20;
        }
        else
        {
            pagecount = (totalOrders / 20) + 1;
        }
        pagecount = pagecount + Convert.ToInt32(page) - 1;
        if (totalOrders > 20)
        {
            totalOrders = totalOrders - 20;
        }
        string back = "<a  class=\"txt-disabd\">上一封</a>";
        DataSet ds = DbHelperSQL.Query(SQLstr);
        if (ds.Tables[0].Rows.Count > 0)
        {
            back = "<a href=\"recyclerdetail.aspx?id=" + ds.Tables[0].Rows[0]["id"] + "&page=" + pagecount + "&number=" + totalOrders + "\">上一封</a>";
        }
        return back;
    }
    public string CutNext()
    {
        int totalOrders = Convert.ToInt32(number) + 1;
        int pagecount = 0;
        if (totalOrders % 20 == 0)
        {
            pagecount = totalOrders / 20;
        }
        else
        {
            pagecount = (totalOrders / 20) + 1;
        }
        pagecount = pagecount + Convert.ToInt32(page) - 1;
        if (totalOrders > 20)
        {
            totalOrders = totalOrders - 20;
        }
        string back = "<a  class=\"txt-disabd\">下一封</a>";
        DataSet ds = DbHelperSQL.Query(SQLstr1);
        if (ds.Tables[0].Rows.Count > 0)
        {
            back = "<a href=\"recyclerdetail.aspx?id=" + ds.Tables[0].Rows[0]["id"] + "&page=" + pagecount + "&number=" + totalOrders + "\">下 一封</a>";
        }
        return back;
    }
    public string CutAddress(string str)//获取物理地址
    {
        if (str == "" || str == null)
        {
            str = Fun.IPAddress(lead.ip.ToString());
        }
        return str;
    }
    public string Cuttitle(string title)//自定义主题
    {
        if (title != null && title != "")
        {
            title = "<ul> <li class=\"xun_tit\">自定义主题</li><li class=\"xun_con\">" + title + "</li></ul>";
        }
        return title;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        contentstr.Value = "";
    }
}

