<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" v-if="editAvatarUrl" open-type="chooseAvatar" @chooseavatar="handleWeixinSync">
            <img v-if="user.avatarUrl && !avatar" :src="user.avatarUrl" class="user-img">
            <img v-if="avatar" :src="avatar" alt="" class="user-img">
            <img v-if="!user.avatarUrl && !avatar" src="https://cdn.cunwuyun.cn/dvcp/myFamily/tx.png" alt="" class="user-img">
          </button> -->
          <div class="user-img-div" v-if="editAvatarUrl">
            <AiUploader v-model="avatarUrlList" multiple placeholder="上传头像" :limit="1"></AiUploader>
          </div>
          <div class="user-img-div" v-else>
            <img v-if="user.avatarUrl && !avatar" :src="user.avatarUrl" class="user-img">
            <img v-else src="https://cdn.cunwuyun.cn/dvcp/myFamily/tx.png" alt="" class="user-img">
          </div>
        </div>
        <div class="item">
          <p>用户昵称</p>
          <div class="item-right" v-if="!editNickName">
            <p class="name">{{ user.nickName }}</p>
          </div>
          <input class="item-right" v-else type="text" v-model="userName">
        </div>
      </div>
      <div class="item-content mar-b8">
        <div class="item">
          <p>手机号</p>
          <div class="item-right" v-if="!!!editPhone">
            <p class="name">{{ user.phone || ''}}</p>
          </div>
          <input class="item-right" v-else type="number" v-model="userPhone" maxlength="11">
        </div>
      </div>

      <div class="item-content mar-b8">
        <!-- <div class="item solid">
          <p>真实姓名</p>
          <div class="item-right" v-if="!editRealName">
            <p class="name">{{ user.realName || '' }}</p>
          </div>
          <input class="item-right" v-else type="text" v-model="userRealName" @input="idNumberInput">
        </div>
        <div class="item solid">
          <p>身份证号</p>
          <div class="item-right" v-if="!editIdNumber">
            <p class="name">{{ user.idNumber || '' }}</p>
          </div>
          <input class="item-right" v-else type="idNumber" v-model="userIdNumber" maxlength="18" @input="idNumberInput">
        </div> -->
        <div class="item">
          <p>所属网格</p>
          <div class="item-right" v-if="!editGird">
            <p class="name">{{ user.girdName || '' }}</p>
          </div>
          <AiPagePicker type="gird" class="item-right" v-model="userGirdId" :params="{formType:2}" @select="handleSelectGrid" nodeKey="id" v-else>
            <AiMore v-model="userGirdName" />
          </AiPagePicker>
        </div>
        
      </div>
      <div class="btn-wrapper btn-edit">
        <b class="btn" @click="btnText? submit(): editBtn()">{{ btnText? '提交': '修改' }}</b>
      </div>
    </div>
  </div>
</template>
<script>
import {mapActions, mapState} from 'vuex'

