<template>
  <view class="page-picking-order">
    <view class="header">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="uni.navigateBack()" />
      </view>
      <view class="title">{{ $t('message.Pi_ByOrderPicking') }}</view>
      <view class="right"> </view>
    </view>
    <view class="table-wrapper">
      <wyb-table class="table" ref="table" width="100%" enable-check="single" show-left-and-right-border :headers="headers" :contents="model.proOrderResultList" :show-vert-border="false" @onCheck="handleRow"> </wyb-table>
    </view>
    <!-- 列表展示数据 -->
    <view class="Exhibition">
      <view class="Exhibition-left">
        <view class="Exhibition-left-title">{{ $t('message.Pi_Station') }}:</view>
        <view class="Exhibition-left-data">{{ some.sendSpot }}</view>
      </view>
      <view class="Exhibition-right">
        <view class="Exhibition-right-title">{{ $t('message.Pi_PickedQuantity') }}:</view>
        <view class="Exhibition-right-data">{{ some.totalMoAmount }}</view>
      </view>
    </view>
    <!-- 添加库位 -->
    <view class="library">
      <view class="library-left">
        <view>{{ $t('message.CommissionedLocation') }}:</view>
        <jPicker sureColor="#ff0000" style="width: 400rpx" @bindpicker="LocationChoice" showKey="value" valKey="value" :val="wlCode.value" class="search" :options="Location" />
      </view>
      <view class="single-left">
        <view>{{ $t('message.Container') }}</view>
        <u-search :placeholder="$t('message.PleaseScan')" v-model="form.XXXXX" :show-action="false"></u-search>
      </view>
      <view class="library-right">
        <view class="library-right-title">{{ $t('message.CommissionedThisNumber') }}:</view>
        <u-input v-model="qty" type="number" :border="border" :placeholder="$t('message.Summary_PleaseInputNumber')" class="input" />
        <view class="add">
          <u-button type="primary" @click="Add">{{ $t('message.product_add') }}</u-button>
        </view>
      </view>
    </view>
    <!-- 添加 -->
    <!-- <view class="add">
      <u-button type="primary" @click="Add">{{ $t('message.product_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.Container') }}</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.qty }}</u-td>
        <u-td class="u-td">{{ item.xxxx }}</u-td>
        <u-td class="u-td">
          <!-- <div class="u-td" @click="deleteItem(index)">
            {{ $t('message.product_Delete') }}
          </div> -->
          <u-button type="error" size="small" style="font-size: 20px" @click="deleteItem(index)">{{ $t('message.product_Delete') }}</u-button>
        </u-td>
      </u-tr>
    </u-table>
    <view class="bottom-bar">
      <div class="extra">
        <u-form ref="form" :model="form" :error-type="['toast']">
          <u-row class="bottom-info"> </u-row>
        </u-form>
      </div>
      <div class="container">
        <u-row>
          <u-col :span="4">
            <u-button type="primary" class="appoint" @click="bill">{{ $t('message.detailed') }}</u-button>
          </u-col>
          <u-col :span="4">
            <u-button type="success" @click="onSubmit">
              {{ $t('message.po_Submit') }}
            </u-button>
          </u-col>
          <u-col :span="4">
            <u-button type="error" @click="uni.navigateBack()">
              {{ $t('message.po_Return') }}
            </u-button>
          </u-col>
        </u-row>
      </div>
    </view>
  </view>
</template>
<script lang="ts">
import { Component, Ref } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import model from './model';
import { orderHeaders } from './config';
import { session } from '@/store/modules/session';
import { VForm, VFormRules } from 'vue/types/form';
import jPicker from '@/components/J-Picker/jPicker.vue';
@Component({
  components: {
    jPicker,
  },
})
export default class RawReceiptDetail extends BasePage {
  /**
   * 表单引用
   */
  @Ref('form') readonly $form!: VForm;
  @Ref('submitForm') readonly $submitForm!: VForm;
  submitForm = {
    receiver: '',
    receiverName: '',
    password: '',
  };
  /**
   * 页面Module
   */
  model = model;
  type = 'text';
  wlCode: any = '';
  LOCKProOrderResultListParams: any = '';
  border = true;
  //添加库位和数量的列表
  LocationList: any = [];
  //库位
  Location: any = [];
  someIndex = 0;
  some: any = {};
  qty: any = '';
  /**
   * 表单数据
   */
  form: any = {
    // locationCode: null,
    amount: null,
  };
  /**
   * 表单校验
   */
  rules: VFormRules<any> = {
    // locationCode: [{ required: true, message: '请选择库存地点！' }],
    amount: [
      { required: true, message: this.$t('message.Pi_tip1') as string },
      // { type: 'integer', min: 1, transform: Number, message: this.$t('message.Pi_tip2') as string },
    ],
  };

