<template>
  <div v-if="show" class="box" :class="position">
    <cr-radio-group v-model="activityPrice" direction="vertical" @change="changePrice">
      <cr-radio
        v-for="item in goodInfo.canSelectPrices"
        :key="item.activityTemplateId"
        :name="item.activityTemplateId"
        ><div class="radioItem">
          {{ item.title }}:
          <p class="price" :style="{ color: '#' + item.priceFontColor }">
            ￥{{ item.price
            }}<img
              :src="item.priceIcon"
              style="height: 14px"
              :style="{ width: 14 * +item.priceIconRate + 'px' }"
            />
          </p></div
      ></cr-radio>
    </cr-radio-group>
  </div>
</template>
<script>
import { changeActivity } from '@/api/shopCart.api';
import { mapActions } from '../shopCartModules';
export default {
  inject: ['cart'],
  props: {
    goodInfo: {
      type: Object,
      default: () => ({})
    },
    position: {
      type: String,
      default: 'top'
    },
    show: Boolean
  },
  data() {
    return {
      activityPrice: ''
    };
  },
  computed: {
    selectedPrice: function() {
      const item = this.goodInfo.canSelectPrices?.find(i => i.selected) || {};
      return item.activityTemplateId;
    }
  },
  watch: {
    selectedPrice: {
      immediate: true,
      handler(v) {
        this.activityPrice = v;
      }
    }
  },
  methods: {
    ...mapActions({
      updateCartList: 'update_cart_list'
    }),
    async changePrice() {
      const item = this.goodInfo.canSelectPrices?.find(
        i => i.activityTemplateId === this.activityPrice
      );
      const params = {
        skuNo: this.goodInfo.skuId,
        activityTemplateId: this.activityPrice,
        activityTemplateDetailId: item.activityTemplateDetailId || ''
      };
      const [res] = await changeActivity(params);
      if (res) {
        this.cart.queryList({ pageNo: 1, pageSize: 1000 });
      }
      this.$emit('closeModal');
      this.cart.closeMask();
    }
  }
};
</script>
<style lang="less" scoped>
.box {
  font-size: 12px;
  width: 351px;
  background: #ffffff;
  position: relative;
  border-radius: 8px;
  z-index: 9000;
  padding: 20px 8px 8px 20px;
  box-sizing: border-box;
}
.bottom::before {
  content: '';
  width: 0;
  height: 0;
  border: 8px solid transparent;
  border-bottom-color: #fff;
  position: absolute;
  top: 0;
  left: 233px;
  margin-top: -14px;
}
.top::before {
  content: '';
  width: 0;
  height: 0;
  border: 8px solid transparent;
  border-top-color: #fff;
  position: absolute;
  bottom: 0;
  left: 233px;
  margin-bottom: -14px;
}
.radioItem {
  display: flex;
  align-items: center;
}
.price {
  font-size: 16px;
  display: flex;
  align-items: center;
  margin-left: 5px;
}
::v-deep .cr-radio {
  margin-bottom: 12px;
}
</style>
