<template>
  <div class="page">
    <div class="header-bg">
      <div class="header-info">
        <div class="wrap" @click="handleLogin()">
          <div class="user-img-div">
            <img :src="user.avatarUrl" alt="" class="user-img" v-if="user.id && user.avatarUrl"/>
            <img class="user-img" src="https://cdn.cunwuyun.cn/dvcp/myFamily/tx.png" v-else alt="">
          </div>
          <div class="user-info">
            <div class="option">
              <template v-if="!user.id">
                <p>登录</p>
                <p>点击进行登录</p>
              </template>
              <template v-else>
                <p v-if="isApprove">{{ user.realName }}</p>
                <p v-else>{{ user.nickName }}</p>
                <p>{{ user.areaName || "" }}</p>
              </template>
            </div>
            <p class="info" v-if="user.id" @click.stop="$linkTo('./userInfo')">个人信息</p>
          </div>
        </div>
      </div>
    </div>
    <div class="approve" @click="$linkTo('./userInfo')">
      <img class="icon" src="https://cdn.cunwuyun.cn/wxmp/mine/approve.png" alt="">
      <div class="flex">
        <span class="cert" v-if="isApprove" v-text="approveLabel"></span>
        <u-icon v-else name="arrow-right" color="#E7F2FF" size="28"
                :label="approveLabel" label-color="#E7F2FF" label-pos="left" @click="approve"/>
      </div>
    </div>
    <div class="list-wrap">
      <div class="card" v-for="(group,index) in listGroup" :key="index">
        <div class="item" v-for="(item) in group" hover-class="bg-hover" :key="item.label"
             @click="linkTo(item.path, item.type)">
          <div class="block">
            <img class="icon" :src="item.icon" alt="">
          </div>
          <span class="desc" v-if="!item.share">{{ item.label }}</span>
          <button open-type="share" v-else>
            <span>{{ item.label }}</span>
          </button>
        </div>
      </div>
    </div>
    <div class="logout" v-if="token">
      <b class="login-out" @click="onLogout">退出登录</b>
    </div>
    <AiLogin ref="login" @success="getAuth()"/>
  </div>
</template>
<script>
import {mapActions, mapState} from "vuex";

export default {
  name: "AppMine",
  appName: "我的",
  customNavigation: true,
  navigationBarBackgroundColor: '#ffffff',
  navigationBarTextStyle: 'black',
  computed: {
    ...mapState(['user', 'token']),
    isApprove() {
      return this.user?.status == 2;
    },
    approveLabel() {
      return this.user?.areaId? "已认证" : "前往认证"
    },
    listGroup() {
      return [
        [
          // {
          //   icon: "https://cdn.cunwuyun.cn/wxmp/mine/wodejiaxiang.png",
          //   label: "我的家乡",
          //   path: "/mods/AppHometown/AppHometown",
          //   type: 'token'
          // },
          {
            icon: "https://cdn.cunwuyun.cn/fengdu/mdpi_icon-fengshouma.png",
            label: "我的丰收码",
            path: "./myHarvestQR",
            type: 'token'
          },
          {
            icon: "https://cdn.cunwuyun.cn/wxmp/mine/wodejifen.png",
            label: "我的积分",
            path: "/mods/AppCreditPoints/AppCreditPoints?type=detail",
            type: 'null'
          },
        ],
        // [
        //   {
        //     icon: "https://cdn.cunwuyun.cn/wxmp/mine/wodejifen.png",
        //     label: "我的积分",
        //     path: "/mods/AppCreditPoints/AppCreditPoints?type=detail",
        //     type: 'idNumber'
        //   },
        //   {
        //     icon: "https://cdn.cunwuyun.cn/wxmp/mine/chaoshidingdan.png",
        //     label: "超市订单",
        //     path: "/mods/AppOrderList/AppOrderList",
        //     type: 'idNumber'
        //   },
        //   {
        //     icon: "https://cdn.cunwuyun.cn/wxmp/mine/banshijindu.png",
        //     label: "办事进度",
        //     path: "/mods/AppProgress/AppProgress",
        //     type: 'idNumber'
        //   }
        // ],
      ]
    }
  },
  methods: {
    ...mapActions(['getUserInfo']),
    approve() {
      if (!this.token) {
        this.$refs.login.show();
      } else if (!this.isApprove) {
        if (this.user.status == 0) {
          this.$linkTo('/mods/AppAuth/AppAuth');
        } else {
          this.$linkTo('/mods/AppAuth/authSuccess')
        }
      }
    },
    linkTo(url, type) {
      if (type) {
        if (this.token) {
          if (type == 'token') {
            this.$linkTo(url)
          }
          if (type == 'idNumber') {
            if (this.user.status == 0) {
              if (!this.user.phone) {
                this.$linkTo('/pages/phone/bingPhoneNumber?from=auth')
              } else {
                this.$linkTo('/mods/AppAuth/AppAuth')
              }
            } else {
              this.$linkTo(url)
            }
          }
          if (type == 'null') {
            this.$u.toast('内容建设中...')
          }
        } else {
          this.$refs.login.show()
        }
      } else {
        this.$linkTo(url)
      }
    },
    handleLogin() {
      if (!this.token) {
        this.$refs.login.show();
      }
    },
    getAuth() {
      this.$nextTick(() => {
        this.token && this.getUserInfo('qujing')
      })
    },
    onLogout() {
      uni.showModal({
        title: '提示',
        content: "是否要退出登录",
        success: res => {
          if (res.confirm) {
            this.$store.commit('logout')
            this.$toast('退出成功');
            setTimeout(() => {
              this.getAuth();
            }, 500)
          }
        }
      })
    },
  },
  onLoad() {
    this.getAuth();
    uni.$on('auth',()=> {
      this.getAuth();
    })
  },
  onShareAppMessage() {
    return {
      title: '欢迎使用数字乡村治理服务一体化平台~',
      path: `/pages/AppModules/AppModules`
    }
  },
}
</script>
<style scoped lang="scss">
@import "~dvcp-wui/common";

