<template>
  <div class="member">
    <div class="member-head">
      <div class="member-head-title">{{ $t('personalCenter.openVip') }}</div>
      <!-- <ul class="member-head-list">
        <li v-for="(item, index) in 4" :key="index">小标题优惠文案</li>
      </ul> -->
    </div>
    <div class="member-list">
      <div v-for="(item, index) in list" :key="index" class="li">
        <div class="name">{{ item.typeName }}</div>
        <div class="desc">{{ item.description }}</div>
        <div class="flex-align-center price">
          <div class="price-left">
            <span>￥</span>
            <span>{{ item.price }}</span>
            <!-- <span>/{{ item.durationUnit }}</span> -->
          </div>
          <!-- <div class="price-right">￥79.00/月</div> -->
        </div>
        <div class="btn" :class="{ btns: item.memberType == 2 }" @click="pay(item)">{{ $t('personalCenter.activate')
        }}{{
            item.typeName }}</div>
        <div class="line">
          <div class=""></div>
          <div class="">{{ $t('personalCenter.zxqy') }}</div>
          <div class=""></div>
        </div>
        <div class="uls">
          <div v-for="(em, ind) in privilegeRes[item.memberType]" :key="ind" class="lis flex-center-between">
            <div class="">{{ em.privilegeName }}</div>
            <div class="">{{ em.privilegeValue == -1 ? $t('personalCenter.wx') : `${em.privilegeValue}${em.unit ?
              em.unit : ''}` }}</div>
          </div>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" :title="$t('common.tip')" width="600" align-center>
      <span>{{ $t('common.payNow') }} {{ itemData.price }} {{ $t('common.baomibi') }}？</span>
      <template #footer>
        <div class="flex-center-between">
          <div class="">
            <el-checkbox v-model="checked" label="Option 1" size="large">
              <template #default>
                <div class="">{{ $t('personalCenter.qydbty') }} <span @click="changeShow"
                    style="color: #2D71FF;font-weight: bold;">《{{ $t('personalCenter.hyxy') }}》</span> </div>
              </template>
            </el-checkbox>
          </div>
          <div class="dialog-footer">
            <el-button @click.stop.prevent="dialogVisible = false">{{ $t('common.cancel') }}</el-button>
            <el-button class="gradient" type="primary" @click.stop.prevent="closeDialog">
              {{
                $t('common.confirm') }}
            </el-button>
          </div>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="showWithdraw" :title="$t('personalCenter.hyxy')" width="50%" align-center>
      <div class="" style="height: 500px;overflow-y: scroll;" v-html="agreementContent"></div>
      <template #footer>
        <div class="dialog-footer flex-center">
          <el-button class="gradient" type="primary" @click="confirmAgreement">{{
            $t('common.confirm') }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">



import { ref, onMounted } from 'vue';


import { useRouter } from 'vue-router'

import { useAppStore } from '@/pinia/appStore'

import { vipList, privilege } from '@/api/my.js';

import { confirmBuy } from '@/utils/util.js'

import { useI18n } from 'vue-i18n'

import { getAgreementType } from '@/api/common.js'

import { createOrder } from '@/api/order.js'

import DGTMessage from '@/utils/message'


import { ElMessage, ElMessageBox } from 'element-plus'

const { t } = useI18n()

const appStore = useAppStore()

const router = useRouter()

const list = ref([]);
const privilegeRes = ref({ 1: [], 2: [] })

const dialogVisible = ref(false);
const itemData = ref({});
const checked = ref(false);
const showWithdraw = ref(false);
const agreementContent = ref('');

const confirmAgreement = async () => {
  checked.value = true;
  showWithdraw.value = false;
}

const changeShow = async () => {
  let res = await getAgreementType({
    agreementType: 'membership_agreement'
  });
  if (res.code === 200) {
    agreementContent.value = res.data.content || '';
    showWithdraw.value = true;
  }
}

const closeDialog = async () => {

  if (!checked.value) {
    return DGTMessage.warning(t('personalCenter.qydbtyhyxy'));
  }
  dialogVisible.value = false;
  if (appStore.userInfo.baoMiBalance < itemData.value.price) {
    ElMessageBox.confirm(t('common.balanceNotEnough'), t('common.tip'), {
      confirmButtonText: t('common.confirm'),
      cancelButtonText: t('common.cancel'),
      type: 'warning'
    }).then(() => {
      // 确认购买
      router.push({
        path: `/order-confirm`,
      })
    }).catch(() => {
      // 取消购买
    });
    return
  }
  const res = await createOrder({
    payMethod:'BMI',
    orderType:'member_recharge',
    orderAmt: itemData.value.price,
    productId: itemData.value.typeId,
    orderNum: 1
  })
  if (res.code === 200) {
    DGTMessage.success(t('common.paySuccess'))
    appStore.USERINFO();
    router.push({ path: '/personal-center/wallet' });
  }
}

const pay = async (item) => {
  console.log('item', item)
  itemData.value = item;
  dialogVisible.value = true;
  // let res = await confirmBuy({
  //   memberType: item.memberType,
  //   price: item.price
  // });
  // if (res) {
  //   appStore.getUserInfo();
  //   router.push({ path: '/personal-center' });
  // }

  // confirmBuy({
  //         callback: getList,
  //         appStore,
  //         router,
  //         type: 'baoMiBalance',
  //         price: item.price,
  //         t,
  //         productId: item.typeId,
  //         orderType: 'member_recharge',
  //         payMethod: 'BMI'
  //       })
}

const getprivilegeRes = async (memberType) => {
  let privilegeRes = await privilege(memberType);
  return privilegeRes.data;
}

const getList = async () => {

  let privilegeRes1 = await privilege(1);
  let privilegeRes2 = await privilege(2);
  for (const key in privilegeRes.value) {
    if (key == 1) {
      privilegeRes.value[key] = privilegeRes1.data;
    } else {
      privilegeRes.value[key] = privilegeRes2.data;
    }
  }
  let res = await vipList();
  list.value = res.data;
}

onMounted(() => {
  getList()
})

</script>
<style lang="scss">
.member {
  max-width: 1246px;
  margin: 0 auto;
  position: relative;
}

.member-head {
  padding: 70px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  line-height: 1;

  .member-head-title {
    color: #8B3D22;
    font-size: 36px;
    font-weight: bold;
  }

  .member-head-list {
    display: flex;

    margin-top: 24px;

    .li {
      font-size: 18px;
      margin-right: 66px;

      &:last-child {
        margin-right: 0;
      }
    }
  }
}

.member-list {
  display: flex;
  gap: 48px;
  flex-wrap: wrap;

  .li {
    width: 383px;
    padding: 24px 32px;
    background: #FFFFFF;
    border-radius: 16px;
    display: flex;
    align-items: center;
    flex-direction: column;

    .desc {
      margin: 16px 0 24px;
      font-size: 16px;
    }

    .name {
      color: #8B3D22;
      font-size: 24px;
      font-weight: bold;
    }

    .price {
      font-size: 14px;
      line-height: 1;
      align-items: flex-end;

      .price-left {
        span {
          &:nth-child(2) {
            font-size: 24px;
            font-weight: bold;
          }
        }
      }

      .price-right {
        margin-left: 20px;
      }
    }

    .btn {
      width: 100%;
      margin: 24px 0;
      color: #5A1D07;
      font-size: 16px;
      font-weight: bold;
      text-align: center;
      line-height: 50px;
      cursor: pointer;
      background: linear-gradient(270deg, #FBE8CF 0%, #F8C79D 52.05%, #FADCB9 100%);
      border-radius: 10px;
    }

    .btns {
      color: #FAD8B4;
      background: #3B3843;
    }

    .line {
      // width: 164px;
      display: flex;
      align-items: center;

      // background: url('/src/assets/imgs/line@2x.png') no-repeat;
      // background-size: 164px 3px;
      // background-position: center center;
      div {
        &:nth-child(2) {
          color: #8B3D22;
          font-weight: bold;
          font-size: 14px;
          text-align: center;
          margin: 0 8px;
        }

        &:first-child,
        &:last-child {
          width: 46px;
          height: 3px;


        }

        &:first-child {
          background: linear-gradient(270deg, #8B3D22 0%, rgba(139, 61, 34, 0) 100%);
        }

        &:last-child {
          background: linear-gradient(90deg, #8B3D22 0%, rgba(139, 61, 34, 0) 100%);
        }
      }
    }

    .uls {
      width: 100%;
      color: #8B3D22;
      font-size: 14px;

      .lis {
        margin-top: 16px;
      }
    }
  }
}
</style>