<!--#include file="Conn.asp"--><%Checklogin()%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理区域</title>
<link rel="stylesheet" href="system/css/common.css" type="text/css" />
<script language="javascript" src="system/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="system/js/js.js"></script>
</head>
<body class="clear">
<%

Page = Request.QueryString("Page")
Keys=Server.UrlEnCode(Request.QueryString("Keys"))
fliterBusiness=Request.QueryString("fliterBusiness")
fliterDeal=Request.QueryString("Deal")

id = Request.Form("id")
cs_code=TextEncode(Request.Form("cs_code"))
cs_company=TextEncode(Request.Form("cs_company"))
cs_name=TextEncode(Request.Form("cs_name"))
cs_country=Request.Form("cs_country")
cs_from=Request.Form("cs_from")
cs_tel=TextEncode(Request.Form("cs_tel"))
cs_wechat=TextEncode(Request.Form("cs_wechat"))	
cs_whatsapp=TextEncode(Request.Form("cs_whatsapp"))
cs_email=TextEncode(Request.Form("cs_email"))	
cs_linkedin=TextEncode(Request.Form("cs_linkedin"))	
cs_facebook=TextEncode(Request.Form("cs_facebook"))	
cs_alibaba=TextEncode(Request.Form("cs_alibaba"))
cs_alibaba=replace(cs_alibaba,":",":")
cs_alibaba=replace(cs_alibaba,"ID:","")	
cs_address=TextEncode(Request.Form("cs_address"))
cs_type=TextEncode(Request.Form("cs_type"))
cs_belongclient=Request.Form("cs_belongclient")
cs_addtime=Request.Form("cs_addtime")
cs_updatetime=now()
cs_belong=Session("employee_id")
cs_state=1
cs_deal=Request.Form("cs_deal")
cs_telformat=NumFormat(Request.Form("cs_tel"))
cs_whatsappformat=NumFormat(Request.Form("cs_whatsapp"))
allowedit=Request.Form("Permissions") '用于处理误报数据

If not isNumeric(allowedit) Then allowedit=0

If cs_country="" or Not isNumeric(cs_country)  Then cs_country=0
If cs_from="" or Not isNumeric(cs_from)  Then cs_from=0
If cs_deal="" or Not isNumeric(cs_deal)  Then cs_deal=1
If cs_type="" or Not isNumeric(cs_type)  Then cs_type=5
If cs_belongClient="" or Not isNumeric(cs_belongClient)  Then cs_belongClient=0

cs_note=HtmlEncode(Request.Form("cs_note"))
mytag=TextEncode(Request.Form("mytag"))
mytag=replace(mytag,"&#60;&#47;span&#62;&#60;span&#62;",",")
mytag=replace(mytag,"&#60;&#47;span&#62;","")
mytag=replace(mytag,"&#60;span&#62;","")
mytag=split(mytag,",") 

If (instr(cs_code,";1688"))>0 Then cs_from=1 End If'1 为1688,2为国际站,判断阿里旺旺是否录入 
If (instr(cs_code,";阿里"))>0 Then cs_from=2 End If'1 为1688,2为国际站,判断阿里旺旺是否录入

If allowedit<>1 Then
   If (cs_from=1 or cs_from=2) and cs_alibaba="" or isNull(cs_alibaba) Then
	Response.write "<script>alert('阿里旺旺为必填项');history.back();</script>"
	Conn.Close:Set Conn=Nothing			
	Response.End	
   End If

If (instr(cs_code,"官网"))>0 Then cs_from=3 End If'3为官网

If cs_from=3 and  cs_tel="" and cs_whatsapp=""  and cs_wechat=""Then
	Response.write "<script>alert('电话和WhatsApp为必填项');history.back();</script>"
	Conn.Close:Set Conn=Nothing			
	Response.End	
End If

If cs_from=8 and  cs_wechat="" Then '8为市场客户
	Response.write "<script>alert('微信为必填项');history.back();</script>"
	Conn.Close:Set Conn=Nothing			
	Response.End	
End If

If cs_from=12 and  cs_facebook="" Then '12为Facebook
	Response.write "<script>alert('Facebook为必填项');history.back();</script>"
	Conn.Close:Set Conn=Nothing			
	Response.End	
End If

End If


Set Rs=Server.CreateObject("ADODB.RecordSet")

If id="" or Not isNumeric(id)  Then
	act="addSave"	
