<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>
    <view class="fiexdcontent">
      <u-form class="form" ref="form" label-width="180rpx">
        <!-- 单号 -->
        <view class="single">
          <view class="single-left">
            <view>备货单:</view>
            <u-search placeholder="请扫描" v-model="orderNo" @search="query" :show-action="false"></u-search>
          </view>
          <view class="single-right">
            <u-button type="primary" @click="tidan">提单</u-button>
          </view>
        </view>
        <!-- 订单 -->
        <u-form-item label="产品编码">
          <!-- <u-input :border="true" v-model="histroyItem.productDescZh" placeholder="" style="overflow: hidden" /> -->
          <jPicker sureColor="#ff0000" @bindpicker="bookTypeChange" showKey="value" valKey="value" :val="aimWl.value" :options="prodlist" />
        </u-form-item>
        <u-form-item label="批次">
          <!-- <u-input :border="true" v-model="histroyItem.productDescZh" placeholder="" style="overflow: hidden" /> -->
          <jPicker sureColor="#ff0000" @bindpicker="bookTypeChangepici" showKey="lotNumber" valKey="lotNumber" :val="bimWl.value" :options="picilist" />
        </u-form-item>
        <!-- <u-form-item label="出库仓库">
          <jPicker sureColor="#ff0000" @bindpicker="bookTypeChange" showKey="value" valKey="value" :val="aimWl.value" :options="materilist" />
        </u-form-item>
        <u-form-item label="客户名称">
          <jPicker sureColor="#ff0000" @bindpicker="bookTypeChange" showKey="value" valKey="value" :val="aimWl.value" :options="materilist" />
        </u-form-item> -->
        <!-- <view class="row-list-top">
          <view class="row-list-item-top">
            <view class="list-item-top"> 出库仓库: </view>
            <view>{{loc.label}}</view>
          </view>
          
        </view> -->
        <!-- 机台 -->
        <!-- <u-form-item label="出库仓库"> 成品库 </u-form-item> -->
      </u-form>
      <u-form :style="{ marginTop: '1rpx', backgroundColor: orderNoItemList.planQuantity <= orderNoItemList.outQuantity ? '#4eea9b59' : '#ffffff' }" class="form3">
        <view class="list">
          <view class="row-list">
            <view class="list-item"> 产品编码: </view>
            <view> {{ orderNoItemList && orderNoItemList.productCode }} </view>
          </view>
          <view class="row-list">
            <view class="list-item"> 产品名称: </view>
            <view> {{ orderNoItemList && orderNoItemList.productName }} </view>
          </view>
          <view class="row-list">
            <view class="row-list-item">
              <view class="list-item"> 计划数量: </view>
              <view> {{ orderNoItemList && orderNoItemList.planQuantity }} </view>
            </view>
            <view class="row-list-item">
              <view class="list-item"> 单位: </view>
              <view> {{ orderNoItemList && orderNoItemList.unitOfMeasure }} </view>
            </view>
          </view>
          <view class="row-list">
            <view style="width: 30%" class="row-list-item">
              <view class="list-item"> 出库数量: </view>
              <view> {{ orderNoItemList && orderNoItemList.outQuantity }} </view>
            </view>
            <view style="width: 50%" class="row-list-item">
              <view style="width: 50%" class="list-item"> 出库仓库: </view>
              <view> {{ loc.userDefined2 }} </view>
            </view>
          </view>
          <view class="row-list">
            <view class="list-item"> 订单类型: </view>
            <view> {{ orderNoItemList && orderNoItemList.attr1 }} </view>
          </view>
        </view>
      </u-form>
      <u-form class="form4">
        <!-- <u-radio-group v-model="selectvalue" style="width: 100%" @change="radioGroupChange">
          <u-radio style="width: 50%" v-for="(item, index) in Radiolist" :key="index" :name="item.code" :disabled="item.disabled">
            {{ item.name }}
          </u-radio>
        </u-radio-group> -->
        <u-search placeholder="请扫描条码" @search="addlist" v-model="boxnumber" :show-action="false"></u-search>
        <!-- <u-input style="border: 0.5rpx solid #8d8989; border-radius: 10rpx" v-model="boxnumber" @click="clickscan"></u-input> -->
      </u-form>
    </view>
    <view style="height: 14rpx"></view>
    <view class="scroll">
      <view v-for="(item, index) in list" :key="item.id">
        <u-form style="margin-top: 10rpx" class="form2">
          <view class="list">
            <view class="rowleft">
              <!-- <view class="row-list">
                <view class="row-list-item">
                  <view class="list-item"> 标志卡: </view>
                  <view>{{ item.sn }} </view>
                </view>
              </view> -->
              <view class="row-list">
                <view class="list-item"> 批次: </view>
                <view> {{ item.batchNumber }} </view>
              </view>

              <view class="row-list">
                <view class="list-item"> 产品名称: </view>
                <view>{{ item.productName }} </view>
              </view>

              <view class="row-list">
                <view class="list-item"> 产品编码: </view>
                <view> {{ item.productCode }} </view>
              </view>
              <view class="row-list">
                <view class="row-list-item-row">
                  <view class="list-item"> 已经出库数量: </view>
                  <view> {{ item.userDefined2 }} </view>
                </view>
                <view class="row-list-item">
                  <view class="list-item"> 状态: </view>
                  <view style="color: red">{{ item.userDefined1 === '0' ? '未出库' : item.userDefined1 === '1' ? '正在出库' : '出库完成' }}</view>
                  <!-- <view>{{item.amount}} </view> -->
                </view>
              </view>
              <view class="row-list">
                <view class="row-list-item-row">
                  <view class="list-item"> 库区: </view>
                  <view> {{ item.waCode }} </view>
                </view>
                <view class="row-list-item">
                  <view class="list-item"> 数量: </view>
                  <u-input v-model="item.amount" type="number " border @blur="confirmNumber(item)" />
                  <!-- <view>{{item.amount}} </view> -->
                </view>
              </view>
            </view>
            <view class="rowright">
              <view class="righttitle">
                <view @click.stop="ondelete(item, index)" class="boder-icon-rigth"> 删除 </view>
              </view>
            </view>
          </view>
        </u-form>
      </view>
    </view>
    <view class="bottom-bar">
      <u-row class="button-bar">
        <!-- 确定 -->
        <u-col :span="6">
          <u-button @click="onSubmit" type="primary">出库</u-button>
        </u-col>
        <!-- 返回 -->
        <u-col :span="6">
          <u-button type="error" @click="uni.navigateBack({})">返回</u-button>
        </u-col>
      </u-row>
    </view>
    <u-toast ref="uToast" />
  </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 model from './model';
