<!-- 扫描rfid,选择缓存区，根据选择的缓存区查询库位，勾选是否取走托盘，提交托盘变更信息 -->
<template>
  <view class="page-product-receipt">
    <view class="header" :style="{ backgroundColor: `rgba(250, 53, 52, ${scrollTop / 100})` }">
      <view class="left">
        <u-icon class="icon" name="arrow-left" @click="uni.navigateBack({})" />
      </view>
      <view class="title">异常托盘处理</view>
      <view class="right"></view>
    </view>
    <u-form class="form" ref="form" label-width="180rpx">
      <!-- 单号 -->
      <view class="single">
        <view class="single-left">
          <view>rfid:</view>
          <u-search placeholder="请扫描" :focus="shouciFocus" v-model="orderNo" :show-action="false" @search="query"></u-search>
        </view>
        <view class="single-right">
          <!-- S0000096 -->
          <u-button type="primary" @click="query">查询</u-button>
        </view>
      </view>
      <u-form-item label="机种名称:">
        <u-input :disabled="true" v-model="orderNoItemList.machineName" placeholder="" style="overflow: hidden" />
      </u-form-item>
      <!-- 机台 -->
      <!-- 工单 -->
      <u-form-item label="工单:">
        <u-input :disabled="true" v-model="orderNoItemList.workorderCode" placeholder="" />
      </u-form-item>
      <u-form-item label="规格:">
        <u-input :disabled="true" v-model="orderNoItemList.productSpc" placeholder="" style="overflow: hidden" />
      </u-form-item>
      <u-form-item label="起始区域类型:">
        <jPicker sureColor="#ff0000" :moren="moren" @bindpicker="selectProductlocCode" showKey="label" valKey="value" :val="productCodelocList.value" :options="model.locList" />
      </u-form-item>

      <u-form-item label="起始位置:">
        <jPicker sureColor="#ff0000" :moren="morenStart" @bindpicker="selectProductCode" showKey="label" valKey="value" :val="productCodeList.value" :options="model.DictList" />
      </u-form-item>
      <u-form-item label="目的区域类型:">
        <jPicker sureColor="#ff0000" :moren="moren" @bindpicker="selectProductlocCode2" showKey="label" valKey="value" :val="productDestinationList.value" :options="model.locList" />
      </u-form-item>
      <u-form-item label="目的地点:">
        <jPicker sureColor="#ff0000" :moren="Destination" @bindpicker="selectProductCode2" showKey="label" valKey="value" :val="DestinationList.value" :options="model.Destination" />
      </u-form-item>
    </u-form>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <!-- 绑定 -->
        <u-col :span="4">
          <u-button @click="onbind" type="warning">绑定</u-button>
        </u-col>
        <!-- 确定 -->
        <u-col :span="4">
          <u-button @click="onsubmit" type="primary">确定</u-button>
        </u-col>
        <!-- 返回 -->
        <u-col :span="4">
          <u-button type="error" @click="uni.navigateBack({})">返回</u-button>
        </u-col>
      </u-row>
    </view>
    <u-modal v-model="isshow" class="modal" :show-cancel-button="true" :show-title="false" @cancel="cancel()" @confirm="confirm">
      <div class="upload-name">
        <div class="name">托盘绑定</div>
        <u-input class="input" style="background: #ffffff; border: 1px solid #9f9e9e; margin-top: 10px; width: 80%; margin: auto; border-radius: 9px; text-align: center" :disabled="true" v-model="orderNo" placeholder="" />
      </div>
    </u-modal>
    <u-toast ref="uToast" />
  </view>