Else
	Rs.Open "Select * from Customer where id="&id,conn,1,1
		If Not Rs.bof and Not Rs.eof Then
			if Rs("cs_belong")=cint(cs_belong) Then
				act="editSave"			
			Else
				Response.write "<script>alert('抱歉,该客户属于另一业务,你没有权限修改');history.back();</script>"
				Rs.close:Set Rs=Nothing
				Conn.Close:Set Conn=Nothing			
				Response.End					
			End if
		Else
				Response.write "<script>alert('没有此客户!');history.back();</script>"
				Rs.close:Set Rs=Nothing
				Conn.Close:Set Conn=Nothing			
				Response.End		
		End if
	Rs.close	
End if


If cs_code="" or IsNull(cs_code) Then
	Response.write "<script>alert('客户编码不能为空');history.back();</script>"
	Conn.Close:Set Conn=Nothing			
	Response.End	
End IF


checkStr="Select  * from Customer where cs_belong<>"&Session("employee_id")&" and (id=0 "

If cs_tel<>"" Then
		checkStr=checkStr & "or cs_telformat like '%"&Mid(cs_telformat,4,9)&"%'" & " or cs_wechat like '%"&Mid(cs_telformat,4,9)&"%'" & " or cs_whatsappformat like '%"&cs_telformat&"%'" 
		Dupli="电话:"&cs_tel
End IF	'查电话	

If cs_email<>"" Then
		checkStr=checkStr & " or cs_email='"&cs_email&"'"
		Dupli=Dupli&"邮箱:"&cs_email
End If '查邮件

If cs_whatsapp<>"" Then
		checkStr=checkStr & " or cs_whatsappformat like '%"&mid(cs_whatsappformat,4,9)&"%'" & " or cs_telformat like '%"&mid(cs_whatsappformat,4,9)&"%'"
		Dupli=Dupli&"WhatsApp:"&cs_whatsapp
End If '查whatsapp

If cs_wechat<>"" Then
		If len(cs_wechat)<10 Then
			checkStr=checkStr & " or cs_wechat like '%"&cs_wechat&"'" & " or cs_telformat like '%"&cs_wechat&"'"	
		Else
			checkStr=checkStr & " or cs_wechat like '%"&mid(cs_wechat,3,12)&"%'" & " or cs_telformat like '%"&mid(cs_wechat,3,12)&"%'"	
	 	End If

		Dupli=Dupli&"微信:"&cs_wechat
End If	'查微信

If cs_linkedin<>"" Then
		checkStr=checkStr & " or cs_linkedin like '%"&cs_linkedin&"%'"	
		Dupli=Dupli&"Linked:"&cs_linkedin		
End If	'查领英

If cs_facebook<>"" Then
		checkStr=checkStr & " or cs_facebook like '%"&cs_facebook&"%'"	
		Dupli=Dupli&"Facebook:"&cs_facebook			
End If	'查facebook

If cs_alibaba<>"" Then

		If len(cs_alibaba)<10 Then
			checkStr=checkStr & " or cs_alibaba like '"&cs_alibaba&"'"
		Else
			checkStr=checkStr & " or cs_alibaba like '%"&mid(cs_alibaba,4,12)&"%'" 	
	 	End If
	
		Dupli=Dupli&"阿里旺旺:"&cs_alibaba	
End If	'查alibaba
	
	checkStr=checkStr & " ) Order by id asc"

