<template>
  <div class="page">
    <div class="info-list">
      <div class="item-content mar-b8">
        <div class="item solid">
          <p class="mar-t22">头像</p>
          <button class="user-img-div" open-type="chooseAvatar" @chooseavatar="handleWeixinSync">
            <img v-if="user.avatarUrl" :src="user.avatarUrl" class="user-img">
            <img v-else src="https://cdn.cunwuyun.cn/dvcp/myFamily/tx.png" alt="" class="user-img">
          </button>
        </div>
        <div class="item">
          <p>用户昵称</p>
          <div class="item-right" v-if="!editNickName" @click="editNickName=true, userName = user.nickName">
            <p class="name">{{ user.nickName }}</p>
          </div>
          <input class="item-right" v-else type="text" v-model="nickName" @blur="handleWeixinSync">
        </div>
      </div>
      <div class="item-content mar-b8">
        <div class="item">
          <p>手机号</p>
          <div class="item-right" v-if="!!!editPhone" @click="editPhone = true, userPhone = user.phone">
            <p class="name">{{ user.phone || '' }}</p>
          </div>
          <input class="item-right" v-else type="number" v-model="userPhone" @blur="handleWeixin" maxlength="11">
        </div>
        
      </div>
      <div class="item-content" @click="onLogout">
        <div class="item">
          <p class="login-out">退出登录</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {mapActions, mapState} from 'vuex'

export default {
  name: "userInfo",
  appName: "个人中心",
  computed: {
    ...mapState(['user', 'token']),
    nickName: {
      set(v) {
        this.userName = v
      },
      get() {
        const {nickName} = this.user
        return nickName
      }
    },
    phone: {
      set(v) {
        this.userPhone = v
      },
      get() {
        const { phone } = this.user
        return phone
      }
    }
  },
  onLoad() {
    this.getUserInfo()
  },
  data() {
    return {
      editNickName: false,
      editPhone: false,
      avatar: '',
      userName: '',
      userPhone: ''
    }
  },
  methods: {
    ...mapActions(['getUserInfo', 'autoLogin']),
    updateInfo() {
      this.$instance.post(`/app/appwechatuser/update-nickName`, null, {
        params: {
          id: this.user.id,
          nickName: this.userName ? this.userName : this.user.nickName,
          avatarUrl: this.avatar ? this.avatar : this.user.avatarUrl,
          phone: this.userPhone ? this.userPhone : this.user.phone,
        }
      }).then(res => {
        if (res?.code == 0) {
          this.$u.toast('修改成功')
        }
      })
    },
    upLoad(img) {
      return new Promise((resolve, reject) => {
        uni.uploadFile({
          url: `${this.$instance.defaults.baseURL}/admin/file/add`,
          filePath: img,
          name: 'file',
          header: {
            'Content-Type': 'multipart/form-data',
            Authorization: uni.getStorageSync('token')
          },
          success: uploadFileRes => {
            this.avatar = JSON.parse(uploadFileRes.data).data[0].split(';')[0]
            resolve(uploadFileRes)
          },
          fail: err => {
            reject(err)
          }
        })
      })
    },
    onLogout() {
      uni.showModal({
        title: '提示',
        content: "是否要退出登录",
        success: res => {
          if (res.confirm) {
            this.$store.commit('logout')
            this.$toast('退出成功');
            setTimeout(() => {
              uni.switchTab({
                url: '/pages/AppMine/AppMine'
              })
            }, 500)
          }
        }
      })
    },
    handleWeixin({ detail }) {
      if (detail.value) {
        this.phone = detail.value
        this.updateInfo()
      }
    },
    handleWeixinSync({detail}) {
      const {value: nickName, avatarUrl} = detail
      this.autoLogin({nickName, avatarUrl})
      if (avatarUrl?.length) {
        this.upLoad(avatarUrl).then(() => {
          this.updateInfo()
        })
      }
      if (nickName) {
        this.updateInfo()
      }
    }
  }
}
</script>
<style scoped lang="scss">
@import "~dvcp-wui/common";

.page {
  width: 100%;
  background-color: #F5F5F5;

  .info-list {
    padding: 50px 32px 0 32px;
    box-sizing: border-box;

    .item-content {
      padding: 0 32px;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 32px;
    }

    .item {
      padding: 36px 0;
      line-height: 40px;
      width: 100%;
      box-sizing: content-box;
      display: flex;

      p {
        color: #333;
        font-weight: 400;
        width: 200px;
      }

      div {
        color: #666;
        font-size: 28px;
      }

      .item-right {
        width: calc(100% - 200px);
        text-align: right;
        font-size: 28px;
        p {
          width: 100%;
        }
      }

      .user-img-div {
        width: calc(100% - 200px);
        text-align: right;
        display: inline-block;
      }

      .user-img {
        width: 104px;
        height: 104px;
        vertical-align: middle;
        border-radius: 50%;
      }

      .right-icon {
        width: 40px;
        height: 40px;
        vertical-align: middle;
      }

      .mar-t22 {
        margin-top: 44px;
      }
    }

    .login-out {
      width: 100%;
      font-size: 30px;
      text-align: center;
      color: #4181FF;
    }
  }

  .self-knowledge-show {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
  }

  .toast-bg {
    position: fixed;
    z-index: 101;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .toast {
    background-color: #fff;
    width: 686px;
    height: 316px;
    font-size: 36px;
  }

  .toast-msg {
    text-align: center;
    line-height: 50px;
    color: #333;
    font-weight: 500;
    padding: 84px 0 80px 0;
  }

  .toast-btn {
    display: inline-block;
    width: 120px;
    text-align: center;
    line-height: 50px;
  }

  .cancel {
    margin-left: 394px;
    margin-right: 40px;
  }

  .confirm {
    color: #197DF0;
  }

  .editNickName {
    text-align: right;
    font-size: 28px;
  }
}
</style>
