export default {
  execute: function (bill, param) {
    if (param) {
      let tableName = param.param.tableName
      let obj = this.getCurrentRowData(bill,tableName)
      bill.addSubData(tableName)
      let len = this.getSubTableLength(bill,tableName) ? this.getSubTableLength(bill,tableName) - 1 : 0
      this.refreshSubDataByIndex(bill, param, tableName, obj, len)
    } else {
      GAMS.Common.messagePrompt('缺少参数');
    }
  },
  getCurrentRowData(bill,tableName) {
    let index = bill.getSubFocus()
    let currentRowData = {}
    if ( index > -1) {
      let rowDataObject = bill.getSubDataRow(tableName, index);
      currentRowData = rowDataObject.getValue("currentRowData");
      if(!this.currentRowData){
         currentRowData = rowDataObject.getRawDataCopy();          
      }
      return currentRowData;
    } else {
      return currentRowData;
    }
  },
  refreshSubDataByIndex(bill, param, tableName, obj, len) {
    // 避免复制其他的id，造成重复
    obj.id = null
    let subObj = bill.getSubDataRow(tableName, len)
    let _obj = {}
    let actionParam = param
    let masterSubRelation =
    actionParam.param && actionParam.param.masterSubRelation
      ? actionParam.param.masterSubRelation[subName]
      : undefined;
    for (const key in obj) {
      this.setTableValue(subObj, masterSubRelation + "." + key, obj[key])
      _obj[key] = {
        value: obj[key],
        readOnly: true
      }
    }
    subObj.setValue("currentRowData", obj)
    // bill.refreshSubDataRow(tableName, "", _obj, len)
    // 复制增行后不跳转到第一页，即在当前页
    bill.refreshSubDataRow(tableName, "", _obj)
  },
  getSubTableLength(bill,tableName) {
    return bill.getSubData(tableName) ? bill.getSubData(tableName).length : 0;
  },
  setTableValue(obj, key, val) {
    obj.setValue(key, val)
  }
}