<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="请扫描" v-model="orderNo" @search="query" :show-action="false"></u-search>
        </view>
        <view class="single-right">
          <u-button type="primary" @click="query">扫描</u-button>
        </view>
      </view>
      <!-- 物料编码 -->
      <!-- <u-form-item :label="$t('message.matcode')">
        <jPicker sureColor="#ff0000" @bindpicker="selectProductCode" showKey="value" valKey="value" :val="productCodeList.value" :options="model.productCodeList" />
      </u-form-item> -->
      <!-- 物料编码 -->
      <!-- <u-form-item :label="$t('message.matcode')">
        <u-input :disabled="true" v-model="histroyItem.materialDesc" placeholder="" style="overflow: hidden" />
      </u-form-item> -->
      <!-- 订单 -->
      <u-form-item label="订单">
        <u-input :disabled="true" v-model="model.orderNoItemList.orderCode" placeholder="" style="overflow: hidden" />
      </u-form-item>
      <!-- 机台 -->
      <u-form-item label="机台">
        <u-input :disabled="true" v-model="model.orderNoItemList.machineName" placeholder="" style="overflow: hidden" />
      </u-form-item>
      <!-- 产品 -->
      <u-form-item label="产品">
        <u-input :disabled="true" v-model="model.orderNoItemList.productName" placeholder="" />
      </u-form-item>
      <!-- 工单 -->
      <u-form-item label="工单">
        <u-input :disabled="true" v-model="model.orderNoItemList.workorderName" placeholder="" />
      </u-form-item>
      <!-- 是否合格 -->
      <u-form-item label="是否合格">
        <jPicker sureColor="#ff0000" @bindpicker="selectProductCode" showKey="label" valKey="value" :val="productCodeList.value" :options="isqualified" />
      </u-form-item>
      <!-- 不良描述 -->
      <u-form-item label="不良描述">
        <u-input v-model="badMeg" placeholder="" type="textarea" :border="border" :height="height" :auto-height="autoHeight" />
      </u-form-item>
      <view class="boxtitle">照片上传</view>
      <scroll-view class="scroll-view_H" scroll-x="true" :scroll-left="scrollLeft()">
        <view class="photoList">
          <view class="itemBox photoBox" v-for="(img, index) in imgUrlList" :key="index">
            <!-- <view class="closeBtn" @click="deleteImg(img)">
									<image class="img" src="../../static/img/iconCloseGray.svg" mode="scaleToFill"></image>
								</view> -->
            <image class="img" :src="img" mode="aspectFit" @click="showScreenImg(index)"></image>
          </view>
          <view class="itemBox chooseBtn" @click="chooseImg">
            <view class="inner">
              <image class="img" src="../../../static/images/upload-pic-icon.svg" mode="scaleToFill"></image>
              <text class="desc">添加照片</text>
            </view>
          </view>
        </view>
      </scroll-view>

      <!-- 库位 -->
      <!-- <u-form-item :label="$t('message.CommissionedLocation')">
        <jPicker sureColor="#ff0000" @bindpicker="selectItem" showKey="value" valKey="value" :val="remove.value" :options="model.WlList" />
      </u-form-item> -->
      <!-- 条码 -->
      <!-- <u-form-item :required="true" :label="$t('message.product_barCode')" prop="productCode">
        <u-search :placeholder="$t('message.barcode')" v-model.trim="barcode" @search="onSubmit" :show-action="false"></u-search>
      </u-form-item> -->
    </u-form>
    <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>
</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 store from '@/store';
@Component({
  components: {
    jPicker,
  },
})
export default class productCheckReceipt extends BasePage {
  model = model;
  // remove: any = {};
  // async onReady() {
  //   await this.model.queryLocation();
  //   this.remove = this.model.WlList[0];
  // }
  // selectItem(e: any) {
  //   this.remove = e.pickerName;
  // }
  orderNo: any = '';
  badMeg = '';
  border = true;
  height = 100;
  autoHeight = true;
  imgList: any = [];
  imgUrlList: any = [];
  isqualified = [
    {
      label: '合格',
      value: '1',
    },
    {
      label: '不合格',
      value: '0',
    },
  ];

