// pages/me/collection/index.js
import api from '../../../utils/Api.js'
import Schedule from '../../../utils/Schedule.js'
import Wx from '../../../utils/Wx.js'
const {
  $Toast
} = require('../../../iview/base/index');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    user: {},
    loading: false,

    familyNum: 0,
    phone: '',
    nickname: '',
    adultNum: 0,
    childrenNum: 0,
    childrenAge: 0,
    remark: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中...',
    })
    new Schedule().task(this.getDetailData).delay(1200).task(() => {
      wx.hideNavigationBarLoading()
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      user: app.globalData.userInfo
    })
  },

  getDetailData: function() {
    api.project.detail({
      id: this.data.id
    }).then(res => {
      if (!res.code) {
        this.setData({
          detail: res.data
        })
      }
    })
  },

  handlePhoneAuth: function(event) {
    Wx.login().then(res => {
      let encryptedData = event.detail.encryptedData
      let iv = event.detail.iv
      this.decodePhoneData(res.code, encryptedData, iv)
    })
  },

  /**
   * 解密手机号数据
   */
  decodePhoneData: function(code, encryptedData, iv) {
    let that = this
    api.getPhoneNumber({
      code: code,
      encryptedData: encryptedData,
      iv: iv
    }).then(res => {
      if (!res.code) {
        $Toast({
          content: '手机号授权成功',
          type: 'success'
        });
        this.setData({
          phone: res.data.phone
        })
      } else {
        $Toast({
          content: '手机号授权失败',
          type: 'error'
        });
      }
    })
  },

  handleFamilyNum: function(event) {
    this.setData({
      familyNum: event.detail.value
    })
  },

  handleNickname: function(event) {
    this.setData({
      nickname: event.detail.value
    })
  },

  handleAdultNum: function(event) {
    this.setData({
      adultNum: event.detail.value
    })
  },

  handleChildrenNum: function(event) {
    this.setData({
      childrenNum: event.detail.value
    })
  },

  handleChildrenAge: function(event) {
    this.setData({
      childrenAge: event.detail.value
    })
  },

  handleRemark: function(event) {
    this.setData({
      remark: event.detail.value
    })
  },

  handleSubmit: function() {
    if (!this.data.familyNum || !this.data.nickname || !this.data.phone) {
      $Toast({
        content: '请完善必要信息',
        type: 'error'
      });
      return
    }
    if (this.data.phone.length == 0) {
      $Toast({
        content: '请授权手机号',
        type: 'error'
      });
      return
    }
    api.project.order({
      pid: this.data.id,
      sign_limits: this.data.familyNum,
      nickname: this.data.nickname,
      phone: this.data.phone,
      adult_number: this.data.adultNum,
      children_number: this.data.childrenNum,
      children_age: this.data.childrenAge,
      remark: this.data.remark
    }).then(res => {
      console.log(res)
    })
  }
})