<template>
  <view class="page-receipt-detail">
    <view class="header">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="uni.navigateBack()" />
      </view>
      <view class="title">{{ $t("message.product_Location") }}</view>
      <view class="right"></view>
    </view>
    <div class="content">
      <div class="content-item">
        <div class="item-left">{{ $t("message.po_MaterielNo") }}</div>
        <div class="item-right">{{ material.materialCode }}</div>
      </div>
      <div class="content-item">
        <div class="item-left">{{ $t("message.po_MaterielDes") }}</div>
        <div class="item-right">{{ material.materialDesc }}</div>
      </div>
      <div class="content-item">
        <div class="item-left">{{ $t("message.po_DemandQuantity") }}</div>
        <div class="item-right">{{ material.amount }}</div>
      </div>
      <div class="content-item">
        <div class="item-left">{{ $t("message.po_ActualQuantity") }}</div>
        <div class="item-right">{{ material.currentAmount }}</div>
      </div>
      <div class="content-item">
        <div class="item-left">{{ $t("message.po_Location") }}</div>
        <div class="item-right">{{ material.location }}</div>
      </div>
    </div>
    <div class="library">
      <div class="title">{{ $t("message.product_Location") }}:</div>
      <jPicker
        sureColor="#ff0000"
        @bindpicker="bookTypeChange"
        showKey="value"
        valKey="value"
        :val="wlCode"
        class="search"
        :options="list"
      />
    </div>
    <div class="library-num">
      <div class="title">{{ $t("message.dn_Number") }}:</div>
      <input type="text" placeholder="请输入数量" v-model="number" />
      <u-button type="primary" size="medium" class="confirm" @click="confirm">{{
        $t("message.product_add")
      }}</u-button>
    </div>
    <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.value.value }}</u-td>
        <u-td class="u-td">{{ item.num }}</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 class="bottom-bar">
      <div class="extra">
        <u-row class="bottom-info">
          <u-button type="primary" class="main" @click="Reservoir">{{
            $t("message.product_Confirm")
          }}</u-button>
          <u-col :span="2" v-if="factoryCode == '6662'">
            { {$t('message.product_Location')}}
          </u-col>
        </u-row>
      </div>
    </view>
  </view>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import { BasePage } from "@/components/base/page";
