var edit_id = helper.request.queryString("edit_id");
$(function() {
// 	//获取所属行业//获取税务类别//获取客户信誉等级
// 	$.when(helper.getDropList.getlistDropByDic($('#customer_subtrade'), 'KHSSHY'),
// 			helper.getDropList.getlistDropByDic($('#customer_taxCategory'), 'SWLB'),
// 			helper.getDropList.getlistDropByDic($('#customer_layer'), 'KHLayer'))
// 		.done(function() {
			
			if (edit_id) {
				getCustomer(edit_id);
			}
			$('.project_save').click((function(){
				addedit_customer(edit_id)
			}))
			
		// });
		
});
//点击显示
$("#customer_taxPointDes").change(function(){
			

	if($("#customer_taxPointDes").find("option:selected").text()=="二级商"){
		$("#bbb").show()
	}else{
		$("#bbb").hide()
	}
			// $(this).find("option").each(function(){
			// 	if ($(this).prop("selected")) {
			// 		$("#bbb"+$(this).val()).show()
			// 	} else {
			// 		$("#bbb"+$(this).val()).hide()
			// 	}
			// })
		})
$("#customer_taxPointDes").change(function(){
	if($("#customer_taxPointDes").find("option:selected").text()=="种田大户"){
		$("#ccc").show()
	}else{
		$("#ccc").hide()
	}
	
		})

$("#customer_feeEx").change(function(){
	if($("#customer_feeEx").find("option:selected").text()=="其他"){
		$("#ddd").show()
	}else{
		$("#ddd").hide()
	}
	
})
$("#customer_Qualification").change(function(){
	if($("#customer_Qualification").find("option:selected").text()=="其他"){
		$("#eee").show()
	}else{
		$("#eee").hide()
	}
	
})



//获取详情
function getCustomer(ids) {
	$.getJSON(huayi.config.callcenter_url + "CustomerNew/GetCustomer", {
		cusid: ids, //id
		token: $.cookie("token")
	}, function(data) {
		if (data.state.toLowerCase() == "success") {
			data = data.data;
			if (data) {			
				$('#customer_code').val(data.F_CustomerCode);
				$('#customer_companyname').val(data.F_CompanyName);
				$("#customer_comaddress option:selected").text(data.F_AreaName); // 大区
				$("#customer_comaddress option:selected").val(data.F_AreaName); // 大区
				// $("#customer_legalName").val(data.F_BranchName); // 分公司
				$("#customer_legalName option:selected").text(data.F_BranchName); // 分公司
				$("#customer_legalName option:selected").val(data.F_BranchName); // 分公司
				$('#customer_lehalTel').val(data.F_Salesman);
				$('#customer_subtrade').val(data.F_SalesPhone);
				$('#customer_taxCategory').val(data.F_CustomerNature);
				// $('#customer_taxPointDes').val(data.F_Channel);
				$("#customer_taxPointDes option:selected").text(data.F_Channel); // 渠道类型
				$("#customer_taxPointDes option:selected").val(data.F_Channel); // 渠道类型
				
				$("#customer_invoiceRange option:selected").text(data.F_Credit); // 信用等级
				$("#customer_invoiceRange option:selected").val(data.F_Credit); // 信用等级
				$('#customer_layer').val(data.F_Legal);
				$('#customer_bedept').val(data.F_LegalPhone);
				$("#source option:selected").val(data.F_AddressProvince); // 省
				$("#source option:selected").text(data.F_AddressProvince); // 省
				// $("#type").text(data.F_AddressCity); // 市
				
				$("#type option:selected").text(data.F_AddressCity); // 市
				$("#type option:selected").val(data.F_AddressCity); // 市
				alert(data.F_AddressCity);
				$("#keyid option:selected").text(data.F_AddressCountry); // 县
				$("#keyid option:selected").val(data.F_AddressCountry); // 县
				$("#input_area option:selected").text(data.F_AddressTownship); // 乡
				$("#input_area option:selected").val(data.F_AddressTownship); // 乡
			
				
				$('#customer_LegalIDCards').val(data.F_AddressFull);
				$('#customer_Qualification').val(data.F_Brands);
				$("#customer_FinancialManager option:selected").text(data.F_Product); // 产品   $("input[type=radio][value='1']").prop("checked", true);
				$("#customer_FinancialManager option:selected").val(data.F_Product); // 产品
				$('#customer_BusinessOwner').val(data.F_FrozenFlag);
				$('#customer_isbankSlips').val(data.F_IsOpenAccount);
				$('#customer_Remark2').val(data.F_LevelCode);
				$('#customer_Remark3').val(data.F_LevelName);
				$('#customer_service').val(data.F_MnemonicCode);
				$('#customer_handoverDate').val(data.F_InvoiceBank);
				$('#customer_address').val(data.F_InvoiceBankAccount);
				$('#customer_address1').val(data.F_TaxRegCode);
				$('#customer').val(data.F_IsRunXLX);
				$('#customer_address2').val(data.F_AnnualSales);
				$('#customer_address3').val(data.F_Formula);
				$('#customer_address4').val(data.F_RaiseCrops);
				$('#customer_feeExpires').val(data.F_PlantingArea);
				$('#customer_feeEx').val(data.F_FertilizerBrand);	
					if(data.F_Gender==="1"){
						$("input[type=radio][value='1']").prop("checked", true);
					}else{
						$("input[type=radio][value='2']").prop("checked", true);
					}
				// if(data.F_BusContent){
				// 	$('#bus_content').html(decodeURIComponent(data.model.F_BusContent));//	string 物料内容	  F_Gender
				// }else{
				// 	$('#bus_content').text('暂无内容。。。');
				// }
				
				$('#F_Remark').html(helper.filter.content(data.F_Remark));//	string 备注
			}
		}
		// alert(111111111111111);
	});
}