</template>
<script lang="ts">
import { Component } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import model from './model';
import chooseOne from '@/components/choose-one/choose-one.vue';
import jPicker from '@/components/J-Picker/jPicker.vue';
import { session } from '@/store/modules/session';
//import { session } from '@/store/modules/session';
//import store from '@/store';
@Component({
  components: {
    chooseOne,
    jPicker,
  },
})
export default class ChangePalletNew extends BasePage {
  model = model;
  moren = '选择区域类型';
  morenStart = '选择起始位置';
  Destination = '选择目的地点';
  shouciFocus = true;
  zaiciFocus = false;
  switchListText = [
    {
      title: '人工',
      value: 0,
    },
    {
      title: 'AGV',
      value: 1,
    },
  ];
  orderNoItemList: any = {};
  productCodeList: any = {};
  productCodelocList: any = {};
  DestinationList: any = {};
  productDestinationList: any = {};
  // 模态框
  isshow = false;
  // remove: any = {};
  async onReady() {
    await this.model.getDictList();
    //await this.model.getpalletLocation();
    //this.remove = this.model.WlList[0];
  }
  // selectItem(e: any) {
  //   this.remove = e.pickerName;
  // }
  orderNo: any = '';
  location: any = '';
  locationStatus: any = 1;
  async query() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入RFID',
        type: 'default',
      });
      return;
    }
    await this.model.queryOrderNo(this.orderNo);
    console.log('???', model.orderNoItemList);
    if (model.orderNoItemList == undefined) {
      (this.$refs.uToast as any).show({
        title: '该rfid未查到数据，请先绑定',
        type: 'default',
      });
    } else {
      this.orderNoItemList = model.orderNoItemList;
      if (this.orderNoItemList.productCode == 'EmptyPallet') {
        (this.$refs.uToast as any).show({
          title: '请绑定',
          type: 'default',
        });
        return;
      }
    }
    //console.log('this.orderNoItemList', this.orderNoItemList);
  }
  generateGUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = (Math.random() * 16) | 0,
        v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }
  async onsubmit() {
    console.log(this.productCodeList.value == '' || this.productCodeList.value == undefined, this.productCodeList.value);
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入RFID',
        type: 'default',
      });
      return;
    }
    if (Object.keys(this.orderNoItemList).length === 0) {
      (this.$refs.uToast as any).show({
        title: '请先查询出RFID数据',
        type: 'default',
      });
      return;
    }
    if (this.productCodelocList.value < 0) {
      (this.$refs.uToast as any).show({
        title: '请选择起始区域',
        type: 'default',
      });
      return;
    }
    if (this.productCodeList.value == '' || this.productCodeList.value == undefined) {
      (this.$refs.uToast as any).show({
        title: '请选择起始位置',
        type: 'default',
      });
      return;
    }
    if (this.productCodelocList.value < 0) {
      (this.$refs.uToast as any).show({
        title: '请选择目的区域',
        type: 'default',
      });
      return;
    }
    if (this.DestinationList.value == '' || this.DestinationList.value == undefined) {
      (this.$refs.uToast as any).show({
        title: '请选择目的地点',
        type: 'default',
      });
      return;
    }
    var currentDate = new Date();

    // 获取年份、月份、日期、小时、分钟和秒
    var year = currentDate.getFullYear();
    var month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
    var day = ('0' + currentDate.getDate()).slice(-2);
    var hours = ('0' + currentDate.getHours()).slice(-2);
    var minutes = ('0' + currentDate.getMinutes()).slice(-2);
    var seconds = ('0' + currentDate.getSeconds()).slice(-2);

    // 格式化为 "YYYY-MM-DD HH:mm:ss" 结构
    var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    let params = {
      rfid: this.orderNo,
      reqTime: formattedDate,
      sku: this.orderNoItemList.productCode,
      spec: this.orderNoItemList.productSpc,
      productionState: this.productCodelocList.value.toString(), //缓存区：2或者3，成品2，烘房3
      //location: this.location,
      destinationAddress: this.DestinationList.value, //库位，其中 库位是根据选择的缓存区来查询的
      sourceAddress: this.productCodeList.value,
      factory: session.FactoryCode,
      //productionState: this.productCodeList.value,
    };
    await this.model.palletInforUpdate(params);
    if (model.SubmitCode.code == 200) {
      this.uni.showToast({ title: '成功' }); //成功弹窗
      this.model.getDictList();
      this.Clear(); //清除数据
    } else {
      (this.$refs.uToast as any).show({
        title: model.SubmitCode.message, //错误信息展示
        type: 'default',
      });
    }
  }
  Clear() {
    this.orderNo = '';
    this.location = '';
    (this.orderNoItemList = {}), (this.locationStatus = 1);
    this.productCodeList = {};
    this.productCodelocList = {};
    this.productDestinationList = {};
    this.DestinationList = {};
    this.shouciFocus = false;
    this.model.empty();
    this.$nextTick(() => {
      this.shouciFocus = true;
    });
  }
  switchChangeFun(e) {
    //console.log(e);
    this.locationStatus = e.swithcSelectItem.value;
  }
  async selectProductCode(e: any) {
    this.productCodeList = e.pickerName;
  }
  async selectProductCode2(e: any) {
    this.DestinationList = e.pickerName;
  }
  async selectProductlocCode(e: any) {
    this.productCodelocList = e.pickerName;
    let params = {
      productionState: this.productCodelocList.value.toString(),
    };
    await this.model.getpalletLocation(params); //参数：成型区2，烘房3
  }
  async selectProductlocCode2(e: any) {
    this.productDestinationList = e.pickerName;
    let params = {
      productionState: this.productDestinationList.value.toString(),
    };
    await this.model.getpalletDestination(params);
  }
  async confirm() {
    await this.model.bindOrderNo(this.orderNo);
    if (model.SubmitCode.code == 200) {
      (this.$refs.uToast as any).show({
        title: '绑定成功',
        type: 'default',
      });
      this.isshow = false;
    } else {
      (this.$refs.uToast as any).show({
        title: model.SubmitCode.message,
        type: 'default',
      });
      this.isshow = false;
    }
  }
  cancel() {
    console.log(this.orderNoItemList);
  }
  onbind() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入RFID',
        type: 'default',
      });
      return;
    }

    if (Object.keys(this.orderNoItemList).length > 0) {
      let orderNoItemList = this.orderNoItemList;
      if (orderNoItemList.productCode != 'EmptyPallet') {
        (this.$refs.uToast as any).show({
          title: '请不要再次绑定',
          type: 'default',
        });
        return;
      }
    }
    this.isshow = true;
  }
}
</script>
<style lang="scss" scoped>
.page-product-receipt {
  background: #f2f2f2 linear-gradient(0deg, #f2f2f2 0%, #f8262c 51%, #d9001b 100%) no-repeat;
  background-size: 100% 600rpx;
  padding: 118rpx 30rpx 162rpx;
  min-height: 100%;

  .header {
    position: fixed;
    top: 0rpx;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    height: 120rpx;
    line-height: 88rpx;
    color: #fff;
    font-size: 34rpx;
    font-weight: 500;
    text-align: center;
    padding: 45rpx 0rpx 0rpx;
    .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;

      .Transfer {
        width: 280rpx;
        height: 64rpx;
        background: rgb(242, 242, 242);
        border-radius: 110rpx;
      }
    }

    .single {
      width: 100%;
      height: 100rpx;
      display: flex;

      .single-left {
        width: 80%;
        height: 100%;
        display: flex;

        view {
          width: 142rpx;
          height: 100%;
          line-height: 100rpx;
        }
      }

      .single-right {
        button {
          margin-top: 15rpx;
          width: 140rpx;
          height: 70rpx;
          background-color: #fa3534;
        }
      }
    }
  }
  .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;
  }
  .modal {
    .upload-name {
      padding: 10px 20px;
      .name {
        font-size: 17px;
        margin-bottom: 15px;
        // font-weight: 600;
      }
    }
  }

  ::v-deep .yjy-switch-container .yjy-switch-item {
    width: 100%;
  }
  ::v-deep .uni-input-input {
    background: none;
  }
  ::v-deep .yjy-switch-container {
    width: 50%;
  }
}
</style>
