<template>
  <view style="background: #fff; min-height: calc(100vh - 44px)">
    <view class="search-header">
      <!-- <uni-search-bar class="uni-mt-10" @confirm="search" placeholder="请输入订单号" radius="100" v-model="keyword" cancelButton="none" clearButton="none" ></uni-search-bar> -->
      <scanlistener
          v-if="showScanlistener"
          suffixIcon="scan"
          v-model="keyword"
          :placeholder="$t('请扫描快递单号')"
          @iconClick="scanCode"
          @confirm="search"
          @submit="keyword=$event;search()"
      ></scanlistener>
      <!--      <button class="margin-left-xs" type="primary" size="mini" @click="quickSign()" style="float: right">-->
      <!--        {{ $t('快速签收') }}-->
      <!--      </button>-->
    </view>
    <uni-forms ref="form" :rules="rules" :modelValue="cons" label-width="100" class="padding-xs">
      <uni-forms-item :label="$t('集运仓库')" required name="wareId">
        <uni-data-checkbox v-model="cons.wareId" :localdata="warehouseList" mode="tag"></uni-data-checkbox>
      </uni-forms-item>
    </uni-forms>

  </view>
</template>

<script>

import {
  createCons,
  getConsById, getConsWarehouseApp, signedPda

} from "@/api/cons";
import selectCombox from "@/components/select-combox/select-combox.vue";

export default {
  name: "quick-sign-cons",
  components: {selectCombox},
  computed: {},
  data() {
    return {
      keyword: '',
      consModel: false,
      warehouseList: [],
      rules: {
        wareId: {
          rules: [{
            required: true,
            errorMessage: '集运仓库不能为空'
          }]
        }
      },
      cons: {}
    };
  },
  onLoad: function () {
    getConsWarehouseApp().then(row => {
      this.warehouseList = row
      if (row) {
        this.cons = {
          wareId: row[0].value
        }
      }
    })
  },
  methods: {
    getList() {
      console.log('cons', this.cons)
      this.$refs['form'].validate().then(res => {
        if (this.keyword !== '') {
          getConsById({expressNo: this.keyword}).then(row => {
            if (row !== null) {
              if (row.signedTime === null) {
                // 获取当前时间精确至[年、月、日、时、分、秒]
                var now = new Date()
                var year = now.getFullYear()
                var month = now.getMonth()
                var date = now.getDate()
                var hours = now.getHours()
                var minutes = now.getMinutes()
                var seconds = now.getSeconds()
                month = month + 1
                month = month.toString().padStart(2, '0')
                date = date.toString().padStart(2, '0')
                hours = hours.toString().padStart(2, '0')
                minutes = minutes.toString().padStart(2, '0')
                seconds = seconds.toString().padStart(2, '0')
                var defaultDate = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`
                row['signedTime'] = defaultDate
              }
              row.mediaUrl = (row.mediaUrl === null || row.mediaUrl === '') ? [] : row.mediaUrl.split(',').map(e => {
                return {
                  url: e,
                  type: ['jpg', 'png', 'jpeg', 'heic'].includes(e.split('.')?.reverse()[0]) ? 'image' : 'video'
                }
              })
              if (row.signed) {
                uni.showToast({
                  title: '包裹已签收，请勿重复扫描',
                  icon: 'none'
                })
                return
              }
              var form = {
                ...row,
                mediaUrl: row.mediaUrl.map(e => e.url).join(',')
              }
              form.status = 1
              form.signer = this.$store.getters.userInfo().id
              form.signed = 1
              this.cons = form
              signedPda(form).then(() => {
                uni.showToast({
                  title: `签收成功`
                })
                this.keyword = ''
              })
            } else {
              var form = {
                wareId: this.cons.wareId,
                expressNo: this.keyword,
                status: 1,
                signer: this.$store.getters.userInfo().id,
                signed: 1
              }
              this.cons = form
              createCons(form).then(() => {
                uni.showToast({
                  title: `签收成功`
                })
                this.keyword = ''
              })
            }
          })
        }
      }).catch(err => {
        console.log('err', err);
      })
    },
    search() {
      this.getList()
    },
    scanCode() {
      uni.scanCode({
        success: (res) => {
          this.keyword = res.result
          this.getList()
        }
      })
    },
    quickSign() {
      if (this.keyword !== '') {
        console.log('quickSign', this.cons)
      } else {
        uni.showToast({
          title: '快递单号不能为空',
          icon: 'none'
        })
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.search-header {
  ::v-deep .uni-easyinput__content {
    border-radius: 20px;
    padding-right: 10px;
    background: rgba($color: #fff, $alpha: 0.5) !important;
    border: none;
    // opacity: 0.5;
  }

  ::v-deep .uni-input-placeholder {
    padding-left: 8px;
    color: #b2c9f3;
  }

  ::v-deep .uni-input-input {
    padding-left: 8px;
  }
}

.scroll {
  margin-top: -8px;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  padding: 8px;
  background: #fff;

  .list-row {
    display: flex;
    align-items: center;
    // line-height: 24px;
    padding: 8px 0;

    view {
      flex: 1;
      text-align: center;
    }
  }

  .list-header {
    font-weight: bold;
  }

  .row-bg {
    background: #ebebed;
  }

  // .red-badge-wrap{
  //   position: absolute;
  //   right: -34upx;
  //   top: -26upx;
  // }
  .red-badge {
    display: inline-block;
    // background-color: #dd514c;
    color: #d9001c;
    // font-size: 24upx;
    // width: 30upx;
    // height: 30upx;
    // line-height: 30upx;
    // vertical-align: middle;
    // border-radius: 30upx;
    // margin-left: 2upx;
    /* transform: scale(0.9); */
  }
}

.img-wrap {
  ::v-deep .uni-grid-item {
    background: #fbf8fb;
  }

  .grid-item-box-add {
    height: 100%;
    display: flex;
    flex-flow: column;
    font-size: 10px;
    padding: 16px 0;
    text-align: center;
    justify-content: center;
  }
}

.dis {
  pointer-events: none
}
</style>
