<template>
    <view class="page">
	<NavBar :title="$t('修改进货单信息')" right="checkbox" @clickRight="submit"></NavBar>
	<view class="infoedit">
		<u-form ref="form" :modelValue="form" class="login-form" :labelWidth="160">
			<u-form-item prop="cd012" :label="$t('序号：')" class="disabled">
				<input
					v-model="form.cd012"
					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="cd017" :label="$t('批次号：')" class="disabled">
        <input
            v-model="form.cd017"
            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="cd018" :label="$t('进货数量：')" class="disabled">
				<input
					v-model="form.cd018"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="cd020" :label="$t('入库数量：')" class="disabled">
				<input
					v-model.number="form.cd020"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
      <u-form-item prop="th009" :label="$t('仓库：')">
        <input
          v-model="form.th009"
          type="text"

          @confirm="submit"
        >
        </input>
      </u-form-item>
      <u-form-item prop="th072" :label="$t('库位：')">
        <input
          v-model="form.th072"
          type="text"
          @confirm="submit"
        >
        </input>
      </u-form-item>
      <u-form-item prop="th015" :label="$t('验收数量：')">
        <input
          v-model.number="form.th015"
          type="text"
          @confirm="submit"
        >
        </input>
      </u-form-item>
      <u-form-item prop="th016" :label="$t('计价数量：')">
        <input
          v-model.number="form.th016"
          type="text"
          @confirm="submit"
        >
        </input>
      </u-form-item>
      <u-form-item prop="th017" :label="$t('验退数量：')">
        <input
          v-model.number="form.th017"
          type="text"
          @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
			this.form = data.purths[this.index]
		}
	},
	computed: {
		chargaArr() {
			return this.info.purths.filter(v => v.batch && v.cd004 === this.form.cd004)
		}
	},
    data() {
      return {
		info: null,
		index: 0,
		form: {
			cd003: null,
			cd004: null,
			cd005: null,
			cd011: null,
			cd008: null,
			cd018: null,
			cd019: null,
		}
      };
    },
    created() {
    },
    methods: {
		addBatch() {
			const item = {
				cd004: this.form.cd004,
				cd005: this.form.cd005,
				th010: '',
				cd001: this.form.cd001,
				cd002: this.form.cd002,
				cd003: this.form.cd003,
				batch: true,
				th009: this.form.th009,
				th072: this.form.th072,
				cd018: this.form.cd018,
				th015: 0,
				th016: 0,
				th017: 0
			}
			const num = this.chargaArr.reduce((pre, cur) => {
				return pre + cur.th015 + cur.th017
			}, 0)
			uni.navigateTo({
              url: '/pages/forcreate/Batch?info=' + encodeURIComponent(JSON.stringify(item)) + '&num=' + num
            });
		},
		editBatch(item, index) {
			const num = this.chargaArr.reduce((pre, cur) => {
				return pre + cur.th015 + cur.th017
			}, 0)
			uni.navigateTo({
              url: '/pages/forcreate/Batch?info=' + encodeURIComponent(JSON.stringify(item))  + '&num=' + num + '&index=' + index
            });
		},
		getValue(item, index, splice = false){
			console.log('item', item, index, splice)
			if (splice) return this.info.purths.splice(index, 1)
			if(index === null) {
				this.info.purths.push(item)
			} else {
				this.$set(this.info.purths, index, item)
			}
		},
		submit(row) {
			if (!this.form.islot) {
				if(!this.form.th009) return this.$u.toast(i18n.t('请输入仓库'))
				if(!this.form.th072) return this.$u.toast(i18n.t('请输入库位'))
				if (this.form['th016'] || this.form['th015']) {
					const th015 = this.form['th015'] || 0
					const th016 = this.form['th016'] || 0
					if (th016 > th015) return this.$u.toast(i18n.t('计价数量不能大于验收数量'))
				}
				if (this.form['th015'] || this.form['th017']) {
					const th015 = this.form['th015'] || 0
					const th017 = this.form['th017'] || 0
					const cd018 = Number(this.form['cd018'])
					if (th015 + th017 > cd018) return this.$u.toast(i18n.t('验收数量+验退数量不能大于进货数量'))
				}
			} else {
			}
			let pages = getCurrentPages() 
			
			// 2. 上一页面实例
				// 注意是length长度，所以要想得到上一页面的实例需要 -2
				// 若要返回上上页面的实例就 -3，以此类推
			let prevPage = pages[pages.length -2] 
			// 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
			prevPage.$vm.getValue(this.info) 
			
			// 4. 返回上一页面
			uni.navigateBack({
				delta: 1 // 返回的页面数
			})
		}
    },
  };
  </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;
	}
	
  }
  .batchContent{
	color: #787878;
	font-size: 17px;
	width: 100%;
	padding: 15rpx;
	.itemtitle{
		font-weight: 600;
		font-size: 19px;
	}
	&>view{
		width: 100%;
		display: flex;
		justify-content: space-between;
	}
  }
  </style>
  