If allowedit<>1 Then
	Rs.Open checkStr,conn,1,3
		 If not Rs.bof and not Rs.eof  Then'信息重复
				Set Rs1=Server.CreateObject("ADODB.RecordSet")
					Rs1.Open "Select em_user from employee where id="&Rs("cs_belong"),conn,1,1
					owner=TextUnCode(Rs1("em_user"))
					Rs1.close
				Set Rs1=Nothing
						
				if DateDiff("n",cs_addtime,Rs("cs_addtime"))>0 Then				
					tstr="Insert into logRecord (loginName,loginIp,loginTime,loginAct) values('"&Session("employee_name")&"','"&getIp()&"','"&now()&"','"&Session("employee_name")&"编辑客户"""&cs_code&""",该客户与"""&TextUncode(Rs("cs_code"))&"""高度类似,<br>重复项为:"&Dupli&"<br>客户由:"&Session("employee_name")&cs_addtime&"首次录入')"			
				Else
					tstr="Insert into logRecord (loginName,loginIp,loginTime,loginAct) values('"&Session("employee_name")&"','"&getIp()&"','"&now()&"','"&Session("employee_name")&"编辑客户"""&cs_code&""",该客户与"""&TextUncode(Rs("cs_code"))&"""高度类似,<br>重复项为:"&Dupli&"<br>客户由:"&owner&Rs("cs_addtime")&"首次录入')"	
				End if			
				Conn.execute(tstr)						
				Response.write"<script>alert('录入信息\n与"&owner&"客户编号:"&TextUncode(Rs("cs_code"))&" \n高度类似,未能保存,请联系管理员核实!');history.back();</script>"				
				Rs.close:Set Rs=Nothing
				Conn.Close:Set Conn=Nothing			
				Response.End	
		 End IF
	Rs.close	 
End If
		 
If act="editSave" or allowedit=1 Then
			hrefstr = "/customers.asp?Keys="&Keys&"&fliterBusiness="&fliterBusiness&"&fliterDeal="&fliterDeal&"&Page="&page
			Rs.Open "Select * from Customer where id="&id,conn,1,3
			Rs("cs_code")=cs_code
			Rs("cs_company")=cs_company
			Rs("cs_name")=cs_name
			Rs("cs_country")=cs_country
			Rs("cs_from")=cs_from				
			Rs("cs_tel")=cs_tel
			Rs("cs_wechat")=cs_wechat
			Rs("cs_whatsapp")=cs_whatsapp			
			Rs("cs_email")=cs_email
			Rs("cs_linkedin")=cs_linkedin		
			Rs("cs_facebook")=cs_facebook
			Rs("cs_address")=cs_address		
			Rs("cs_type")=cs_type				
			Rs("cs_alibaba")=cs_alibaba			
			Rs("cs_updatetime")=cs_updatetime
			Rs("cs_belong")=cs_belong
			Rs("cs_belongclient")=cs_belongclient			
			Rs("cs_state")=cs_state
			Rs("cs_telformat")=cs_telformat
			Rs("cs_whatsappformat")=cs_whatsappformat
			Rs("cs_deal")=cs_deal				
			Rs("cs_note")=cs_note		
			IF cs_deal=3 and IsNull(Rs("cs_dealdate")) Then	Rs("cs_dealdate")=now()	End If
			Rs.update
			'保存标签
			conn.execute("delete from tagTable where customerId="&id&"")
			for i=0 to ubound(mytag) 
				conn.execute("Insert into tagTable (tagName,employeeId,customerId) values('"&mytag(i)&"',"&Session("employee_id")&","&id&")")			
			Next			
			'保存标签end
			Rs.close
			Set Rs=Nothing
			Conn.Close:Set Conn=Nothing	
			response.write "<script>location.href='../"&hrefstr&"';</script>"
Else '新增
			Rs.Open "Select * from Customer",conn,1,3
			Rs.addNew()
			Rs("cs_code")=cs_code
			Rs("cs_company")=cs_company
			Rs("cs_name")=cs_name
			Rs("cs_country")=cs_country
			Rs("cs_from")=cs_from			
			Rs("cs_tel")=cs_tel
			Rs("cs_wechat")=cs_wechat	
			Rs("cs_whatsapp")=cs_whatsapp			
			Rs("cs_email")=cs_email
			Rs("cs_linkedin")=cs_linkedin		
			Rs("cs_facebook")=cs_facebook	
			Rs("cs_alibaba")=cs_alibaba				
			Rs("cs_address")=cs_address	
			Rs("cs_telBu")=cs_tel '以下Bu for BackUp防止误删
			Rs("cs_wechatBu")=cs_wechat	
			Rs("cs_whatsappBu")=cs_whatsapp		 	
			Rs("cs_emailBu")=cs_email 
			Rs("cs_linkedinBu")=cs_linkedin		 
			Rs("cs_facebookBu")=cs_facebook	
			Rs("cs_alibabaBu")=cs_alibaba					
			Rs("cs_type")=cs_type			
			Rs("cs_addtime")=now()
			Rs("cs_updatetime")=now()
			Rs("cs_belong")=cs_belong	
			Rs("cs_belongClient")=cs_belongClient				
			Rs("cs_state")=cs_state
			Rs("cs_telformat")=cs_telformat
			Rs("cs_whatsappformat")=cs_whatsappformat
			Rs("cs_deal")=cs_deal
			Rs("colortag")=0			
			Rs("cs_note")=cs_note		
			Rs("cs_chain")=cs_belong
			Rs("is_silent")=0
			IF cs_deal=3 Then Rs("cs_dealdate")=now() End If			
			Rs.update
			Rs.close

			Rs.OPEN "select top 1 id from Customer order by id desc",conn,1,1
				id=Rs("id")
			Rs.close			
			
			'保存标签
			for i=0 to ubound(mytag) 
				conn.execute("Insert into tagTable (tagName,employeeId,customerId) values('"&mytag(i)&"',"&Session("employee_id")&","&id&")")			
			Next			
			'保存标签end	

			Set Rs=Nothing
			Conn.Close:Set Conn=Nothing				
			response.write "<script>location.href='../customerAdd.asp';</script>"
End If
%>
</body>
</html>