<template>
	<u-popup v-model="show" mode="center" border-radius="16" width="85%" :closeable="true" @close="onClose">
		<view class="modal">
			<view class="modal-title">{{ isPlatform ? '修改价格' : '修改售价' }}</view>
			<view v-if="drugName" class="modal-drug">{{ drugName }}</view>
			<view v-if="suggestPrice && !isPlatform" class="modal-hint">建议售价 {{ suggestPrice }}</view>
			<view v-if="isPlatform" class="field-block">
				<text class="field-label">供货价</text>
				<input
					class="modal-input"
					type="digit"
					v-model="marketPriceInput"
					placeholder="请输入供货价"
				/>
			</view>
			<view class="field-block">
				<text class="field-label">{{ isPlatform ? '建议售价' : '售价' }}</text>
				<input
					class="modal-input"
					type="digit"
					v-model="priceInput"
					placeholder="请输入新售价"
				/>
			</view>
			<view class="modal-actions">
				<button class="btn cancel" @click="onClose">取消</button>
				<button class="btn confirm" :loading="submitting" @click="onConfirm">确定</button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import businessMixin from '@/subPackages/sub_business_shared/common/businessMixin.js'

export default {
  mixins: [businessMixin],
		name: 'PriceEditModal',
		props: {
			visible: { type: Boolean, default: false },
			item: { type: Object, default: null },
		},
		data() {
			return {
				show: false,
				priceInput: '',
				marketPriceInput: '',
				submitting: false,
			}
		},
		computed: {
			isPlatform() {
				return this.bizCap.warehouseType === 'platform'
			},
			drugName() {
				const drug = (this.item && this.item.drug) || {}
				return drug.drug_name || ''
			},
			suggestPrice() {
				if (!this.item) return ''
				const drug = this.item.drug || {}
				const storeDrug = drug.drug_store_drug || {}
				const p = storeDrug.price != null ? storeDrug.price : drug.price
				if (p == null || p === '') return ''
				return '¥' + Number(p).toFixed(2)
			},
		},
		watch: {
			visible: {
				immediate: true,
				handler(v) {
					this.show = v
					if (v && this.item) {
						const raw = this.item.price
						this.priceInput = raw != null && raw !== '' ? String(raw) : ''
						const supply = this.item.market_price ?? this.item.buy_price
						this.marketPriceInput = supply != null && supply !== '' ? String(supply) : ''
					}
				},
			},
			show(v) {
				if (!v) this.$emit('close')
			},
		},
		methods: {
			onClose() {
				this.show = false
				this.$emit('close')
			},
			onConfirm() {
				const price = parseFloat(String(this.priceInput).trim())
				if (!price || price <= 0 || Number.isNaN(price)) {
					uni.showToast({ title: '请输入有效售价', icon: 'none' })
					return
				}
				const rounded = Math.round(price * 100) / 100
				if (this.isPlatform) {
					const marketPrice = parseFloat(String(this.marketPriceInput).trim())
					if (!marketPrice || marketPrice <= 0 || Number.isNaN(marketPrice)) {
						uni.showToast({ title: '请输入有效供货价', icon: 'none' })
						return
					}
					this.$emit('confirm', {
						price: rounded,
						market_price: Math.round(marketPrice * 100) / 100,
					})
					return
				}
				this.$emit('confirm', rounded)
			},
		},
	}
</script>

<style lang="scss" scoped>
.modal {
	padding: 40rpx 32rpx 32rpx;
}
.modal-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	text-align: center;
}
.modal-drug {
	font-size: 26rpx;
	color: #666;
	margin-top: 16rpx;
	text-align: center;
}
.modal-hint {
	font-size: 24rpx;
	color: #999;
	margin-top: 8rpx;
	text-align: center;
}
.field-block {
	margin-top: 24rpx;
}
.field-label {
	display: block;
	font-size: 24rpx;
	color: #666;
	margin-bottom: 8rpx;
}
.modal-input {
	padding: 20rpx 24rpx;
	background: #f5f5f5;
	border-radius: 12rpx;
	font-size: 28rpx;
}
.modal-actions {
	display: flex;
	gap: 24rpx;
	margin-top: 40rpx;
}
.btn {
	flex: 1;
	font-size: 28rpx;
	border-radius: 44rpx;
	margin: 0;
}
.btn.cancel {
	background: #f5f5f5;
	color: #666;
}
.btn.confirm {
	background: #6acdbb;
	color: #fff;
}
</style>
