import {
  Modal,
  message
} from 'antd'
import {addContentPath} from './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);
}

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

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

//培训记录--详细信息
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.html#/main/hrm/resource${window.WeaHrm4Formal?'4Formal':''}/HrmResourceBase${window.WeaHrm4Formal?'4Formal':''}/${id}`);
  window.open(`/spa/hrm/index_mobx.html#/main/hrm/card/cardInfo/${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 showAuthorizationRefs = (groupID, coulddetach = 0, id) => {
  const group = groupID == null ? '' : `&groupID=${groupID}`;
  openFullWindowForXtable(`/hrm/HrmDialogTab.jsp?_fromURL=systemRightGroup&method=showAuth&detachable=1${group}&coulddetach=${coulddetach}&id=${id}`)
}

function openProcSetting(id) {
  window.open(`/spa/workflow/static4engine/engine.html#/main/workflowengine/path/pathSet/pathDetail/baseSet/baseInfo?workflowId=${id}&showLeftTree=false`);
}

const bindTokenKey = (id) => {
  // const hrmId = id || '';
  // const url = "/login/bindTokenKey.jsp?requestFrom=system&userid=" + hrmId;
  // var redirectUrl = url;
  // var width = 800;
  // var height = 600;
  // 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 title = "";
  let url = "/spa/hrm/index_mobx.html#/main/hrm/BindTokenKey?requestFrom=system&userid="+id;
  const dialog = ecCom.WeaTools.createDialog({ title, url:addContentPath(url), style: { width: 600, height: 365 }, zIndex: 9999 });
  dialog.show();
}

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

export {
  sendEmessage,
  sendMail,
  sendSmsMessage,
  addCoWork,
  doAddWorkPlanByHrm,
  jsHrmResourceSystemView,
  openFullWindowForXtable,
  jsRequestView,
  docSearch,
  doProjcSearch,
  openCRM,
  cptSearch,
  openHrmResourceView,
  openFnaBudgetResourceDetail,
  openFnaExpenseResourceDetail,
  jsTrainRecordShowDetail,
  jsHrmTrainApply,
  jsRewardsRecord2Detial,
  getUserName,
  chkMail,
  jumpToHrmCard,
  onNewRequest,
  showAuthorizationRefs,
  openProcSetting,
  bindTokenKey,
}