//修改保存
function addedit_customer(editId) {
		$.post(huayi.config.callcenter_url + "CustomerNew/Update", {
					F_CustomerId:editId,
					F_MaterialCode: $("#F_MaterialCode").val(),//物料编码
					F_CustomerCode: $('#customer_code').val(),
					F_CompanyName:  $('#customer_companyname').val(),
					F_AreaID: $('#customer_comaddress').val(),
					F_BranchID: $('#customer_legalName').val(),
					F_Salesman:$('#customer_lehalTel').val(),
					F_SalesPhone:$('#customer_subtrade').val(),
					F_CustomerNature:$('#customer_taxCategory').val(),
					F_Channel:$('#customer_taxPointDes').val(),
					F_Credit:$('#customer_invoiceRange').val(),
					F_Legal:$('#customer_layer').val(),
					F_LegalPhone:$('#customer_bedept').val(),
					F_AddressProvince:$('#source').val(),
					F_AddressCity:$('#type').val(),
					F_AddressCountry:$('#keyid').val(),
					F_AddressTownship:$('#input_area').val(),
					F_AddressFull:$('#customer_LegalIDCards').val(),
					F_Brands:$('#customer_Qualification').val(),
					F_Product:$('#customer_FinancialManager').val(),
					F_FrozenFlag:$('#customer_BusinessOwner').val(),
					F_IsOpenAccount:$('#customer_isbankSlips').val(),
					F_LevelCode:$('#customer_Remark2').val(),
					F_LevelName:$('#customer_Remark3').val(),
					F_MnemonicCode:$('#customer_service').val(),
					F_InvoiceBank:$('#customer_handoverDate').val(),
					F_InvoiceBankAccount:$('#customer_address').val(),
					F_TaxRegCode:$('#customer_address1').val(),
					F_IsRunXLX:$('#customer').val(),
					F_Gender:$('#gender').find('input[type="radio"]:checked').val(),//男女
					F_AnnualSales:$('#customer_address2').val(),
					F_Formula:$('#customer_address3').val(),
					F_RaiseCrops:$('#customer_address4').val(),
					F_PlantingArea:$('#customer_feeExpires').val(),
					F_FertilizerBrand:$('#customer_feeEx').val(),					
					F_Remark:$('#F_Remark').val(),
					token: $.cookie("token")
			}, function(result) {
				result = $.parseJSON(result);
				if(result.state.toLowerCase() == "success") {
					debugger
					 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					 parent.layer.close(index); //再执行关闭
					 parent.$('#cusList').bootstrapTable('refresh');						
					 parent.layer.msg("修改成功");
					 top.workcount();
				}
			})
		}





 $(document).ready(function () {
            var wid = helper.request.queryString("wid");
            LAISHENG($("#source"));//省
            
           
            CHOUDQ($("#customer_comaddress"));//大区
		    QDLX($("#customer_taxPointDes"));//渠道类型
		    XYDJ($("#customer_invoiceRange"));//信用等级
		    JYPP($("#customer_Qualification"));//经营品牌
		    CAPI($("#customer_FinancialManager"));//产品  
			YFPP($("#customer_feeEx"));//用肥品牌
        });