  productCodeList: any = {};
  scrollLeft() {
    return 222 * (this.imgList.length + 1);
  }
  showScreenImg(i) {
    let list = this.imgUrlList;
    uni.previewImage({
      current: i,
      urls: list,
      // loop: true
    });
  }
  chooseImg() {
    // if(!this.materialObj){
    // 	this.$refs.toastRef.show({
    // 		type:"error",
    // 		text:"请选择物料！"
    // 	})
    // 	return
    // }
    uni.chooseImage({
      count: 9, //默认9
      // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: (res) => {
        // uni.showLoading();
        //console.log('/////',res);
        let tempFilePaths: any = res.tempFilePaths;
        let imgUrlList: any = res.tempFilePaths;
        tempFilePaths.map((item, index) => {
          uni.uploadFile({
            url: '/api/dev-api/file/upload',
            fileType: 'image',
            filePath: item,
            header: {
              Authorization: 'Bearer ' + session.user?.access_token,
              // 'cosmo-env-sitecode': this.$store.state.siteCode
            },
            name: 'file',
            // formData: {
            // 	materialCode: this.materialObj.materialCode,
            // },
            success: (res) => {
              console.log(res);
              uni.hideLoading();
              if (!res.data) {
                // this.$refs.toastRef.show({
                // 	type:"error",
                // 	text:"上传失败！"
                // })
                return;
              }
              let data = JSON.parse(res.data);
              console.log('123dddd', data);
              this.imgList = [...this.imgList, data.data.url];
              //this.imgListName = [...this.imgListName,data.fileName];
              this.imgUrlList = [...this.imgUrlList, imgUrlList[index]];
              //this.picUrls = this.imgList.join(",");
              //this.picNames = this.imgListName.join(",");
              console.log('this.imgList', this.imgList);
              uni.hideLoading();
            },
            fail: (err) => {
              console.log(err);
              // this.$refs.toastRef.show({
              // 	type:"error",
              // 	text:"上传失败！"
              // })
              uni.hideLoading();
            },
          });
        });
      },
    });
  }
  async query() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      this.customToast('请输入nficl');
      return;
    }
    await this.model.queryOrderNo(this.orderNo);
  }
  histroyItem: any = {};
  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 selectProductCode(e: any) {
    this.productCodeList = e.pickerName;
    if (this.productCodeList.value == '1') {
      // 创建一个新的日期对象
      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 = {
        rfidNo: this.orderNo,
        reqCode: this.generateGUID(),
        reqTime: formattedDate,
        currentStationNo: model.orderNoItemList.machineCode,
      };
      await this.model.sendDestinationStations(params);
    }
  }
  barcode: any = '';
  async onSubmit() {
    if (this.orderNo == '' || this.orderNo.length == 0) {
      this.customToast('请输入nficl');
      return;
    }
    if (this.productCodeList.value == '') {
      this.customToast('请选择是否合格');
      return;
    }
    if (this.badMeg == '') {
      this.customToast('请输入不良描述');
      return;
    }
    console.log('tupian', this.imgList.join(','));
    let params = {
      rfid: this.orderNo,
      factoryCode: session.FactoryCode,
      orderCode: model.orderNoItemList.orderCode,
      workorderCode: model.orderNoItemList.workorderCode,
      machineCode: model.orderNoItemList.machineCode,
      machineName: model.orderNoItemList.machineName,
      badMeg: this.badMeg,
      productCode: model.orderNoItemList.productCode,
      productName: model.orderNoItemList.productName,
      productSpc: model.orderNoItemList.productSpc,
      isqua: this.productCodeList.value,
      fileId: this.imgList.join(','),
    };
    await this.model.OnSubmit(params);
    if (this.model.SubmitCode == '1') {
      this.customToast('质检成功！');
      this.orderNo = '';
      this.badMeg = '';
      this.productCodeList = {};
      this.model.empty();
      await 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;
    }
  }

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

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