import model from "./model";
import { headers } from "./config";
import jPicker from "@/components/J-Picker/jPicker.vue";
import { session } from "@/store/modules/session";
import combox from "@/components/J-Picker/jPicker.vue";
import { forEach } from "lodash";
// @Component
@Component({
  components: {
    jPicker,
    combox,
  },
})
export default class RawReceiptDetail extends BasePage {
  /**
   * 页面Module
   */
  model = model;
  factoryCode = session.factoryCode;
  locationCode = "";
  candidates = ["库位1", "库位2", "库位3"];
  /**
   * 实际数量
   */
  number = "";
  wlCode: any = "";
  material = <any>{};
  list: any = [];
  LocationList: any = [];
  documentNo = "";
  /**
   * 表头
   */
  headers = headers;
  async onReady() {
    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.list.push(pickerName);
    });
    let ant: any = sessionStorage.getItem("mater");
    this.material = JSON.parse(ant);
    console.log("this.material", this.material);
    this.number = this.material.amount;
    sessionStorage.removeItem("mater");
    if (this.material.hasOwnProperty("wlList")) {
      this.material.wlList.forEach((item: any) => {
        let arr: object = {
          value: item.wlCode,
        };
        let obj: any = {
          value: arr,
          num: item.qty,
        };
        this.LocationList.push(obj as never);
      });
    }
  }
  confirm() {
    if (this.wlCode == "" || this.number == "") {
      uni.showToast({
        icon: "none",
        title: "请输入正确的库位和数量",
      });
      return;
    }
    if (parseFloat(this.number) <= 0) {
      uni.showToast({
        icon: "none",
        title: "请输入正确的库位和数量",
      });
      return;
    }
    if (this.LocationList.length != 0) {
      this.LocationList.forEach((item: any) => {
        if (item.value.sendSpot != this.wlCode.sendSpot) {
          uni.showToast({
            icon: "none",
            title: "请重新选择库位",
          });
          return;
        }
      });
      let num: number = 0;
      this.LocationList.forEach((item: any) => {
        num += parseFloat(item.num);
      });
      num += parseFloat(this.number);
      if (num > parseFloat(this.material.amount)) {
        uni.showToast({
          icon: "none",
          title: "实际数量不能大于需求数量",
        });
        return;
      }
      let arr: object = {
        value: this.wlCode,
        num: this.number,
      };
      this.LocationList.push(arr as never);
      this.material.currentAmount = num;
      this.number = "";
      return;
    }
    if (this.LocationList.length == 0) {
      if (this.number > this.material.amount) {
        uni.showToast({
          icon: "none",
          title: "实际数量不能大于需求数量",
        });
        return;
      }
      let arr: object = {
        value: this.wlCode,
        num: this.number,
      };
      this.LocationList.push(arr as never);
      this.material.currentAmount = this.number;
      this.number = "";
      return;
    }
  }
  deleteItem(index: any) {
    this.LocationList.splice(index, 1);
  }
  bookTypeChange(e: any) {
    this.wlCode = e.pickerName;
  }
  Reservoir() {
    let wlList: Array<string> = [];
    let currentAmount = 0;
    this.LocationList.forEach((item: any) => {
      let objString = JSON.stringify(this.material);
      let TEM = JSON.parse(objString);
      TEM.wlCode = item.value.label;
      TEM.qty = item.num;
      wlList.push(TEM);
      let pras = parseInt(item.num);
      currentAmount += pras;
    });
    this.material.wlList = wlList;
    this.material.currentAmount = currentAmount;
    sessionStorage.setItem("material", JSON.stringify(this.material));
    this.redirectTo(this.page.raw.handover.picking.order);
  }
}
</script>

<style lang="scss" scoped>
.page-receipt-detail {
  background: #f2f2f2
    linear-gradient(0deg, #f2f2f2 0%, #4a78ea 51%, #1753ea 100%) no-repeat;
  background-size: 100% 600rpx;
  padding: 30rpx;
  min-height: 100%;
  padding-top: 118rpx;

  .header {
    position: fixed;
    top: 36rpx;
    left: 0;
    right: 0;
    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%;
    height: 500rpx;

    .content-item {
      width: 100%;
      height: 100rpx;
      display: flex;
      text-align: center;
      line-height: 100rpx;
      background-color: white;
      .item-left {
        width: 30%;
      }
      .item-right {
        width: 70%;
      }
    }
  }
  .library {
    height: 60rpx;
    position: relative;
    padding-bottom: 10rpx;
    .search {
      position: absolute;
      width: 80%;
      right: 38rpx;
      top: 15rpx;
      height: 60rpx;
      background-color: white;
    }
    .title {
      width: 20%;
      height: 100%;
      text-align: left;
      position: absolute;
      line-height: 50rpx;
      top: 15rpx;
      left: 0;
    }
  }
  .library-num {
    position: relative;
    margin-top: 15rpx;
    .title {
      width: 100rpx;
      height: 100%;
      line-height: 50rpx;
    }
    input {
      display: block;
      width: 40%;
      padding: 10rpx;
      position: absolute;
      left: 100rpx;
      top: 0;
      background-color: white;
    }
    .confirm {
      position: absolute;
      top: 0;
      right: 20rpx;
      margin-left: 30rpx;
    }
  }
  .library-table {
    margin-top: 30rpx;
  }

  .table-wrapper {
    background-color: #fff;
  }

  .form {
    background-color: #fff;
    padding: 40rpx;
    border-radius: 10rpx;
  }

  .bottom-bar {
    z-index: 21;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;

    .container {
      background: #ffffff;
      box-shadow: 0 1rpx 20rpx 0 rgba(128, 128, 128, 0.2);
      padding: 20rpx;
    }

    .extra {
      margin: 5px;
      border-radius: 5px;
      padding: 0 10px;

      .main {
        margin-right: 0;
      }
    }
  }
}
</style>
