<template>
  <div class="fruit4">
    <div :class="[this.$route.query.qrCodeType == 1 ? 'content' : 'content1']">
      <div
        :class="[item.useState == 0 ? 'select' : 'unselect']"
        @click="change(item)"
        v-for="(item, index) in list"
        :key="index"
      >
        <div
          class="left"
          style="display: flex; align-items: center; height: 125px"
        >
          <div class="selectimg" style="margin: 0 10px">
            <img v-if="item.useState == 0" src="./images/4-5.png" alt="" />
            <img v-else src="./images/4-4.png" alt="" />
          </div>
          <div class="fruitimg" style="margin: 0 10px">
            <img
              style="width: 100px; height: 100px"
              :src="item.fruitsPicture"
              alt=""
            />
          </div>
          <div class="title">
            <div class="name">{{ item.fruitsName }}</div>
            <div
              class="amount"
              style="
                width: 130px;
                display: flex;
                align-items: center;
                justify-content: space-between;
              "
            >
              <div class="num" style="color: #eb4e3f">{{ item.weight }}kg</div>
              <div class="count" style="color: #999999; font-size: 16px">
                数量：1
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="card" v-for="(item, index) in list1" :key="index">
        <div
          class="left"
          style="display: flex; align-items: center; height: 125px"
        >
          <div class="selectimg" style="margin: 0 10px">
            <img src="./images/4-4.png" alt="" />
          </div>
          <div class="fruitimg" style="margin: 0 10px">
            <img
              style="width: 100px; height: 100px"
              :src="item.fruitsPicture"
              alt=""
            />
          </div>
          <div class="title">
            <div class="name">{{ item.fruitsName }}</div>
            <div
              class="amount"
              style="
                width: 130px;
                display: flex;
                align-items: center;
                justify-content: space-between;
              "
            >
              <div class="num" style="color: #eb4e3f">{{ item.weight }}kg</div>
              <div class="count" style="color: #999999; font-size: 16px">
                数量：1
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="title" v-if="this.$route.query.qrCodeType == 1">
        当前为线上兑换，是否转为<span @click="jump">线下兑换券</span>
      </div>
      <div class="operation" @click="ensure">确认兑换</div>
      <div class="safe-area"></div>
    </div>
  </div>
</template>

<script>
import { onLinePickUpGoods } from "@/api/fruit.js";
import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);
export default {
  name: "fruit4",
  data() {
    return {
      userInfo: {},
      list: [],
      list1: [],
    };
  },
  created() {
    let res = sessionStorage.getItem("ZJCD-UserInfo");
    this.userInfo = JSON.parse(res);
    this.list = this.$route.query.convertibleInfoList.filter((item) => {
      if (item.useState == 0 || item.useState == 2) {
        return item;
      } else if (item.useState == 1) {
        this.list1.push(item);
      }
    });
  },
  methods: {
    change(val) {
      this.list.map((item) => {
        if (item.id == val.id) {
          if (item.useState == 0) {
            item.useState = 2;
          } else {
            item.useState = 0;
          }
        }
      });
    },
    jump() {
      this.$router.replace({
        name: "Fruit5",
        query: this.$route.query,
        params: { qrCode: this.$route.params.qrCode },
      });
    },
    ensure() {
      let fruitsList = this.list
        .filter((item) => {
          if (item.useState == 0) {
            return item;
          }
        })
        .map((ele) => {
          return {
            fruitsCode: ele.fruitsCode,
            id: ele.id,
            weight: ele.weight,
            extractOrder: ele.sort,
          };
        });
      if (fruitsList.length > 0 && fruitsList) {
        let params = {
          fruitsList,
          qrCode: this.$route.params.qrCode,
          supplyCode: "",
          uid: this.userInfo.userId,
          userMobile: this.userInfo.mobileNo,
          userName: this.userInfo.realName,
        };
        onLinePickUpGoods(params).then(() => {
          this.$router.replace({
            name: "Fruit6",
            query: this.$route.params.qrCode,
          });
        });
      } else {
        Toast.fail("至少选择一项！");
      }
    },
  },
};
</script>

<style lang="stylus" scoped>
.fruit4 {
  height: 100vh;
  display: flex;
  flex-direction: column;

  .content {
    box-sizing: border-box;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(100vh - 100px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .content1 {
    box-sizing: border-box;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(100vh - 100px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .card {
    box-sizing: border-box;
    width: 90%;
    height: 260px;
    background: url('./images/4-1.png');
    margin: 10px 0;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    font-size: 50px;
    display: flex;
    justify-content: space-between;

    .title {
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }

  .select {
    box-sizing: border-box;
    width: 90%;
    background: #fff;
    height: 260px;
    margin: 10px 0;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    font-size: 50px;
    display: flex;
    justify-content: space-between;

    .title {
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }

  .unselect {
    box-sizing: border-box;
    width: 90%;
    background: #fff;
    height: 260px;
    margin: 10px 0;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    font-size: 50px;
    display: flex;
    justify-content: space-between;

    .title {
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }

  .bottom {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 0;
    width: 100%;
    left: 0px;
    box-sizing: border-box;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);

    .title {
      text-align: center;
      font-size: 35px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      line-height: 40px;

      span {
        color: #EB4E3F;
      }
    }

    .operation {
      width: 100%;
      background: #EB4E3F;
      font-size: 40px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #fff;
      line-height: 100px;
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>