/**
 * 其他合同签订_批量拓展js
 * coyle
 * 202410
 */

var partyName = "";
var selectPerson = "";
shr.defineClass("shr.empContract.BatchOtherContractAddEditEx", shr.empContract.OtherContractAddEdit,
  {
    initalizeDOM: function () {
      shr.empContract.BatchOtherContractAddEditEx.superClass.initalizeDOM.call( this );
      var _self = this;
    },



    initalizeDefaultData:function(){
      var self = this ;
      //初始化连续签订、连续签订次数
      this.initContinuousSigned();
      if (self.getOperateState()== 'VIEW') {
        return;
      }
      var selectObj =  self.getField("dataRangeSelected"),
      batchParma = empBatchParamUtils.getCacheData(),
      selectIds = batchParma.selectedIds,
      currentPageIds = batchParma.currentPageAllIds;
      if(selectIds){
        selectObj.shrSelect('setValue', 'selectedValue');
        if(self.operateState == 'ADDNEW'){
            self.setPartyData(selectIds); 
        }
      }else {//弃用currentPageValue
        selectObj.shrSelect('setValue', 'allValue');
        selectObj.shrSelect('disable');
      }
    },


    
        setPartyData: function (selectIds) {
          var self = this;
          selectPerson = selectIds;
          //查询第一个的劳动合同主体
          //对selectIds使用","分割,去第一个元素
          var firstId = self.getFirstElement(selectIds);
          //调用
          self.remoteCall({
            type: "post",
            async: false,
            method: "queryContractSub",
            param: {
              personId: firstId,
            },
            success: function (res) {
              //赋值到控件
              if (res) {
                $("#" + self.getRealId("labContractFirstParty")).shrPromptBox(
                  "setValue",
                  { id: res["id"], name: res["name"], number: res["number"] }
                );
              }
            },
            error: function (res) {
              console.log(res);
            },
          });
        },
    
    getFirstElement: function (selectIds) {
          // 检查 selectIds 是否为 null 或空字符串
          if (selectIds == null || selectIds == undefined) {
            return null;
          }
          // 使用 "," 分割字符串
          var elements = selectIds.split(",");
          // 检查分割后的数组是否为空
          if (elements.length == 0) {
            return null;
          }
          // 返回第一个元素
          return elements[0];
        },
      
        //如果labContractFirstParty的值不同,则提示:
        valiPartyAndSave: function(event) {
          var self = this;
          var partyId = $("#" + self.getRealId("labContractFirstParty")).shrPromptBox('getValue').id;
          if ("" == selectPerson || null == partyId) {
            shr.showError({message : "选取的员工或者劳动合同主体为空，请检查"});
            return false;
          }
          self.remoteCall({
              type: "post",
              async: true,
              method: "checkContractSub",
              param: {
                personIds: selectPerson,
                partyId: partyId
              },
              success: function(resList) {
                if (resList && Array.isArray(resList) && resList.length > 0) {
                  const personName = resList.map(personData => personData["personName"]).join(",");
                  const message = '员工:'+personName+',选择劳动合同主体与入职流程约定不一致，是否继续提交?';
                  // 如果确认，返回true，否则返回false
                  shr.showConfirm(message, function(){
                    self.doSave(event, 'save');
                  });
                } else {
                  self.doSave(event, 'save');
                }
              },
              error: function(res) {
                shr.showError({message :"远程调用失败:", res});
              },
            });
          
        },
    
    
      /**
         * 保存
         */
      saveHandler:function(event){
        var _self = this;
        if (_self.validate() && _self.verify()) {
          //批量不再支持插入，转为后台校验
          // _self.doSave(event, 'save');
          _self.valiPartyAndSave(event);
        }
        else{
          if(_self != top){shr.setIframeHeight(window.name);} 
        }
      },
      
      



});




