<template>
  <uv-navbar leftIcon="" title="我的" bg-color="#19A497"></uv-navbar>
  <image src="/static/user/user-bg.png" mode="widthFix" class="bg"/>

  <block>
    <view class="container" :style="containerStyle">
      <view class="header flex-column">
        <view class="flex-grow flex-center">
          <image
              src="/static/user/round.png"
              mode="heightFix"
              style="width: 12orpx;height: 120rpx"/>
        </view>
        <view class="main flex-shrink">
          <view
              class="avatar"
              :style="{
              'background-image': `url('/static/user.png')`,
            }"></view>
          <view class="phone fs-40 fw-500">{{ user.mobilePhone }}</view>

          <view class="money" @click="toPage({path: '/pages-user/wallet/index'})">
            <view class="money-left">
              <uv-icon name="red-packet" size="24" color="#19A497"></uv-icon>
              <view style="font-size: 13px;">钱包|充值</view>
            </view>

            <view class="money-right">
              <view>¥</view>
              <view class="money-right_balance">{{ ((user.balance || 0) / 100).toFixed(2) }}</view>
            </view>

          </view>
        </view>
      </view>

      <view class="body">
        <block v-for="(item, index) in menu" :key="index">
          <view
              class="menu-item"
              @click="toPage(item)">
            <view class="menu-item_left">
              <image :src="item.icon"></image>
              <view class="menu-item_left-title">{{ item.title }}</view>
            </view>
            <view class="menu-item_right">
              <uv-icon name="arrow-right" size="16"></uv-icon>
            </view>
          </view>
        </block>

        <view class="logout-btn" v-if="isLogin">
          <view class="mt20 logout-btn-wrap"  type="error" @click="logoutUser">退出登录</view>
        </view>
      </view>
    </view>
  </block>

  <cover-view class="login_bar" v-if="!isLogin">
    <login-bar class="w100 text-center"></login-bar>
  </cover-view>

<!--  <view class="logout-btn">
    <uv-button :custom-style="customStyle" type="error" @click="logoutUser">退出登录</uv-button>
  </view>-->

  <tab-bar :index="2"/>
</template>

<script setup lang="ts">
import {onLoad, onShow} from "@dcloudio/uni-app";
import {computed, ref} from "vue";
import TabBar from "@/components/tab-bar/index.vue";
import LoginBar from "@/components/login-bar/index.vue";
import {checkLogin, clearToken} from "@/utils/auth"
import {get} from "@/utils/https";

const containerStyle = ref({});
const user = ref<any>({
  id: 0,
  avatar: "",
  mobilePhoneFormat: '',
  balance: 0,
});
const isLogin = ref(false)
const service = ref("15012341234");
const menu = ref([
  {
    title: "绑定车辆",
    path: "/pages-user/profile/index",
    icon: '/static/user/profile.png'
  },
  /*  {
      title: "我的卡包",
      path: "/pages/coupon/index",
      icon: '/static/user/coupon.png'
    },*/
  {
    title: "我的订单",
    path: "/pages-user/wallet/index?tab=3",
    icon: '/static/user/order.png'
  },
  /*  {
      title: "我的收藏",
      path: "/pages-user/fav/index",
      icon: '/static/user/fav.png'
    },*/

  {
    title: "联系我们",
    path: "/pages-user/contact/index",
    icon: '/static/user/contact.png'
  },
  {
    title: "洗车指导",
    path: "/pages-user/faq/index",
    icon: '/static/user/faq.png'
  },
  {
    title: "纠错上报",
    path: "/pages-user/feedback/index",
    icon: '/static/user/feedback.png'
  },
]);



const toPage = (item: any) => {
  checkLogin().then(() => {
    let {title, path} = item;
    if (path.includes('contact')) {
      uni.makePhoneCall({
        phoneNumber: service.value,
        fail: (error) => {
          console.log(error)
        }
      });
      return;
    } else if (path.includes("order") || path.includes("coupon")) {
      uni.switchTab({
        url: path
      })
    }
    uni.navigateTo({
      url: item.path,
    });
  })

};

