<template>
	<view class="warehouseEntry">
		<ul>
			<li>
				<view>
					账存数量
				</view>
				<view>
					{{datas.item.balance}}
				</view>
			</li>
			<li>
				<view>
					实际数量
				</view>
				<view>
					<input type="text" v-model="datas.form.balance" placeholder="输入数量" name="" id="">
				</view>
			</li>
			<li>
				<view>
					单价
				</view>
				<view>
					{{datas.item.price}}
				</view>
			</li>
			<li>
				<view>
					盈亏数量
				</view>
				<view>
					{{profitNumber}}
				</view>
			</li>
			<li>
				<view>
					总盈亏
				</view>
				<view>
					{{profitPrice}}
				</view>
			</li>
		</ul>
	</view>
	<view class="bottombutton">
		<up-button type="primary" style="background-color:  #318AFE;color: #fff;" @tap="toggle" :plain="true"
			text="保存"></up-button>
		<!-- 	<up-button type="text" style="background-color:  #f9f9f9;color: #999;" @tap="toggle" :plain="true"
			text="取消"></up-button> -->
	</view>
</template>


<script setup>
	import {
		ref,
		computed,
		onMounted,
		reactive
	} from 'vue';
	import go from '@/commons/utils/go.js';
	import color from '@/commons/color.js';
	import {
		tbConsInfotbConCheck
	} from '@/http/yskApi/requestAll.js';
	let showStatus = ref(false)
	const props = defineProps({
		item: {
			type: Object
		}
	})
	let datas = reactive({
		item: "",
		form: {}
	})
	onMounted(() => {
		datas.item = JSON.parse(props.item)
		datas.form = {
			...datas.item
		}
	})
	let profitPrice = computed(() => {
		// 如果剩余数量为负数 
		// if (datas.item.balance < 0) {
		// 	let a = datas.item.balance * -1
		// 	return formatDecimal((a - datas.form.balance) * datas.item.price)
		// } else {
		return formatDecimal((datas.item.balance - datas.form.balance) * datas.item.price)
		// }
	})
	let profitNumber = computed(() => {
		// 盈亏数量
		return datas.item.balance - datas.form.balance
	})

	function toggle() {
		tbConsInfotbConCheck({
			stockNumber: datas.item.balance,
			conInfoId: datas.form.id,
			lpNum: profitNumber._value
		}).then(res => {
			go.to('PAGES_SALES_CONSUMABLES')
		})
	}
	/**
	 * 保留小数n位，不进行四舍五入
	 * num你传递过来的数字,
	 * decimal你保留的几位,默认保留小数后两位
	 * isInt 是否保留0
	 */
	function formatDecimal(num = 0, decimal = 2, isInt = false) {
		num = num.toFixed(3).toString();
		const index = num.indexOf(".");
		if (index !== -1) {
			num = num.substring(0, decimal + index + 1);
		} else {
			num = num.substring(0);
		}
		//截取后保留两位小数
		if (isInt) {
			return parseFloat(num);
		} else {
			return parseFloat(num).toFixed(decimal);
		}
	}

	function showStatusToggle() {
		showStatus.value = !showStatus.value
	}
</script>

<style scoped lang="less">
	page {
		background-color: #f9f9f9;
	}

	.df() {
		display: flex;
		align-items: center;
	}

	ul,
	li {
		list-style: none;
		padding: 0;
	}

	.status {
		margin: 0 32rpx;
		position: absolute;

		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
	}

	.warehouseEntry {
		width: 694rpx;
		height: 650rpx;
		background: #FFFFFF;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		margin: 32rpx;
		padding: 1rpx 24rpx;
		box-sizing: border-box;

		>ul {
			>li {
				width: 646rpx;
				height: 84rpx;
				background: #fcfcfc;
				border: 2rpx solid #F9F9F9;
				margin-top: 32rpx;
				.df;

				>view:first-child {
					width: 190rpx;
					height: 84rpx;
					line-height: 84rpx;
					// text-align: left;
					padding-left: 24rpx;
					background: #F9F9F9;
					border-radius: 8rpx 0rpx 0rpx 8rpx;
					border: 2rpx solid #F9F9F9;
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: 400;
					font-size: 32rpx;
					color: #333333;
				}
			}
		}
	}

	.bottombutton {
		margin-top: 84rpx;

		>button {
			width: 530rpx;
			height: 80rpx;
			border-radius: 56rpx 56rpx 56rpx 56rpx;
		}
	}
</style>