<template>
  <div class="my">
    <div class="top">
      <img class="avatar" v-if="data.userimg" :src="$file(data.userimg)">
      <div class="_info">
        <div class="name">
          {{ $substring(data.nickname, 0, 12) }}
        </div>
        <div class="user">
          <img src="/img/my-i1.png" />{{ data.cellphone }}
        </div>
        <div class="box">
          <div class="vBicon vip">
            <!-- {{ data.userlevelname }}  -->
            V1
          </div>
          <div class="vBicon svip">
            {{ data.col2name }} SVIP
          </div>
          <div class="vBicon director">
            {{ data.col2name }}董事
          </div>
        </div>
      </div>
      <!-- v-if="data.isstudio" -->
      <div class="shopcenter" @click="$showManagerPopup()">
        <span>
          管理中心
          <van-icon name="arrow" />
        </span>
      </div>
    </div>

    <div class="wallet_box">
      <div>
        <div class="t">
          <img src="/img/my_w1.png" alt="">
          <span>余额</span>
        </div>
        <b>1377.00</b>
      </div>

      <div>
        <div class="t">
          <img src="/img/my_w2.png" alt="">
          <span>会员卡额度</span>
        </div>
        <b>13777.00</b>
      </div>

      <div>
        <div class="t">
          <img src="/img/my_w3.png" alt="">
          <span>积分</span>
        </div>
        <b>1377.00</b>
      </div>
    </div>

    <div class="my_order">
      <div class="t">
        <b>我的订单</b>

        <span class="r" @click="$navigate('TradeList')">
          更多
          <van-icon name="arrow"></van-icon>
        </span>
      </div>
      <div class="c">
        <van-badge :dot="data.UnPayQty > 0" :offset="[3, -3]">
          <div class="st_box" @click="$navigate(`TradeList?TradeArea=3&state=0`)">
            <img src="/img/my_o1.png">
            <p>待付款</p>
          </div>
        </van-badge>
        <van-badge :dot="data.UnSendQty > 0" :offset="[3, -3]">
          <div class="st_box" @click="$navigate(`TradeList?TradeArea=3&state=1`)">
            <img src="/img/my_o2.png">
            <p>待发货</p>
          </div>
        </van-badge>
        <van-badge :dot="data.SendingQty > 0" :offset="[3, -3]">
          <div class="st_box" @click="$navigate(`TradeList?TradeArea=3&state=3`)">
            <img src="/img/my_o3.png">
            <p>待收货</p>
          </div>
        </van-badge>
        <div class="st_box" @click="$navigate(`TradeList?TradeArea=3&state=4`)">
          <img src="/img/my_o4.png">
          <p>已完成</p>
        </div>
      </div>
    </div>

    <div class="fast_to">
      <img src="/img/my_f1.jpg" @click="$navigate('Invite')" alt="">
      <img src="/img/my_f2.jpg" @click="$navigate('Team')" alt="">
      <img src="/img/my_f3.jpg" @click="$navigate('Allow')" alt="">
      <img src="/img/my_f4.jpg" @click="$navigate('Certificate')" alt="">
    </div>

    <div class="service_center">
      <div class="tit">
        <b>服务中心</b>
      </div>
      <div class="list">
        <div @click="$navigate('CashoutAccount')">
          <img src="/img/my_s1.png" alt="">
          <span>提现账号</span>
          <van-icon name="arrow" class="r"></van-icon>
        </div>

        <div @click="$navigate('Address')">
          <img src="/img/my_s2.png" alt="">
          <span>收货地址</span>
          <van-icon name="arrow" class="r"></van-icon>
        </div>

        <div @click="$navigate('Service')">
          <img src="/img/my_s3.png" alt="">
          <span>联系客服</span>
          <van-icon name="arrow" class="r"></van-icon>
        </div>

        <div @click="settingVisibled = true">
          <img src="/img/my_s4.png" alt="">
          <span>设置中心</span>
          <van-icon name="arrow" class="r"></van-icon>
        </div>
      </div>
    </div>

    <van-action-sheet v-model:show="settingVisibled" title="设置中心" class="setting">
      <div style="height: 1rem" />
      <van-cell is-link icon="/img/set-i1.png" title="个人资料" size="large"
        @click="tempData = {}; Object.assign(tempData, data); tempData.AvatarUploader = [{ url: $file(tempData.userimg), isImage: true }]; showInfo = true;">
        <template #right-icon>
          <img class="y50" :src="$file(data.userimg)" width="29" height="29" />
        </template>
      </van-cell>
      <van-cell is-link icon="/img/set-i2.png" title="登录密码" size="large" value="修改"
        @click="changePasswordVisibled = true">
      </van-cell>
      <van-cell is-link icon="/img/set-i3.png" title="提现账户" size="large" value="管理"
        @click="$navigate('/CashoutAccount')">
      </van-cell>
      <van-cell is-link icon="/img/set-i4.png" title="退出登录" size="large" @click="logout">
        <template #right-icon> </template>
      </van-cell>
      <div style="height: 2rem" />
    </van-action-sheet>

    <van-action-sheet v-model:show="showInfo" title="个人信息">
      <van-form @submit="save">
        <van-cell-group inset>
          <van-field name="uploader" label="头像" required>
            <template #input>
              <van-uploader v-model="tempData.AvatarUploader" :after-read="upload" :max-count="1" upload-text="头像" />
            </template>
          </van-field>
          <van-field v-model="tempData.nickname" label="昵称" placeholder="请输入昵称"
            :rules="[{ required: true, message: '请填写昵称' }]" clearable required />
          <van-field v-model="tempData.cellphone" label="手机号" readonly>
            <template #button>
              不可更改
            </template>
          </van-field>
          <van-cell>
            <template #title>
              <van-button color="#ca2904" style="width: 100%" round type="primary" native-type="save"
                :loading="saving">保存</van-button>
            </template>
          </van-cell>
        </van-cell-group>
      </van-form>
    </van-action-sheet>

    <van-action-sheet v-model:show="changePasswordVisibled" title="修改登录密码">
      <div class="my-setting-password">
        <van-cell-group inset>
          <van-field v-model="tempPasswordData.oldpwd" type="password" label="原密码" placeholder="请输入原密码" required />
          <van-field v-model="tempPasswordData.newpwd" type="password" label="新密码" placeholder="请输入新密码" required />
          <van-field v-model="tempPasswordData.repwd" type="password" label="确认密码" placeholder="请再次输入新密码" required />
          <van-cell size="large">
            <template #title>
              <van-button round color="#ca2904" type="primary" size="large" @click="setPassword"
                :loading="saving">保存</van-button>
            </template>
          </van-cell>
        </van-cell-group>
      </div>
    </van-action-sheet>

    <van-dialog v-model:show="showFollow" :closeOnClickOverlay="false" :showConfirmButton="false"
      style="background: transparent; display: flex; align-items: center; flex-direction: column;">
      <img :src="$file(data.FollowQRCode)" style="width: 60vw" />
      <div style="margin: 3.333vw 0">
        <span style="color: #fff">为了更好为您提供服务<br />请长按识别二维码关注公众号</span>
      </div>
      <van-button style="background: #fff; width: 60vw" @click="showFollow = false">关闭</van-button>
    </van-dialog>

  </div>

