/* java端通用方法 */
var ajaxurl = "http://ichicaketes.com:8088";
/* 线下 */
//ajaxurl='http://192.168.0.188:8088';

/* function javaApiPost(data){
	var postStr = "{'requestParam':"+data+"}";
	var results1;
	$.ajaxSettings.async = false;
	$.post(ajaxurl+'/commodityType/inventoryCategoryEnquiry/',{MESSAGE:postStr},function(result){
		results1=result;
	},'json');
	$.ajaxSettings.async = true;   
	return results1;
}
function javaApiGet(){
	var results1;
	//$.ajaxSettings.async = false;
	$.post(ajaxurl+'/commodityType/inventoryCategoryEnquiry/',function(result){
		//results1=result;
		return result;
	},'json');
	//$.ajaxSettings.async = true;   
	return results1;
} */

/* 图片加载 */
function jizai(id) {
  $(id).html('<img src="/Public/img/jiazai.gif" class="jiazai" />');
}
/* 文字加载 */
function jizaiWz(id) {
  $(id).html('<span class="jiazai2">' + ichilang.JSLANG22 + "</span>");
}
/* 首页分类查询 渲染 */
function api1() {
  /* 首页独有 */
  /* jizai('#api1');
	//return false;	
	var url='/wGoodsClass/selectClass';
	var data=null;
	var html='';
	$.post('/Public/js_api',{url:url,data:data,name:'api1_1'},function(result){
		if(result==null){
			return false;
		}
		api1_html(result);
	},'json'); */
}
//判断二维数组中是否包含该值
function deel_in_arraSy(array, key, value) {
  var type = false;
  $(array).each(function (k, v) {
    if (v[key] == value) {
      type = true;
    }
  });
  return type;
}
function api1_html(result, msg) {
  var html = "";
  $(result).each(function (k, v) {
    if (v.pcode == 0 || v.pcode == "0") {
      html += '<li><a href="/list.html?sid=' + v["code"] + '">';
      html +=
        '<span><img src="' +
        http +
        v["iconUrl"] +
        '" /></span><div>' +
        v["name"] +
        "</div>";
      if (deel_in_arraSy(result, "pcode", v["code"])) {
        html += "<i></i>";
      }
      html += "</a><dl>";
      $(result).each(function (k1, v1) {
        if (v["code"] == v1["pcode"]) {
          html +=
            '<dd><a href="/list.html?sid=' +
            v1["code"] +
            '">' +
            v1["name"] +
            "</a></dd>";
        }
      });
      html += "</dl></li>";
    }
  });
  $("#api1").html(html);
}

/* 所有商品分类查询 渲染 */
function api2() {
  jizai("#api2");
  //return false;
  var url = "/wGoodsClass/selectClass";
  var data = null;
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data, name: "api1_1" },
    function (result) {
      //console.log(result);
      if (result == null) {
        return false;
      }
      var url = "/commodityType/inventoryCategory2Enquiry";
      var data = { CatCode: "*" };
      //$.post('/Public/js_api',{url:url,data:data,name:'api1_2'},function(msg){
      $(result).each(function (k, v) {
        if (v.pcode == 0 || v.pcode == "0") {
          html += '<dd><a href="/list.html?sid=' + v["id"] + '">' + v["name"];
          html += "</a><ul>";
          $(result).each(function (k1, v1) {
            if (v["code"] == v1["pcode"]) {
              html +=
                '<li><a href="/list.html?sid=' +
                v1["id"] +
                '">' +
                v1["name"] +
                "</a></li>";
            }
          });
          html += "</ul></dd>";
        }
      });
      $("#api2").html(html);
      //},'json');
    },
    "json"
  );
}

/* 右侧商品查询 渲染 */
function api3() {
  jizai("#api3");
  //return false;
  var url = "/commodity/itemSearchEnquiry";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 6, DESCRIPT: "" };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data, name: "api3" },
    function (result) {
      if (result == null) {
        return false;
      }
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);
        if (v["path1"] == "") {
          v["path1"] = "/Public/img/shangpin.jpg";
        } else {
          v["path1"] = http + v["path1"];
        }
        html +=
          '<li><a href="/list/details/id/' +
          v["scode"] +
          '"><img src="' +
          v["path1"] +
          '" /><p>$' +
          v[PriceField] +
          "</p></a></li>";
      });
      $("#api3").html(html);
    },
    "json"
  );
}