const loginListen = () => {
  uni.$on('loginEvent', function (data) {
    isLogin.value = data.isLogin;
    console.log("event>>>", data)
    if (data.isLogin) {
      user.value = getApp<any>().globalData.user;
      console.log("event1111>>>", user.value)
    }
  })
}

const logoutUser = () => {
  uni.showModal({
    title: "温馨提示",
    content: "确定退出登录吗？",
    confirmColor: "#2d9e95",
    confirmText: "确定退出",
    cancelText: "手滑了",
    success: (res) => {
      if (res.confirm) {
        uni.showLoading({
          title: "退出中",
        });

        get(`/user/logout`).then(() => {
          uni.hideLoading();
          uni.showToast({
            icon: "success",
            title: "已退出",
          });

          clearToken();
          setTimeout(() => {
            uni.exitMiniProgram()
          }, 1500);

        })
      }
    },
  });
};

onLoad(() => {
  uni.setNavigationBarTitle({title: '个人中心'})
  const bound = uni.getMenuButtonBoundingClientRect();
  containerStyle.value = {
    top: `${bound.bottom + 10}px`,
  };
});


onShow(() => {
  const userData = getApp<any>().globalData.user;
  if (userData && userData.id) {
    isLogin.value = true;
    user.value = userData;
  }
  loginListen();
});
</script>

<style lang="scss" scope>
.bg {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 460rpx;
}

.container {
  position: absolute;
  left: 0;
  top: 0;
  height: 100vh;
  width: 100%;
  overflow-y: auto;
}

.header {
  //height: 400rpx;
  width: 100%;

  .main {
    height: 334rpx;
    background: rgba(254, 255, 255, 0.7);
    border-radius: 40rpx 40rpx 0 0;
    position: relative;
    text-align: center;

    .avatar {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      top: -90rpx;
      width: 110rpx;
      height: 110rpx;
      border: 2rpx solid #ffffff;
      filter: drop-shadow(0px 4rpx 8rpx rgba(0, 24, 60, 0.1));
      background-size: cover;
      background-repeat: no-repeat;
      border-radius: 50%;
    }

    .phone {
      padding-top: 78rpx;
      color: #000;
    }

    .money {
      //position: absolute;
      //left: 0;
      //bottom: 0;
      width: 690rpx;
      height: 118rpx;
      background: #feffff;
      border-radius: 120rpx;
      margin: 0 auto;
      margin-top: 30rpx;
      color: #000000;
      padding: 0 24rpx 0 40rpx;

      display: flex;
      align-content: center;
      align-items: center;
      justify-content: space-between;

      &-left {
        color: $uni-color-primary;
        display: inline-flex;
        align-items: flex-end;
      }

      &-right {
        color: $uni-color-primary;
        display: inline-flex;
        align-items: flex-end;

        &_balance {
          font-weight: 600;
          font-size: 20px;
        }
      }
    }
  }
}

.body {
  width: 100%;
  background-color: #fff;
  //padding: 0rpx 30rpx;

  .menu-item {
    display: inline-flex;
    justify-content: space-between;
    background-color: #fff;
    height: 90rpx;
    border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
    align-items: center;
    width: 100%;

    &_left {
      display: flex;
      align-items: center;
      align-content: center;
      margin-left: 20rpx;

      image {
        width: 42rpx;
        height: 42rpx;
        margin-right: 20rpx;
      }

      &-title {
        font-size: 13px;
      }
    }

    &_right {
      text-align: right;
      width: 60rpx;
    }


    &:last-child {
      border-bottom: none;
    }
  }
}

.logout-btn {
  width: calc(100vw - 60rpx);
  margin: 60rpx auto;

  &-wrap{
    height: 60rpx;
    font-size: 24rpx;
    margin: 20px auto;
    width: 540rpx;
    text-align: center;
    background: #dd524d;
    color: white;
    border-radius: 10rpx;
    display: flex;
    align-items: center;
    align-content: center;
    justify-content: center;
  }
}
</style>
