costEdit.vue 2.69 KB
<template>
  <BasicDrawer
    v-cloakv-bind="$attrs"
    @register="register"
    title="编辑"
    width="35%"
    showFooter
    @ok="handleSubmit"
    ref="formRef"
    okText="保存"
    :destroyOnClose="true"
    :isDetail="true"
    :showDetailBack="false"
    :mask="false"
    class="z-20"
  >
    <a-space direction="vertical" style="width: 100%">
      <a-input v-model:value="fixCost" addonBefore="固定成本  " />
      <a-input v-model:value="ratio" addonBefore="提成比例  " />
      <a-input v-model:value="spainRatio" addonBefore="西班牙提成比例  " />
      <!-- <a-input v-model:value="price" addonBefore="生产提成单价" /> -->
    </a-space>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
  import { ref } from 'vue';
  import { saveConfig } from '/@/api/sys/config';

  const emit = defineEmits(['success']);

  const [register, { closeDrawer }] = useDrawerInner((data) => {
    listAll.value = data.data;
    relationValue.value = JSON.parse(listAll.value.relationValue);
    fixCost.value = relationValue.value[0].relationValue;
    // 将小数转换为百分比格式显示
    ratio.value = relationValue.value[1].relationValue 
      ? (parseFloat(relationValue.value[1].relationValue) * 100).toFixed(2) + '%' 
      : '';
    spainRatio.value = relationValue.value[2].relationValue 
      ? (parseFloat(relationValue.value[2].relationValue) * 100).toFixed(2) + '%' 
      : '';
  });
  //获取现有的列表
  const listAll = ref();
  const fixCost = ref();
  const ratio = ref();
  const spainRatio = ref();
  const relationValue = ref();

  // 格式化百分比值,去除百分号并除以100
  function formatPercentage(value) {
    if (!value) return '';
    // 检查值是否包含百分号
    if (typeof value === 'string' && value.includes('%')) {
      // 去除百分号并转为数值,然后除以100
      return (parseFloat(value.replace('%', '')) / 100).toString();
    }
    // 如果没有百分号但是是数值,直接除以100
    return value;
  }

  //完成编辑
  async function handleSubmit() {
    relationValue.value[0].relationValue = fixCost.value;
    relationValue.value[1].relationValue = formatPercentage(ratio.value);
    relationValue.value[2].relationValue = formatPercentage(spainRatio.value);
    await saveConfig({
      id: listAll.value.id,
      settingCode: 'customerCode',
      settingName: '客户提成成本配置',
      settingValue: listAll.value.settingValue,
      settingType: 3,
      relationCode: 'costSettingItem',
      relationName: '成本配置项集合',
      costSettingItemVOS: relationValue.value,
    });
    emit('success');
    closeDrawer();
  }
</script>