<template>
  <u-popup :show="show" :round="10">
    <view class="avatar-popup">
      <view class="popup-header">
        <view class="popup-header-title">更换头像</view>
      </view>
      <scroll-view scroll-y="true">
        <u-grid :border="false" col="4" class="avatar-popup-grid">
          <u-grid-item v-for="index in 9" :key="index">
            <view class="avatar-item" @click="select(index)">
              <image :src="'/static/avatar/'+ (user.user_sex === 2 ? 'woman' : 'man') +'/'+ index +'.png'" mode="widthFix" />
              <view v-if="portrait === (user.user_sex === 2 ? 'woman' : 'man') +'/'+ index +'.png'" class="selected">
                <uni-icons type="checkmarkempty" color="#ffffff" size="12"></uni-icons>
              </view>
            </view>
          </u-grid-item>
        </u-grid>
      </scroll-view>
      <view class="close-button" @click="close">取消</view>
    </view>
  </u-popup>
</template>

<script>
export default {
  props: {
    user: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      avatar: '',
      show: false
    }
  },
  computed: {
    portrait() {
      if (this.user.user_portrait) {
        const match = this.user.user_portrait.match(/\/avatar\/(.*)/)
        return match ? match[1] : ''
      }
      return ''
    }
  },
  methods: {
    select(value) {
      this.show = false
      this.$emit('change', (this.user.user_sex === 2 ? 'woman' : 'man') +'/'+ value +'.png')
    },
    close: function() {
      this.show = false
    }
  }
}
</script>

<style lang="scss">
.avatar-popup {
  .popup-header {
    height: 50px;
    line-height: 50px;
    position: relative;
    .popup-header-title {
      font-size: 16px;
      font-weight: 500;
      text-align: center;
    }
    .popup-header-button {
      width: 50px;
      color: $uni-color-primary;
      background: none; 
      border: none;
      position: absolute;
      right: 5px;
      top: 5px;
      &::before {
        display: none;
      }
    }
  }
  uni-scroll-view {
    max-height: 270px;
    .avatar-popup-grid {
      padding: 10px;
      .u-grid-item {
        padding: 5px;
        display: flex;
        justify-content: center;
        .avatar-item {
          width: 100%;
          max-width: 70px;
          position: relative;
          uni-image {
            width: 100%;
          }
          .selected {
            width: 18px;
            height: 18px;
            border-radius: 50%;
            background-color: $uni-color-primary;
            position: absolute;
            right: 5%;
            bottom: 5%;
            display: flex;
            align-items: center;
            justify-content: center;
          }
        }
      }
    }
  }
  .close-button {
    width: 100%;
    height: 50px;
    margin-top: 15px;
    border-top: 1px solid #f3f3f3;
    font-size: 16px;
    font-weight: 700;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>