// pages/dataScreeningDetails/dataScreeningDetails.js
const {
  API_BASE_URL,
  saveOrEditCustomerEntry
} = require("../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    API_BASE_URL: API_BASE_URL,
    photo: {
      idCardFrontFileList: [], //身份证正面
      idCardBackFileList: [], //身份证反面
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


  /**
   * 
   * 
   * 提交登记
   * 
   * @param {*} e 
   */
  submitFrom(e) {
    if (!this.data.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    } else if (!this.data.phone) {
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none'
      })
      return
    } else if (!this.data.idnum) {
      wx.showToast({
        title: "请输入身份证号码",
        icon: 'none'
      })
      return
    } else if (!this.data.region) {
      wx.showToast({
        title: "请选择安装地址",
        icon: 'none'
      })
      return
    } else if (!this.data.address) {
      wx.showToast({
        title: "请输入安装地址",
        icon: 'none'
      })
      return
    } else if (this.data.photo.idCardBackFileList.length == 0 || this.data.photo.idCardFrontFileList.length == 0) {
      wx.showToast({
        title: "请上传身份证照片",
        icon: 'none'
      })
      return
    }

    var userId = wx.getStorageSync('userId')
    var CustomerEntry = {
      type: 2, //准入登记
      tenantName: this.data.name, //业主姓名
      mobile: this.data.phone, //联系方式
      province: this.data.region[0], //省
      city: this.data.region[1], //市
      area: this.data.region[2], //区
      idCode: this.data.idnum ? this.data.idnum : '', //身份证卡号
      address: this.data.address, //详细地址
      idCardFront: this.data.photo.idCardFrontFileList[0].name, //身份证正面
      idCardBack: this.data.photo.idCardBackFileList[0].name, //身份证反面
    }
    var data = {
      userId: userId,
      CustomerEntry: CustomerEntry,
    }
    wx.showLoading({
      title: '提交中',
    })
    saveOrEditCustomerEntry(data).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success: function () {
            setTimeout(function () {
              wx.navigateBack()
            }, 1500) //延迟时间
          }
        })
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },


  /**
   * 
   * 
   * 选择省市区
   * 
   * @param {*} e 
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },


  /**
   * 
   * 上传图片
   * 
   * @param {*} e 
   */
  afteruploadPhotoRead(e) {
    console.log("上传图片", e)
    const {
      file
    } = e.detail;
    const img = file.url;
    wx.showLoading({
      title: '上传中',
    })
    var that = this
    wx.uploadFile({
      url: that.data.API_BASE_URL + 'common/weChat/uploadImage',
      filePath: file.url,
      name: 'file',
      complete: function (res) {
        console.log("上传图片", res)
        wx.hideLoading()
        if (res.statusCode == 200) {
          wx.showToast({
            title: '上传成功',
            icon: 'none',
          })
          var item = {
            url: "",
            name: "",
          }
          // 渲染各自图片
          switch (e.currentTarget.dataset.type) {
            case "1": //身份证反面
              item.url = img;
              item.name = res.data;
              that.data.photo.idCardBackFileList.push(item)
              that.setData({
                'photo.idCardBackFileList': that.data.photo.idCardBackFileList
              })
              break
          }
        } else {
          wx.showToast({
            title: '上传失败',
            icon: 'error'
          })
        }
      }
    })
  },



  /**
   * 
   * 上传身份证正面
   * 
   * @param {*} e 
   */
  afteruploadPhotoReadPositive(e) {
    const {
      file
    } = e.detail;
    const img = file.url;
    wx.showLoading({
      title: '上传中',
    })
    var that = this
    wx.uploadFile({
      url: that.data.API_BASE_URL + 'common/weChat/uploadImageIden',
      filePath: file.url,
      name: 'file',
      complete: function (res) {
        console.log("上传图片", res)
        wx.hideLoading()
        if (res.statusCode == 200) {
          const data = JSON.parse(res.data)
          const OCRdata = JSON.parse(data.info)
          // if (OCRdata.data.result == 0) { //OCR识别成功
          //   if (OCRdata.data.info) {
          //     if (OCRdata.data.info.name) {
          //       that.setData({
          //         name: OCRdata.data.info.name,
          //       })
          //     }
          //     if (OCRdata.data.info.number) {
          //       that.setData({
          //         idnum: OCRdata.data.info.number
          //       })
          //     }
          //   }
          // }
          if (OCRdata.data) {
            if (OCRdata.data.face) { 
              if (OCRdata.data.face.data) {
                if (OCRdata.data.face.data.name) {
                  that.setData({
                    name: OCRdata.data.face.data.name,
                  })
                }
                if (OCRdata.data.face.data.idNumber) {
                  that.setData({
                    idnum: OCRdata.data.face.data.idNumber
                  })
                }
              }
            }
          }

          wx.showToast({
            title: '上传成功',
            icon: 'none',
          })
          var item = {
            url: "",
            name: "",
          }
          // 渲染各自图片
          switch (e.currentTarget.dataset.type) {
            case "0": //身份证正面
              item.url = img;
              item.name = data.path;
              that.data.photo.idCardFrontFileList.push(item)
              that.setData({
                'photo.idCardFrontFileList': that.data.photo.idCardFrontFileList
              })
              break;
          }
        } else {
          wx.showToast({
            title: '上传失败',
            icon: 'error'
          })
        }
      }
    })
  },


  /**
   * 
   * 删除图片
   * 
   * @param {*} e 
   */
  deleteImg(e) {
    console.log("删除图片事件", e)
    switch (e.currentTarget.dataset.type) {
      case "0": //删除身份证正面
        this.setData({
          'photo.idCardFrontFileList': []
        })
        break;
      case "1": //删除身份证反面
        this.setData({
          'photo.idCardBackFileList': []
        })
        break;
    }
  },

})