const GLJ_INIT = (() => {
  const params = location.href.split('/');
  const curUserID = params[params.length - 1];
  console.log('curUserID', curUserID);

  // 头部按钮相关
  function initHeaderTools(receiveList, shareList) {
    // 设置下拉项
    const selectHtml = `
    <select class="form-control  form-control-sm" id="glj-libs">
    </select>`;
    // 设置分享按钮
    const shareHtml = `
    <span id="share-tip" class="ml-2" data-toggle="tooltip" data-placement="bottom" data-original-title="">
        <a id="init-share" href="javascript:;" class="btn btn-xs btn-primary ${isReadOnly ? 'disabled' : ''}"><i class="fa fa-share-alt"></i> 分享</a>
    </span>`;
    const html = `
        <div style="display: flex; align-items: center">
            ${selectHtml}
            ${shareHtml}
        </div>
    `
    $('#fullpath').html(html);
    initShareTip(shareList);
    $('[data-toggle="tooltip"]').tooltip();
    initLibOptions(receiveList);
    // 分享给
    SHARE_TO.handleEventListener();
    $('#init-share').click(() => {
      SHARE_TO.initModal(SHARE_TO.Mode.GLJ_LIB);
      $('#share-tip').tooltip('hide');
    });

    // 库下拉
    function initShareTip(shareList) {
      if (shareList.length) {
        const owners = shareList.map(owner => owner.real_name);
        const tips = `已分享给 ${owners.join(' ')}`;
        $('#share-tip').attr('data-original-title', tips);
      }
    }

    // 库下拉
    function initLibOptions(receiveList) {
      const libList = [
        `<option value="${userID}">我的补充人材机库</option>`
      ];
      const options = receiveList.map(owner => `<option value=${owner._id} ${curUserID === owner._id ? 'selected' : ''}>${owner.real_name}的补充人材机库</option>`);
      libList.push(...options);
      $('#glj-libs').html(libList.join(''))
    }

    $('#glj-libs').change(function () {
      const userID = $(this).val();
      window.location.href = `/complementaryGlj/${userID}`;
    });
  }

  $(document).ready(() => {
    $.bootstrapLoading.start();
    const projection = {
      _id: 0,
      ID: 1,
      code: 1,
      unit: 1,
      name: 1,
      priceProperty: 1,
      basePrice: 1
    };
    CommonAjax.post('/complementartGlj/api/initData', { projection, userID: curUserID }, res => {
        if (res.error) {
            alert('数据初始化失败，请重试。');
            setTimeout(() => {
                window.location.href = '/'
            }, 1200);
        } else {
          const { receiveLibs, shareLibs, gljItems, mixTree, distTypeData } = res;
          initHeaderTools(receiveLibs, shareLibs);
          pageOprObj.stdGljLibId = stdGljLibId;
          repositoryGljObj.currentRepositoryId = stdGljLibId;
          pageOprObj.userId = userId;
          pageOprObj.compilationId = compilationId;
          //生成人材机表格列头
          repositoryGljObj.setting.header = repositoryGljObj.initHeaders(priceProperties);
          //生成列映射
          sheetCommonObj.initColMapping(repositoryGljObj, repositoryGljObj.setting.header);
          //生成单价列下标数组
          repositoryGljObj.initPriceCols(priceProperties, repositoryGljObj.colMapping);
          repositoryGljObj.buildSheet($("#GLJListSheet")[0]);
          gljComponentOprObj.buildSheet($('#gljComponentSheet')[0]);
          componentOprObj.buildSheet($("#componentSheet")[0]);
          // distTypeTree
          repositoryGljObj.distTypeTree = repositoryGljObj.getComboData(distTypeData);
          // tree
          gljClassTreeObj.treeData = mixTree;
          if (mixTree.comple && mixTree.comple.length > 0) {
            gljClassTreeObj.gljCurTypeId = mixTree.comple[0].ID;
          }
          gljClassTreeObj.buildSheet();
          gljClassTreeObj.initTree(mixTree.comple);
          gljClassTreeObj.cache = gljClassTreeObj.tree.items;
          repositoryGljObj.updateParentNodeIds(gljClassTreeObj.cache, repositoryGljObj);
          gljClassTreeObj.initController(gljClassTreeObj.tree, gljClassTreeObj.sheet, gljClassTreeObj.setting.sheet);
          gljClassTreeObj.controller.showTreeData();
          gljClassTreeObj.sheet.setFormatter(-1, 0, '@');
          // gljItems
          repositoryGljObj.stdGljList = gljItems.stdGljs;
          // 兼容多单价情况
          for (const sGlj of repositoryGljObj.stdGljList) {
              if (sGlj.priceProperty && typeof sGlj.priceProperty.price1 !== 'undefined') {
                  sGlj.basePrice = sGlj.priceProperty.price1;
              }
          }
          repositoryGljObj.sortGlj(repositoryGljObj.stdGljList);
          repositoryGljObj.setProp('isStd', true, repositoryGljObj.stdGljList);
          repositoryGljObj.complementaryGljList = gljItems.complementaryGljs;
          repositoryGljObj.sortGlj(repositoryGljObj.complementaryGljList);
          gljClassTreeObj.initSelection(gljClassTreeObj.tree.selected);
        }
        $.bootstrapLoading.end();
    });
  });

})();