<template>
    <view class="page">
	<NavBar :title="$t('修改到货单打印信息')"></NavBar>
	<view class="infoedit">
		<u-form ref="form" :modelValue="form" class="login-form" :labelWidth="160">
			<u-form-item prop="cd003" :label="$t('序号：')" class="disabled">
				<input
					v-model="form.cd003"
					type="text"
					disabled
					
				>
				</input>
			</u-form-item>
			<u-form-item prop="cd004" :label="$t('品号：')" class="disabled">
				<input
					v-model="form.cd004"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="cd005" :label="$t('品名：')" class="disabled">
				<input
					v-model="form.cd005"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="cd011" :label="$t('采购单号：')" class="disabled">
				<input
					v-model="form.cd011"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="cd008" :label="$t('到货数量：')" class="disabled">
				<input
					v-model="form.cd008"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="count" :label="$t('标签个数：')">
				<input
					v-model="form.count"
					:disabled="!!effectivePerBox"
					type="number"
					:placeholder="effectivePerBox ? '' : $t('每箱数量为空时填写')"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="mb073" :label="$t('每箱数量：')">
				<input
					v-model="form.mb073"
					type="number"
					:placeholder="$t('为空或0则只打1张标签')"
					@input="onMb073Input"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item style="width:100%;">
			<button
				size="medium"
				type="primary"
				style="width:95%;margin-top: 50rpx"
				@click="submit()"
			>
				<span>{{$t('确定')}}</span>
			</button>
			</u-form-item>
		</u-form>
	</view>
    </view>
  </template>
  
  <script>
import i18n from '../../lang/index'

  import StickyNavBar from "@/components/StickyNavBar/index.vue";
  import ContentLoadingMore from "@/components/ContentLoadingMore/index.vue";
  export default {
    components: {
      StickyNavBar,
      ContentLoadingMore,
    },
	onLoad(option) {
		if (option.info) {
			let data = JSON.parse(decodeURIComponent(option.info));
			this.index = option.index
			this.info = data
			const row = data.purcds[this.index]
			this.form = { ...row, count: this.calcLabelCount(row.cd008, row.mb073) }
		}
	},
    computed: {
		effectivePerBox() {
			const v = this.form.mb073;
			if (v == null || v === '') return 0;
			const n = typeof v === 'number' ? v : parseInt(v, 10);
			return isNaN(n) ? 0 : n;
		}
	},
    data() {
      return {
		info: null,
		index: 0,
		form: {
			cd003: null,
			cd004: null,
			cd005: null,
			cd011: null,
			cd008: null,
			count: null,
			mb073: null,
		}
      };
    },
    created() {
    },
    methods: {
		calcLabelCount(cd008, mb073) {
			const qty = Number(cd008) || 0;
			const perBox = mb073 != null && mb073 !== '' ? (typeof mb073 === 'number' ? mb073 : parseInt(mb073, 10)) : 0;
			if (perBox > 0 && qty > 0) return Math.ceil(qty / perBox);
			return 1;
		},
		onMb073Input() {
			this.$nextTick(() => {
				this.form.count = this.calcLabelCount(this.form.cd008, this.form.mb073);
			});
		},
		submit(row) {
			const count = this.calcLabelCount(this.form.cd008, this.form.mb073);
			if (count < 1) return this.$u.toast(i18n.t('需要填写标签个数'));
			const mb073 = this.form.mb073 !== '' && this.form.mb073 != null ? (typeof this.form.mb073 === 'number' ? this.form.mb073 : parseInt(this.form.mb073, 10)) : null;
			this.info.purcds[this.index] = { ...this.info.purcds[this.index], ...this.form, count, mb073 };
			uni.redirectTo({
              url: '/pages/abnormal/abnormalRemoveInfo?info=' + encodeURIComponent(JSON.stringify(this.info))
            });
		}
    },
  };
  </script>
  
  <style lang="scss" scoped>
  ::v-deep .u-sticky {
    top: 0 !important;
    background: #fff;
  }
  .infoedit {
	padding: 20rpx 20rpx;
	.underline {
		border-bottom: 1px solid #d5d5d5;
		padding-bottom: 0px;
    	line-height: 40px;
		font-weight: 500;
		margin-left: 20rpx;
		font-size: 19px;
	}
	.u-border-bottom:after {
		display: none;
	}
	
  }
  </style>
  