window.ACC_CARD_TYPE = [{ k: 'T4', v: 'T4' }, { k: 'A100', v: 'A100' }, { k: 'V100', v: 'V100' }, { k: 'ASCEND910', v: 'Ascend 910' }, { k: 'MLU270', v: 'MLU270' }, { k: 'RTX3080', v: 'RTX3080' }, { k: 'ENFLAME-T20', v: 'ENFLAME-T20' }];

window.getListValueWithKey = (list, key, k = 'k', v = 'v', defaultV = '') => {
  for (let i = 0, iLen = list.length; i < iLen; i++) {
    const listI = list[i];
    if (listI[k] === key) return listI[v];
  }
  return defaultV || key;
};

window.renderSpecStr = (spec, showPoint, langObj) => {
  if (!spec) return '';
  var ngpu = `${spec.ComputeResource}: ${spec.AccCardsNum + '*' + getListValueWithKey(ACC_CARD_TYPE, spec.AccCardType)}`;
  var gpuMemStr = spec.GPUMemGiB != 0 ? `${langObj.gpu_memory}: ${spec.GPUMemGiB}GB, ` : '';
  var sharedMemStr = spec.ShareMemGiB != 0 ? `, ${langObj.shared_memory}: ${spec.ShareMemGiB}GB` : '';
  var pointStr = showPoint ? `, ${spec.UnitPrice == 0 ? langObj.free : spec.UnitPrice + langObj.point_hr}` : '';
  var specStr = `${ngpu}, CPU: ${spec.CpuCores}, ${gpuMemStr}${langObj.memory}: ${spec.MemGiB}GB${sharedMemStr}${pointStr}`;
  return specStr;
};

window.renderSpecsSelect = (specsSel, data, showPoint, langObj) => {
  specsSel.empty();
  data = data || [];
  showPoint = specsSel.attr('blance') ? true : false;
  var oValue = specsSel.attr('ovalue');
  for (var i = 0, iLen = data.length; i < iLen; i++) {
    var spec = data[i];
    var specStr = window.renderSpecStr(spec, showPoint, langObj);
    specsSel.append(`<option name="spec_id" value="${spec.ID}" queueCode="${spec.QueueCode}" unitprice="${spec.UnitPrice}">${specStr}</option>`);
  }
  oValue && specsSel.val(oValue);
  if (showPoint) {
    specsSel.on('change', function (e) {
      var cloudbrain_resource_spec_blance_tip_el = $('.cloudbrain_resource_spec_blance_tip');
      var blance = $(this).attr('blance');
      var unitPrice = $(this).find('option:selected').attr('unitprice');
      if (!blance || !unitPrice) return;
      if (unitPrice == 0) {
        cloudbrain_resource_spec_blance_tip_el.find('.can-use-time').parent().hide();
      } else {
        var canUseTime = Number(blance) / Number(unitPrice);
        cloudbrain_resource_spec_blance_tip_el.find('.can-use-time').text(canUseTime.toFixed(2)).parent().show();
      }
    }).trigger('change');
  }
}
