<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>
        <view class="row-list">
          <view class="list-item">行项目: </view>
          <jPicker style="width: 80%" sureColor="#ff0000" @bindpicker="arrtChange" showKey="value" valKey="value" :val="aimWl.value" :options="orderNoItemList" />
        </view>
        <view class="row-list">
          <view class="list-item">物料编码: </view>
          <view class="item-input">{{ orderNoItem.productCode }}</view>
          <!-- <jPicker style="width: 80%" sureColor="#ff0000" @bindpicker="warehouseChange" showKey="value" valKey="value" :val="cimWl.value" :options="warehouselist" /> -->
        </view>

        <view class="row-list">
          <view class="list-item">收货仓库: </view>
          <jPicker style="width: 80%" sureColor="#ff0000" @bindpicker="warehouseChange" showKey="value" valKey="value" :val="cimWl.value" :options="warehouselist" />
        </view>
        <u-form-item label="收货工厂"> {{ session.FactoryCode }}</u-form-item>
        <view class="row-list">
          <view class="list-item">应收/已收: </view>
          <view class="item-input">{{ orderNoItem.planQuantity ? orderNoItem.planQuantity : 0 }}/{{ putQuantity }}</view>
        </view>
      </u-form>
      <u-form class="form4" style="">
        <!-- <u-search placeholder="请扫描" v-model="boxnumber" :show-action="false"></u-search> -->
        <view class="buttontostore" @click="rukubutn">入库</view>
        <!-- <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 style="margin-top: 180rpx" class="scroll">
      <view v-for="(item, index) in list" :key="item.id" @dblclick="onFormDoubleClick(item, index)">
        <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.batchNumber }} </view>
                </view> -->
                <view class="row-list-item">
                  <view class="list-item"> 箱数: </view>
                  <view>{{ item.number }} </view>
                </view>
              </view>
              <view class="row-list">
                <view class="row-list-item">
                  <view class="list-item"> 库位码: </view>
                  <view>{{ item.wlCode }} </view>
                </view>
                <view class="row-list-item">
                  <view class="list-item"> 托盘码: </view>
                  <view>{{ item.sn }} </view>
                </view>
              </view>
            </view>
            <view class="rowright">
              <view class="righttitle">
                <view @click.stop="onPassdelet(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>
    <view>
      <u-popup v-model="show" mode="center" width="550rpx" height="450rpx">
        <view class="form5">
          <view class="poupitem">
            <view class="pouptitle">托盘号：</view>
            <u-input class="poupcontent" :clearable="false" v-model="form5.sn" placeholder="" style="overflow: hidden" />
          </view>
          <view class="poupitem">
            <view class="pouptitle">入库库位：</view>
            <u-input class="poupcontent" :clearable="false" v-model="form5.wlCode" placeholder="" style="overflow: hidden" />
          </view>
          <!-- <view class="poupitem">
            <view class="pouptitle">箱数：</view>
            <u-input class="poupcontent" :clearable="false" v-model="form5.number" placeholder="" style="overflow: hidden" />
          </view> -->
        </view>
        <view class="poupbutton">
          <!-- 确定 -->
          <u-button @click="onOk" type="primary">确定</u-button>
          <!-- 返回 -->
          <u-button type="error" @click="nopoup">取消</u-button>
        </view>
      </u-popup>
      <view v-if="show" class="mask"></view>
    </view>
    <u-modal v-model="showmodal" @confirm="confirm" :content="content" :show-cancel-button="true"></u-modal>
    <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';