</template>

<script>
export default {
  name: 'My',
  computed: {
  },
  mounted() {
    this.init();
  },
  data() {
    return {
      data: { userimg: '', username: '' },
      settingVisibled: false,
      changePasswordVisibled: false,
      tempPasswordData: {
        oldpwd: '',
        newpwd: '',
        repwd: '',
      },
      showInfo: false,
      saving: false,
      tempData: {},
      showFollow: false,
      hf_data: {},
      hfLoading: true,
    }
  },
  methods: {
    init() {
      this.$get('/v1/client/DUsersClient').then(data => {
        // console.log(data);
        this.data = data.data;
        this.$ls.set('cellphone', data.data.cellphone);
        this.$ls.set('userimg', data.data.userimg);
        this.$ls.set('nickname', data.data.nickname)
      })
    },
    logout() {
      this.$showConfirmDialog({
        title: "确认退出？",
      }).then(() => {
        this.$ls.remove('token');
        location.replace('#/Login');
        setTimeout(() => {
          location.reload();
        }, 100);
      }).catch(() => { });
    },
    upload(file) {
      this.tempData.userimg = file.file;
    },
    save() {
      if (this.tempData.userimg instanceof File) {
        const fd = new FormData();
        fd.append('file', this.tempData.userimg);
        fetch(`${import.meta.env.VITE_API_URL}/v1/client/DUsersClient/userimg`, {
          method: 'POST',
          headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` },
          body: fd,
        }).then(res => res.json()).then(res => {
          if (res.status !== 200) throw new Error(res.errmsg);
          this.tempData.userimg = res.data;
        }).then(() => {
          return this.$put('/v1/client/DUsersClient', {
            nickname: this.tempData.nickname,
            cellphone: this.tempData.cellphone,
          });
        }).then(() => {
          this.$showSuccessToast('保存成功');
          this.showInfo = false;
          window.location.reload();
        }).catch(err => {
          this.$showFailToast(err.errmsg || err.message || '保存失败');
        });
      } else {
        this.$put('/v1/client/DUsersClient', {
          nickname: this.tempData.nickname,
          cellphone: this.tempData.cellphone,
        }).then(() => {
          this.$showSuccessToast('保存成功');
          this.showInfo = false;
          window.location.reload();
        }).catch(err => {
          this.$showFailToast(err.errmsg || '保存失败');
        });
      }
    },
    valid(field, msg) {
      if (!field) {
        this.$showFailToast(msg);
        return false;
      }
      return true;
    },
    setPassword() {
      if (!this.tempPasswordData.oldpwd) {
        this.$showFailToast('请输入原密码');
        return;
      }
      if (!this.tempPasswordData.newpwd) {
        this.$showFailToast('请输入新密码');
        return;
      }
      if (this.tempPasswordData.newpwd !== this.tempPasswordData.repwd) {
        this.$showFailToast('两次输入的密码不一致');
        return;
      }
      this.saving = true;
      this.$post('/v1/client/DUsersClient/changepwd', {
        oldpwd: this.tempPasswordData.oldpwd,
        newpwd: this.tempPasswordData.newpwd,
      }).then((res) => {
        if (res.status !== 200) {
          this.$showFailToast(res.message || '修改失败');
          return;
        }
        this.$showSuccessToast('密码修改成功');
        this.changePasswordVisibled = false;
      }).catch(err => {
        this.$showFailToast(err.message || '修改失败');
      }).finally(() => {
        this.saving = false;
      });
    },
  }
}
</script>