<template>
  <!-- 未支付 -->
  <div class="Nopyment" v-loading="loading">
    <div class="message">
      <div>
        <img
          :src="require('../../assets/teacherdev/' + getimgurl(data) + '.png')"
          alt=""
        />
      </div>
      <div class="text">
        <p class="p1">
          {{ data.name }}
        </p>
        <p class="p2">
          <span><!-- 教研资料 -->{{ $t("Key214") }}</span>
        </p>
        <p class="p3">
          <span v-for="(item, i) in data.teacher" :key="i + item">
            {{ item }}{{ i == data.teacher.length - 1 ? "" : "," }}
          </span>
        </p>
      </div>
      <div class="price">
        <p>¥<span v-html="changePrice(data.price, 16)"></span></p>
      </div>
    </div>
    <div class="promotionCode">
      <span class="sp1"> <!-- 使用优惠码 -->{{ $t("Key107") }} </span>
      <input
        v-model="discount_code"
        type="text"
        placeholder="请输入兑换码"
        @input="changeCode"
      />
      <span
        class="sp2"
        v-loading="codeloading"
        @click="verifyCode"
        v-if="isShow"
      >
        <!-- 确定 -->{{ $t("Key94") }}
      </span>
      <template v-else>
        <span class="sp2" v-if="discount_code_status == 1"
          >-¥<span v-html="changePrice(discount_money, 16)"></span
        ></span>
      </template>
    </div>
    <div class="total">
      <p class="p1">
        <span> {{ $t("Key53") }}： </span>
        <span class="co-value"
          >¥<span v-html="changePrice(data.price, 16)"></span
        ></span>
      </p>
      <p class="p2">
        <span> {{ $t("Key54") }}： </span>
        <span class="co-value"
          >-¥<span v-html="changePrice(discount_money, 16)"></span
        ></span>
      </p>
      <p class="p3">
        <span> {{ $t("Key55") }}： </span>
        <span class="co-value"
          >¥<span v-html="changePrice(receivables_money, 24)"></span
        ></span>
      </p>
    </div>
    <div class="submitBtn">
      <button @click="buy(data)"><!-- 确定订单 -->{{ $t("Key58") }}</button>
    </div>
  </div>
</template>

