<template>
  <view class="page-receipt-detail">
    <view class="header">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="gohome" />
      </view>
      <view class="title">{{ $t('message.dn_Detailed') }}</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.orderInInfoList" :show-vert-border="false"></wyb-table>
    </view>
    <view class="bottom-bar">
      <!-- <div>
        <u-button type="primary" class="appoint" size="medium" @click="appoint"
          >指定库位</u-button
        >
      </div> -->
      <div class="container">
        <u-row>
          <u-col :span="3">
            <u-button type="warning" @click="model.checkAllOrderInInfoList(!model.isCheckedAll)">
              {{ model.isCheckedAll ? this.$t('message.po_noSelectAll') : this.$t('message.po_SelectAll') }}
            </u-button>
          </u-col>
          <u-col :span="3">
            <u-button type="primary" class="appoint" @click="appoint">{{ $t('message.Pi_distribution') }}</u-button>
          </u-col>
          <u-col :span="3">
            <u-button type="success" :loading="$wait.is('*submitOrderInEnter')" @click="onSubmit">
              {{ $t('message.po_Submit') }}
            </u-button>
          </u-col>
          <u-col :span="3">
            <u-button type="error" @click="gohome">{{ $t('message.po_Return') }}</u-button>
          </u-col>
        </u-row>
      </div>
    </view>
  </view>
</template>

<script lang="ts">
// inventory
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 { truncate } from 'lodash';
// @Component
@Component({
  components: {
    jPicker,
  },
})
export default class RawReceiptDetail extends BasePage {
  /**
   * 页面Module
   */
  model = model;
  factoryCode = session.factoryCode;
  locationCode = '';
  /**
   * 实际数量
   */
  number = '';
  /**
   * 表头
   */
  headers = headers;
  dnNo = '';
  onShow() {
    this.model.queryLocation();
    let ant: any = JSON.parse(sessionStorage.getItem('material') as string);
    let index: any = JSON.parse(sessionStorage.getItem('index') as string);
    this.model.orderInInfoList[index] = ant;
    this.dnNo = JSON.parse(sessionStorage.getItem('dnNo') as string);
    sessionStorage.removeItem('material');
    sessionStorage.removeItem('index');
  }
  gohome() {
    this.toPage(this.page.raw.ingoods.dnReceipt.index);
    sessionStorage.removeItem('material');
    sessionStorage.removeItem('index');
    sessionStorage.removeItem('dnNo');
  }
  bookTypeChange(e: any) {
    this.locationCode = e.pickerName.value;
  }
  /**
   * 逐条修改
   */
  changeNumber() {
    if (!this.model.hasChecked) {
      this.customToast(this.$t('message.dn_Select') as string);
      return;
    }
    //改变列表的值
    this.model.orderInInfoList.forEach((item: any) => {
      if (item.checked) {
        if (this.locationCode) {
          item.location = this.locationCode;
        }
        if (this.number > item.requestAmount) {
          this.customToast(this.$t('message.dn_Tip') as string);
          return;
        } else if (this.number == '') {
          item.receiptAmount = item.requestAmount;
        } else if (this.number < item.requestAmount) {
          this.customToast(this.$t('message.dn_Tip3') as string);
          item.receiptAmount = this.number;
        } else {
          item.receiptAmount = this.number;
        }
      }
    });
  }
  appoint() {
    let selectStatus = model.orderInInfoList.map((item) => {
      if (item.checked == true) {
        return true;
      } else {
        return false;
      }
    });
    let i: number = 0;
    selectStatus.forEach((item) => {
      if (item == true) {
        i++;
      }
    });
    if (i > 1) {
      this.customToast(this.$t('message.Pi_tip16') as string);
    } else if (i == 0) {
      this.customToast(this.$t('message.Pi_tip17') as string);
    } else {
      let mater: number = selectStatus.indexOf(true);
      sessionStorage.setItem('mater', JSON.stringify(model.orderInInfoList[mater]) as string);
      sessionStorage.setItem('index', JSON.stringify(mater) as string);
      this.redirectTo(this.page.raw.ingoods.dnReceipt.idetailed);
    }
  }
  /**
   * 提交
   */
  async onSubmit() {
    let selectStatus = model.orderInInfoList.map((item) => {
      if (item.checked == true) {
        return true;
      } else {
        return false;
      }
    });

    // let LocStatus = model.orderInInfoList.map((item) => {
    //   if (
    //     item.checked == true &&
    //     (item.location == null || item.location == "")
    //   ) {
    //     return true;
    //   } else {
    //     return false;
    //   }
    // });
    // if (LocStatus[0]) {
    //   uni.showToast({
    //     icon: "none",
    //     title: this.$t("message.Warehouse_Tip2") as string,
    //   });
    //   return;
    // }
    await this.model.submitOrderInEnter();
    this.locationCode = '';
    // setTimeout(() => {
    //   uni.navigateBack({ delta: 1 });
    // }, 2000);
    this.model.orderInInfo.carNo = '';
    this.model.orderInInfo.bookingTime = '';
    this.model.orderInInfo.supplyCode = '';
    this.model.orderInInfo.poType = '';
    this.model.orderInInfo.location = '';
    this.model.queryOrderInInfo(this.dnNo);
    // if (selectStatus[0]) {
    //   await this.model.submitOrderInEnter();
    //   this.locationCode = "";
    //   setTimeout(() => {
    //     uni.navigateBack({ delta: 1 });
    //   }, 2000);
    //   this.model.orderInInfo.carNo = "";
    //   this.model.orderInInfo.bookingTime = "";
    //   this.model.orderInInfo.supplyCode = "";
    //   this.model.orderInInfo.poType = "";
    //   this.model.orderInInfo.location = "";
    // } else {
    //   uni.showToast({
    //     icon: "none",
    //     title: this.$t("message.product_Tip2") as string,
    //   });
    //   return;
    // }
  }
}
</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;
    }
  }

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