<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({})" />
      </view>
      <view class="title">{{ $t('message.Inventory') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :model="form" label-width="180rpx">
      <!-- 单号 -->
      <u-form-item :required="true" :label="$t('message.Warehouse_OrderNo')" prop="pddNo">
        <u-search :placeholder="$t('message.po_PleaseInput')" v-model.trim="form.pddNo" @search="query" :show-action="false"></u-search>
      </u-form-item>
      <!-- 库位 -->
      <u-form-item :required="true" :label="$t('message.product_Location')">
        <u-input v-model="form.originWl.locCode" type="select" @click="originWlSelect = true" :placeholder="$t('message.po_PleaseInput')" />
        <u-select :confirm-text="$t('message.product_Confirm')" :cancel-text="$t('message.product_Cancel')" v-model="originWlSelect" :list="model.WlList" @confirm="onchange"></u-select>
      </u-form-item>
      <!-- 扫码 -->
      <u-form-item :label="$t('message.Warehouse_ScanCode')">
        <u-search v-model.trim="form.code" :placeholder="$t('message.po_PleaseInput')" @search="scanCode" :show-action="false"></u-search>
      </u-form-item>
      <u-row>
        <!-- 已扫 -->
        <u-col :span="6">
          <u-form-item :label="$t('message.product_Swept')" label-width="80rpx">
            <u-input :disabled="true" v-model="form.spQty" placeholder="" />
          </u-form-item>
        </u-col>
        <!-- 总数 -->
        <u-col :span="6">
          <u-form-item :label="$t('message.Warehouse_TotalNumber')" label-width="80rpx">
            <u-input v-model="form.ypQty" :placeholder="$t('message.po_PleaseInput')" type="number" />
          </u-form-item>
        </u-col>
      </u-row>
      <!-- 成品描述 -->
      <u-form-item :label="$t('message.product_FGDes')">
        <u-input v-model="productDescZh" placeholder="" :disabled="true" style="overflow: hidden" />
      </u-form-item>
      <!-- 成品编码 -->
      <u-form-item :label="$t('message.product_FGCode')">
        <u-input v-model="productCode" placeholder="" :disabled="true" />
      </u-form-item>
    </u-form>
    <!-- 底部Button -->
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="6">
          <u-button type="primary" @click="onSubmit">{{ $t('message.workArea_Confirm') }}</u-button>
        </u-col>
        <u-col :span="6">
          <u-button type="error" @click="uni.navigateBack({})">{{ $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 { session } from '@/store/modules/session';

interface OptionType {
  label: string;
  value: string;
  ypQty: number;
  productCode: string;
  locCode: string;
  snFlag: string;
  pdMode: string;
  spQty: number;
}

@Component
export default class productCheckReceipt extends BasePage {
  /**
   * 表单引用
   */
  @Ref('form') readonly $form!: VForm;

  /**
   * 页面Module
   */
  model = model;
  aaa: number = null;
  bbb: number = null;
  /**
   * 表单数据
   */
  form = {
    originWl: {} as OptionType,
    pddNo: '',
    code: '',
    pdMode: '',
    // 已扫
    spQty: null,
    // 总数
    ypQty: null,
  };

  productCode: any = ''; //成品编码
  productDescZh: any = ''; //成品描述
  // 库位select是否显示
  originWlSelect = false;
  aimWlSelect = false;
  cboPlaceSelect = false;

  rules: VFormRules<any> = {
    originWl: [
      { required: true, message: this.$t('message.Warehouse_Tip2') as string }, //请选择库位地点
    ],
  };
  show = false;
  async onReady() {
    this.$form.setRules(this.rules);
  }
  async query() {
    if (this.form.pddNo == '') {
      this.customToast(this.$t('message.Commission_tips1') as string);
    }
    await this.model.getProductCode(this.form.pddNo);
    if (model.code == '1') {
      this.customToast(this.$t('message.product_Tip8') as string);
      // 获取选择库位的集合
      const storeList = [];
      storeList.push(this.model.WlListaa[0]);
      // 获取现有的 spQty、ypQty 总数
      const total = storeList.reduce(
        (total, item) => {
          return {
            spQty: total.spQty + item.spQty,
            ypQty: total.ypQty + item.ypQty,
          };
        },
        { spQty: 0, ypQty: 0 },
      );
      const originWl = this.model.WlListaa[0];
      // 为form表单绑定值
      Object.assign(this.form, {
        originWl: originWl,
        ...total,
      });
    }
  }
  onchange(e: any) {
    // 获取选择库位的集合
    const storeList = this.model.WlListaa.filter((item) => item.locCode === e[0].value);
    console.log(storeList);
    // 获取现有的 spQty、ypQty 总数
    const total = storeList.reduce(
      (total, item) => {
        return {
          spQty: total.spQty + item.spQty,
          ypQty: total.ypQty + item.ypQty,
        };
      },
      { spQty: 0, ypQty: 0 },
    );
    const originWl = this.model.WlListaa.find((item) => item.locCode === e[0].value);
    // 为form表单绑定值
    Object.assign(this.form, {
      originWl: originWl,
      ...total,
    });
  }
  // 成功查询后，已扫+1
  async scanCode(e: any) {
    if (!this.form.pddNo) {
      this.customToast(this.$t('message.Warehouse_Tip5') as string);
      return;
    }
    const list = {
      user: session.loginName,
      factoryCode: session.factoryCode,
      regionCode: session.regionCode,
      areaCode: session.regionCode,
      whCode: session.warehouseCode,
      pdCd: this.form.pddNo,
      locCode: this.form.originWl.locCode,
      snFlag: this.form.originWl.snFlag,
      pdMode: this.form.originWl.pdMode,
      sn: e,
      qty: 1,
    };
    await this.model.toScanCode(list);
    if (this.model.records.code == '1') {
      this.form.spQty = this.form.spQty + 1;
      this.productCode = this.model.productCode;
      this.productDescZh = this.model.productDescZh;
    }
  }
  onSubmit() {
    this.$form.validate((valid: boolean) => {
      if (!valid) return;
      if (!this.form.pddNo) {
        this.customToast(this.$t('message.Warehouse_Tip5') as string);
        return;
      }
      if (!this.form.originWl.productCode) {
        this.customToast(this.$t('message.Warehouse_Tip6') as string);
        return;
      }
      if (this.form.originWl.ypQty == this.form.originWl.spQty) {
        this.commit();
      } else {
        uni.showModal({
          title: 'Tip',
          content: this.$t('message.Warehouse_Tip7') as string, //盘点未完成 确定继续嘛
          confirmText: this.$t('message.workArea_Confirm') as string, //确定
          cancelText: this.$t('message.Cancel') as string, //取消
          showCancel: true,
          success: (res) => {
            if (res.confirm) {
              this.commit();
            }
          },
        });
      }
    });
  }
  async commit() {
    const list = {
      // user: '1999',
      user: session.loginName,
      pddno: this.form.pddNo,
      factoryCode: session.factoryCode,
      whCode: session.warehouseCode,
      regionCode: session.regionCode,
      areaCode: session.regionCode,
      locCode: this.form.originWl.locCode,
      productCode: this.form.originWl.productCode,
      snFlag: this.form.originWl.snFlag,
      pdMode: this.form.originWl.pdMode,
      sn: this.form.code,
      spQty: this.form.spQty,
    };
    await this.model.onTakeoutConfirm(list);
  }
}
</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;
    }
  }

  .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>
