<template>
  <view class="page-kan-dan-index">
    <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.RowRelocation') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :error-type="['toast']" label-width="150rpx">
      <!-- 单号查询 -->
      <u-form-item :required="true" :label="$t('message.InventoryOrderNo')" prop="orderNo">
        <u-search :placeholder="$t('message.Commission_tips1')" v-model.trim="order3" @search="query" :show-action="false"></u-search>
        <view class="single-right">
          <u-button type="primary" @click="query">{{ $t('message.Query') }}</u-button>
        </view>
      </u-form-item>
      <!-- 物料号选择 -->
      <u-form-item :required="true" :label="$t('message.CommissionedMaterielNo')">
        <jPicker sureColor="#ff0000" style="width: 230rpx" @bindpicker="materialConfirm" showKey="value" valKey="value" :val="orderlist.productCode" class="search" :options="model.materielList" />
      </u-form-item>
      <!-- 物料描述 -->
      <u-form-item :label="$t('message.po_MaterielDes')">
        <u-input :disabled="true" v-model="orderlist.productDescZh" placeholder="" style="overflow: hidden" />
      </u-form-item>
      <!-- 出库库位 -->
      <u-form-item :label="$t('message.Outbound')">
        <u-input :disabled="true" v-model="orderlist.originWl" placeholder="" />
      </u-form-item>
      <!-- 入库库位 -->
      <u-form-item :label="$t('message.Warehousing')">
        <u-input :disabled="true" v-model="orderlist.aimWl" placeholder="" />
      </u-form-item>
      <view class="newly">
        <!-- 需求数 -->
        <u-form-item :label="$t('message.DemandQuantity')" style="width: 50%">
          <u-input style="flex: 0.9" v-model="orderlist.qty" placeholder=" " :disabled="true" />
        </u-form-item>
        <!-- 累计 -->
        <u-col :span="6">
          <u-form-item :label="$t('message.Cumulative')" prop="sapFactoryCode">
            <u-input style="flex: 0.9" :disabled="true" v-model="orderlist.actualQty" placeholder="" />
          </u-form-item>
        </u-col>
      </view>
      <!-- 容器编码 -->
      <view class="single">
        <view class="single-left">
          <view>{{ $t('message.Container') }}:</view>
          <u-search :placeholder="$t('message.PleaseScan')" style="z-index: 10000" v-model="Barcode" @search="searchBarcode" :show-action="false"></u-search>
        </view>
        <view class="single-right">
          <u-button type="primary" @click="Add">{{ $t('message.product_add') }}</u-button>
        </view>
      </view>
      <!-- 添加 -->
      <!-- <view class="add">
        <u-button type="primary" @click="Add">{{ $t('message.product_add') }}</u-button>
      </view> -->
      <!-- 本次数量 -->
      <u-form-item :label="$t('message.CommissionedThisNumber')">
        <u-input :disabled="true" v-model="total" placeholder="" />
      </u-form-item>
    </u-form>
    <wyb-table class="table" ref="table" width="100%" show-left-and-right-border :headers="RUSdetailHeader" :contents="SubmitOrderlist" :show-vert-border="false" @onCellClick="deleteItem($event)"></wyb-table>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <!-- 确定 -->
        <u-col :span="6">
          <u-button type="primary" @click="submit">{{ $t('message.product_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 { RUSdetailHeader } from './config';
import { Component } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import model from './model';
import { session } from '@/store/modules/session';
import jPicker from '@/components/J-Picker/jPicker.vue';
@Component({
  components: {
    jPicker,
  },
})
export default class KanDanHandOver extends BasePage {
  RUSdetailHeader = RUSdetailHeader;
  order3 = '';
  model = model;
  border = true;
  orderlist: any = [];
  orderlistIndex = 0;
  Barcode: any = '';
  SubmitOrderlist: any = [];
  Container: any = '';
  async query() {
    if (this.order3 === '') {
      this.customToast(this.$t('message.Warehouse_Tip') as string);
      return;
    } else {
      await this.model.takeYKOrder({
        order3: this.order3,
      });
      if (model.code == '1') {
        uni.showToast({
          title: this.$t('message.successful') as string,
          duration: 2000,
          image: '/static/icons/icon-51.png',
        });
        this.orderlist = model.materielList[0];
      }
    }
  }
  materialConfirm(v: any) {
    this.orderlist.productCode = v.pickerName.productCode;
    model.materielList.forEach((item: any, index: any) => {
      if (this.orderlist.productCode == item.productCode) {
        this.orderlist = item;
        this.orderlistIndex = index;
      }
    });
  }
  total = 0;
  async searchBarcode() {
    if (this.Barcode == '') {
      this.customToast(this.$t('message.barcode') as string);
      return;
    }
    let params = {
      productCode: this.orderlist.productCode,
      barcode: this.Barcode,
      locCode: this.orderlist.originWl,
    };
    await this.model.searchBarcode(params);
    this.Container = this.model.ContainerCode;
    if (this.Container.cpRef4 == null) {
      this.total = this.Container.qty;
    } else {
      this.total = this.Container.qty - this.Container.cpRef4;
    }
  }
  Add() {
    if (this.Container == '' || !this.Container.barcode) {
      this.customToast(this.$t('message.scanningContainer') as string);
      return;
    }
    if (this.orderlist.length == 0) {
      this.customToast(this.$t('message.Commission_tips6') as string);
      return;
    }
    if (parseFloat(this.orderlist.actualQty) + parseFloat(this.Container.qty) > parseFloat(this.orderlist.qty)) {
      this.customToast(this.$t('message.Commission_tips4') as string);
      return;
    }
    this.SubmitOrderlist.push(JSON.parse(JSON.stringify(this.orderlist)));
    this.SubmitOrderlist[this.SubmitOrderlist.length - 1].type = '1';
    this.SubmitOrderlist[this.SubmitOrderlist.length - 1].keepBy = session.loginName;
    this.SubmitOrderlist[this.SubmitOrderlist.length - 1].orderType = '0';
    this.SubmitOrderlist[this.SubmitOrderlist.length - 1].barCode = this.Container.barcode;
    this.SubmitOrderlist[this.SubmitOrderlist.length - 1].qty = this.total;
    // if (this.Container.cpRef4 == null) {
    //   this.SubmitOrderlist[this.SubmitOrderlist.length - 1].qty = parseFloat(this.Container.qty);
    // } else {
    //   this.SubmitOrderlist[this.SubmitOrderlist.length - 1].qty = parseFloat(this.Container.qty) - parseFloat(this.Container.cpRef4);
    // }
    this.model.materielList[this.orderlistIndex].actualQty += parseFloat(this.SubmitOrderlist[this.SubmitOrderlist.length - 1].qty);
    this.Barcode = '';
    this.Container = '';
  }
  deleteItem(e: any) {
    uni.showModal({
      content: this.$t('message.product_Delete') as string,
      cancelText: this.$t('message.Cancel') as string,
      confirmText: this.$t('message.workArea_Confirm') as string,
      success: (res) => {
        if (res.confirm) {
          this.model.materielList.forEach((item: any) => {
            if (item.productCode == e.lineData.productCode) {
              item.actualQty -= parseFloat(e.lineData.qty);
            }
          });
          this.SubmitOrderlist.splice(e.contentIndex, 1);
        } else if (res.cancel) {
          return;
        }
      },
    });
  }
  //提交数据
  async submit() {
    if (this.SubmitOrderlist.length == 0) {
      this.customToast(this.$t('message.Pi_NoData') as string);
      return;
    }
    await model.onTakeoutConfirm(this.SubmitOrderlist);
    if (model.code == '1') {
      uni.showToast({
        title: this.$t('message.Warehouse_Tip9') as string,
        duration: 2000,
        image: '/static/icons/icon-51.png',
      });
      this.orderlist = {};
      this.model.materielList.length = 0;
      this.SubmitOrderlist = [];
      this.Barcode = '';
      this.Container = '';
      this.total = 0;
      this.query();
    }
  }
}
</script>
<style lang="scss" scoped>
.page-kan-dan-index {
  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: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    height: 67px;
    line-height: 88rpx;
    color: #fff;
    font-size: 34rpx;
    font-weight: 500;
    text-align: center;
    padding-top: 19px;
    .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;
          z-index: 10000;
        }
      }
      .single-right {
        button {
          margin-top: 15rpx;
          width: 150rpx;
          height: 70rpx;
        }
      }
    }
    .add {
      width: 100%;
      height: 100rpx;
      position: absolute;
      top: 493px;
      left: 0;
      button {
        position: absolute;
        top: 3rpx;
        right: 35rpx;
        z-index: 10000;
      }
    }
    .newly {
      display: flex;
    }
  }
  .single-right {
    button {
      //margin-top: 15rpx;
      width: 110rpx;
      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>