function YFPP(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag", {
		        "token": $.cookie("token"),
		        flag:"YFPP",
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_Name + "'>" + n.F_Name + "</option>").appendTo(obj);
		            })
		        }
		    })
		}
    // 省/市/区/县	
     function LAISHENG(obj) {
        		obj.empty();
        		obj.append('<option selected="selected" value="">请选择</option>');
        		$.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
        			"token": $.cookie("token"),
        			F_Layer: 1,
        		}, function (data) {
        			if (data.state.toLowerCase() == "success") {
        				var content = data.data;                 
        				$(content).each(function (i, n) {
        					$("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo(obj);                       
        				})
        			}
        		})
        	}
        //市
        function LAISHI(obj) {
        	obj.empty();
        	obj.append('<option selected="selected" value="">请选择</option>');
        	$.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
        		"token": $.cookie("token"),
        		F_Layer: 2,
        		F_RegionName:$('#source').val()
        	}, function (data) {
        		if (data.state.toLowerCase() == "success") {
        			var content = data.data;
        			$(content).each(function (i, n) {
        				$("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo(obj);					
        			})
        		}
        		
        	})
        }
        //		区/县
        function LAIQU(obj) {
        	obj.empty();
        	obj.append('<option selected="selected" value="">请选择</option>');
        	$.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
        		"token": $.cookie("token"),
        		F_Layer: 3,
        		F_RegionName:$('#type').val()
        	}, function (data) {
        		if (data.state.toLowerCase() == "success") {
        			var content = data.data;
        			$(content).each(function (i, n) {
        				$("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo(obj);
        			})
        		}
        	})
        }
        		//  乡
        function LAIXIANG(obj) {
        	obj.empty();
        	obj.append('<option selected="selected" value="">请选择</option>');
        	$.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
        		"token": $.cookie("token"),
        		F_Layer: 4,
        		F_RegionName:$('#keyid').val()
        	}, function (data) {
        		if (data.state.toLowerCase() == "success") {
        			var content = data.data;
        			$(content).each(function (i, n) {
        				$("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo(obj);
        			})
        		}
        	})
        }
        
        $('#source').on('change',function(){
        	 $.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
        	    "token": $.cookie("token"),
        	    F_Layer: 2,
        		F_RegionName:$('#source').val(),
        		
        	}, function (data) {
        	    if (data.state.toLowerCase() == "success") {
        	        var content = data.data;
        	        $(content).each(function (i, n) {
        	            $("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo("#type");
        	        })
        	      LAISHI($("#type"));
        	    }
        	})
        });
        $('#type').on('change',function(){
               	 $.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
               	    "token": $.cookie("token"),
               	    F_Layer: 3,
               		F_RegionName:$('#type').val()			
               	}, function (data) {
               	    if (data.state.toLowerCase() == "success") {
               	        var content = data.data;
               	        $(content).each(function (i, n) {
               	            $("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo("#keyid");			
               	        })	
        				LAIQU($("#keyid"));
               	    }
               	})
        }); 
        $('#keyid').on('change',function(){
               $.getJSON(huayi.config.callcenter_url + "RegionCategory/GetAllList", {
                     	    "token": $.cookie("token"),
                     	    F_Layer: 4,
                     		F_RegionName:$('#keyid').val()
                     	}, function (data) {
                     	    if (data.state.toLowerCase() == "success") {
                     	        var content = data.data;
                     	        $(content).each(function (i, n) {
                     	            $("<option value='" + n.F_RegionName + "'>" + n.F_RegionName + "</option>").appendTo("#input_area");
                     	        })
              				LAIXIANG($('#input_area'));
                     	    }
                     	})
              }); 
			  
			  
			  
function CHOUDQ(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Department/GetList", {
		        "token": $.cookie("token"),
		        F_Layer: 1,
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_DeptId + "'>" + n.F_DeptName + "</option>").appendTo(obj);
		            })
		        }
		    })
		}
	function CHOUFEN(obj) {
				obj.empty();
				obj.append('<option selected="selected" value="">请选择</option>');
				$.getJSON(huayi.config.callcenter_url + "Department/GetList", {
					"token": $.cookie("token"),
					F_Layer:2,
					F_DeptName:$("#customer_comaddress").find("option:selected").text(),
				}, function (data) {
					if (data.state.toLowerCase() == "success") {
						var content = data.data;
						$(content).each(function (i, n) {
							$("<option value='" + n.F_DeptId + "'>" + n.F_DeptName + "</option>").appendTo(obj);
						})
					}
				})
			}

$('#customer_comaddress').on('change',function(){
 	 $.getJSON(huayi.config.callcenter_url + "Department/GetList", {
 	    "token": $.cookie("token"),
 	    F_Layer: 2,
 		F_DeptName:$("#customer_comaddress").find("option:selected").text(),
 		
 	}, function (data) {
 	    if (data.state.toLowerCase() == "success") {
 	        var content = data.data;
 
 	        $(content).each(function (i, n) {
 	            $("<option value='" + n.F_DeptId + "'>" + n.F_DeptName + "</option>").appendTo("#customer_legalName");
 	        })
 	   CHOUFEN($('#customer_legalName'));
 	    }
 	})
 });

//渠道类型
function QDLX(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag", {
		        "token": $.cookie("token"),
		        flag:"QDLX",
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_Name + "'>" + n.F_Name + "</option>").appendTo(obj);
		            })
		        }
		    })
		}
//信用等级   
function XYDJ(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag", {
		        "token": $.cookie("token"),
		        flag:"XYDJ",
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_Name + "'>" + n.F_Name + "</option>").appendTo(obj);
		            })
		        }
		    })
		}
//经营品牌 
function JYPP(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag", {
		        "token": $.cookie("token"),
		        flag:"JYPP",
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_Name + "'>" + n.F_Name + "</option>").appendTo(obj);
		            })
		        }
		    })
		}
//产品
function CAPI(obj) {
		    obj.empty();
		    obj.append('<option selected="selected" value="">请选择</option>');
		    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag", {
		        "token": $.cookie("token"),
		        flag:"CAPI",
		    }, function (data) {
		        if (data.state.toLowerCase() == "success") {
		            var content = data.data;
		            $(content).each(function (i, n) {
		                $("<option value='" + n.F_Name + "'>" + n.F_Name + "</option>").appendTo(obj);
		            })
		        }
		    })
		}

 
 