/* 首页新闻查询 渲染 */
function api4() {
  jizai("#api4");
  var url = "/wNews/getNewsListPage";
  var data = { type: 1, pageSize: 5, pageNo: 1 };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data },
    function (result) {
      if (result == null) {
        return false;
      }
      $(result["records"]).each(function (k, v) {
        html +=
          '<dd title="' + v["newsTitle"] + '">' + v["newsTitle"] + "</dd>";
      });
      $("#api4").html(html);
    },
    "json"
  );
}

/* 首页最新 */
function api5() {
  jizai("#api5");
  //return false;
  var url = "/commodity/itemSearchEnquiry";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 6, DESCRIPT: "" };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data },
    function (result) {
      if (result.success == false) {
        jizaiWz("#api5");
        return false;
      }
      //console.log(result);
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);

        html += '<a href="/list/details/id/' + v["scode"] + '">';
        html += '<img src="' + http + v["path1"] + '" />';
        html += "<p>" + v["cdescript"] + "</p>";
        html +=
          "<p><span>" + v["f18"] + "</span><em>$" + v[PriceField] + "</em></p>";
        html += "</a>";
      });
      $("#api5").html(html);
    },
    "json"
  );
}
/* 首页特价 */
function api6() {
  jizai("#api6");
  //return false;
  var url = "/commodity/ItemSearch";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 6, KEY: "F21", VALUE: 1 };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data },
    function (result) {
      if (result.success == true) {
        jizaiWz("#api6");
        return false;
      }
      //console.log(result);
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);
        html += '<a href="/list/details/id/' + v["scode"] + '">';
        html += '<img src="' + http + v["path1"] + '" />';
        html += "<p>" + v["cdescript"] + "</p>";
        html +=
          "<p><span>" + v["f18"] + "</span><em>$" + v[PriceField] + "</em></p>";
        html += "</a>";
      });
      $("#api6").html(html);
    },
    "json"
  );
}
/* 首页热卖 */
function api7() {
  jizai("#api7");
  //return false;
  var url = "/commodity/ItemSearch";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 6, KEY: "F20", VALUE: 1 };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data },
    function (result) {
      if (result.success == true) {
        jizaiWz("#api7");
        return false;
      }
      //console.log(result);
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);
        html += '<a href="/list/details/id/' + v["scode"] + '">';
        html += '<img src="' + http + v["path1"] + '" />';
        html += "<p>" + v["cdescript"] + "</p>";
        html +=
          "<p><span>" + v["f18"] + "</span><em>$" + v[PriceField] + "</em></p>";
        html += "</a>";
      });
      $("#api7").html(html);
    },
    "json"
  );
}

/* 右侧购物车 渲染 */
function api8() {
  jizai("#api8");
  //return false;
  var url = "/shoppingCart/getShoppingCartList";
  var data = {};
  var html = "";
  $.post(
    "/Public/js_api_login",
    { url: url, data: data },
    function (result) {
      console.log(result);
      if (result.success == false) {
        jizaiWz("#api8");
        return false;
      }
      $(result).each(function (k, v) {
        var money = userPriceMoney(v["goods"][userPrice["PriceField"]]);
        money = parseFloat(money);
        money = money.toFixed(2);
        if (v["flag"] == 1 || v["flag"] == 2) {
          html += '<dd style="background: #e1e1e1;">';
        } else {
          html += "<dd>";
        }

        html += '	<div class="d1">';
        html += "		<label>";
        if (v["flag"] == 1 || v["flag"] == 2) {
          html +=
            '<input onchange="right_jisuan();" class="sid" flag="' +
            v["flag"] +
            '" name="sid[]" disabled="disabled" type="checkbox" value="' +
            v["id"] +
            '" />' +
            v["goods"]["cdescript"] +
            "";
        } else {
          html +=
            '<input onchange="right_jisuan();" class="sid" name="sid[]" type="checkbox" value="' +
            v["id"] +
            '" />' +
            v["goods"]["cdescript"] +
            "";
        }

        html += "		</label>";
        html += "		<span>$" + money * v["count"] + "</span>";
        html += "	</div>";
        html += '	<div class="d2">';
        if (v["goods"]["path1"] == "") {
          v["goods"]["path1"] = "/Public/img/shangpin.jpg";
        }
        html += '		<img src="' + http + v["goods"]["path1"] + '" />';
        html += '		<div class="right">';
        if (v["flag"] == 1) {
          //1：缺货 2：预购 3：批发
          html += '<div class="guige"><i class="quehuo"></i></div>';
        } else if (v["flag"] == 2) {
          html += '<div class="guige"><i class="quehuo2"></i></div>';
        } else if (v["flag"] == 3) {
          html += '<div class="guige"><i class="quehuo1"></i></div>';
        }

        html +=
          '			<div class="shuliang">' +
          v["count"] +
          '</div><input class="num" name="num[]" type="hidden" value="' +
          v["count"] +
          '" />';
        html += '			<div class="jiaqian">$' + money + "</div>";
        html += "		</div>";
        html += "	</div>";
        html += "</dd>";
      });
      $("#api8").html(html);
    },
    "json"
  );
}

