<template>
  <div class="pop" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="ul">
      <van-uploader :after-read="afterRead" result-type="file" capture="camera">
        <div class="item">
          <!-- 拍照 -->
          {{ $t('about.l10') }}
        </div>
      </van-uploader>
      <van-uploader :after-read="afterRead" result-type="file">
        <div class="item">
          <!-- 从相册选择 -->
          {{ $t('about.l11') }}
        </div>
      </van-uploader>
      <!-- 查看头像   查看背景图-->
      <div class="item" v-if="images.length" @click="imagePreShow = true">{{changeImgKey == 'head_file' ? $t('about.l12') : $t('about.l13')}}</div>
    </div>
    <div class="item" @click="cancel">
      <!-- 取消 -->
      {{ $t('common.l2') }}
    </div>
    <van-image-preview getContainer="body" v-model="imagePreShow" :showIndex="false" :images="images">
    </van-image-preview>
  </div>
</template>
<script>
import axios from 'axios'
import { updateUserInfo } from "@/api/user";
import { mapGetters } from "vuex";
export default {
  props: ['changeImgKey'],
  data() {
    return {
      imagePreShow: false
    };
  },
  computed: {
    ...mapGetters(['userInfo']),
    images() {
      if (this.changeImgKey == 'bg_file' && this.userInfo.bg_file) {
        return [this.userInfo.bg_file]
      }
      if (this.changeImgKey == 'head_file' && this.userInfo.head_file) {
        return [this.userInfo.head_file]
      }
      return []
    }
  },
  methods: {
    afterRead(file) {
      if (['image/jpg', 'image/jpeg', 'image/png'].indexOf(file.file.type) == -1) {
        // 请上传 jpg/jpeg/png 格式图片
        this.$toast(this.$t('ajax.l5'));
        return false;
      }
      this.upload(file.file)
    },
    upload(file) {
      const formData = new FormData();
      formData.append("file", file);
      this.fullscreenLoading = true
      axios.post(this.$uploadUrl, formData, {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        })
        .then((res) => {
          var file = res.data
          if (file.msg != 'ok') {
            this.$notify({
              message: file.msg || this.$t('fund.l83'),
              type: 'danger'
            });
            return
          }
          this.updateUserInfo(file.data)
        })
        .catch((err) => {
          this.fullscreenLoading = false;
          this.$notify({
            message: this.$t('fund.l83'),
            type: 'danger'
          });
        });
    },
    updateUserInfo(file) {
      var data = {}
      data[this.changeImgKey] = file
      updateUserInfo({
        info: JSON.stringify(data)
      }).then(() => {
        this.$notify({
          // message: '更新成功',
          message: this.$t('common.l28'),
          type: 'success'
        });
        this.$store.dispatch('getUserInfo');
        this.cancel();
      }).catch(() => {}).finally(() => {
        this.fullscreenLoading = false;
      })
    },
    cancel() {
      this.$emit("cancel");
    },
  },
};
</script>
<style lang="less" scoped>
.pop {
  background-color: #f4f4f4;
}
.ul {
  margin-bottom: 6px;
}
.van-uploader {
  display: block;
  border-bottom: 1px solid #f6f6f6;
}
.item {
  cursor: pointer;
  text-align: center;
  line-height: 47px;
  color: var(--color-1-);
  background-color: #fff;
}
::v-deep {
  .van-uploader__wrapper {
    display: block;
  }
}
</style>