var logBaseUrl = 'https://report.zhangxinhulian.com';
// 页面加载的时候调用
import $ from 'jquery';
export function reportEvent(action, value, nodeId, ext) {
  let cid = getCookie('cid') || ''
  if(cid == '') {
    cid = genCid()
    setCookie('cid', cid, 30*24, "")
  }

  var clickajaxData = {
    url: window.location.href,
    action: action,
    value: value,
    nodeId: nodeId,
    ext: ext,
    refer: document.referrer,
    user_agent: navigator.userAgent,
    platform: navigator.platform,
    avail_height: window.screen.availHeight,
    avail_width: window.screen.availWidth,
    height: window.screen.height,
    width: window.screen.width,
    color_depth: window.screen.colorDepth,
    pixel_depth: window.screen.pixelDepth
  };
  $.ajax({
    type: "POST",
    url: logBaseUrl + "/logCollection/log/v1/userLogCollection?module=web&cid=" + cid,
    async: true,
    xhrFields: {
      withCredentials: true
    },
    crossDomain: true,
    dataType: "json",
    contentType: "application/json;charset=utf-8",
    data: JSON.stringify(clickajaxData),
    success: function(datas) {
    }
  });
}

function bin2hex(s) {
  var i,
    l,
    o = "",
    n;

  s += "";

  for (i = 0, l = s.length; i < l; i++) {
    n = s.charCodeAt(i).toString(16);
    o += n.length < 2 ? "0" + n : n;
  }

  return o;
}

function genCid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0,
      v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

function getCookie(name) {
  var arr,
    reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

  if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
  else return null;
}

function setCookie(name, value, hours, path) {
  name = escape(name);
  value = escape(value);
  let expires = new Date();
  expires.setTime(expires.getTime() + hours * 3600000);
  path = path == "" ? "" : ";path=" + path;
  let _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();
  document.cookie = name + "=" + value + _expires + path;
}
