// pages/inspected/inspected.ts
import { apply, timeCheck } from "../../apis/inspection"
import { getPayInfo } from '../../apis/pay'
const app = getApp<IAppOption>()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberShow: false,
    userInfo: app.globalData.userInfo,
    name: '',
    phone: '',
    num: 0,
    inspectionSinglePayment: 0, // 单次购买费用
    dayPrice: '0.00',  // 年费每天费用
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { name, phone } = options
    this.setData({
      name,
      phone
    })
  },

  submitForm() {
    const { name, phone } = this.data
    if (!name) {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名'
      })
      return
    }
    if (!phone) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    }
    if (!app.certificationVerify(true)) return
    apply({
      name,
      phone
    }).then(() => {
      wx.showToast({
        icon: 'none',
        title: '查验成功!'
      })
      this.setData({
        name: '',
        phone: ''
      })
      this.getNum()
      this.hideShadow()
      this.jumpRecord()
    }).catch(err => {
      if (err.c === 501009) {
        this.showShadow()
      } else {
        this.hideShadow()
      }
    })
  },
  buyOnce() {
    app.pay('INSPECTION_SINGLE_PAYMENT').then(() => {
      this.submitForm()
    })
  },
  getNum() {
    if (!app.memberVerify()) return
    timeCheck().then((res: any) => {
      this.setData({
        num: res.d
      })
    })
  },
  getPrice() {
    getPayInfo({}).then((res: any) => {
      const { inspectionSinglePayment, annualFee } = res.d
      this.setData({
        inspectionSinglePayment,
        dayPrice: (annualFee / 100 / 356).toFixed(2)
      })
    })
  },
  jumpMember() {
    wx.navigateTo({
      url: '/pages/member/member'
    })
    this.hideShadow()
  },
  showShadow() {
    this.setData({
      memberShow: true
    })
  },
  hideShadow() {
    this.setData({
      memberShow: false
    })
  },
  jumpRecord() {
    if (!app.certificationVerify(true)) return
    wx.navigateTo({
      url: '/pages/inspectedRecords/inspectedRecords'
    })
  },
  showChooseContact() {
    if (!wx.getStorageSync('authContact')) {
      this.selectComponent('#' + 'shadow2').show()
    } else {
      this.chooseContact()
    }
  },
  chooseContact() {
    let { phone } = this.data
    wx.setStorageSync('authContact', true)
    wx.chooseContact({
      success: t => {
        // console.log(t);
        let phone = t.phoneNumber.replaceAll('-', '').replaceAll(' ', '')
        if (phone.length !== 11) {
          wx.showToast({
            icon: 'none',
            title: '手机号格式错误!'
          })
          return
        } else {
          this.setData({
            phone
          })
        }
      },
      fail: e => console.log(e),
      complete: () => { }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    this.getNum()
    this.getPrice()
  },
  onPageScroll(e) {
    const { scrollTop } = e
    if (scrollTop > 150) {
      this.setData({
        headeBg: '#3A61B1'
      })
    } else {
      this.setData({
        headeBg: ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})