<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.spSo') }}</view>
      <view class="right"></view>
    </view>
    <view class="content">
      <!-- 单号 -->
      <view class="single">
        <view class="single-left">
          <view>{{ $t('message.CommissionedSingleNumber') }}</view>
          <u-search :placeholder="$t('message.InventoryPleaseScan')" v-model.trim="form.documentNo" @search="query" :show-action="false"></u-search>
        </view>
        <view class="single-right">
          <u-button type="primary" @click="query">{{ $t('message.Query') }}</u-button>
        </view>
      </view>
      <!-- 物料 -->
      <view class="material">
        <view class="material-left">
          <view>{{ $t('message.CommissionedMaterielNo') }}</view>
          <jPicker sureColor="#ff0000" style="width: 260rpx" @bindpicker="materialChoice" showKey="value" valKey="value" :val="every.materialCode" class="search" :options="materialList" />
        </view>
      </view>
      <view class="material">
        <view class="material-right">
          <view class="material-right-title">{{ $t('message.CommissionedMaterielDesc') }}</view>
          <view class="material-right-code" style="overflow: hidden">{{ every.materialDesc }}</view>
        </view>
      </view>
      <!-- 需求数量 -->
      <view class="number">
        <view class="number-left">
          <view class="number-left-title">{{ $t('message.CommissionedDemandData') }}</view>
          <u-input v-model="every.orderAmount" placeholder=" " :type="type" :border="border" class="input" disabled style="padding: '0'; flex: 0.9" />
        </view>
        <view class="number-right">
          <view class="number-right-title">{{ $t('message.Cumulative') }}</view>
          <u-input placeholder=" " v-model="every.outAmount" :type="type" :border="border" class="input" style="padding: '0'" disabled />
        </view>
      </view>
      <!-- 条码 -->
      <!-- <view class="single"> -->
      <!-- <view class="single-left">
          <view>{{ $t('message.product_barCode') }}:</view>
          <u-search :placeholder="$t('message.PleaseScan')" :focus="isfocus" 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="library">
        <view class="library-left">
          <view>{{ $t('message.CommissionedLocation') }}</view>
          <jPicker sureColor="#ff0000" style="width: 230rpx" @bindpicker="LocationChoice" showKey="value" valKey="value" :val="every.userDefined1" class="search" :options="Loc" />
        </view>
        <view class="library-right">
          <view class="library-right-title">{{ $t('message.CommissionedThisNumber') }}</view>
          <u-input :placeholder="$t('message.po_PleaseInput')" v-model="nowAmount" type="number" :border="border" class="input" />
        </view>
      </view>
      <!-- 添加 -->
      <view class="add">
        <u-button type="primary" @click="Add">{{ $t('message.product_add') }}</u-button>
      </view>
      <!-- 表格 -->
      <wyb-table :style="{ marginTop: '60px' }" class="table" ref="table" width="100%" show-left-and-right-border :headers="detailHeader" :contents="list" :show-vert-border="false" @onCellClick="deleteItem($event)"></wyb-table>
    </view>
    <!-- 底部按钮 -->
    <view class="bottom-bar">
      <u-row class="button-bar">
        <!-- <u-col :span="4">
          <u-button type="primary" @click="bill">{{ $t('message.detailed') }}</u-button>
        </u-col> -->
        <u-col :span="6">
          <u-button type="primary" @click="onSubmit">{{ $t('message.product_Upload') }}</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 } 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 { detailHeader } from './config';
