using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using DSWeb.DataAccess;
using System.Data.SqlClient;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Web.Script.Serialization;

namespace DSWeb.CRM
{
    public partial class CRMClientOverview : System.Web.UI.Page
    {
        public string strHandleType = String.Empty;
        public string sSQL = String.Empty;
        public string gvid = String.Empty;
        public string strUserID = String.Empty;
        public string strCompanyID;//公司GID
        public string strShowName;//用户显示名
        public string strDeptName;//部门名称
        MoneyConvert MoneyConvert = new MoneyConvert();
        TopSeaeEntity TopSeaeEntity = new TopSeaeEntity();
        SeaExportInfoDA SeaExportInfoDA = new SeaExportInfoDA();
        RangeDA RangeDA = new RangeDA();
        T_ALL_DA T_ALL_DA = new T_ALL_DA();
        public int isOpenAnnounce;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] != null)
            {
                strUserID = Session["USERID"].ToString().Trim();
                this.hdUserID.Value = strUserID.Trim();
            }
            else
            {
                Server.Transfer("~/Error/FriendError.aspx");
                return;
                //strUserID = ""; 
            }
            if (Session["SHOWNAME"] != null)
            {
                strShowName = Session["SHOWNAME"].ToString();
            }
            if (Session["COMPANYID"] != null)
            {
                strCompanyID = Session["COMPANYID"].ToString();
            }
            if (Session["DEPTNAME"] != null)
            {
                strDeptName = Session["DEPTNAME"].ToString();
            }
            //
            if (Request.QueryString["id"] != null)//if (Request.QueryString["id"] != null)
            {
                ViewState["GID"] = Request.QueryString["id"].ToString();//"topseaeusername129481962859531250";
                this.hdGid.Value = ViewState["GID"].ToString().Trim();
            }
            if (Request.QueryString["isenabled"] != null)//if (Request.QueryString["id"] != null)
            {
                ViewState["isenabled"] = Request.QueryString["isenabled"].ToString();
                this.hdEnabled.Value = ViewState["isenabled"].ToString().Trim();
            }
            //
            //权限菜单范围
            //string strAction = T_ALL_DA.GetStrSQL("USERID", "select USERID from [user_action] where USERID='" + strUserID.Trim() + "' and ACTIONID='7309DD9E-3A7F-44E9-93A2-3CA571C374E9'");
            //if (strAction.Trim().Equals(""))
            //{
            //    Response.Write("<script>alert('无权限打开往来单位信息!');window.parent.close();window.close();</script>");
            //    Response.End();
            //    //Response.Close();
            //    return;
            //}
            //权限可视范围
            //if (strHandleType.Trim() == "edit")
            //{
            //
            string strV = RangeDA.GetVISIBLERANGE(this.hdUserID.Value.Trim(), "modCRMClientList");
            string strO = RangeDA.GetOPERATERANGE(this.hdUserID.Value.Trim(), "modCRMClientList");
                string alt = "";
                if (strV.Trim().Equals("4"))
                {
                    Response.Write("<script>alert('无权限打开往来单位信息!');window.parent.close();window.close();</script>");
                    Response.End();
                    //Response.Close();
                    return;
                }
                else if (strV.Trim().Equals("0"))
                {
                    if (strV.Trim() != strO.Trim())
                    {
                        if (strO.Trim().Equals("1"))//分公司
                        {
                            if (ViewState["GID"] != null)
                            {
                                alt = RangeDA.GetCRMClientListCompany(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                                if (alt == "")
                                {
                                    getEnabled();
                                }
                            }
                        }
                        else if (strO.Trim().Equals("2"))//部门
                        {
                        }
                        else if (strO.Trim().Equals("3"))//个人
                        {
                            if (ViewState["GID"] != null)
                            {
                                alt = RangeDA.GetCRMClientListPerson(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                                if (alt == "")
                                {
                                    getEnabled();
                                }
                            }
                        }
                        else if (strO.Trim().Equals("4"))//无
                        {
                            if (ViewState["GID"] == null)
                            {
                                Response.Write("<script>alert('无权限新建往来单位信息!');</script>");
                                Response.End();
                                //Response.Close();
                                return;
                            }
                            getEnabled();
                        }
                    }
                }
                else if (strV.Trim().Equals("1"))
                {
                    if (strO.Trim().Equals("1"))//公司
                    {
                        if (ViewState["GID"] != null)
                        {
                            alt = RangeDA.GetCRMClientListCompany(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                    }
                    if (strO.Trim().Equals("2"))//部门
                    {
                    }
                    else if (strO.Trim().Equals("3"))//个人
                    {
                        if (ViewState["GID"] != null)
                        {
                            alt = RangeDA.GetCRMClientListPerson(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                    }
                    else if (strO.Trim().Equals("4"))//无
                    {
                        if (ViewState["GID"] == null)
                        {
                            Response.Write("<script>alert('无权限打开往来单位信息!');</script>");
                            Response.End();
                            //Response.Close();
                            return;
                        }
                        getEnabled();
                    }
                }
                else if (strV.Trim().Equals("2"))
                {
                    if (strO.Trim().Equals("2"))//个人
                    {
                    }
                    else if (strO.Trim().Equals("3"))//个人
                    {
                        if (ViewState["GID"] != null)
                        {
                            alt = RangeDA.GetCRMClientListPerson(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                    }
                    else if (strO.Trim().Equals("4"))//无
                    {
                        if (ViewState["GID"] == null)
                        {
                            Response.Write("<script>alert('无权限打开往来单位信息!');</script>");
                            Response.End();
                            //Response.Close();
                            return;
                        }
                        getEnabled();
                    }
                }
                else if (strV.Trim().Equals("3"))
                {
                    if (strO.Trim().Equals("3"))//个人
                    {
                        if (ViewState["GID"] != null)
                        {
                            alt = RangeDA.GetCRMClientListPerson(this.hdUserID.Value.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                    }
                    else if (strO.Trim().Equals("4"))//无
                    {
                        if (ViewState["GID"] == null)
                        {
                            Response.Write("<script>alert('无权限打开往来单位信息!');window.parent.close();window.close();</script>");
                            Response.End();
                            //Response.Close();
                            return;
                        }
                        getEnabled();
                    }
                }
            //}
            //else
            //{
            //    getEnabledTrue();
            //}
            
            //
            if (this.hdEnabled.Value.ToString() == "false")
            {
                getEnabled();
            }
            //
            if (!IsPostBack)
            {
                if (Request.QueryString["handle"] != null) 
                {
                    ViewState["strHandleType"] = Request.QueryString["handle"].ToString();//"edit";
                    strHandleType = ViewState["strHandleType"].ToString().Trim();
                    this.hdhandle.Value = Request.QueryString["handle"].ToString();
                }
                //
                getdl();
            }          
        }
        protected void getdl()
        {
            //客户资料
            sSQL = "select top 1 GID,CODENAME,SHORTNAME,DESCRIPTION,EnFullName,CHIEF,(case TEL when '86-0-0' then '' when '86--' then '' when '86' then '' else TEL end) as TEL,(case FAX when '86-0-0' then '' when '86--' then '' when '86' then '' else FAX end) as FAX,(case STLNAME when '0' then 'RMB' else 'USD' end) as STLNAME,SALE,OPNAME,OPTIME,ADDR,EMAIL,WEB,AREA,PRODUCT,CTNNUM,IMAGEURL,IMAGEURL2,LANE,ISLANE1,ISLANE2,ISLANE3,ISLANE4,ISLANE5,ISLANE6,ISLANE7,ISLANE8,ISLANE9,ISLANE10,ISLANE11,ISLANE12,ISLANE13,ENFULLNAME2,ENFULLNAME3,REMARK from crm_info_client where gid='" + ViewState["GID"].ToString().Trim() + "' ";
            DataSet DS = T_ALL_DA.GetAllSQL(sSQL);
            DLClient.DataSource = DS;
            DLClient.DataBind();

            //
            //权限可视范围
            string strRange = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modCRMClientList");
            if (strRange.Trim().Equals("0"))//全部
            {
                strRange = "";
            }
            else if (strRange.Trim().Equals("1"))//分公司
            {
                strRange = " and LINKID in(" + RangeDA.GetCRMClientListCompany(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("2"))//部门
            {
            }
            else if (strRange.Trim().Equals("3"))//个人
            {
                strRange = " and LINKID in(" + RangeDA.GetCRMClientListPerson(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("4"))//无
            {
                strRange = " and 1<0";
            }
            else//空
            {
                strRange = " and 1<0";
            }
            //联系人
            sSQL = "select top 5 GID,SHOWNAME 姓名,TEL 电话,MOBILE 手机,EMAIL 邮箱,JOB 职务 from info_client_contact where ISDELETED=0 ";
            if (ViewState["GID"].ToString().Trim() == "")
            {
                sSQL += " and 1<0";
            }
            else
            {
                sSQL += " and LINKID='" + ViewState["GID"].ToString().Trim() + "' " + strRange + " order by CREATETIME desc";
            }
            DS = T_ALL_DA.GetAllSQL(sSQL);
            gvContact.DataSource = DS;
            gvContact.DataBind();

            //
            //权限可视范围
            strRange = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modCRMClientVisitList");//往来单位列表
            if (strRange.Trim().Equals("0"))//全部
            {
                strRange = "";
            }
            else if (strRange.Trim().Equals("1"))//分公司
            {
                strRange = " and GID in(" + RangeDA.GetCRMClientVisitListCompany(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("2"))//部门
            {
            }
            else if (strRange.Trim().Equals("3"))//个人
            {
                strRange = " and GID in(" + RangeDA.GetCRMClientVisitListPerson(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("4"))//无
            {
                strRange = " and 1<0";
            }
            else//空
            {
                strRange = " and 1<0";
            }
            //跟踪记录
            sSQL = "select top 5 GID,CONTACTNAME 联系人,SALE 销售,VISITWAY 拜访方式,convert(char(10),VISITDATE,20) 拜访时间,NEXTACTION 下次行动,convert(char(10),NEXTDATE,20)下次回访时间 from crm_info_client_visit where 1=1 ";
            if (ViewState["GID"].ToString().Trim() == "")
            {
                sSQL += " and 1<0";
            }
            else
            {
                sSQL += " and CLIENTGID='" + ViewState["GID"].ToString().Trim() + "' " + strRange + " order by VISITDATE desc";
            }
            DS = T_ALL_DA.GetAllSQL(sSQL);
            gvVisit.DataSource = DS;
            gvVisit.DataBind();
            //
            //权限可视范围
            strRange = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modCRMInfoFaqList");//往来单位列表
            if (strRange.Trim().Equals("0"))//全部
            {
                strRange = "";
            }
            else if (strRange.Trim().Equals("1"))//分公司
            {
                strRange = " and GID in(" + RangeDA.GetCRMInfoFaqListCompany(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("2"))//部门
            {
            }
            else if (strRange.Trim().Equals("3"))//个人
            {
                strRange = " and GID in(" + RangeDA.GetCRMInfoFaqListPerson(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("4"))//无
            {
                strRange = " and 1<0";
            }
            else//空
            {
                strRange = " and 1<0";
            }
            //询价记录
            sSQL = "select top 5 GID,(case ISTYPE when 0 then '待解决' else '已解决' end) as 解决状态,PROBLEMTYPE 问题类型,substring(PROBLEMTITLE,0,25)+'...' 标题,(select SHOWNAME from [user] where gid=crm_info_faq.CREATEUSER) as 创建人,CREATETIME 创建时间 from crm_info_faq where 1=1 ";
            if (ViewState["GID"].ToString().Trim() == "")
            {
                sSQL += " and 1<0";
            }
            else
            {
                sSQL += " and CLIENTGID='" + ViewState["GID"].ToString().Trim() + "' " + strRange + " and ISPROBLEM=1 and PROBLEMGID='#' order by CREATETIME desc";
            }
            DS = T_ALL_DA.GetAllSQL(sSQL);
            gvFaq.DataSource = DS;
            gvFaq.DataBind();

            //
            //权限可视范围
            strRange = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modCRMClientQuotationList");//往来单位列表
            if (strRange.Trim().Equals("0"))//全部
            {
                strRange = "";
            }
            else if (strRange.Trim().Equals("1"))//分公司
            {
                strRange = " and GID in(" + RangeDA.GetCRMClientQuotationListCompany(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("2"))//部门
            {
            }
            else if (strRange.Trim().Equals("3"))//个人
            {
                strRange = " and GID in(" + RangeDA.GetCRMClientQuotationListPerson(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("4"))//无
            {
                strRange = " and 1<0";
            }
            else//空
            {
                strRange = " and 1<0";
            }
            //报价记录
            sSQL = "select top 5 GID,SALE 销售,convert(char(10),QUOTATIONDATE,20) 报价日期,PORTDISCHARGE 目的港,LANE 航线,CARRIER 船公司 from crm_quotation where 1=1 ";
            if (ViewState["GID"].ToString().Trim() == "")
            {
                sSQL += " and 1<0";
            }
            else
            {
                sSQL += " and CLIENTGID='" + ViewState["GID"].ToString().Trim() + "' " + strRange + " order by QUOTATIONDATE desc";
            }
            DS = T_ALL_DA.GetAllSQL(sSQL);
            gvQuotation.DataSource = DS;
            gvQuotation.DataBind();
        }
        protected void getEnabled()
        {
            //
            //this.ibEnter.Disabled = true;
            //this.imgDel.Disabled = true;
            //ibDel.Enabled = false;
            imgAdd1.Disabled = true;
            //
            this.hdEnabled.Value = "false";
            //
        }
        protected void getEnabledTrue()
        {
            //ibAdd.Enabled = true;
            //this.ibEnter.Disabled = false;
            //this.imgDel.Disabled = false;
            //ibDel.Enabled = true;
            imgAdd1.Disabled = false;
            //
            this.hdEnabled.Value = "true";
            //
        }
        protected String getBSNO()
        {
            string strBSNO = Guid.NewGuid().ToString();
            strBSNO = strBSNO.Replace("-", "");
            strBSNO = "InfoClient" + strBSNO;
            return strBSNO;
        }
        public bool IsNumeric(string s)
        {
            bool bReturn = true;
            try
            {
                int inum = int.Parse(s);
            }
            catch
            {
                bReturn = false;
            }
            return bReturn;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public string GetTitle(string o, int num)
        {
            if (o.Length > num)
            {
                o = o.Substring(0, num) + "...";
            }
            return o;
        }
        public string GetTitle(object o, int num)
        {
            string str = o.ToString();
            if (str.Length > num)
            {
                str = str.Substring(0, num) + "...";
            }
            return str;
        }

        protected void gvQuotation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //隐藏列
            e.Row.Cells[0].Visible = false;

            //下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6B696B';");//,this.style.fontWeight='Bold';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //设置悬浮鼠标指针形状为"小手"
            //e.Row.Attributes["style"] = "Cursor:hand";
            //单击行选中变色
            e.Row.Attributes.Add("onclick ", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#6B696B';window.oldtr=this;");//字体变色this.style.color='#FFFFFF';
            //e.Row.ForeColor = System.Drawing.Color.White;

            //双击该行,弹出详细的信息页面
            string strGID = e.Row.Cells[0].Text;
            //string strHandle = e.Row.Cells[e.Row.Cells.Count - 1].Text.Replace("&amp;", "&");
            //string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
            e.Row.Attributes.Add("OnDblClick", "return openQuotation('" + strGID + "')");
            e.Row.ToolTip = "双击该行,显示更详细的信息。";
            //e.Row.BackColor = System.Drawing.Color.FromName("#FFE4B5");
        }

        protected void gvFaq_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //隐藏列
            e.Row.Cells[0].Visible = false;

            //下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6B696B';");//,this.style.fontWeight='Bold';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //设置悬浮鼠标指针形状为"小手"
            //e.Row.Attributes["style"] = "Cursor:hand";
            //单击行选中变色
            e.Row.Attributes.Add("onclick ", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#6B696B';window.oldtr=this;");//字体变色this.style.color='#FFFFFF';
            //e.Row.ForeColor = System.Drawing.Color.White;

            //双击该行,弹出详细的信息页面
            string strGID = e.Row.Cells[0].Text;
            //string strHandle = e.Row.Cells[e.Row.Cells.Count - 1].Text.Replace("&amp;", "&");
            //string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
            e.Row.Attributes.Add("OnDblClick", "return openFaq('" + strGID + "')");
            e.Row.ToolTip = "双击该行,显示更详细的信息。";
            //e.Row.BackColor = System.Drawing.Color.FromName("#FFE4B5");
        }

        protected void gvVisit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //隐藏列
            e.Row.Cells[0].Visible = false;

            //下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6B696B';");//,this.style.fontWeight='Bold';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //设置悬浮鼠标指针形状为"小手"
            //e.Row.Attributes["style"] = "Cursor:hand";
            //单击行选中变色
            e.Row.Attributes.Add("onclick ", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#6B696B';window.oldtr=this;");//字体变色this.style.color='#FFFFFF';
            //e.Row.ForeColor = System.Drawing.Color.White;

            //双击该行,弹出详细的信息页面
            string strGID = e.Row.Cells[0].Text;
            //string strHandle = e.Row.Cells[e.Row.Cells.Count - 1].Text.Replace("&amp;", "&");
            //string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
            e.Row.Attributes.Add("OnDblClick", "return openVisit('" + strGID + "')");
            e.Row.ToolTip = "双击该行,显示更详细的信息。";
            //e.Row.BackColor = System.Drawing.Color.FromName("#FFE4B5");

            //列的宽度
            e.Row.Cells[0].Width = 0;
            e.Row.Cells[1].Width = 100;
            e.Row.Cells[2].Width = 100;
            e.Row.Cells[3].Width = 100;
            e.Row.Cells[4].Width = 120;
            e.Row.Cells[5].Width = 100;
            //sSQL = "select top 5 GID,SHOWNAME 姓名,TEL 电话,MOBILE 手机,EMAIL 邮箱,JOB 职务 from info_client_contact where ISDELETED=0 and LINKID='" + ViewState["GID"].ToString().Trim() + "' order by CREATETIME desc";
        }

        protected void gvContact_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //隐藏列
            e.Row.Cells[0].Visible = false;

            //下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6B696B';");//,this.style.fontWeight='Bold';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //设置悬浮鼠标指针形状为"小手"
            //e.Row.Attributes["style"] = "Cursor:hand";
            //单击行选中变色
            e.Row.Attributes.Add("onclick ", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#6B696B';window.oldtr=this;");//字体变色this.style.color='#FFFFFF';
            //e.Row.ForeColor = System.Drawing.Color.White;

            //双击该行,弹出详细的信息页面
            string strGID = e.Row.Cells[0].Text;
            //string strHandle = e.Row.Cells[e.Row.Cells.Count - 1].Text.Replace("&amp;", "&");
            //string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
            e.Row.Attributes.Add("OnDblClick", "return openContact('" + strGID + "')");
            e.Row.ToolTip = "双击该行,显示更详细的信息。";
            //e.Row.BackColor = System.Drawing.Color.FromName("#FFE4B5");
        }
    }
}