<script>
//这里可以导入其它文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》from ‘《组件路径》';
import { LearnWebSI } from "@/api/api";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: ["data", "changeOrderNumber", "goods_type", "goods_detail_type"],
  data() {
    //这里存放数据
    return {
      discount_code: "", //优惠码
      discount_money: 0, // 优惠金额
      receivables_money: this.data.price, // 应收款
      loading: false,
      codeloading: false,
      isShow: true,
      discount_code_status: 0,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中数据变化
  watch: {},
  //方法集合
  methods: {
    changePrice(price, fontSize1, fontSize2) {
      price = price ? price * 1 : 0;
      price = price.toFixed(2);
      price = price.toString();
      let arr = price.split(".");
      let str = `<span style="font-size: ${fontSize1 ? fontSize1 : 16}px;">${
        arr[0]
      }</span>.<span style="font-size: ${fontSize2 ? fontSize2 : 16}px;">${
        arr[1]
      }</span>`;

      return str;
    },
    changediscount_money(value) {
      if (value == 0) {
        return "-¥" + value + ".00";
      }
    },
    //   根据不同的文件类型展示不同图片
    getimgurl(item) {
      let index = item.tag.indexOf("downloadable");
      let type = "";
      if (index != -1) {
        type = item.tag[2];
      } else {
        type = item.tag[1];
      }
      switch (type) {
        case "ppt":
          return "ppt";
        case "pptx":
          return "ppt";
        case "pdf":
          return "pdf";
        case "xlsx":
          return "exceil";
        case "xls":
          return "exceil";
        case "doc":
          return "word";
        case "docx":
          return "word";
        case "word":
          return "word";
        default:
          return "word";
      }
    },
    // 购买
    buy(item) {
      this.loading = true;
      // 首先添加订单
      let Mnam = "order-order_manager-AddMyOrder";
      LearnWebSI(Mnam, {
        goods_id: item.id, //商品id
        goods_type: this.goods_type, //商品类型
        goods_name: item.name, //商品名称
        goods_picture_id: item.coverFileId, //封面图片id
        goods_person_name_desc: this.data.pymentTeacher, //老师
        price: item.price, //价格
        discount_code: this.back_discount_code,
        goods_detail_type: this.goods_detail_type,
      })
        .then((res) => {
          this.changeOrderNumber(
            res.id,
            this.back_discount_code,
            this.discount_money,
            this.receivables_money
          );
          this.loading = false;
          // 调取支付接口
        })
        .catch((res) => {
          this.loading = false;
        });
    },
    //更改优惠码
    changeCode() {
      this.isShow = true;
      this.receivables_money = this.data.price;
      this.discount_money = "0.00";
    },
    clearData() {
      this.discount_code = "";
      this.discount_money = "0.00";
      this.receivables_money = this.data.price;
    },
    //验证优惠码
    verifyCode() {
      if (this.discount_code == "") {
        this.$message.warning(this.$t("Key109"));
        return;
      }
      this.codeloading = true;
      let MethodName = "order-order_manager-VerificationDiscountCode";
      let resData = JSON.parse(JSON.stringify(this.data));
      let data = {
        goods_id: resData.id, //商品id
        goods_type: this.goods_type, //商品类型
        price: resData.price, //价格
        discount_code: this.discount_code.trim(),
      };
      LearnWebSI(MethodName, data).then((res) => {
        this.codeloading = false;
        this.discount_code_status = res.discount_code_status;
        if (this.discount_code_status != 1 && this.discount_code_status != 2) {
          this.$message.warning(this.$t("Key696")); //优惠码无效
        } else {
          if (this.discount_code_status == 2) {
            this.$message.warning(this.$t("Key697")); //优惠码已被使用
          } else {
            this.isShow = false;
            this.back_discount_code = res.discount_code;
            this.discount_money = res.discount_money; // 优惠金额
            this.receivables_money = res.receivables_money; // 应收款
          }
        }
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  //生命周期-创建之前
  beforeCreated() {},
  //生命周期-挂载之前
  beforeMount() {},
  //生命周期-更新之前
  beforUpdate() {},
  //生命周期-更新之后
  updated() {},
  //生命周期-销毁之前
  beforeDestory() {},
  //生命周期-销毁完成
  destoryed() {},
  //如果页面有keep-alive缓存功能，这个函数会触发
  activated() {},
};
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
.Nopyment {
  .message {
    width: 100%;
    height: 152px;
    background: rgba(70, 70, 70, 0.03);
    border-radius: 8px;
    display: flex;
    img {
      width: 120px;
      height: 120px;
      margin-left: 16px;
      margin-top: 16px;
    }
    .text {
      margin-left: 24px;
      .p1 {
        width: 360px;
        //   height: 45px;
        max-height: 48px;
        font-size: 16px;
        line-height: 20px;
        color: #2c2c2c;

        margin-top: 22px;
        word-break: break-all;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .p2 {
        margin-top: 10px;
        span {
          width: 64px;
          height: 24px;
          background: #ffefd8;

          border-radius: 4px;
          font-weight: bold;
          font-size: 12px;
          text-align: center;
          color: #ff9900;

          line-height: 24px;
          padding: 2px 8px;
        }
      }
      .p3 {
        margin-top: 10px;
      }
    }
    .price {
      p {
        margin-left: 67px;
        margin-top: 22px;
        font-weight: bold;
        font-size: 16px;
        text-align: right;
        color: #ff4c00;
      }
    }
  }
  .promotionCode {
    display: flex;
    justify-content: flex-start;
    align-self: center;
    width: 100%;
    margin-top: 24px;
    height: 58px;
    line-height: 56px;
    border-top: 1px solid rgba(44, 44, 44, 0.15);
    border-bottom: 1px solid rgba(44, 44, 44, 0.15);
    .sp1 {
      font-size: 16px;
      color: #000000;
    }
    input {
      padding: 0 24px;
      flex: 1;
      height: 56px;
      outline: none;
      border: none;
      box-sizing: border-box;
    }
    .sp2 {
      cursor: pointer;
      font-size: 16px;
      color: #ff9900;
    }
  }
  .total {
    width: 656px;
    text-align: right;
    color: #000000;
    font-size: 16px;
    padding-top: 24px;
    > p {
      > span {
        display: inline-block;
      }
      .co-value {
        width: 160px;
      }
    }
    .p1 {
      > span {
        display: inline-block;
        line-height: 24px;
      }
    }
    .p2 {
      margin: 16px 0 10px;
    }
    .p3 {
      > span {
        line-height: 36px;
      }
      .co-value {
        font-size: 24px;
        color: #ff4c00;
      }
    }
  }
  .submitBtn {
    text-align: right;
    margin-top: 16px;
    button {
      width: 120px;
      height: 40px;
      background: #ff9900;

      border-radius: 4px;
      color: white;
      line-height: 40px;
      text-align: center;
      border: none;
      outline: none;
      cursor: pointer;
    }
  }
}
</style>
