import { doNavigateWithUser } from '../../../utils/helper';
import { headerStorage, postUrl,postUrl2 } from '../../../utils/util';
import { apiArr } from '../../../api/doorToDoor';
import { apiArr as userApiArr } from '../../../api/user';
import PayJs from '../../../api/pay';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isOnShow: true,
    orderSn: '', // 订单编号，依赖上一个页面传递
    orderId: '', // 商品id 依赖上一个页面传递
    price: '',
    shopInfo: {}, // 商品详情信息
    orderMsg: {},
    GoodsMsg: '',
    addAress: {},
    checked: 'wx',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      orderSn: options.orderSn,
      orderId: options.orderId,
      price: options.price,
    })
    postUrl(userApiArr.getOne, {
      order_id: Number(options.orderId)
    },
     res => {
       wx.hideLoading();
      console.log('订单详情res', res);
      this.setData({
        shopInfo: res
      })
    })
  },
  headerPositionClick() {
    console.log('跳转管理地址页');
    this.setData({
      isOnShow: false
    });
    doNavigateWithUser({ url: `/packages/user/addressAdministration/index`});
  },

  // 提交订单
  headersubmitOrderClick() {

    

    const { addAress, shopInfo } = this.data;
    postUrl(apiArr.createServerOrder,{
      addr: addAress.optionAddr,
      appoint_id: shopInfo.appoint_id,
      capon_id: 0,
      cate_id: shopInfo.cate_id,
      name: shopInfo.name,
      order_type: shopInfo.order_type,
      shop_id: shopInfo.shop_id,
      svctime: shopInfo.svctime,
      tel: shopInfo.tel
    },res=>{
      this.setData({
        orderMsg:res
      })
      this.getGoodsMsg()
    })
  },

  //获取商户id
  getGoodsMsg(){
    postUrl(apiArr.getCurrentShopMsg,{
      shop_id :Number(this.data.orderMsg.shop_id ),
    },res=>{
      this.setData({
        GoodsMsg:res
      })
      this.pay()
    })
  },

  pay(){
      let that = this
    const { addAress, shopInfo, orderMsg, GoodsMsg } = this.data;
    postUrl2(PayJs.pay,{
      address: addAress.optionAddr,
      goods_id: String(orderMsg.order_id),
      merchant_no: GoodsMsg.merchant_no,
      out_trade_no: orderMsg.order_sn,
      quantity: 1,
      remark: shopInfo.remark,
      subject: shopInfo.appoint.title,
      term_no: GoodsMsg.term_no,
      total_amount: String(shopInfo.total_price * 100),
    //   total_amount: String(1),
      user: shopInfo.name,
      user_id: wx.getStorageSync('openId')
    },res=>{
        const {resp_data,msg} = res
        console.log('res', res);
        if(msg == '成功'){
            wx.requestPayment({
            timeStamp:String(resp_data.acc_resp_fields.time_stamp),
            nonceStr:String(resp_data.acc_resp_fields.nonce_str),
            package:String(resp_data.acc_resp_fields.package),
            signType:String(resp_data.acc_resp_fields.sign_type),
            paySign:String(resp_data.acc_resp_fields.pay_sign),
            success(res){
                // this.queryPay() 
                that.queryPay()
            },
            fail(fal){
                console.log(fal);
            }
            })
        }
    })
  },

  queryPay(){
    const { GoodsMsg, orderMsg } = this.data;
    postUrl2(PayJs.queryPay,{
      merchant_no: GoodsMsg.merchant_no,
      term_no: GoodsMsg.term_no,
      out_trade_no: orderMsg.order_no
    },res=>{
        if(res.statusCode == '200'){
            wx.showToast({
                title: '支付成功！',
            })
        }else{
            wx.showToast({
                title: res.msg,
            })
        }
    })
  },

  headerPaySelectClick(event) {
    console.log('eveee', event);
    this.setData({
      checked: event.currentTarget.dataset.name
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const _this = this;
    const { isOnShow } = this.data;
    const isNewAddress = wx.getStorageSync('isNewAddress');
    // 默认show 获取跳转过来携带地址 跳转地址管理页后 如果未选择地址而返回上一页面 则展示原数据
    if (isOnShow) {
      console.log('更新地址后 走读取新的地址')
      headerStorage(_this, 'newAddress', 'addAress');
    }
    // 跳转地址管理页后 选择新地址 则展示新地址
    if (isNewAddress) {
      headerStorage(_this, 'newAddress', 'addAress');
      wx.removeStorageSync('isNewAddress')
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})