/* 计算价格 */
function userPriceMoney(money = false) {
  var mbType = userInfo ? userInfo["mbType"] : false;
  if (mbType && USERMember[mbType]["Discount"] != "0") {
    /* 登录状态 */
    return money * USERMember[mbType]["Discount"];
  } else {
    return money;
  }
}
/* 首页人气精选 */
function api9() {
  jizai("#api9");
  //return false;
  var url = "/commodity/ItemSearch";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 5, KEY: "F19", VALUE: 1 };
  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data },
    function (result) {
      if (result.success == true) {
        jizaiWz("#api9");
        return false;
      }
      //console.log(result);
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);
        html += '<li class="on">';
        html += '<a href="' + v["scode"] + '">';
        html += '<div class="t">';
        html += v["cdescript"];
        html += "</div>";
        html += '<div class="b">';
        html += '<div class="l">';
        html += '<img src="' + http + v["path1"] + '" />';
        html += "</div>";
        html += '<div class="r">';
        html += "<h3>" + v["cdescript"] + "</h3>";
        html += "<p>$" + v[PriceField] + "</p>";
        html += "</div>";
        html += "</div>";
        html += "<span></span>";
        html += "</a>";
        html += "</li>";
      });
      $("#api9").html(html);
    },
    "json"
  );
}

function api10(id, VALUE) {
  jizai(id);
  //return false;
  var url = "/commodity/ItemSearch";
  var data = { PAGECOUNT: 1, ITEMCOUNT: 10, KEY: "F22", VALUE: VALUE };

  var html = "";
  $.post(
    "/Public/js_api",
    { url: url, data: data, name: VALUE },
    function (result) {
      if (result.success == true) {
        jizaiWz(id);
        return false;
      }
      //console.log(result);
      $(result).each(function (k, v) {
        var aprice = parseFloat(v[PriceField]);
        v[PriceField] = aprice.toFixed(2);
        html += "<li>";
        html += '<a href="">';
        html += '<div class="img">';
        //html+='<img class="img_i" src="/public/img/index/Bosch_logo.png" />';
        html += '<img src="' + http + v["path1"] + '" />';
        html += "</div>";
        html += '<div class="title">' + v["cdescript"] + "</div>";
        html += '<div class="money">';
        html += "<span><i>$</i>" + v[PriceField] + "</span>";
        //html+='<em>$'+v[PriceField]+'</em>';
        //html+='<i>收藏416</i>';
        html += "</div>";
        html += "</a>";
        html += "</li>";
      });
      $(id).html(html);
    },
    "json"
  );
}
function api11() {
  //window.onload = function(){
  $("#api11 li").each(function (k, v) {
    var id = $("#api10 ul").eq(k).attr("id");
    var val = $(v).html();
    //console.log(id);
    api10("#" + id, val);
  });
  //}
}

function buildHtml(file) {
  setTimeout(function () {
    $.post("/Index/buildHtmls", { buildHtml: file }, function (result) {});
  }, 1000 * 10);
}

function clickPinpai(that) {
  var pinpai = $(that).closest(".r").siblings(".on").attr("kd");
  location.href = getComputedUrl("/list.html?key=F22&descript=" + pinpai);
}
