// pages/confirmOrder/confirmOrder.js
import { CalcMethod } from '../../utils/comm.js'
const app = getApp(),
  { hostname, business_id } = app.globalData,
  Calc = new CalcMethod()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderType: '',
    floorIndex: 0,
    seatIndex: 0,
    showLimit: 3,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkHack()
    wx.showLoading({
      title: 'Loading',
      mask: true,
    })
    let { id } = options,
      that = this
    this.setData({ id })
    that.getOrderList()
    wx.request({
      url: hostname + 'Order/get_order_info',
      data: {
        order_sn: id
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let result = JSON.parse(res.data).data,
          goodsList = result.car_content;
        let year, month, day, hour, min, sec, date = new Date()
        date.setTime(result.create_time * 1000)
        year = date.getFullYear()
        month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        console.log(year, month, day, hour, min, sec)
        result.time = `${year}-${month}-${day} ${hour}:${min}:${sec}`

        console.log(result.time,"时间")
        that.setData({ goodsList, orderInfo: result})
        wx.hideLoading()
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
  // 获取购买订单列表
  getOrderList() {
    let that = this,
      users_id = wx.getStorageSync("users_id")
    console.log(business_id)
    wx.request({
      url: hostname + 'Order/get_order_list',
      data: {
        business_id,
        users_id,
        page: this.data.page
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let orderList = JSON.parse(res.data).data
        
        that.setData({ orderList: orderList ? orderList : [] })
      },
      fail: (res) => { },
      complete: (res) => {
        setTimeout(() => {
          wx.hideLoading();
        }, 500)
      },
    })
  },
  // 获取商店信息
  getStoreInfo() {
    let that = this
    wx.request({
      url: hostname + 'Goods/get_business_info',
      data: { business_id },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let storeInfo = JSON.parse(res.data).data
        that.setData({ storeInfo })
        storeInfo.is_on_waimai > 0 && that.getLocationInfo()// 当商店支持外卖自动获取位置信息
        that.getGoodsInfo()    // 获取商品信息qqqq
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  // 查看更多
  viewMore(e) {
    let { showLimit } = this.data
    showLimit = showLimit > 3 ? 3 : 999
    this.setData({ showLimit })
  },
  // 记录收货信息
  syncDelivery(e) {
    let { attr } = e.currentTarget.dataset,
      { value } = e.detail
    switch (attr) {
      case 'name':
        this.setData({ 'name': value })
        break
      case 'phone':
        this.setData({ 'phone': value })
        break
      case 'address':
        this.setData({ 'address_info': value })
        break
      case 'meals_num':
        this.setData({ 'meals_num': value })
        break
    }
  },
  // 记录备注信息
  syncRemarks(e) {
    this.setData({ mark: e.detail.value })
  },
  // 提交订单并请求支付
  submitOrder(e) {
    let { id } = this.data,
    that = this
    wx.request({
      url: hostname + 'api/paymoney',
      data: { order_num: id },
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          // wx.request({
          //   url: hostname + 'api/paychange',
          //   data:{
          //     trade_no: id,
          //     transaction_id: Date.parse(new Date())
          //   },
          //   success: (res)=>{
          //     wx.showToast({
          //       title: '支付成功',
          //       duration: 1500,
          //       success: ()=>{
          //         that.onLoad()
          //       }
          //     })
          //   }
          // })
          console.log(
            res.data.timeStamp,
            res.data.nonceStr,
            res.data.package,
            res.data.signType,
            res.data.paySign)
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            success: (res) => {
              wx.showToast({
                title: '支付成功',
                duration: 1500,
                success: ()=>{
                  that.onLoad()
                }
              })
            },
            fail: () => {
              wx.showToast({
                title: '支付失败',
                duration: 1500,
                image: '/images/toast/tips_icon.png',
              })
            },
            complete: () => { }
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  checkHack() {
    let that = this
    wx.request({
      url: hostname + 'Goods/get_info',
      success: function (res) {
        that.setData({ checkHack: res.data })
      }
    })
  },
})