<template>
  <view class="page-raw-returning">
    <view class="header" :style="{ backgroundColor: `rgba(23, 83, 234, ${scrollTop / 100})` }">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="back" />
      </view>
      <view class="title">{{ $t('message.ReturnedMaterials') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" :model="form" :error-type="['toast']" label-width="150rpx">
      <!-- 订单号 -->
      <u-form-item :required="true" :label="$t('message.Pi_OrderNo')" prop="prdOrder">
        <u-search :placeholder="$t('message.po_PleaseInput')" v-model.trim="form.prdOrder" @search="model.queryOrderInInfo" :show-action="false"></u-search>
        <u-button size="mini" style="margin-left: 10rpx" type="primary" @click="screen">{{ $t('message.screen') }}</u-button>
        <!-- <jPicker
          sureColor="#ff0000"
          style="width: 230rpx"
          @bindpicker="LocationChoice"
          showKey="value"
          valKey="value"
          :val="wlCode"
          class="search"
          :options="prdOrder"
        /> -->
      </u-form-item>
      <!-- 物料号 -->
      <u-form-item :required="true" :label="$t('message.Pi_materielNo')" prop="wlName">
        <u-input v-model="form.wlName" type="select" @click="materialCodeSelect = true" :placeholder="$t('message.po_PleaseInput')" />
        <u-select :confirm-text="$t('message.product_Confirm')" :cancel-text="$t('message.product_Cancel')" v-model="materialCodeSelect" :list="model.materielList" @confirm="materialConfirm"></u-select>
      </u-form-item>
      <!-- 工位 -->
      <u-form-item :label="$t('message.Pi_Station')">
        <u-input v-model="form.sendSpot" disabled placeholder=" " />
      </u-form-item>
      <!-- 库存 -->
      <view class="newly">
        <u-form-item :label="$t('message.Pi_factory')" style="width: 50%">
          <u-input v-model="model.orderInInfo.sapFactoryCode" placeholder=" " :disabled="true" />
        </u-form-item>
        <u-form-item :label="$t('message.product_Location')" style="width: 50%; margin-top: 16rpx">
          <!-- <u-input
            v-model="form.wkposCode"
            :placeholder="this.$t('message.po_PleaseInput')"
          /> -->
          <jPicker sureColor="#ff0000" style="width: 230rpx" @bindpicker="LocationChoice" showKey="value" valKey="value" :val="wlCode.value" class="search" :options="Location" />
        </u-form-item>
      </view>
      <!-- 工厂 -->
      <u-row gutter="0">
        <u-col :span="6">
          <u-form-item :label="$t('message.QuantityReturned')" prop="sapFactoryCode">
            <u-input :disabled="true" v-model="form.totalBackAmount" placeholder="" />
          </u-form-item>
        </u-col>
        <u-col :span="6">
          <u-form-item :required="true" :label="$t('message.Summary_Number')" prop="amount" label-width="80rpx">
            <u-input v-model="form.amount" type="number" :placeholder="$t('message.po_PleaseInput')" style="margin-left: 12px" />
          </u-form-item>
        </u-col>
      </u-row>
      <!-- 工位退料人 -->
      <u-form-item :required="true" :label="$t('message.return_OperatorName')" prop="operator">
        <u-input v-model="form.operator" :placeholder="$t('message.po_PleaseInput')" />
      </u-form-item>
      <!-- 口令 -->
      <u-form-item :label="$t('message.Summary_Password')">
        <u-input v-model="form.rfPwd" type="password" :placeholder="$t('message.po_PleaseInput')" />
      </u-form-item>
    </u-form>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <u-col :span="6">
          <u-button type="primary" @click="onSubmit">{{ $t('message.po_Submit') }}</u-button>
        </u-col>
        <u-col :span="6">
          <u-button type="error" @click="back">{{ $t('message.po_Return') }}</u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>

<script lang="ts">
import { Component, Ref } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import { VForm, VFormRules } from 'vue/types/form';
import { auth } from '@/store/modules/auth';
import model from './model';
import jPicker from '@/components/J-Picker/jPicker.vue';
import { session } from '@/store/modules/session';

@Component({
  components: {
    jPicker,
  },
})
export default class returningDom extends BasePage {
  /**
   * 表单引用
   */
  @Ref('form') readonly $form!: VForm;

  /**
   * 页面Module
   */
  model = model;
  /**
   * 表单数据
   */
  form = {
    sendSpot: '',
    prdOrder: null,
    sapFactoryCode: null,
    operator: '',
    amount: '',
    rfPwd: null,
    wkposCode: '',
    // tlType: null, // 退料类型code
    // tlName: null, // 退料类型name
    materialCode: null, // 物料类型code
    wlName: '', // 物料类型name
    wlCode: '' as any,
    totalBackAmount: '0',
  };
  prdOrder: any = [];
  wlCode: any = '';
  Location: any = [];
  /**
   * 工位退料人名称
   */
  operatorName = '';
  // 筛选带回数据
  data: any = {};
  /**
   * 退料类型选择
   */
  // returningTypeSelect = false;
  // 物料类型选择
  materialCodeSelect = false;
  /**
   * 表单验证规则
   */
  rules: VFormRules<any> = {
    operator: [{ required: true, message: this.$t('message.return_Tip1') as string }],
    prdOrder: [{ required: true, message: this.$t('message.Warehouse_Tip5') as string }],
    wlName: [{ required: true, message: this.$t('message.Warehouse_Tip6') as string }],
    amount: [{ required: true, message: this.$t('message.return_Tip3') as string }],
  };
  screen() {
    this.redirectTo(this.page.raw.handover.returning.filter);
  }
  LocationChoice(e: any) {
    console.log('e>>>>>>>>>>>>>>>>>>>>>>>>', e);
    this.wlCode = e.pickerName;
    this.form.wkposCode = e.pickerName.sendSpot;
  }
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  async onReady() {
    this.$form.setRules(this.rules);
    // this.model.queryReturningTypeList();
    this.form.sapFactoryCode = await this.model.queryDefaultFactoryCode();
    //页面初始化 获取可选库位
    let means: any = JSON.parse(localStorage.getItem('__GWMS_APP_STATE_DATA__') as any);
    let sendSpot = JSON.parse(localStorage.getItem('sendSpot') as any);
    if (JSON.parse(sessionStorage.getItem('data') as any)) {
      this.data = JSON.parse(sessionStorage.getItem('data') as any);
      this.form.prdOrder = this.data.label;
      this.form.wlName = this.data.value;
      this.form.sendSpot = this.data.sendSpot;
      // this.form.amount = this.data.amount;
    }

    if (this.form.prdOrder != null) {
      model.queryOrderInInfo(this.form.prdOrder);
    }
    sessionStorage.removeItem('data');
    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.Location.push(pickerName);
    });
  }
  back() {
    // this.form.sendSpot = '';
    // this.form.wkposCode = '';
    this.model.orderInInfo.sapFactoryCode = '';
    this.operatorName = '';
    // this.form.rfPwd = null;
    this.model.materielList.length = 0;
    uni.navigateBack({ delta: 1 });
    this.$form.resetFields();
  }
  materialConfirm(v: any) {
    this.form.materialCode = v[0].value;
    this.form.wlName = v[0].label;
    this.model.materielList.forEach((item: any) => {
      if (this.form.materialCode == item.label) {
        // this.form.amount = item.amount;
        this.form.sendSpot = item.sendSpot;
        this.form.totalBackAmount = item.totalBackAmount;
        // this.form.wkposCode = item.wkposCode;
      }
    });
    console.log('this.form', this.form);
  }
  /**
   * 上传
   */
  onSubmit() {
    if (this.form.amount > this.model.orderInInfo.amount) {
      this.customToast(this.$t('message.return_Tip4') as string);
    } else {
      this.$form.validate(async (valid: boolean) => {
        if (!valid) return;
        // if (!this.model.orderInInfo.sapFactoryCode) {
        //   this.customToast(this.$t('message.return_Tip2') as string);
        // }
        // (this.form.sendSpot as any) = this.model.orderInInfo.sendSpot;
        (this.form.sapFactoryCode as any) = this.model.orderInInfo.sapFactoryCode;
        // delete this.form.wlName;
        this.form.wlCode = this.wlCode.label
        await this.model.tluSubmit(this.form);
        this.$form.resetFields();
        this.operatorName = '';
        // this.form.sendSpot = '';
        // this.form.wkposCode = '';
        this.model.orderInInfo.sapFactoryCode = '';
        this.model.materielList = [];
      });
    }
  }

  /**
   * 操作人输入回车
   */
  async onOperatorConfirm(loginName: string) {
    try {
      const { userName } = await auth.getUserInfo({ loginName });
      this.form.operator = loginName;
      this.operatorName = userName;
    } catch {
      this.form.operator = '';
      this.operatorName = '';
    }
  }
}
</script>

<style lang="scss" scoped>
.page-raw-returning {
  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;
    }
  }

  .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;
    }
    .newly {
      display: flex;
    }
  }

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