<template>
  <view class="page-product-receipt">
    <view class="header" :style="{ backgroundColor: `rgba(23, 83, 234, ${scrollTop / 100})` }">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="uni.navigateBack({ delta: 1 })" />
      </view>
      <view class="title">{{ $t('message.LocationInventory') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :model="form" label-width="180rpx">
      <!-- 盘点单号 -->
      <view class="single">
        <view class="single-left">
          <view>{{ $t('message.code') }}</view>
          <u-search :placeholder="$t('message.Commission_tips1')" v-model.trim="form.pddNo" @search="query" :show-action="false"></u-search>
        </view>
        <view class="single-right">
          <u-button type="primary" @click="query">{{ $t('message.Query') }}</u-button>
        </view>
      </view>
      <!-- 单据已扫 -->
      <u-form-item :label="$t('message.DocumentScanned')">
        <u-input :disabled="true" v-model="total" placeholder="" />
      </u-form-item>
      <!-- 库位 -->
      <u-form-item :label="$t('message.product_Location')">
        <jPicker sureColor="#ff0000" style="width: 260rpx" @bindpicker="materialChoice" showKey="value" valKey="value" :val="wl.value" class="search" :options="codeList" />
      </u-form-item>
      <!-- 库位已扫 -->
      <u-form-item :label="$t('message.LocationScanned')">
        <u-input v-model="Some.spQty" :disabled="true" placeholder="" />
      </u-form-item>
      <!-- 条码 -->
      <u-form-item :label="$t('message.product_BarCode')">
        <u-search :placeholder="$t('message.barcode')" v-model.trim="form.barcode" @search="scanning" :show-action="false"></u-search>
      </u-form-item>
    </u-form>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="6">
          <u-button type="primary" @click="onSubmit">{{ $t('message.product_Confirm') }}</u-button>
        </u-col>
        <u-col :span="6">
          <u-button type="error" @click="uni.navigateBack({ delta: 1 })">{{ $t('message.po_Return') }}</u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>
<script lang="ts">
import { Component, Ref } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import { VForm, VFormRules } from 'vue/types/form';
import model from './model';
import jPicker from '@/components/J-Picker/jPicker.vue';
import { session } from '@/store/modules/session';
interface OptionType {
  label: string;
  value: string;
}
@Component({
  components: {
    jPicker,
  },
})
export default class ProductCheckReceipt extends BasePage {
  @Ref('form') readonly $form!: VForm;
  model = model;
  //表单
  form = {
    pddNo: '',
    barcode: '',
  };
  wl: any = {};
  Some: any = {};
  codeList: any = [];
  total: any = '';
  areaCode: any = '';
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.model.orderInInfo.productDescZh = '';
    this.model.orderInInfo.productCode = '';
    this.model.orderInInfo.qty = '';
    this.model.orderInInfo.locCode = '';
    this.model.orderInInfo.costCenter = '';
  }
  //单号查询
  async query() {
    if (!this.form.pddNo) {
      this.customToast(this.$t('message.Commission_tips1') as string);
      return;
    }
    let param = {
      factoryCode: session.factoryCode,
      loginName: session.loginName,
      pddNo: this.form.pddNo,
    };
    await this.model.queryScrapList(param);
    if (this.model.code == '1') {
      this.customToast(this.$t('message.product_Tip8') as string);
      let num = 0;
      this.model.modelList.forEach((item: any) => {
        num += item.spQty;
        let arr: any = {};
        arr.label = item.locCode;
        arr.value = item.locCode;
        this.codeList.push(arr);
      });
      this.total = num;
      this.Some = this.model.modelList[0];
      this.wl = this.codeList[0];
    }
  }
  //选择成品编码
  materialChoice(e: any) {
    this.wl = e.pickerName;
    this.model.modelList.forEach((item: any) => {
      if (item.locCode == this.wl.value) {
        this.Some = item;
      }
    });
  }
  //条码扫码
  async scanning() {
    if (!this.form.pddNo) {
      this.customToast(this.$t('message.Commission_tips1') as string);
    }
    if (!this.form.barcode) {
      this.customToast(this.$t('message.barcode') as string);
    }
    let params = {
      factoryCode: session.factoryCode,
      user: session.loginName,
      pdCd: this.form.pddNo,
      regionCode: this.Some.regionCode,
      areaCode: this.Some.areaCode,
      whCode: this.Some.whCode,
      snFlag: '1',
      pdMode: '10',
      sn: this.form.barcode,
      locCode: this.wl.value,
      qty: '1',
    };
    await this.model.getProductCode(params);
    if (this.model.code == '1') {
      this.customToast(this.$t('message.product_Tip8') as string);
      this.total += 1;
      this.model.modelList.forEach((item: any) => {
        if (item.locCode == this.wl.value) {
          item.spQty += 1;
          this.Some = item;
        }
      });
      this.form.barcode = '';
    }
  }
  //确定
  async onSubmit() {
    if (this.form.pddNo == '') {
      this.customToast(this.$t('message.Warehouse_Tip5') as string);
      return;
    }
    if (this.total == '') {
      this.customToast(this.$t('message.Warehouse_Tip5') as string);
      return;
    }
    if (!this.Some) {
      this.customToast(this.$t('message.product_Tip3') as string);
      return;
    }
    const params = {
      factoryCode: session.factoryCode,
      user: session.loginName,
      pddno: this.form.pddNo,
      regionCode: this.Some.regionCode,
      areaCode: this.Some.areaCode,
      whCode: this.Some.whCode,
      snFlag: '1',
      pdMode: '10',
    };
    await this.model.onTakeoutConfirm(params);
    if (this.model.status == 'S') {
      uni.showToast({
        icon: 'success',
        title: this.$t('message.success') as string,
      });
      this.form.pddNo = '';
      this.form.barcode = '';
      this.wl = {};
      this.Some = {};
      this.codeList = [];
      this.total = '';
      this.areaCode = '';
      this.model.modelList = [];
    }
  }
}
</script>
<style lang="scss" scoped>
.page-product-receipt {
  background: #f2f2f2 linear-gradient(0deg, #f2f2f2 0%, #4a78ea 51%, #1753ea 100%) no-repeat;
  background-size: 100% 600rpx;
  padding: 118rpx 30rpx 162rpx;
  min-height: 100%;

  .header {
    position: fixed;
    top: 36rpx;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    height: 88rpx;
    line-height: 88rpx;
    color: #fff;
    font-size: 34rpx;
    font-weight: 500;
    text-align: center;
    .title {
      flex: 3;
    }
    .left,
    .right {
      flex: 1;
    }
    .icon {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 88rpx;
      height: 88rpx;
    }
  }

  .form {
    background-color: #fff;
    padding: 40rpx;
    border-radius: 10rpx;
    box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);

    .u-form-item {
      padding: 30rpx 0;
      line-height: 35rpx;
    }
    .single {
      width: 100%;
      height: 100rpx;
      display: flex;
      .single-left {
        width: 80%;
        height: 100%;
        display: flex;
        view {
          width: 110rpx;
          height: 100%;
          //line-height: 100rpx;
        }
      }
      .single-right {
        button {
          margin-top: 15rpx;
          width: 150rpx;
          height: 70rpx;
        }
      }
    }
  }

  .bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 99;
    background: #ffffff;
    box-shadow: 0 1rpx 20rpx 0 rgba(128, 128, 128, 0.2);
    padding: 20rpx;
  }
}
</style>
