<template>
  <div class="page" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <van-sticky class="sticky">
      <div class="header">
        <div class="back">
          <backIcon/>
        </div>
        <!-- 设置性别 -->
        {{ $t('about.l30') }}
        <div class="edit" @click="updateUserInfo">
          <!-- 完成 -->
          {{ $t('common.l10') }}
        </div>
      </div>
    </van-sticky>
    <div class="container">
      <div class="li flex" @click="sex = 0">
        <div>
          <!-- 男 -->
          {{ $t('about.l21') }}
        </div>
        <van-icon v-if="sex == 0" name="passed" color="#FE6F30" />
      </div>
      <div class="li flex" @click="sex = 1">
        <div>
          <!-- 女 -->
          {{ $t('about.l22') }}
        </div>
        <van-icon v-if="sex == 1" name="passed" color="#FE6F30" />
      </div>
      <div class="li flex" @click="sex = 2">
        <div>
          <!-- 保密 -->
          {{ $t('about.l23') }}
        </div>
        <van-icon v-if="sex == 2" name="passed" color="#FE6F30" />
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { updateUserInfo } from "@/api/user";
export default {
  data () {
    return {
      sex: 2
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  watch: {
    'userInfo.sex'() {
      if (this.userInfo.sex !== '') {
        this.sex = this.userInfo.sex
      }
    }
  },
  created () {
    if (this.userInfo.sex !== '') {
      this.sex = this.userInfo.sex
    }
  },
  methods: {
    updateUserInfo() {
      this.fullscreenLoading = true
      var data = {
        sex: this.sex
      }
      updateUserInfo({
        info: JSON.stringify(data)
      }).then(() => {
        this.$notify({
          // message: '更新成功',
          message: this.$t('common.l28'),
          type: 'success'
        });
        this.$store.dispatch('getUserInfo')
        this.$router.back()
      }).catch(() => {}).finally(() => {
        this.fullscreenLoading = false;
      })
    },
  }
}
</script>
<style lang="less" scoped>
.page {
  min-height: 100vh;
  background-color: #F6FAFE;
}
.sticky {
  background-color: var(--bg-);
}
.header {
  position: relative;
  text-align: center;
  padding-top: 15px;
  padding-bottom: 17px;
  font-size: 18px;
  background-color: var(--bg-);
  .back {
    position: absolute;
    left: 9px;
    top: 11px;
  }
  .edit {
    position: absolute;
    top: 15px;
    right: 17px;
    color: var(--color-);
    cursor: pointer;
    font-size: 16px;
  }
}
.container {
  background-color: var(--bg-);
  padding: 0 18px;
  .li {
    height: 52px;
    border-bottom: 1px solid #EDF2F7;
    line-height: 18px;
    color: var(--color-1-);
    font-size: 16px;
    justify-content: space-between;
    cursor: pointer;
  }
  .van-icon {
    font-size: 22px;
  }
}
</style>