<template>
  <view class="pay-type-list">
    <view class="type-title b-b">{{ value.title }}</view>
    <payGroupItem
      v-if="value.isGroupPay"
      :value="payList"
      :pay-type="payType"
      :disabled="disabled"
      :coupon-info="couponInfo"
      :show-coupon="showCoupon"
      :pay-coupon-list="payCouponList"
      :coupon-disabled="showCoupon && !payCouponCouldBeUsed"
    />
    <template v-else>
      <template v-for="(item, key) of payList">
        <payCardItem
          v-if="item.show"
          :key="key"
          :pay-type="payType"
          :value="item"
          :disabled="disabled"
          :coupon-info="couponInfo"
          :show-coupon="showCoupon"
          :pay-coupon-list="payCouponList"
          :coupon-disabled="showCoupon && !payCouponCouldBeUsed"
        />
      </template>
    </template>
    <!-- MAX会员分期免息提示，小程序暂不支持分期功能， -->
    <maxMemberInstallmentDesc v-if="showMaxMemberNotice" :display-info="displayInfo" />
  </view>
</template>
<script>
import payCardItem from "./payCardItem";
import payGroupItem from "./payGroupCard";
import maxMemberInstallmentDesc from "./maxMemberInstallmentDesc";
export default {
  name: "PayCard",
  components: {
    payCardItem,
    payGroupItem,
    maxMemberInstallmentDesc
  },
  provide() {
    return {
      payCard: this
    };
  },
  props: {
    value: Object,
    payType: Number,
    payText: String,
    disabled: Boolean,
    couponInfo: Object,
    riskLimit: Number,
    showCoupon: Boolean,
    payCouponCouldBeUsed: Boolean,
    payCouponList: { type: Array, default: () => [] },
    single: { type: Boolean, default: false },
    creditPay: { type: Boolean, default: false },
    displayInfo: { type: Object, default: () => {} }
  },
  data() {
    return {
      payList: this?.value?.payList || {}
    };
  },
  computed: {
    showMaxMemberNotice() {
      // 小程序收银台不支持分期功能，MAX会员引导去app下单
      if (!this.displayInfo) return false;
      let installmentStatus = false; // 分期付款状态
      let detailList = []; // 分期付款详情列表
      const { installmentInfo } = this.displayInfo;
      try {
        detailList = installmentInfo.detailList || [];
      } catch (e) {
        detailList = [];
      }
      // 遍历分期付款选项，选项中包括免息字段allowCancelInterest，并且值为1，则允许免息
      for (let i = 0; i < detailList.length; i++) {
        if (detailList[i].allowCancelInterest == 1) {
          installmentStatus = true;
          break;
        }
      }
      if (!installmentInfo || !installmentInfo.isInstallment || !installmentStatus) return false;

      return true;
    }
  },
  watch: {
    value(newValuem) {
      this.payList = newValuem?.payList || {};
    }
  }
};
</script>
<style lang="scss">
.pay-type-list {
  background-color: #fff;
  border-radius: $border-radius-md;
  margin-top: $margin-base;
  .type-title {
    height: 96rpx;
    line-height: 96rpx;
    font-size: $uni-font-size-lg;
    font-weight: bold;
    padding-left: $padding-md;
    position: relative;
  }
}
</style>