  /**
   * 库存地点选择
   */
  locationCodeSelect = false;

  /**
   * 表头
   */
  headers = orderHeaders;
  LocationChoice(e: any) {
    console.log('e>>>>>>>>>>>>>>>>>>>>>>>>', e);
    this.wlCode = e.pickerName;
  }
  async handleRow({ data }: any) {
    console.log('data[0]', data[0]);
    if (!this.model.proOrderResultList[data[0].index].sendSpot) {
      uni.showModal({
        content: this.$t('message.SAPLocation') as any,
        success: (res) => {
          if (res.confirm) {
            this.model.proOrderResultList[data[0].index].checked = false;
          }
        },
      });
      return;
    }
    this.someIndex = data[0].index;
    this.some = data[0].lineData;
    this.model.proOrderResultList[this.someIndex].wlList = [];
    this.model.proOrderResultList[this.someIndex].currentAmount = 0;
    this.LocationList = [];
    this.qty = '';
    this.form.xxxx = '';
    //根据选择一行数据查询库位
    await this.model.queryItemLoc({
      workArea: session.workareaCode,
      loginName: session.loginName,
      sendSpot: null,
      materialCode: this.some.materialCode,
      factoryCode: session.factoryCode,
    });
    this.Location = [];
    if (this.model.LocList.length == 0) {
      uni.showModal({
        content: this.$t('message.selected') as any,
        showCancel: false,
        confirmText: this.$t('message.workArea_Confirm') as any,
      });
    } else {
      this.Location = this.model.LocList;
      console.log('this.Location', this.Location);
      this.wlCode = this.Location[0];
    }
  }
  Add() {
    if (this.qty == '' || this.wlCode == '') {
      this.customToast(this.$t('message.Commission_tips2') as any);
      return;
    }
    if (parseFloat(this.qty) <= 0) {
      this.customToast(this.$t('message.Commission_tips3') as any);
      return;
    }
    if (JSON.stringify(this.some) == '{}') {
      this.customToast(this.$t('message.Commission_tips6') as any);
      return;
    }
    let isTrue = true;
    if (this.LocationList.length != 0) {
      this.LocationList.forEach((item: any) => {
        if (this.wlCode.sendSpot != item.Code || this.wlCode.label === item.wlCode) {
          this.customToast(this.$t('message.Commission_tips7') as any);
          return (isTrue = false);
        }
      });
    }
    if (isTrue == true) {
      if (parseFloat(this.qty) + parseFloat(this.some.totalMoAmount) > parseFloat(this.some.amount)) {
        uni.showModal({
          title: 'Tip',
          content: this.$t('message.Pi_tip11') as string,
          confirmText: this.$t('message.workArea_Confirm') as string,
          cancelText: this.$t('message.Cancel') as string,
          showCancel: true,
          success: (res) => {
            if (res.confirm) {
              let arr = {
                wlCode: this.wlCode.label,
                qty: this.qty,
                Code: this.wlCode.sendSpot,
                xxxx: this.form.xxxx,
              };
              this.LocationList.push(arr);
              let objString = JSON.stringify(this.some);
              let TEM = JSON.parse(objString);
              TEM.wlList = null;
              TEM.wlCode = this.wlCode.label;
              TEM.qty = this.qty;
              TEM.xxxx = this.form.xxxx;
              //add之后有什么操作
              this.model.proOrderResultList[this.someIndex].wlList.push(TEM);
              this.model.proOrderResultList[this.someIndex].currentAmount += parseFloat(this.qty);
              this.qty = '';
              this.form.xxxx = '';
            }
          },
        });
      } else {
        let arr = {
          wlCode: this.wlCode.label,
          qty: this.qty,
          Code: this.wlCode.sendSpot,
          xxxx: this.form.xxxx,
        };
        this.LocationList.push(arr);
        let objString = JSON.stringify(this.some);
        let TEM = JSON.parse(objString);
        TEM.wlList = null;
        TEM.wlCode = this.wlCode.label;
        TEM.qty = this.qty;
        TEM.xxxx = this.form.xxxx;
        this.model.proOrderResultList[this.someIndex].wlList.push(TEM);
        this.model.proOrderResultList[this.someIndex].currentAmount += parseFloat(this.qty);
        this.qty = '';
        this.form.xxxx = '';
      }
    }
  }
  deleteItem(index: any) {
    this.LocationList.splice(index, 1);
    this.model.proOrderResultList[this.someIndex].wlList.splice(index, 1);
    this.business();
  }
  business() {
    let num = 0;
    this.LocationList.forEach((item: any) => {
      num += parseFloat(item.qty);
    });
    this.model.proOrderResultList[this.someIndex].currentAmount = num;
  }
  async onReady() {
    await this.onAggregate();
    // await this.GetLocation();
  }
  async bill() {
    if (this.some == {} || this.some.prdOrder == null) {
      this.customToast(this.$t('message.Commission_tips6') as any);
      return;
    }
    let person = JSON.parse(uni.getStorageSync('__GWMS_APP_STATE_DATA__') as any);
    let content = {
      loginName: person.session.user.loginName,
      prdOrder: this.some.prdOrder,
    };
    await this.model.querydetaildlist(content);
    this.redirectTo(this.page.raw.handover.RUSpicking.Local);
  }
  /**
   * 提交
   */
  async onSubmit() {
    const dataList: any = [];
    this.model.proOrderResultList.forEach((item: any) => {
      if (item.checked == true) {
        dataList.push(item);
      }
    });
    if (dataList.length == 0) {
      this.customToast(this.$t('message.Warehouse_Tip6') as any);
      return;
    }
    await this.model.saveProOrderResultUpload(dataList);
    if (this.model.code == '1') {
      uni.showToast({
        //icon: 'success',
        title: this.$t('message.Warehouse_Tip9') as any,
        duration: 2000,
        image: '/static/icons/icon-51.png',
      });
      // this.GetLocation();
      setTimeout(() => {
        this.onAggregate();
      }, 2000);
    }
  }
  async onAggregate() {
    await this.model.queryProOrder(this.model.formParams);
    await this.model.queryProOrderResult(this.model.NEWparams);
    if (this.model.proOrderResultList.length == 0) {
      this.some = {};
      this.LocationList = [];
      this.wlCode = '';
      return;
    }
    this.LOCKProOrderResultListParams = this.model.proOrderResultList;
    await this.model.lockProOrderResult(this.model.proOrderResultList);
    this.some = {};
    this.LocationList = [];
    this.wlCode = '';
  }
  async GetLocation() {
    this.Location = [];
    let means: any = JSON.parse(uni.getStorageSync('__GWMS_APP_STATE_DATA__') as any);
    let content = {
      loginName: means.session.user.loginName,
      sendSpot: null,
      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);
    });
  }
  beforeDestroy() {
    this.unlock();
  }
  onHide() {
    this.unlock();
  }
  async unlock() {
    await this.model.unlockProOrderResult(this.LOCKProOrderResultListParams);
  }
}
</script>

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

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

  .table-wrapper {
    background-color: #fff;
    height: 500rpx;
    overflow-x: auto;
  }

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

  .Exhibition {
    width: 100%;
    height: 100rpx;
    margin-top: 20rpx;
    display: flex;

    .Exhibition-left {
      width: 50%;
      height: 100%;
      display: flex;

      .Exhibition-left-title {
        width: 50%;
        height: 100%;
        text-align: center;
        line-height: 100rpx;
      }

      .Exhibition-left-data {
        width: 50%;
        height: 100%;
        text-align: center;
        line-height: 100rpx;
      }
    }

    .Exhibition-right {
      width: 50%;
      height: 100%;
      display: flex;

      .Exhibition-right-title {
        width: 50%;
        height: 100%;
        // text-align: center;
        line-height: 100rpx;
      }

      .Exhibition-right-data {
        width: 50%;
        height: 100%;
        text-align: center;
        line-height: 100rpx;
      }
    }
  }

  .library {
    width: 100%;
    height: 200rpx;
    // display: flex;
    display: block;

    .library-left {
      width: 100%;
      height: 100rpx;
      padding-left: 36rpx;
      // display: flex;
      // view {
      //   width: 230rpx;
      //   line-height: 100rpx;
      //   text-align: center;
      // }
    }
    .single-left {
      width: 100%;
      height: 100rpx;
      display: flex;

      view {
        width: 110rpx;
        height: 100rpx;
        line-height: 100rpx;
      }
    }
    .library-right {
      width: 100%;
      height: 100rpx;
      padding-left: 36rpx;
      display: flex;

      .library-right-title {
        width: 120rpx;
        height: 100%;
        line-height: 100rpx;
      }

      .input {
        width: 200rpx;
        height: 70rpx;
        margin-top: 15rpx;
        margin-left: 4rpx;
      }

      .add {
        width: 200rpx;
        height: 100rpx;
        position: relative;

        button {
          position: absolute;
          top: 10rpx;
          right: 20rpx;
        }
      }
    }
  }

  .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 {
      background-color: #fff;
      margin: 5px;
      border-radius: 5px;
      padding: 0 10px;
    }

    .bottom-info {
      .u-form-item {
        padding: 0;
      }
    }
  }
}
</style>
