import {
  Modal,
  message
} from 'antd'
import {addContentPath} from '../util/index.js'
const confirm = Modal.confirm;
//发消息、发起群聊
function sendEmessage(emessageId) {
  var isGroup = false;
  if (!!emessageId) {
    if (emessageId.indexOf(",") > -1) {
      isGroup = true;
    }
  }
  if (!emessageId) {
    message.warning(SystemEnv.getHtmlNoteName(4780, readCookie("languageidweaver")));
    return;
  }
  if (emessageId.match(/,$/)) {
    emessageId = emessageId.substring(0, emessageId.length - 1);
  }
  var openType = isGroup ? "2" : "0";
  if (isGroup) {
    confirm({
      content: SystemEnv.getHtmlNoteName(4781, readCookie("languageidweaver")),
      onOk() {
        sendMsgToPCorWeb(emessageId, openType, '', '');
      },
      onCancel() {},
    });
  } else {
    sendMsgToPCorWeb(emessageId, openType, '', '');
  }
}

//发送邮件
function sendMail(id) {
  openFullWindowForXtable("/email/new/MailInBox.jsp?opNewEmail=1&amp;isInternal=1&amp;internalto&id=" + id);
}

//发送短信
function sendSmsMessage(id) {
  openFullWindowForXtable("/sms/SmsMessageEdit.jsp?hrmid=" + id);
}

//新建协作
function addCoWork(id) {
  openFullWindowForXtable("/cowork/AddCoWork.jsp?hrmid=" + id);
}

//新建日程
function doAddWorkPlanByHrm(id) {
  openFullWindowForXtable("/workplan/data/WorkPlan.jsp?resourceid=" + id + "&add=1")
}

//系统信息
function jsHrmResourceSystemView(id) {
  //window.open(`/spa/hrm/index.html#/main/hrm/systeminfo${window.WeaHrm4Formal?'4Formal':''}/${id}`);
  window.open(`/spa/hrm/index_mobx.html#/main/hrm/systeminfo/${id}`);
}

//我的下属--流程
function jsRequestView(id) {
  openFullWindowForXtable("/workflow/request/RequestView.jsp?resourceid=" + id);
}

//我的下属--文档
function docSearch(id) {
  openFullWindowForXtable('/docs/search/DocSearchTemp.jsp?docstatus=6&hrmresid=' + id);
}

//我的下属--项目
function doProjcSearch(id) {
  openFullWindowForXtable("/proj/search/SearchOperation.jsp?member=" + id);
}

//我的下属--客户
function openCRM(id) {
  openFullWindowForXtable('/CRM/search/SearchOperation.jsp?destination=myAccount&resourceid=' + id);
}

//我的下属--资产
function cptSearch(id) {
  openFullWindowForXtable('/cpt/search/SearchOperation.jsp?resourceid=' + id + '&isdata=2');
}

//我的下属--下属
function openHrmResourceView(id) {
  //openFullWindowForXtable("/hrm/HrmTab.jsp?_fromURL=HrmResourceView&id="+id+"&srcid="+id);
  window.open(`/spa/hrm/index_mobx.html#/main/hrm/underling/${id}`);
}

//我的下属--预算
function openFnaBudgetResourceDetail(id) {
  openFullWindowForXtable("/fna/report/budget/FnaBudgetResourceDetail.jsp?resourceid=" + id);
}

//我的下属--收支
function openFnaExpenseResourceDetail(id) {
  openFullWindowForXtable("/fna/report/expense/FnaExpenseResourceDetail.jsp?resourceid=" + id);
}

function openTrainArrange(workflowid, trainplanid) {
  // openFullWindowForXtable(`/workflow/request/AddRequest.jsp?workflowid=${workflowid}&TrainPlanId=${trainplanid}`);
  openFullWindowForXtable(`/spa/workflow/index_form.jsp#/main/workflow/req?iscreate=1&beagenter=0&f_weaver_belongto_usertype=0&f_weaver_belongto_userid=&workflowid=${workflowid}&TrainPlanId=${trainplanid}`);
}

//培训记录--详细信息
function jsTrainRecordShowDetail(id) {
  let dialog = new window.top.Dialog();
  dialog.currentWindow = window;
  const url = "/hrm/HrmDialogTab.jsp?_fromURL=HrmResourceTrainRecordBasic&isdialog=1&id=" + id;
  dialog.Title = "培训记录详细信息";
  dialog.Width = 800;
  dialog.Height = 503;
  dialog.Drag = true;
  dialog.URL =  addContentPath(url) ;
  dialog.show();
}