import model from './model';
@Component({
  components: {
    jPicker,
  },
})
export default class SpSo extends BasePage {
  model = model;
  detailHeader = detailHeader;
  disabled = false;
  isfocus = false;
  form: any = {
    documentNo: '',
  };
  material: any = {};
  materialList: any = [];
  list: any = [];
  every: any = '';
  value = '';
  type = 'text';
  border = true;
  nowAmount: any = '';
  //库位
  Loc: any = [];
  //所选择的库位
  wlCode: any = '';
  materialIndex: any = null;
  //Barcode: any = '';
  materialCode: any = '';
  Container: any = '';
  async QueryLoc() {
    await this.model.queryItemLoc({
      workArea: session.workareaCode,
      loginName: session.loginName,
      sendSpot: null,
      materialCode: this.every.materialCode,
      factoryCode: session.factoryCode,
    });
    this.Loc = [];
    this.wlCode = '';
    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.Loc = this.model.LocList;
      this.wlCode = this.Loc[0];
    }
  }
  empty() {
    this.list = [];
    this.wlCode = '';
    this.nowAmount = '';
    this.every = '';
    this.materialList = [];
    this.Loc = [];
    //this.Barcode = '';
    this.materialCode = '';
    this.Container = '';
  }
  //输入单号 查询数据
  async query() {
    this.list = [];
    //this.wlCode = '';
    this.nowAmount = '';
    this.every = '';
    if (this.form.documentNo == '') {
      this.customToast(this.$t('message.Commission_tips1') as any);
      return;
    }
    let res = await this.model.queryOutsourcing(this.form.documentNo);
    this.material = res.data;
    this.materialList = [];
    this.Loc = [];
    this.material.forEach((item: any) => {
      let pickerName: any = {};
      let userDefined1List: any = {};
      pickerName.label = item.materialCode;
      pickerName.value = item.materialCode;
      userDefined1List.label = item.userDefined1;
      userDefined1List.value = item.userDefined1;
      this.materialList.push(pickerName);
      this.Loc.push(userDefined1List);
    });
    //this.wlCode = this.Loc[0];
    this.every = this.material[0];
    this.materialIndex = 0;
    // await this.QueryLoc();
  }
  materialChoice(e: any) {
    this.nowAmount = '';
    this.material.forEach(async (item: any, index: any) => {
      if (item.materialCode == e.pickerName.value) {
        this.every = item;
        this.materialIndex = index;
        // await this.QueryLoc();
        return;
      }
    });
  }
  LocationChoice(e: any) {
    this.wlCode = e.pickerName;
  }
  // async searchBarcode() {
  //   if (this.Barcode == '') {
  //     this.customToast(this.$t('message.barcode') as string);
  //     return;
  //   }
  //   this.Loc = [];
  //   this.Container = '';
  //   this.nowAmount = '';
  //   //this.disabled = false;
  //   let params = {
  //     productCode: this.every.materialCode,
  //     barcode: this.Barcode,
  //   };
  //   await this.model.searchBarcode(params);
  //   this.Container = this.model.ContainerCode;
  //   this.Loc.push({
  //     value: this.Container.locCode,
  //     label: this.Container.locCode + '(' + this.Container.locCodesendSpot + ')',
  //   });
  //   this.wlCode = this.Loc[0];
  //   // this.nowAmount = 1;
  //   if (this.Container.cpRef4 == null) {
  //     this.nowAmount = parseFloat(this.Container.qty);
  //     //this.disabled = true;
  //   } else {
  //     this.nowAmount = parseFloat(this.Container.qty) - parseFloat(this.Container.cpRef4);
  //     //this.disabled = true;
  //   }
  // }

  //点击添加
  Add() {
    if (this.Container.cpRef4 == null) {
      if (parseFloat(this.nowAmount) > this.Container.qty) {
        this.customToast(this.$t('message.AppendMateriel3') as any);
        console.log('1');
        return;
      }
    } else {
      if (parseFloat(this.nowAmount) > this.Container.qty - parseFloat(this.Container.cpRef4)) {
        this.customToast(this.$t('message.AppendMateriel3') as any);
        console.log('2');
        return;
      }
    }
    if (!this.every) {
      this.customToast(this.$t('message.Commission_tips6') as any);
      return;
    }
    if (parseFloat(this.nowAmount) <= 0 || !this.nowAmount) {
      this.customToast(this.$t('message.Commission_tips3') as any);
      return;
    }
    let isTrue = true;
    console.log(this.every.materialCode + '：12');
    if (this.list.length != 0) {
      this.list.forEach((item: any) => {
        console.log(item.materialCode + ':::' + this.every.materialCode);
        if (item.materialCode == this.every.materialCode) {
          return (isTrue = false);
        }
      });
    }
    if (isTrue != true) {
      this.customToast(this.$t('message.repeatedly') as any);
      this.Container = '';
      //this.Barcode = '';
      this.materialCode = '';
      return;
    }
    let num: number = parseFloat(this.nowAmount) + parseFloat(this.every.outAmount);
    if (num > parseFloat(this.every.orderAmount)) {
      this.customToast(this.$t('message.AppendMateriel3') as any);
      console.log('5');
      return;
    } else {
      let arr = {
        loginName: this.session.loginName,
        factoryCode: this.session.factoryCode,
        nxOutCode: this.form.documentNo,
        rowItem: this.every.rowItem,
        materialCode: this.every.materialCode,
        materialDesc: this.every.materialDesc,
        locCode: this.every.userDefined1,
        //barcode: this.Container.barcode,
        qty: this.nowAmount,
        proOutType: 'sp',
      };
      this.list.push(arr);
      this.material[this.materialIndex].outAmount += parseFloat(this.list[this.list.length - 1].qty);
      this.nowAmount = '';
      //this.Barcode = '';
      this.materialCode = '';
      this.Container = '';
      this.isfocus = false;
      this.$nextTick(() => {
        this.isfocus = true;
      });
      //this.disabled = false;
    }
  }
  deleteItem(e: any) {
    uni.showModal({
      content: this.$t('message.product_Delete') as any,
      cancelText: this.$t('message.Cancel') as any,
      confirmText: this.$t('message.workArea_Confirm') as any,
      success: (res) => {
        if (res.confirm) {
          this.list.splice(e.contentIndex, 1);
          let i: any;
          this.material.forEach((item: any, index: any) => {
            if (item.materialCode == e.lineData.materialCode) {
              i = index;
            }
          });
          this.material[i].outAmount -= parseFloat(e.lineData.qty);
        } else if (res.cancel) {
          return;
        }
      },
    });
  }
  async onSubmit() {
    if (this.list == null || this.list.length == 0) {
      this.customToast(this.$t('message.Pi_NoData') as any);
      return;
    }
    await this.model.submitOutsourcing(this.list);
    this.empty();
    await this.query();
  }
  async bill() {
    if (this.form.documentNo == '') {
      this.customToast(this.$t('message._tips6') as any);
      return;
    }
    let content = {
      loginName: session.loginName,
      order1: this.form.documentNo,
      factoryCode: session.factoryCode,
    };
    await this.model.querydetaildlist(content);
    this.toPage(this.page.raw.so.spSo.detail);
  }
}
</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: 130rpx;
          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: 100%;
        height: 100%;
        display: flex;
        view {
          width: 140rpx;
          line-height: 100rpx;
        }
        .search {
          padding-left: 9px;
        }
      }
    }
    .material {
      width: 100%;
      height: 100rpx;
      display: flex;
      .material-right {
        width: 100%;
        height: 100%;
        display: flex;
        .material-right-title {
          width: 140rpx;
          height: 100%;
          line-height: 100rpx;
        }
        .material-right-code {
          width: 80%;
          height: 100%;
          line-height: 100rpx;
          text-align: left;
          padding-left: 10px;
          // 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;
          text-align: center;
        }
        .input {
          width: 200rpx;
          height: 70rpx;
          margin-top: 15rpx;
          margin-left: 30rpx;
        }
      }
    }
    .library {
      width: 100%;
      height: 100rpx;
      display: flex;
      .library-left {
        width: 50%;
        height: 100%;
        display: flex;
        view {
          width: 70rpx;
          line-height: 100rpx;
        }
        .search {
          padding-left: 1px;
          text-align: center;
          white-space: nowrap;
          overflow: scroll;
        }
      }
      .library-right {
        width: 50%;
        height: 100%;
        display: flex;
        .library-right-title {
          width: 120rpx;
          height: 100%;
          line-height: 100rpx;
          text-align: center;
        }
        .input {
          width: 200rpx;
          height: 70rpx;
          margin-top: 15rpx;
          margin-left: 30rpx;
        }
      }
    }
    .add {
      width: 100%;
      height: 100rpx;
      top: 0px;
      left: 0;
      float: right;
      width: 100px;
      button {
        top: 10rpx;
        right: 15rpx;
        z-index: 10000;
      }
    }
  }
  .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;
  }
}
.class {
  margin-top: 50px !important;
}
</style>
