fix some bugs
This commit is contained in:
@@ -6,6 +6,18 @@
|
||||
class="wallet-charge-form"
|
||||
:class="formClass"
|
||||
>
|
||||
<a-form-item label="کیف پول / محصول" name="chargeTargetKey" :rules="targetRules">
|
||||
<a-select
|
||||
v-model:value="modelValue.chargeTargetKey"
|
||||
show-search
|
||||
placeholder="انتخاب کنید"
|
||||
class="w-full"
|
||||
:options="targetOptions"
|
||||
:filter-option="filterTarget"
|
||||
@change="onTargetChange"
|
||||
/>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item label="نوع عملیات" name="direction">
|
||||
<a-radio-group
|
||||
v-model:value="modelValue.direction"
|
||||
@@ -36,10 +48,10 @@
|
||||
<BasicInput
|
||||
v-model:value="modelValue.amountAbs"
|
||||
:addon-before="unit"
|
||||
:type="variant === 'money' ? 'price' : 'number'"
|
||||
:type="isMoney ? 'price' : 'number'"
|
||||
:place-holder="amountPlaceholder"
|
||||
/>
|
||||
<div v-if="variant === 'money' && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint">
|
||||
<div v-if="isMoney && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint">
|
||||
معادل {{ usePrice(Number(modelValue.amountAbs) / 10, true, 'تومان') }}
|
||||
</div>
|
||||
</a-form-item>
|
||||
@@ -51,22 +63,46 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import type { FormInstance } from 'ant-design-vue'
|
||||
import type { Charge } from '@/model/wallet'
|
||||
import type { Charge, WalletChargeTarget } from '@/model/wallet'
|
||||
import { inputRules } from '@/utils/inputRules'
|
||||
import { usePrice } from '@core/utils/usePrice'
|
||||
import { applyChargeTarget, resolveChargeTarget, walletPreviewLabel } from '../utils/walletChargeForm'
|
||||
|
||||
const props = defineProps<{
|
||||
variant: 'money' | 'gold'
|
||||
modelValue: Charge
|
||||
targets: WalletChargeTarget[]
|
||||
}>()
|
||||
|
||||
const formRef = ref<FormInstance>()
|
||||
|
||||
const unit = computed(() => (props.variant === 'money' ? 'ریال' : 'گرم'))
|
||||
const selectedTarget = computed(() =>
|
||||
resolveChargeTarget(props.targets, props.modelValue.chargeTargetKey)
|
||||
)
|
||||
|
||||
const isMoney = computed(() => selectedTarget.value?.kind === 'money')
|
||||
const unit = computed(() => selectedTarget.value?.unit ?? 'ریال')
|
||||
const isDischarge = computed(() => props.modelValue.direction === 'discharge')
|
||||
|
||||
const targetOptions = computed(() =>
|
||||
props.targets.map((target) => ({
|
||||
value: target.key,
|
||||
label: target.label
|
||||
}))
|
||||
)
|
||||
|
||||
const targetRules = [
|
||||
{
|
||||
validator: (_rule: unknown, value: string | undefined) => {
|
||||
if (!value || !resolveChargeTarget(props.targets, value)) {
|
||||
return Promise.reject('کیف پول یا محصول را انتخاب کنید')
|
||||
}
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const formClass = computed(() =>
|
||||
isDischarge.value ? 'wallet-charge-form--discharge' : 'wallet-charge-form--charge'
|
||||
)
|
||||
@@ -81,8 +117,7 @@ const previewSign = computed(() => (isDischarge.value ? '−' : '+'))
|
||||
|
||||
const previewText = computed(() => {
|
||||
const action = isDischarge.value ? 'کسر از' : 'افزایش'
|
||||
const wallet = props.variant === 'money' ? 'موجودی ریالی' : 'موجودی طلایی'
|
||||
return `${action} ${wallet} کاربر`
|
||||
return `${action} ${walletPreviewLabel(selectedTarget.value)}`
|
||||
})
|
||||
|
||||
const amountLabel = computed(() =>
|
||||
@@ -92,7 +127,7 @@ const amountLabel = computed(() =>
|
||||
const noteLabel = computed(() => (isDischarge.value ? 'علت دشارژ' : 'علت شارژ'))
|
||||
|
||||
const amountPlaceholder = computed(() =>
|
||||
props.variant === 'money' ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵'
|
||||
isMoney.value ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵'
|
||||
)
|
||||
|
||||
const notePlaceholder = computed(() =>
|
||||
@@ -112,6 +147,28 @@ const amountRules = [
|
||||
}
|
||||
]
|
||||
|
||||
function filterTarget(input: string, option?: { label?: string }) {
|
||||
return String(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
|
||||
}
|
||||
|
||||
function onTargetChange(key: string) {
|
||||
const target = resolveChargeTarget(props.targets, key)
|
||||
if (!target) return
|
||||
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, target))
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.targets,
|
||||
(targets) => {
|
||||
if (!targets.length) return
|
||||
const current = resolveChargeTarget(targets, props.modelValue.chargeTargetKey)
|
||||
if (!current) {
|
||||
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, targets[0]))
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
defineExpose({
|
||||
validateFields: () => formRef.value?.validateFields()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user