//培训记录--培训安排--申请
function jsHrmTrainApply(trainplanid, applyworkflowid) {
  openFullWindowForXtable("/workflow/request/AddRequest.jsp?workflowid=" + applyworkflowid + "&TrainPlanId=" + trainplanid);
}

//考核奖惩--奖惩情况--详细信息
function jsRewardsRecord2Detial(id) {
  //openFullWindowForXtable("/hrm/HrmDialogTab.jsp?_fromURL=HrmAwardEdit&id="+id+"&cmd=showdetail");
  let dialog = new window.top.Dialog();
  dialog.currentWindow = window;
  dialog.Title = "奖惩信息";
  const url = "/hrm/HrmDialogTab.jsp?_fromURL=HrmAwardEdit&id=" + id + "&cmd=showdetail";
  dialog.Width = 800;
  dialog.Height = 500;
  dialog.Drag = true;
  dialog.URL = addContentPath(url) ;
  dialog.show();
}

function openFullWindowForXtable(url) {
  var redirectUrl = addContentPath(url);
  var width = screen.availWidth - 10;
  var height = screen.availHeight - 60;
  //if (height == 768 ) height -= 75 ;
  //if (height == 600 ) height -= 60 ;
  var szFeatures = "top=0,";
  szFeatures += "left=0,";
  szFeatures += "width=" + width + ",";
  szFeatures += "height=" + height + ",";
  szFeatures += "directories=no,";
  szFeatures += "status=yes,";
  szFeatures += "menubar=no,";
  szFeatures += "scrollbars=yes,";
  szFeatures += "resizable=yes"; //channelmode
  window.open(redirectUrl, "", szFeatures);
}

const openFullWindowHaveBar = (url) => {
  var redirectUrl = addContentPath(url);
  var width = screen.availWidth - 10;
  var height = screen.availHeight - 50;
  var szFeatures = "top=0,";
  szFeatures += "left=0,";
  szFeatures += "width=" + width + ",";
  szFeatures += "height=" + height + ",";
  szFeatures += "directories=no,";
  szFeatures += "status=yes,toolbar=no,location=no,";
  szFeatures += "menubar=no,";
  szFeatures += "scrollbars=yes,";
  szFeatures += "resizable=yes"; //channelmode
  window.open(redirectUrl, "", szFeatures);
}

function getUserName() {
  try {
    let vbsserial = "";
    let hCard = htactx.OpenDevice(1); //打开设备
    if (hCard == 0) {
      message.warning("请确认您已经正确地安装了驱动程序并插入了usb令牌");
      return vbsserial;
    }

    try {
      vbsserial = htactx.GetUserName(hCard); //获取用户名
      htactx.CloseDevice(hCard)
      return vbsserial;
    } catch (e) {
      message.warning("请确认您已经正确地安装了驱动程序并插入了usb令牌2");
      htactx.CloseDevice(hCard);
      return vbsserial;
    }
  } catch (e) {
    message.warning("请确认您已经正确地安装了驱动程序并插入了usb令牌");
    htactx.CloseDevice(hCard);
    return vbsserial;
  }
}

function chkMail(email) {
  if (email == '') {
    return true;
  }

  let pattern = /^(?:[a-z\d]+[_\-\+\.]?)*[a-z\d]+@(?:([a-z\d]+\-?)*[a-z\d]+\.)+([a-z]{2,})+$/i;
  let chkFlag = pattern.test(email);
  if (chkFlag) {
    return true;
  } else {
    message.warning("邮箱地址的格式不正确");
    return false;
  }
}

function jumpToHrmCard(id) {
  window.open(`/spa/hrm/index_mobx.html#/main/hrm/card/cardInfo/${id}`);
}

function jumpToHrmDept(id) {
  // window.open('/hrm/HrmTab.jsp?_fromURL=HrmDepartmentDsp&id=' + id);
  window.open(`/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=department&id=${id}`);
}

function jumpToHrmSubCompany(id) {
  // window.open('/hrm/HrmTab.jsp?_fromURL=HrmSubCompanyDsp&id=' + id);
  window.open(`/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=subcompany&id=${id}`);
}