export default {
  name: "userInfo",
  computed: {
    ...mapState(['user', 'token']),
  },
  onLoad(o) {
    this.isFromTabbar = o.isFromTabbar
    this.path = o.path
    this.getUserInfo('qujing') 
    uni.setNavigationBarTitle({
      title: '个人中心'
    })
  },
  data() {
    return {
      editAvatarUrl: false,
      editNickName: false,
      editPhone: false,
      editRealName: false,
      editIdNumber: false,
      editGird: false,
      avatar: '',
      userName: '',
      userPhone: '',
      userRealName: '',
      userIdNumber: '',
      userGirdId: '',
      userGirdName: '',
      btnText: false,
      isFromTabbar: '',
      path: '',
      avatarUrlList: []
    }
  },
  methods: {
    ...mapActions(['getUserInfo', 'autoLogin']),
    editBtn() {
      this.btnText = true
      this.editAvatarUrl = true;
      this.avatar = this.user.avatarUrl
      this.editNickName = true;
      this.userName = this.user.nickName
      this.editPhone = true;
      this.userPhone = this.user.phone
      this.editRealName = true;
      this.userRealName = this.user.realName
      this.editIdNumber = true;
      this.userIdNumber = this.user.idNumber
      this.editGird = true
      this.userGirdId = this.user.girdId
      this.userGirdName = this.user.girdName
      this.avatarUrlList = [{'url': this.user.avatarUrl}]
    },
    submit() {
      if(this.flag) return
      if(!this.avatarUrlList.length) {
        return this.$u.toast('请上传头像')
      }
      if (!this.userName) {
        return this.$u.toast('请输入用户昵称')
      }
      if (this.userName == '微信用户') {
        return this.$u.toast('请修改用户昵称')
      }
      if (!this.userPhone) {
        return this.$u.toast('请输入手机号')
      }
      // if (!this.userRealName) {
      //   return this.$u.toast('请输入真实姓名')
      // }
      // if (!this.userIdNumber) {
      //   return this.$u.toast('请输入身份证号')
      // }
      // if (!this.$idCardNoUtil.checkIdCardNo(this.userIdNumber)) {
      //   return this.$u.toast('请输入正确的身份证号码')
      // }
      if (!this.userGirdId) {
        return this.$u.toast('请选择所属网格')
      }
      // if (!this.userAreaId) {
      //   return this.$u.toast('请选择所属地区')
      // }
      // if (!/[^0]0{0,2}$/.test(this.userAreaId)) {
      //   return this.$u.toast('所属地区必须选到村级')
      // }
      this.flag = true
      this.$instance.post(`/app/appwechatuserqujing/idNumberAttestation`, {
        // avatarUrl: this.avatar,
        avatarUrl: this.avatarUrlList[0].url,
        nickName: this.userName,
        phone: this.userPhone,
        name: this.userName,
        idNumber: this.userIdNumber,
        girdId: this.userGirdId,
        girdName: this.userGirdName
      }).then(res=> {
        if(res?.code==0) {
          this.$u.toast('提交成功')
          this.autoLogin({ loginWay:'qujing'})
          uni.$emit('auth')
          setTimeout(() => {
            if (this.isFromTabbar == 1) {
              uni.switchTab({ url: this.path })
            } else {
              uni.navigateBack()
            }
          }, 600);
        }
      }).catch(err=> {
        this.$u.toast(err.msg)
      })
    },
    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)
          }
        })
      })
    },
    handleWeixinSync({detail}) {
      const { avatarUrl } = detail
      if(avatarUrl?.length) {
        this.upLoad(avatarUrl)
      }
    },
    handleSelectGrid(v) {
      this.userGirdName = v.girdName
      this.userGirdId = v.id
    },
    idNumberInput() {
      if(this.userIdNumber.length == 18 && this.userRealName) {
        if(!this.userGirdId) {
          this.$instance.post(`/app/appresidentapplet/queryDetailByIdNumberAndName`,{
            name: this.userRealName,
            idNumber: this.userIdNumber
          }).then(res=> {
            if(res.data && res.data.id) {
              this.userGirdId = res.data.id
              this.userGirdName = res.data.girdName
            }
          })
        }
      }
    }
  }
}
</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%;
        }

        .ai-area__wrapper {
          display: flex;
          align-items: center;
          padding-left: 100px;

          span {
            color: #333;
            font-size: 30px;
          }

          i {
            color: #999;
            font-size: 30px;
          }

          image {
            width: 16px;
            height: 8px;
          }
        }
      }

      ::v-deep .AiAreaPicker {
        float: right;
      }

      .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;
  }

  .btn-edit {
    background: #FFF;
  }

  ::v-deep.ai-uploader {
    text-align: right;
    display: inline-block;
    .imgList {
      width: 228px;
      height: 200px;
      display: inline-block!important;
    }
  } 
}
</style>
