<template>
  <view class="page-raw-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.po_ReceivingGoods") }}</view>
      <view class="right"></view>
    </view>
    <view class="content">
      <!-- 单号 -->
      <view class="single">
        <view class="single-left">
          <view>{{ $t("message.po_OddNumbers") }}</view>
          <u-search
            :placeholder="$t('message.po_PleaseScan')"
            v-model.trim="form.documentNo"
            @search="model.queryOrderInInfo"
            :show-action="false"
          ></u-search>
        </view>
        <view class="single-right">
          <u-button type="primary" @click="query">查询</u-button>
        </view>
      </view>
      <!-- 物料 -->
      <view class="material">
        <view class="material-left">
          <view>物料号</view>
          <jPicker
            sureColor="#ff0000"
            style="width: 260rpx"
            @bindpicker="materialChoice"
            showKey="value"
            valKey="value"
            :val="wlCode"
            class="search"
            :options="materialList"
          />
        </view>
        <view class="material-right">
          <view class="material-right-title">物料描述</view>
          <view class="material-right-code">{{ every.materialDesc }}</view>
        </view>
      </view>
      <!-- 需求数量 -->
      <view class="number">
        <view class="number-left">
          <view class="number-left-title">需求数量</view>
          <u-input
            v-model="every.requestAmount"
            :type="type"
            :border="border"
            placeholder=" "
            class="input"
            disabled
          />
        </view>
        <view class="number-right">
          <view class="number-right-title">累计数量</view>
          <u-input
            v-model="Cumulative"
            :type="type"
            :border="border"
            class="input"
            disabled
          />
        </view>
      </view>
      <!-- 库位 -->
      <view class="library">
        <view class="library-left">
          <view>库位</view>
          <jPicker
            sureColor="#ff0000"
            style="width: 230rpx"
            @bindpicker="LocationChoice"
            showKey="value"
            valKey="value"
            :val="wlCode"
            class="search"
            :options="Location"
          />
        </view>
        <view class="library-right">
          <view class="library-right-title">本次数量</view>
          <u-input
            v-model="receiptAmount"
            :type="type"
            :border="border"
            class="input"
          />
        </view>
      </view>
      <!-- 添加 -->
      <view class="add">
        <u-button type="primary" @click="Add">添加</u-button>
      </view>
      <!-- 表格 -->
      <u-table class="library-table">
        <u-tr class="u-tr">
          <u-th class="u-th">{{ $t("message.product_Location") }}</u-th>
          <u-th class="u-th">{{ $t("message.product_Number") }}</u-th>
          <u-th class="u-th">{{ $t("message.operation") }}</u-th>
        </u-tr>
        <u-tr class="u-tr" v-for="(item, index) in LocationList" :key="index">
          <u-td class="u-td">{{ item.wlCode }}</u-td>
          <u-td class="u-td">{{ item.receiptAmount }}</u-td>
          <u-td class="u-td">
            <div class="u-td" @click="deleteItem(index)">
              {{ $t("message.product_Delete") }}
            </div>
          </u-td>
        </u-tr>
      </u-table>
    </view>
    <!-- 底部按钮 -->
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="4">
          <u-button type="primary" @click="bill">收货明细</u-button>
        </u-col>
        <u-col :span="4">
          <u-button type="primary" @click="onSubmit">提交</u-button>
        </u-col>
        <u-col :span="4">
          <u-button type="error" @click="back">返回</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 jPicker from "@/components/J-Picker/jPicker.vue";