function jumpToHrmPost(id) {
  // window.open('/hrm/HrmDialogTab.jsp?_fromURL=HrmJobTitlesEdit&id=' + id);
  window.open(`/spa/hrm/engine.html#/hrmengine/posts?id=${id}`);
}

function openBatchEdit(type){
  window.open(`/spa/hrm/engine.html#/hrmengine/batchMaintenance/edit/${type}`);
}

function jumpToHrmContract(id) {
  window.open('/hrm/contract/contract/HrmContractView.jsp?id=' + id);
}

//新建流程
function onNewRequest(params) {
  const wfid = params.wfid || '';
  const f_weaver_belongto_userid = params.f_weaver_belongto_userid || '';
  const beagenter = params.beagenter || '';
  const isagent = params.isagent || '';
  //计数
  jQuery.post('/workflow/request/AddWorkflowUseCount.jsp', {
    wfid: wfid
  });
  let paramstr = `workflowid=${wfid}&isagent=${isagent}&beagenter=${beagenter}&f_weaver_belongto_userid=${f_weaver_belongto_userid}&f_weaver_belongto_usertype=0`;
  openFullWindowHaveBar('/workflow/request/CreateRequestForward.jsp?' + paramstr);
}

const openFullWindowHaveBarForWFList = (url, requestid) => {
  try {
    document.getElementById("wflist_" + requestid + "span").innerHTML = "";
  } catch (e) {}
  const redirectUrl = addContentPath(url);
  const width = screen.availWidth - 10;
  const height = screen.availHeight - 50;
  let szFeatures = "top=0,left=0,";
  szFeatures += "width=" + width + ",";
  szFeatures += "height=" + height + ",";
  szFeatures += "directories=no,status=yes,toolbar=no,location=no,";
  szFeatures += "menubar=no,scrollbars=yes,resizable=yes";
  window.open(redirectUrl, "", szFeatures);
}

const viewDepartment = (departmentid) => {
  openFullWindowForXtable("/hrm/HrmTab.jsp?_fromURL=HrmDepartmentDsp&id=" + departmentid)
}

const viewSubCompany = (subcompanyid) => {
  openFullWindowForXtable("/hrm/HrmTab.jsp?_fromURL=HrmSubCompanyDsp&id=" + subcompanyid)
}

const IEVersion = () => {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
  var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  if (isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if (fIEVersion == 7) {
      return 7;
    } else if (fIEVersion == 8) {
      return 8;
    } else if (fIEVersion == 9) {
      return 9;
    } else if (fIEVersion == 10) {
      return 10;
    } else {
      return 6; //IE版本<=7
    }
  } else if (isEdge) {
    return 'edge'; //edge
  } else if (isIE11) {
    return 11; //IE11  
  } else {
    return -1; //不是ie浏览器
  }
}

const sectionToChinese = (section) => {
  var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
  var chnUnitChar = ["", "十", "百", "千"];

  var strIns = '',
    chnStr = '';
  var unitPos = 0;
  var zero = true;
  while (section > 0) {
    var v = section % 10;
    if (v === 0) {
      if (!zero) {
        zero = true;
        chnStr = chnNumChar[v] + chnStr;
      }
    } else {
      zero = false;
      strIns = chnNumChar[v];
      strIns += chnUnitChar[unitPos];
      chnStr = strIns + chnStr;
    }
    unitPos++;
    section = Math.floor(section / 10);
  }
  return chnStr;
}

window.viewDepartment = viewDepartment;
window.viewSubCompany = viewSubCompany;
window.openFullWindowHaveBarForWFList = openFullWindowHaveBarForWFList;
window.openFullWindowForXtable = openFullWindowForXtable;

export {
  sendEmessage,
  sendMail,
  sendSmsMessage,
  addCoWork,
  doAddWorkPlanByHrm,
  jsHrmResourceSystemView,
  openFullWindowForXtable,
  jsRequestView,
  docSearch,
  doProjcSearch,
  openCRM,
  cptSearch,
  openHrmResourceView,
  openFnaBudgetResourceDetail,
  openFnaExpenseResourceDetail,
  openTrainArrange,
  jsTrainRecordShowDetail,
  jsHrmTrainApply,
  jsRewardsRecord2Detial,
  getUserName,
  chkMail,
  jumpToHrmCard,
  jumpToHrmDept,
  jumpToHrmSubCompany,
  jumpToHrmPost,
  jumpToHrmContract,
  onNewRequest,
  IEVersion,
  sectionToChinese,
  openBatchEdit,
}