import { usedata } from '@/store/use/salesreturn';
interface OptionType {
  label: string;
  value: string;
}
//import { session } from '@/store/modules/session';
@Component({
  components: {
    jPicker,
  },
})
export default class SalesReturn extends BasePage {
  model = model;
  list: any = [];
  orderNoItemList: any = [];
  show = false;
  form5: any = {};
  boxnumber: any = '';
  itemdetail: any = '';
  showmodal = false;
  content: any = '';
  number1: any = '';
  border = true;
  delindex = -1;
  aimWl: any = {};
  cimWl = {} as OptionType;
  warehouselist = [];
  orderNo: any = '';
  histroyItem: any = {};
  barcode: any = '';
  productCodeList: any = {};
  biaojiindex = '';
  biaojisn = '';
  number = '';
  orderNoItem: any = {};
  putQuantity = 0;
  async onReady() {
    await this.model.getwarehouselist();
    this.warehouselist = model.warehouselist;
    this.cimWl = this.warehouselist[0];
  }
  // 在页面A中
  onUnload() {
    // 清空缓存数据
    usedata.cacheData(null);
  }
  async onShow() {
    const cacheData = usedata.salesreturndata;
    console.log('cacheData执行了', cacheData);

    if (cacheData) {
      console.log('cacheData执行了', cacheData);

      this.list = cacheData?.list;
      //this.cimWl.value = cacheData?.kuwei;
      this.biaojiindex = cacheData?.index;
      let sum = 0;
      this.list.forEach((element) => {
        sum += element.number;
      });
      this.putQuantity = this.orderNoItem.putQuantity + sum;
      //this.aimWl.value = cacheData?.rowitem;
    }
  }
  //列表删除按钮
  async onPassdelet(item, index) {
    console.log('1234567', item);
    this.itemdetail = item;
    this.delindex = index;
    this.showmodal = true;
    this.content = '是否删除?';
    // let  params = {
    //   item
    // }
  }
  async confirm() {
    this.itemdetail.orderNo = this.orderNo;
    console.log(this.itemdetail, this.orderNo);
    this.list.splice(this.delindex, 1);
    this.putQuantity = this.putQuantity - this.itemdetail.number;
  }
  //选择行项目
  arrtChange(e: any) {
    this.aimWl = e.pickerName;
    this.orderNoItem = e.pickerName;
    this.list = [];
    this.putQuantity = this.orderNoItem.putQuantity;
  }
  //选择收获仓库
  warehouseChange(e: any) {
    this.cimWl = e.pickerName;
  }
  //取消按钮
  nopoup() {
    this.show = false;
    this.form5 = {};
  }
  //查询货单关联采购单信息
  async query() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入退货单号',
        // url: '/pages/user/index'
      });
      return;
    }
    await this.model.queryOrderNo(this.orderNo);
    if (model.orderNoItemList) {
      model.orderNoItemList.map((item) => {
        item.label = item.attr10;
        item.value = item.attr10;
      });
      this.orderNoItemList = model.orderNoItemList;
      this.orderNoItem = this.orderNoItemList[0];
      this.aimWl = this.orderNoItemList[0];
      this.putQuantity = this.orderNoItemList[0].putQuantity;
    } else {
      this.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;
    }
    // let insertTarynumber = 0;
    // this.list.forEach((item) => {
    //   insertTarynumber = insertTarynumber + parseInt(item.number);
    // });
    //console.log(insertTarynumber, model.orderNoItemList.putQuantity, insertTarynumber == model.orderNoItemList.putQuantity);
    if (this.putQuantity <= parseFloat(this.orderNoItem.planQuantity)) {
      let params = {
        //workOrder: this.orderNoItemList.workOrder,
        factoryCode: session.FactoryCode,
        whCode: this.cimWl.value, //――仓库编号
        id: this.orderNoItem.id,
        attr10: this.aimWl.value,
        unitOfMeasure: this.orderNoItem.unitOfMeasure,
        productOrder: this.orderNoItem.productOrder,
        productCode: this.orderNoItem.productCode,
        productName: this.orderNoItem.productName,
        createBy: session.loginName,
        putQuantity: this.putQuantity,
        wmsProductPut: this.list,
      };
      //console.log('参数',params);
      await this.model.handPut(params);
      if (this.model.SubmitCode == '200') {
        (this.$refs.uToast as any).show({
          title: '成功',
          type: 'success',
        });
        this.show = false;
        this.list = [];
        this.query();
      } else {
        (this.$refs.uToast as any).show({
          title: '失败',
          type: 'error',
        });
        this.show = false;
      }
    } else {
      (this.$refs.uToast as any).show({
        title: '总收货数量不能大于计划数量',
        // url: '/pages/user/index'
      });
    }
  }

  async rukubutn() {
    this.form5 = {};
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入退货单号',
        // url: '/pages/user/index'
      });
      return;
    }
    if (this.list.length > 0) {
      // let insertTarynumber = 0;
      // this.list.forEach((item) => {
      //   insertTarynumber = insertTarynumber + parseInt(item.number);
      // });
      //let ordernumber = parseFloat(this.number1) || 0;
      if (this.putQuantity >= this.orderNoItem.planQuantity) {
        (this.$refs.uToast as any).show({
          title: '总收货数量不能大于计划数量',
          // url: '/pages/user/index'
        });
        return;
      }
      this.show = true;
    } else {
      this.show = true;
    }
  }
  //弹窗入库按钮
  async onOk() {
    console.log(this.form5);
    if (this.form5.sn == '' || this.form5.wlCode == '') {
      (this.$refs.uToast as any).show({
        title: '不能为空',
        // url: '/pages/user/index'
      });
      return;
    }
    // this.list.push({
    //   number: 0,
    //   wlCode: this.form5.wlCode,
    //   sn: this.form5.sn,
    //   //batchNumber: this.orderNoItemList.batchNumber,
    // });
    // this.show = false;
    await this.model.selectSnCorrelationBarcode(this.form5.sn);
    if (model.selectcode) {
      this.list.push({
        number: model.selectdata.length || 0,
        wlCode: this.form5.wlCode,
        sn: this.form5.sn,
        //batchNumber: this.orderNoItemList.batchNumber,
      });
      this.show = false;
      this.putQuantity = this.putQuantity + model.selectdata.length;
    }
  }
  async onFormDoubleClick(item, index) {
    console.log('双击', item);
    //await this.model.OnSubmit(item.id);
    usedata.cacheData({ kuwei: this.cimWl.value, list: this.list, index: index });
    this.uni.navigateTo({
      url: this.page.wms.SalesReturndetail + '?sn=' + item.sn,
    });
  }
  async tidan(){
    if (this.orderNo == '' || this.orderNo.length == 0) {
      (this.$refs.uToast as any).show({
        title: '请输入退货单号',
        // url: '/pages/user/index'
      });
      return;
    }
    await this.model.finishedProductSalesReturnTD(this.orderNo);
    if(model.finishCode == 200){
      (this.$refs.uToast as any).show({
        title: '提单成功',
        // url: '/pages/user/index'
      });
      this.query();
    }
  }
}
</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 {
      display: flex;
      flex-direction: row;
      padding: 10rpx 0rpx;
      .list-item {
        width: 30%;
      }
      .item-input {
        border: 1rpx solid #8e8a8a;
        width: 100%;
        border-radius: 4rpx;
      }
    }
  }
  .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: 1rpx;
    border-radius: 10rpx;
    margin-top: 2rpx;
    display: flex;
    justify-content: flex-end;
    // margin-bottom: 5px;
    padding-bottom: 5px;
    .buttontostore {
      margin-top: 5px;
      background: red;
      width: 13%;
      /* height: 100%; */
      height: 33px;
      text-align: center;
      /* margin: auto; */
      color: #ffff;
      line-height: 30px;
      border-radius: 5px;
    }
    .u-input__input {
      min-height: 36px;
    }
  }

  .scroll {
    // margin-top: 2rpx;
    height: 835rpx;
    position: absolute;
    width: 92%;
    top: 550rpx;
    /* 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%;
            }
            .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;
  }

  .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;
    // }
  }
  .form5 {
    padding: 20rpx;
    //border-radius: 10rpx;
    //box-shadow: 0 0 20rpx 0 rgba(128, 128, 128, 0.2);
    .poupitem {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      margin: 20rpx;
      align-items: center;
      .pouptitle {
        // width: 25%;
        min-width: 35%;
        // white-space: nowrap;
      }
      .poupcontent {
        border: 1px solid rgba(128, 128, 128, 0.2);
      }
    }
  }
  .poupbutton {
    display: flex;
    justify-content: center;
  }
}
</style>