.page {
  width: 100%;
  min-height: 100%;
  background-color: #F3F6F9;
  position: relative;

  .header-bg {
    width: 100%;
    height: 512px;
    position: relative;

    .header-info {
      width: 100%;
      height: 100%;
      background: url('https://cdn.cunwuyun.cn/fengdu/mdpi_img-bg2.png') no-repeat;
      background: 100% auto;
      box-sizing: border-box;
      padding: 240px 0 0 48px;

      .wrap {
        width: 100%;
        height: 96px;
        display: flex;
        align-items: center;

        .user-img-div {
          display: inline-block;
          width: 96px;
          height: 96px;
          border-radius: 50%;
          overflow: hidden;
          border: 4px solid #FFFFFF;
          flex-shrink: 0;

          .user-img {
            display: inline-block;
            width: 96px;
            height: 96px;
            border-radius: 58px;
          }
        }

        .user-info {
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-left: 26px;
          box-sizing: border-box;
          padding-right: 32px;

          .option {
            & > p:first-child {
              font-size: 34px;
              font-weight: 600;
              color: #333333;
              line-height: 54px;
            }

            & > p:last-child {
              font-size: 26px;
              font-weight: 400;
              color: #7088A0;
              line-height: 36px;
            }
          }

          .info {
            width: 136px;
            height: 48px;
            border-radius: 8px;
            border: 2px solid #7088A0;
            font-size: 26px;
            font-weight: 400;
            color: #7088A0;
            display: flex;
            align-items: center;
            justify-content: center;
          }
        }
      }
    }
  }

  .approve {
    position: absolute;
    left: 50%;
    top: 400px;
    transform: translateX(-50%);
    width: 686px;
    height: 112px;
    background: linear-gradient(90deg, #8FB4FF 0%, #4181FF 100%);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 0 20px;

    .icon {
      width: 296px;
      height: 52px;
    }

    .cert {
      font-family: PingFangSC-Regular, PingFang SC;
      font-size: 28px;
      color: #E7F2FF;
      margin-right: 4px;
    }
  }

  .list-wrap {
    box-sizing: border-box;
    padding: 32px;

    .card {
      width: 100%;
      min-height: 100px;
      border-radius: 16px;
      background-color: #fff;
      margin-bottom: 32px;

      .item {
        height: 100px;
        display: flex;
        align-items: center;

        &:last-child {
          .desc {
            border-bottom: none !important;
          }
        }

        .block {
          width: 80px;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;

          .icon {
            width: 44px;
            height: 44px;
          }
        }

        .desc, & > button {
          height: 100%;
          flex: 1;
          font-size: 30px;
          font-weight: 400;
          color: #666666;
          display: flex;
          align-items: center;
          border-bottom: 1px solid #EEEEEE;

          & > span {
            width: 100%;
            height: 100%;
            text-align: left;
            display: flex;
            align-items: center;
          }

          &::after {
            border: none;
          }
        }

        .no-border {
          border-bottom: none;
        }
      }

      &:first-of-type {
        margin-bottom: 32px;
      }
    }
  }
  .logout {
    width: 100%;
    height: 96px;
    padding: 0 32px;
    box-sizing: border-box;
    line-height: 96px;
    text-align: center;
    .login-out {
      background: #FFF;
      border-radius: 16px;
      font-size: 34px;
      font-weight: 500;
      color: #4181FF;
    }
  }
}
</style>
