<template>
  <view class="container">
    <view class="example">
      <uni-forms ref="form" :model="user" labelWidth="80px">
        <uni-forms-item label="姓名" name="realName">
          <uni-easyinput v-model="user.realName" placeholder="请输入姓名" />
        </uni-forms-item>
        <uni-forms-item label="手机号" name="phone">
          <uni-easyinput v-model="user.phone" placeholder="请输入手机号" />
        </uni-forms-item>
        <uni-forms-item label="身份证" name="idCard">
          <uni-easyinput v-model="user.idCard" placeholder="请输入身份证" />
        </uni-forms-item>
		<uni-forms-item label="地址" name="address">
		  <uni-easyinput v-model="user.address" placeholder="请输入地址" />
		</uni-forms-item>
      </uni-forms>
	<!-- <view class="recharge-box recharge-upload">
	 	<view class="upload-title">
	 		<view class="upload-btn" @tap="uploadImage">上传凭证</view>
	 	</view>
	 	<view class="upload-box-img">
	 		<image v-if="uploadIMG" class="upload-img" :src="uploadIMG+'png'" mode="aspectFit" />
	 		<image v-else class="upload-bg-img" :src="uploadBgIMG" mode="aspectFit" @tap="uploadImage" />
	 	</view>

	 </view> -->
      <button type="primary" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
		import {
			upload
		} from "@/api/system/user.js"
  import { getAuthentication, getApplyAuth } from "@/api/system/user"

  export default {
    data() {
      return {
        authFlag: null,
        emptyIcon: require('@/static/empty.png'),
        uploadBgIMG: require('@/static/indexIMG/upload.png'),
        uploadIMG: "",
        user: {
          id: "",
          realName: "",
          phone: "",
          idCard: "",
          address: "",
		  type:"1"
        },
        rules: {
          realName: {
            rules: [{
              required: true,
              errorMessage:'名字不能为空'
            }]
          },
          phone: {
            rules: [{
              required: true,
              errorMessage: '手机号码不能为空'
            }, {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码'
            }]
          },
          idCard: {
            rules: [{
              required: true,
              errorMessage: '身份证不能为空'
            }, {
              pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
              errorMessage: '请输入正确的身份证'
            }]
          },
          address: {
            rules: [{
              required: true,
              errorMessage: '地址不能为空'
            }]
          },
        }
      }
    },
    onLoad(option) {
        if (option.authFlag) {
            this.authFlag = option.authFlag;
            this.applyAuth()
        }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {

	  // 上传头像
	  			uploadImage() {

	  				// 从相册选择图片
	  				const _this = this;
	  				uni.chooseImage({
	  					count: 1,
	  					sizeType: ['original'],
	  					sourceType: ['album'],
	  					success: function(res) {
	  						uni.showLoading({title:"上传中"})
	  						_this.handleUploadFile(res.tempFilePaths);
	  					},
	  					fail:function() {
	  						uni.hideLoading()
	  					}
	  				});
	  			},
	  // 上传凭证
	  		   async handleUploadFile(data) {
				   console.log(data,"data")
	  				const _this = this;
	  				const filePath = data.path || data[0];
	  				let res = await upload(filePath)
	  				console.log(res)
	  				_this.uploadIMG = res.msg
	  				// _this.profileInfo.head_portrait = r.url;
	  				// _this.handleUpdateInfo(_this.profileInfo);
	  				uni.hideLoading()

	  			},
      submit(ref) {
        this.$refs.form.validate().then(res => {
          getAuthentication(this.user).then(response => {
            console.log(response,"response");
            if(response.code == 200){
              uni.showToast({
                icon: 'none',
                title: '实名认证成功',
                success: () => {
                  setTimeout(() => {
                    this.$tab.navigateBack()
                  },1000)
                }
              });
            }else{
              this.$modal.msgError(res.msg)
            }

          })
        })
      },
      applyAuth() {
          getApplyAuth({"status":this.authFlag,"type":"1"}).then(res => {
              this.user.id = res.data.id
              this.user.realName = res.data.realName
              this.user.phone = res.data.phone
              this.user.idCard = res.data.idCard
              this.user.address = res.data.address
          })
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
