// pages/check-submit/check-submit.js
Page({
  data: {
    mch_list: [],
    car_appoint_id:''
  },
  onLoad: function (options) {
    this.newSubmitPreview(options.data)
    this.setData({
      phone: options.phone
    })
  },
  newSubmitPreview: function(t){
    let data = JSON.parse(decodeURIComponent(t))
    console.log('预约接口数据',data)
    this.setData({
      car_appoint_id: data.car_appoint_id

    })
    // 最终数据
    let o = {
    	mch_list:[]
    }
    // mch数据
    let mch = {
    	goods_list:[]
    }
    // good_list数据
    let good_list = {
    	attr:[]
    }
    // atrrId数据
    let attr = JSON.parse(data.goods.attr)
    let attr_id = attr[0].attr_list[0].attr_id
    let attr_group_id = attr[0].attr_list[0].attr_group_id

    // this.data.car_appoint_id  = data.car_appoint_id
    good_list.goods_id = data.goods.id
    good_list.num = 1
    good_list.attr.push({
    	attr_id:attr_id,
    	attr_group_id:attr_group_id
    })

    mch.mch_id = data.goods.mch_id
    mch.goods_list.push(good_list)
    console.log(mch,'mch')

    o.mch_list = JSON.stringify([mch])
    let that = this
    getApp().request({
      url: getApp().api.order.new_submit_preview,
      method: "POST",
      data: o,
      success: function (res) {
        console.log(res,'下单')
        if(res.code == 0){
          res.data.mch_list[0].show = false
          res.data.mch_list[0].show_length = 0
          res.data.mch_list[0].offline = 0
          that.setData({
            mch_list: res.data.mch_list
          })
        }else{
          wx.showToast({
            icon: 'none',
            title: res.msg,
          })
          setTimeout(()=>{
            wx.navigateBack()
          },2000)
        }
      }
    })
  },
  pay: function(){
    let o = {
      mch_list:[]
    }
    o.mch_list = JSON.stringify(this.data.mch_list)
    o.car_appoint_id = this.data.car_appoint_id
    o.payment = 3
    o.phone = this.data.phone
    o.formId = undefined
    getApp().request({
      url: getApp().api.order.new_submit,
      method: "POST",
      data: o,
      success: function (res) {
        console.log(res,'下单sssss')
        getApp().request({
          url: getApp().api.order.pay_data,
          data: {
            order_id: res.data.order_id,
            pay_type: "WECHAT_PAY"
          },
          complete: function () {
            getApp().core.hideLoading()
          },
          success: function (t) {
            0 == t.code && getApp().core.requestPayment({
              _res: t,
              timeStamp: t.data.timeStamp,
              nonceStr: t.data.nonceStr,
              package: t.data.package,
              signType: t.data.signType,
              paySign: t.data.paySign,
              success: function (t) {},
              fail: function (t) {},
              complete: function (t) {
                "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? getApp().core.redirectTo({
                  url: "/pages/order/order?status=2"
                }) : getApp().core.showModal({
                  title: "提示",
                  content: "订单尚未支付",
                  showCancel: !1,
                  confirmText: "确认",
                  success: function (t) {
                    t.confirm && getApp().core.redirectTo({
                      url: "/pages/order/order?status=0"
                    })
                  }
                })
              }
            }), 1 == t.code && getApp().core.showToast({
              title: t.msg,
              image: "/images/icon-warning.png"
            })
          }
        })
      //   getApp().request({
      //     url: getApp().api.order.pay_data,
      //     data: {
      //       order_id: res.data.order_id,
      //       pay_type: "WECHAT_PAY"
      //     },
      //     success: function (e) {
      //       0 == e.code ? (getApp().core.hideLoading(), 
      //       getApp().core.requestPayment({
      //         _res: e,
      //         timeStamp: e.data.timeStamp,
      //         nonceStr: e.data.nonceStr,
      //         package: e.data.package,
      //         signType: e.data.signType,
      //         paySign: e.data.paySign,
      //         complete: function (e) {
      //           "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? "requestPayment:ok" == e.errMsg && getApp().core.redirectTo({
      //             url: "/pages/integral-mall/order/order?status=1"
      //           }) : getApp().core.showModal({
      //             title: "提示",
      //             content: "订单尚未支付",
      //             showCancel: !1,
      //             confirmText: "确认"
      //           })
      //         }
      //       })) : (getApp().core.hideLoading(), getApp().core.showModal({
      //         title: "提示",
      //         content: e.msg,
      //         showCancel: !1,
      //         confirmText: "确认"
      //       }))
      //     }
      //   })
      }
    })
  },
})