import { session } from "@/store/modules/session";
import { VForm, VFormRules } from "vue/types/form";
import model from "./model";
@Component({
  components: {
    jPicker,
  },
})
export default class dnReceiptDom extends BasePage {
  model = model;
  form: any = {
    documentNo: "",
  };
  material: any = {};
  materialList: any = [];
  every: any = "";
  value = "";
  type = "text";
  border = true;
  //累计数量
  Cumulative: any = 0;
  //判断要添加的库位和已添加的库位是否相等
  sendSpotList: any = [];
  //本次数量
  receiptAmount: any = "";
  //库位
  Location: any = [];
  //添加库位和数量的列表
  LocationList: any = [];
  //所选择的库位
  wlCode: any = "";
  //上传
  upload: any = [];
  //页面初始化
  async onShow() {
    //页面初始化 获取可选库位
    let means: any = JSON.parse(
      localStorage.getItem("__GWMS_APP_STATE_DATA__") as any
    );
    let sendSpot = JSON.parse(localStorage.getItem("sendSpot") as any);
    let content = {
      loginName: means.session.user.loginName,
      sendSpot: sendSpot,
      factoryCode: means.session.user.factoryCode,
      workArea: session.workareaCode,
    };
    let res: any = await this.model.queryByFactoryCodeAndWorkAreaCode(content);
    res.forEach((item: any) => {
      let pickerName: any = {};
      pickerName.label = item.locationCode;
      pickerName.value = item.locationCode + "(" + item.sendSpot + ")";
      pickerName.sendSpot = item.sendSpot;
      this.Location.push(pickerName);
    });
  }
  //输入单号 查询数据
  async query() {
    if (this.form.documentNo == "") {
      uni.showToast({
        icon: "none",
        title: "请输入单号",
      });
      return;
    }
    await this.model.queryOrderInInfo(this.form.documentNo);
    let list: any = localStorage.getItem("list");
    localStorage.removeItem("list");
    this.material = JSON.parse(list);
    this.material.forEach((item: any) => {
      let pickerName: any = {};
      pickerName.label = item.materialCode;
      pickerName.value = item.materialCode;
      this.materialList.push(pickerName);
    });
  }
  //选择物料后触发的回调事件  本次数量：receiptAmount  需求数量：requestAmount
  materialChoice(e: any) {
    this.material.forEach((item: any) => {
      if (item.materialCode == e.pickerName.value) {
        this.every = item;
        return;
      }
    });
  }
  //选择库位后触发的回调事件
  LocationChoice(e: any) {
    console.log("e>>>>>>>>>>>>>>>>>>>>>>>>", e);
    this.wlCode = e.pickerName;
  }
  //点击添加
  Add() {
    if (this.receiptAmount == "" || this.wlCode == "") {
      uni.showToast({
        icon: "none",
        title: "请输入正确的库位和数量" as any,
      });
      return;
    }
    if (parseFloat(this.receiptAmount) <= 0) {
      uni.showToast({
        icon: "none",
        title: "请输入大于0的数量" as any,
      });
      return;
    }
    if (parseFloat(this.receiptAmount) > this.every.requestAmount) {
      uni.showToast({
        icon: "none",
        title: "不能大于需求数量，请重新输入" as any,
      });
      return;
    }
    let isTrue: boolean = true;
    if (this.sendSpotList.length != 0) {
      this.sendSpotList.forEach((item: any) => {
        if (this.wlCode.sendSpot != item) {
          isTrue = false;
        }
      });
    }
    if (isTrue != true) {
      uni.showToast({
        icon: "none",
        title: "请选择相同库位" as any,
      });
      return;
    }
    let objString = JSON.stringify(this.every);
    let TEM = JSON.parse(objString);
    TEM.wllist = null;
    TEM.receiptAmount = this.receiptAmount;
    TEM.wlCode = this.wlCode.label;
    this.sendSpotList.push(this.wlCode.sendSpot);
    console.log("this.sendSpotList", this.sendSpotList);
    console.log("TEM", TEM);
    this.LocationList.push(TEM);
    this.every.wllist = this.LocationList;
    console.log("表格数据", this.LocationList);
    console.log("要上传的数据", this.every);
    this.receiptAmount = "";
    isTrue = true;
    this.business();
  }
  deleteItem(index: any) {
    this.LocationList.splice(index, 1);
    this.every.wllist.splice(index, 1);
    console.log("表格数据", this.LocationList);
    console.log("要上传的数据", this.every);
    this.business();
  }
  async onSubmit() {
    if (this.every.wllist == null || this.every.wllist.length == 0) {
      uni.showToast({
        icon: "none",
        title: "请确保所选物料已分配库位" as any,
      });
    }
    this.upload.push(this.every);
    await this.model.submitOrderInEnter(this.upload);
    this.LocationList = [];
    this.wlCode = "";
    this.receiptAmount = "";
    this.Cumulative = 0;
    this.every = "";
    this.materialList = [];
  }
  bill() {
    this.redirectTo(this.page.raw.ingoods.dnReceipt.bill);
  }
  //添加和删除操作的时候  计算累计收货数量
  business() {
    let num: number = 0;
    this.LocationList.forEach((item: any) => {
      num += parseFloat(item.receiptAmount);
    });
    this.Cumulative = num;
    this.every.receiptAmount = num;
    console.log("this.every", this.every);
  }
}
</script>
<style lang="scss" scoped>
.page-raw-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;
    }
  }
  .content {
    width: 100%;
    background: white;
    border-radius: 15rpx;
    padding: 10rpx;
    .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;
        }
      }
    }
    .material {
      width: 100%;
      height: 100rpx;
      display: flex;
      .material-left {
        width: 50%;
        height: 100%;
        display: flex;
        view {
          width: 100rpx;
          line-height: 100rpx;
        }
      }
      .material-right {
        width: 50%;
        height: 100%;
        display: flex;
        .material-right-title {
          width: 120rpx;
          height: 100%;
          line-height: 100rpx;
        }
        .material-right-code {
          width: 190rpx;
          height: 100%;
          // line-height: 100rpx;
        }
      }
    }
    .number {
      height: 100rpx;
      display: flex;
      .number-left {
        width: 50%;
        height: 100%;
        display: flex;
        .number-left-title {
          width: 120rpx;
          height: 100%;
          line-height: 100rpx;
        }
        .input {
          width: 200rpx;
          height: 70rpx;
          margin-top: 15rpx;
          margin-left: 15rpx;
        }
      }
      .number-right {
        width: 50%;
        height: 100%;
        display: flex;
        .number-right-title {
          width: 120rpx;
          height: 100%;
          line-height: 100rpx;
        }
        .input {
          width: 200rpx;
          height: 70rpx;
          margin-top: 15rpx;
          margin-left: 15rpx;
        }
      }
    }
    .library {
      width: 100%;
      height: 100rpx;
      display: flex;
      .library-left {
        width: 50%;
        height: 100%;
        display: flex;
        view {
          width: 100rpx;
          line-height: 100rpx;
        }
      }
      .library-right {
        width: 50%;
        height: 100%;
        display: flex;
        .library-right-title {
          width: 120rpx;
          height: 100%;
          line-height: 100rpx;
        }
        .input {
          width: 200rpx;
          height: 70rpx;
          margin-top: 15rpx;
          margin-left: 15rpx;
        }
      }
    }
    .add {
      width: 100%;
      height: 100rpx;
      position: relative;
      button {
        position: absolute;
        top: 10rpx;
        right: 20rpx;
      }
    }
  }
  .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>
