<template>
  <div>
    <el-row :gutter="20" style="margin-top:10px;">
      <el-col :span="8" v-for="(item, index) in userinfo" :key="index">
        <div class="grid-content bg-purple">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>个人中心</span>
            </div>
            <div class="name-role">
              <span class="sender">姓名 - {{item.userName}}</span>
            </div>
            <div class="registe-info">
              <span class="registe-info">
                状态：
                <li class="fa fa-clock-o"></li>
                {{item.idCard==null||item.idCard==''?"未实名":"已实名"}}
              </span>
            </div>
            <el-divider></el-divider>
            <div class="personal-relation">
              <div class="relation-item">手机号: <div style="float: right; padding-right:20px;">{{item.phone}}</div></div>
            </div>
            <div class="personal-relation">
              <div class="relation-item">驾驶证: <div style="float: right; padding-right:20px;"><img :src="dataForm.DriverLicense" style="width: 50%;"></div></div>
            </div>
            <div class="personal-relation">
              <div class="relation-item">身份信息: <div style="float: right; padding-right:20px;">{{item.idCard}}</div></div>
            </div>
          </el-card>
        </div>
      </el-col>
      <el-col :span="16">
        <div class="grid-content bg-purple">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>实名认证</span>
            </div>
            <div>
              <el-form label-width="80px" v-model="dataForm" size="small" label-position="right">
                <el-form-item label="姓名" prop="nickName">
                  <el-input auto-complete="off" v-model="dataForm.UserName"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                  <el-input auto-complete="off" v-model="dataForm.Phone"></el-input>
                </el-form-item>
                <el-form-item label="身份证" prop="idCard">
                  <el-input maxlength="18" v-model="dataForm.idCard"></el-input>
                </el-form-item>
                <el-form-item label="驾驶证" prop="idCard">
                  <el-input maxlength="18" v-model="dataForm.DriverLicense"></el-input>
                  <input type="file"  id="uploadFile" @change="handleFileChange">
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button size="mini" type="primary" @click="RealUser">提交</el-button>
                <el-button size="mini" type="warning">关闭</el-button>
              </div>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userinfo: [],
      dataForm: {
        UserName: '',
        RealUser: '',
        idCard: '',
        DriverLicense: '',
        Phone: '',
      }
    };
  },
  created() {
    this.fetchOrderPact(); // 调用 fetchOrderPact 方法
  },
  methods: {
    async fetchOrderPact(id) {
      let data = {
        UserId: window.localStorage.getItem("userId"),
      };
      try {
        let result = await this.$API.userinfo.getInfo(data);
        if (result.status === 200) {
          this.userinfo = result.data;
          this.dataForm.idCard=result.data[0].idCard
          this.dataForm.UserName=result.data[0].userName
          this.dataForm.DriverLicense=result.data[0].driverLicense
          this.dataForm.Phone=result.data[0].phone
        } else {
          this.$message.error(result.message);
        }
      } catch (error) {
        console.error("An error occurred:", error);
      }
    },async RealUser(id) {
      let data = {
        UserId: window.localStorage.getItem("userId"),
        Phone:this.dataForm.Phone,
        UserName:this.dataForm.UserName,
        idCard:this.dataForm.idCard,
        DriverLicense:this.dataForm.DriverLicense 
      };
      try {
        let result = await this.$API.userinfo.RealUser(data);
        if (result.status === 200) {
          window.localStorage.setItem("userName",this.dataForm.UserName);
          this.$message({
            message: result.message,
            type: 'success'
          });
        } else {
          this.$message.error(result.message);
        }
      } catch (error) {
        console.error("An error occurred:", error);
      }
    },handleFileChange(event) {
      // 获取上传的文件
      const file = event.target.files[0];
      
      // 创建 FormData 对象，并添加文件和其他需要上传的数据
      const formData = new FormData();
      formData.append('image', file);
      formData.append('token', '1c17b11693cb5ec63859b091c5b9c1b2');
      
      // 发送 AJAX 请求
  fetch('https://img.pqblog.com/api/index.php', {
  method: 'POST',
  body: formData,
})
.then(response => response.json())
.then(data => {
  if (data.code === 200) {
    // 更新 Vue 组件中的数据
   this.dataForm.DriverLicense=data.url
    // 提示用户上传成功
    this.$message.success('上传成功');
  } else {
    // 响应中可能包含错误信息，您可以根据实际情况处理
    console.error('上传图片失败:', data.message);
    this.$message.error('上传失败，请重试');
  }
})
.catch(error => {
  // 处理错误
  console.error('上传图片失败:', error);
  this.$message.error('上传失败，请重试');
});
    }
    
  }
}
</script>

<style lang="scss" scoped>
// 卡片样式
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

// 文本样式区
.name-role {
  font-size: 16px;
  padding: 5px;
  text-align: center;
}

.sender {
  text-align: center;
}

.registe-info {
  text-align: center;
  padding-top: 10px;
}

.personal-relation {
  font-size: 16px;
  padding: 0px 5px 15px;
  margin-right: 1px;
  width: 100%;
}

.relation-item {
  padding: 12px;
}

.dialog-footer {
  padding-top: 10px;
  padding-left: 10%;
}

// 布局样式区
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
