<template>
  <div class="fruit2">
    <div class="content1">
      <div class="top">
        <div class="left">
          <div class="icon"></div>
          <div class="title" v-if="type != 3">线下兑换历史</div>
          <div class="title" v-else>线上兑换历史</div>
        </div>
        <van-button
          v-if="type == 3"
          round
          size="small"
          type="danger"
          @click="instruction"
          >使用说明</van-button
        >
      </div>
      <div class="bottom">
        <div class="card" v-for="(item, index) in list" :key="index">
          <div class="smallTop">
            <div class="left">
              <div class="img"></div>
              <div class="name">{{ item.fruitsName }}</div>
              <div class="num">{{ item.weight }}kg</div>
            </div>
            <div class="right" v-if="item.exchangeState == 0">未使用</div>
            <div class="right" v-else>已使用</div>
          </div>
          <div class="content" v-if="type != 3">
            <div class="title">提货点：</div>
            <div class="address">{{ item.address }}</div>
          </div>
          <div class="smallBottom" v-if="type == 3">
            <div class="title">兑换码：</div>
            <div class="time" style="margin-right: 20px; color: #1989fa">
              {{ item.exchangeCode }}
            </div>
            <van-button
              plain
              hairline
              round
              type="info"
              size="mini"
              @click="copyClick(item.exchangeCode)"
            >
              点击复制
            </van-button>
          </div>
          <div class="smallBottom" v-if="type == 3">
            <div class="title">提货用户：</div>
            <div class="time">{{ item.userMobile }}</div>
          </div>
          <div class="smallBottom" style="justify-content: space-between">
            <div style="display: flex; align-items: center">
              <div class="title">提货时间：</div>
              <div class="time" style="margin-right: 20px">
                {{ item.createTime }}
              </div>
              <button
                v-if="type == 3 && item.exchangeState == 0"
                style="width: 80px; background: #07c160; border-radius: 5px"
              >
                <a
                  style="color: #fff"
                  href="weixin://dl/business/?t=i8DMFbqGUng"
                  >去使用</a
                >
              </button>
              <button
                v-if="type == 3 && item.exchangeState != 0"
                style="width: 80px; background: #07c160; border-radius: 5px"
              >
                <a
                  style="color: #fff"
                  href="weixin://dl/business/?t=i8DMFbqGUng"
                  >订单查询</a
                >
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="button" v-if="type == 3">
      <div class="operation" @click="ensure">领取记录</div>
      <div class="safe-area"></div>
    </div>
    <div class="button" v-if="type != 3">
      <div class="operation" @click="scan">扫一扫</div>
      <div class="safe-area"></div>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import { Button, Dialog } from "vant";
Vue.use(Button).use(Dialog);
export default {
  name: "fruit2",
  data() {
    return {
      list: [],
      type: "",
    };
  },
  methods: {
    // 新增一个input标签，设置其value为复制的内容，再使用execCommand()方法实现复制功能
    copyClick(val) {
      const input = document.createElement("input");
      document.body.appendChild(input);
      input.setAttribute("value", val);
      input.select();
      if (document.execCommand("copy")) {
        // IOS不支持此方法，所以复制不成功
        document.execCommand("copy");
      }
      document.body.removeChild(input);
      Dialog.alert({
        message: "复制成功！",
      }).then(() => {
        // on close
      });
    },
    instruction() {
      Dialog.alert({
        messageAlign: 'left',
        title: "使用说明",
        message:"1.您已成功领取扶贫水果线上兑换券，可复制以下兑换码，点击去使用进入供应商页面进行兑换。\n2. 如有APP中领券的使用问题，可拔打0511-87050298进行咨询；水果领取等相关问题，请拨打水果兑换券背面联系电话。",
      }).then(() => {
        // on close
      });
    },
    ensure() {
      this.$router.replace({
        name: "FruitDetail",
      });
    },
    scan() {
      this.$router.replace({
        name: "ReportScancode",
      })
    }
  },
  mounted() {
    this.list = this.$route.query.exchangeRecordList;
    this.type = this.$route.query.qrCodeType;
  },
};
</script>
<style lang="stylus" scoped>
.fruit2 {
  height: 100vh;
  display: flex;
  flex-direction: column;

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

  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #fff;
    padding: 30px;

    .left {
      display: flex;
      align-items: center;

      .icon {
        width: 6px;
        height: 30px;
        background: #EB4E3F;
      }

      .title {
        font-size: 30px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
        margin-left: 10px;
      }
    }
  }

  .bottom {
    display: flex;
    flex-direction: column;

    .card {
      display: flex;
      flex-direction: column;
      padding: 30px;
      margin-top: 10px;
      background: #fff;

      .smallTop {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 32px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333;

        .left {
          display: flex;
          align-items: center;

          .img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: #EB4E3F;
          }

          .name {
            margin: 0 20px;
          }
        }

        .right {
          color: #EB4E3F;
        }
      }

      .content {
        display: flex;
        align-items: center;
        margin-top: 20px;
        font-size: 32px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #999;

        .title {
          width: 180px;
        }
      }

      .smallBottom {
        display: flex;
        align-items: center;
        margin-top: 20px;
        font-size: 32px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #999;

        .title {
          width: 180px;
        }
      }
    }
  }

  .button {
    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);

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