import { session } from '@/store/modules/session';
interface OptionType {
  label: string;
  value: string;
}
//import { session } from '@/store/modules/session';
@Component({
  components: {
    jPicker,
  },
})
export default class SpecialDelivery extends BasePage {
  model = model;
  list: any = [];
  //productCodebiaozhiwei = '';
  cimWl = {} as OptionType;
  aimWl: any = {};
  bimWl: any = {};
  boxnumber: any = '';
  materilist = [];
  prodlist = [];
  picilist = [];
  loc: any = {};
  // remove: any = {};
  // async onReady() {
  //   await this.model.queryLocation();
  //   this.remove = this.model.WlList[0];
  // }
  // selectItem(e: any) {
  //   this.remove = e.pickerName;
  // }
  orderNo: any = '';
  border = true;
  orderNoItemList: any = [];
  showProduceCodeList: any = [];
  itemdetail: any = '';
  outnumber: any = 0;
  warehouselist = [];
  Radiolist = [
    {
      code: 0,
      name: '整托扫描',
      disabled: false,
    },
    // {
    //   code: 1,
    //   name: '单箱扫描',
    //   disabled: false,
    // },
  ];
  selectvalue: any = 0;
  async onLoad() {
    await this.model.getwarehouselist();
    this.warehouselist = model.warehouselist;
    //this.cimWl = this.warehouselist[0];
  }
  bookTypeChange(e: any) {
    this.aimWl = e.pickerName;
    this.picilist = this.materilist.filter((item) => {
      return item.productCode === this.aimWl.value;
    });
    this.bimWl = this.picilist[0];
    this.list = [];
    this.orderNoItemList = this.picilist[0];
    this.orderNoItemList.outQuantity = this.orderNoItemList.outnumber;
    this.outnumber = this.orderNoItemList.outQuantity;
    this.warehouselist.some((item) => {
      if (item.warehouseCode === this.orderNoItemList.whCode) {
        this.loc = item;
        return true; // 在 some 中返回 true 会停止循环
      }
      return false; // 继续循环
    });
  }
  //选批次
  bookTypeChangepici(e: any) {
    this.bimWl = e.pickerName;
    this.orderNoItemList = e.pickerName;
    this.list = [];
    this.orderNoItemList.outQuantity = this.orderNoItemList.outnumber;
    this.outnumber = this.orderNoItemList.outQuantity;
  }
  wlChange(e: any) {
    this.cimWl = e.pickerName;
  }
  // 选中任一radio时，由radio-group触发
  radioGroupChange(e) {
    console.log(e);
    console.log('selectvalue', this.selectvalue);
  }
  //收货按钮
  async ondelete(item, index) {
    // console.log('this.list', Array.isArray(this.list));
    // const newList = [...this.list]; // 创建一个新的数组
    // newList.splice(index, 1); // 在新数组上执行 splice 操作
    this.list.splice(index, 1); // 将新数组赋值回原数组
    this.orderNoItemList.outQuantity -= item.amount;
    (this.$refs.uToast as any).show({
      title: '删除成功',
      type: 'success',
    });
    //console.log('this.outnumber', this.outnumber);

    // if(!this.list.length){
    //   this.productCodebiaozhiwei = '';
    // }
  }
  //查询货单关联采购单信息
  async query() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入备货单',
        // url: '/pages/user/index'
      });
      return;
    }
    await this.model.showProduceCode(this.orderNo);
    if (model.orderNoItemList) {
      this.materilist = [];
      //let a: any = {};
      this.model.orderNoItemList.forEach((item) => {
        item.label = item.productName;
        item.value = item.productCode;
        item.outnumber = item.outQuantity;
        this.materilist.push(item);
      });
      const productCodeMap = {};

      // 创建一个新的数组用于存放去重后的元素
      const uniqueItems = [];

      this.materilist.forEach((item) => {
        if (!productCodeMap[item.productCode]) {
          productCodeMap[item.productCode] = true;
          uniqueItems.push(item);
        }
      });
      this.prodlist = uniqueItems;
      this.aimWl.value = this.prodlist[0].value;

      //批次

      this.picilist = this.materilist.filter((item) => {
        return item.productCode === this.aimWl.value;
      });
      this.bimWl.value = this.picilist[0].lotNumber;
      this.orderNoItemList = this.picilist[0];
      this.outnumber = this.orderNoItemList.outQuantity;
      this.warehouselist.some((item) => {
        if (item.warehouseCode === this.orderNoItemList.whCode) {
          this.loc = item;
          return true; // 在 some 中返回 true 会停止循环
        }
        return false; // 继续循环
      });

      // this.loc = this.warehouselist.filter((item)=>{
      //   return item.userDefined1 === this.orderNoItemList.whCode;
      // })
      console.log('库位', this.loc);
      this.list = [];
      // this.orderNoItemList = this.materilist[0];
      // this.outnumber = this.orderNoItemList.outQuantity;
    } else {
      this.materilist = [];
      this.prodlist = [];
    }
  }

  async addlist() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入备货单',
        // url: '/pages/user/index'
      });
      return;
    }
    if (Object.keys(this.orderNoItemList).length == 0) {
      (this.$refs.uToast as any).show({
        title: '请先选择产品',
        // url: '/pages/user/index'
      });
      return;
    }

    // if (this.selectvalue == 0) {
    //选择整托扫描
    let item: any = {};
    item.sn = this.boxnumber;
    item.siteCode = session.FactoryCode;
    await this.model.ScanCodeWholePallet(item);
    if (this.model.scanCode == '200') {
      let chongfuscan = this.list.some((item) => item.storageId === model.scandatalist[0].storageId);
      if (chongfuscan) {
        (this.$refs.uToast as any).show({
          title: '请勿重复扫描标志卡',
          // url: '/pages/user/index'
        });
        this.boxnumber = '';
        return;
      } else {
        (this.$refs.uToast as any).show({
          title: '成功',
          type: 'success',
        });
        model.scandatalist.map((item) => {
          //item.productCode = item.productCode.slice(item.productCode.search(/[1-9]/));
          if (item.productCode == this.orderNoItemList.productCode) {
            this.list.push(item);
            this.orderNoItemList.outQuantity += item.amount;
          } else {
            (this.$refs.uToast as any).show({
              title: '扫描的物料必须与选择的物料相同',
              type: 'error',
            });
          }
        });
      }

      // this.list.push()

      this.boxnumber = '';
    } else {
      (this.$refs.uToast as any).show({
        title: '失败',
        type: 'error',
      });
      this.boxnumber = '';
    }
    //}

    // //入库后的查询下面列表的接口
    // await this.model.queryOrderNo(this.form5);
    // this.list = model.orderNoItemList;
  }
  async onSubmit() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入备货单',
        // url: '/pages/user/index'
      });
      return;
    }

    if (this.list.length == 0 || this.list == '') {
      (this.$refs.uToast as any).show({
        title: '请先扫描标志卡',
        // url: '/pages/user/index'
      });
      return;
    }
    //this.list[0].outnumber = this.outnumber;
    let wmsFpStorageNewsSns = [];
    this.list.map((item) => {
      wmsFpStorageNewsSns.push({
        storageId: item.storageId,
        amount: item.amount,
      });
    });
    let item = {
      factoryCode: session.FactoryCode,
      createBy: session.loginName,
      id: this.orderNoItemList.id,
      wmsFpStorageNewsSns: wmsFpStorageNewsSns,
    };
    //console.log('000------',item);
    await this.model.out(item);
    if (this.model.SubmitCode == '200') {
      (this.$refs.uToast as any).show({
        title: '成功',
        type: 'success',
      });
      this.list = [];
      this.query();
      //this.productCodebiaozhiwei = '';
    } else {
      (this.$refs.uToast as any).show({
        title: '失败',
        type: 'error',
      });
    }
  }
  async tidan() {
    //提单方法
    // if (this.orderNo == '' || this.orderNo.length == 0) {
    //   (this.$refs.uToast as any).show({
    //     title: '请输入备货单号',
    //     // url: '/pages/user/index'
    //   });
    //   return;
    // }
    let item = {
      factoryCode: session.FactoryCode,
      //deliveryOrder: this.orderNo,
      createBy: session.loginName,
    };
    await this.model.selectFinishedProductSales(item);
    if (this.model.tidancode == '200') {
      (this.$refs.uToast as any).show({
        title: '提单成功',
        type: 'success',
      });
      //this.list = [];
      this.query();
      //this.productCodebiaozhiwei = '';
    } else {
      (this.$refs.uToast as any).show({
        title: '提单失败',
        type: 'error',
      });
    }
  }

  confirmNumber(item) {
    console.log(item);
    let addnumber = 0;
    this.list.forEach((item) => {
      addnumber += parseFloat(item.amount ? item.amount : '0');
    });
    this.orderNoItemList.outQuantity = this.orderNoItemList.outnumber + addnumber;
  }
}
</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;
    }
  }

  .fiexdcontent {
    position: fixed;
    width: 92%;
    box-shadow: 0 0 14px 0 rgb(128 128 128 / 46%);
    z-index: 100;
  }
  .form {
    background-color: #fff;
    padding: 30rpx;
    border-radius: 10rpx;
    //box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);

    .u-form-item {
      padding: 20rpx 0;
      line-height: 35rpx;

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

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

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

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

      .single-right {
        button {
          // margin-top: 15rpx;
          width: 140rpx;
          height: 70rpx;
          background-color: #fa3534;
        }
      }
    }

    .row {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      flex: 1;
    }
    .row-list-top {
      display: flex;
      flex-direction: row;
      width: 100%;
      .row-list-item-top {
        display: flex;
        flex-direction: row;
        width: 50%;
      }
    }
  }
  .form3 {
    background-color: #fff;
    padding: 25rpx 25rpx 25rpx;
    border-radius: 10rpx;
    //box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);
    //color: #8d8989;
    // .list {
    //   display: flex;
    //   flex-direction: row;
    // }
    .row-list {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      .row-list-item {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        width: 40%;
      }
      .list-item {
        display: flex;
        flex-direction: row;
        flex: 1;
      }
    }

    .righttitle {
      display: flex;
      justify-content: flex-end;

      .boder-icon-rigth {
        border: 1rpx solid #fa3534;
        width: 130rpx;
        text-align: center;
        background: #fa3534;
        color: #ffffff;
        margin-top: 10px;
      }
    }
  }
  .form4 {
    background-color: #fff;
    padding: 18rpx;
    border-radius: 10rpx;
    margin-top: 2rpx;
    .u-input__input {
      min-height: 36px;
    }
  }

  .scroll {
    // margin-top: 2rpx;
    height: 400rpx;
    position: absolute;
    width: 92%;
    top: 800rpx;
    /* bottom: 40px; */
    overflow: auto;
    .form2 {
      background-color: #fff;
      padding: 25rpx 25rpx 25rpx;
      border-radius: 10rpx;
      box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);
      color: #8d8989;
      .list {
        display: flex;
        flex-direction: row;
        .rowleft {
          width: 80%;
          .row-list {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            .row-list-item {
              display: flex;
              flex-direction: row;
              justify-content: space-between;
              width: 40%;
            }
            .row-list-item-row {
              display: flex;
              flex-direction: row;
              justify-content: space-between;
              width: 60%;
            }
            .list-item {
              display: flex;
              flex-direction: row;
              // flex: 1;
            }
          }
        }
        .rowright {
          width: 20%;
          display: flex;
          justify-content: right;
          .righttitle {
            width: 60%;
            background-color: #8d898947;
            color: red;
            text-align: center;
            /* margin: auto; */
            height: 100%;
            line-height: 70rpx;
          }
        }
      }

      // .righttitle {
      //   display: flex;
      //   justify-content: flex-end;

      //   .boder-icon-rigth {
      //     border: 1rpx solid #fa3534;
      //     width: 130rpx;
      //     text-align: center;
      //     background: #fa3534;
      //     color: #ffffff;
      //     margin-top: 10px;
      //   }

      //   .zhuangtai {
      //     background: #a7a0a04a;
      //     width: 224rpx;
      //     margin-right: -27rpx;
      //     border: 2rpx solid #a7a0a04a;
      //     border-radius: 12rpx;
      //     text-align: center;
      //     margin-bottom: 10rpx;
      //   }

      //   .zhuangtai[data-state='1'] {
      //     color: green;
      //   }

      //   .zhuangtai[data-state='0'] {
      //     color: blue;
      //   }
      // }
    }
  }

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

  .button-bar {
  }

  .photoList {
    display: flex;
    padding-top: 12rpx;

    .chooseBtn {
      width: 202rpx;
      height: 202rpx;
      background: #fafafa;
      border-radius: 12rpx;
      border: 2rpx dashed #e2e2e2;
      display: flex;
      justify-content: center;
      align-items: center;

      .inner {
        text-align: center;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }

      .desc {
        font-size: 28rpx;
        font-weight: 400;
        color: #24252a;
        line-height: 40rpx;
      }

      .img {
        width: 50rpx;
        height: 50rpx;
        margin-bottom: 6rpx;
      }
    }

    .photoBox {
      width: 202rpx;
      height: 202rpx;
      background: #fafafa;
      border-radius: 12rpx;
      position: relative;

      .closeBtn {
        position: absolute;
        top: -12rpx;
        right: -12rpx;
        z-index: 2;
        width: 36rpx;
        height: 36rpx;

        .img {
          width: 36rpx;
          height: 36rpx;
        }
      }

      .img {
        width: 202rpx;
        height: 202rpx;
        border-radius: 12rpx;
      }
    }

    .itemBox {
      flex: 0 0 202rpx;
      margin-right: 20rpx;

      &:last-child {
        margin-right: 0;
      }
    }

    // /deep/ .remarksBox textarea {
    //   font-size: 24rpx;
    // }
  }
}
</style>
