<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>

    <!-- Payment Dialog -->
    <el-dialog v-model="dialogVisible" :title="$t('common.tip')" width="600" align-center custom-class="member-payment-dialog">
      <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.balanceNotEnoughRecharge'), 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 {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  padding: 0;
}

.member-head {
  padding: 60px 0 40px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  box-shadow: 0 4px 20px rgba(79, 70, 229, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  line-height: 1;

  .member-head-title {
    color: white;
    font-size: 36px;
    font-weight: 700;
    letter-spacing: -0.02em;
  }

  .member-head-list {
    display: flex;
    margin-top: 24px;

    .li {
      font-size: 16px;
      margin-right: 40px;
      opacity: 0.9;

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

.member-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px 48px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 24px;
  margin-top: -24px;

  .li {
    position: relative;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    padding: 32px 24px;
    display: flex;
    align-items: center;
    flex-direction: column;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 4px;
      background: linear-gradient(90deg, #4f46e5, #7c3aed);
    }
    
    &:hover {
      transform: translateY(-8px);
      box-shadow: 
        0 12px 32px rgba(79, 70, 229, 0.15),
        0 4px 16px rgba(0, 0, 0, 0.08);
    }

    .name {
      font-size: 20px;
      font-weight: 600;
      color: #1f2937;
      margin-top: 16px;
    }

    .desc {
      margin: 16px 0 20px;
      font-size: 14px;
      color: #6b7280;
      text-align: center;
      line-height: 1.5;
    }

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

      .price-left {
        span {
          &:first-child {
            font-size: 16px;
            color: #4f46e5;
          }
          &:nth-child(2) {
            font-size: 32px;
            font-weight: 700;
            color: #4f46e5;
          }
        }
      }

      .price-right {
        margin-left: 20px;
        color: #9ca3af;
        text-decoration: line-through;
      }
    }

    .btn {
      width: 100%;
      margin: 24px 0;
      color: white;
      font-size: 16px;
      font-weight: 600;
      text-align: center;
      line-height: 50px;
      cursor: pointer;
      background: linear-gradient(135deg, #4f46e5, #7c3aed);
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
      transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(79, 70, 229, 0.4);
      }
    }

    .btns {
      background: linear-gradient(135deg, #1e293b, #334155);
      box-shadow: 0 4px 12px rgba(30, 41, 59, 0.3);
      
      &:hover {
        box-shadow: 0 6px 16px rgba(30, 41, 59, 0.4);
      }
    }

    .line {
      display: flex;
      align-items: center;
      width: 100%;
      margin: 20px 0;
      
      div {
        &:nth-child(2) {
          color: #4f46e5;
          font-weight: 600;
          font-size: 14px;
          text-align: center;
          margin: 0 12px;
        }

        &:first-child,
        &:last-child {
          flex: 1;
          height: 2px;
        }

        &:first-child {
          background: linear-gradient(270deg, #4f46e5 0%, rgba(79, 70, 229, 0) 100%);
        }

        &:last-child {
          background: linear-gradient(90deg, #4f46e5 0%, rgba(79, 70, 229, 0) 100%);
        }
      }
    }

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

      .lis {
        margin-top: 12px;
        padding: 8px 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        
        &::before {
          content: '✓';
          color: #10b981;
          font-weight: 600;
          margin-right: 12px;
        }
      }
    }

    &.popular {
      border-color: #4f46e5;
      box-shadow: 
        0 8px 24px rgba(79, 70, 229, 0.2),
        0 2px 8px rgba(0, 0, 0, 0.1);
      
      .popular-badge {
        position: absolute;
        top: -10px;
        right: 20px;
        background: linear-gradient(135deg, #4f46e5, #7c3aed);
        color: white;
        padding: 6px 16px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 600;
        box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
      }
    }
  }
}

/* ===== 响应式设计 ===== */
@media (max-width: 1200px) {
  .member-list {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .member-head {
    padding: 40px 0 32px;
    
    .member-head-title {
      font-size: 28px;
    }
  }
  
  .member-list {
    padding: 0 16px 32px;
  }
  
  .member-list .li {
    padding: 24px 20px;
  }
}

@media (max-width: 480px) {
  .member-head {
    padding: 32px 0 24px;
    
    .member-head-title {
      font-size: 24px;
    }
    
    .member-head-list {
      flex-direction: column;
      align-items: center;
      gap: 8px;
      
      .li {
        margin-right: 0;
      }
    }
  }
  
  .member-list {
    padding: 0 12px 24px;
  }
  
  .member-list .li {
    padding: 20px 16px;
  }
  
  .price .price-left span:nth-child(2) {
    font-size: 24px;
  }
}
</style>