var util = require('../../utils/util.js');
var app = getApp();
var status = require('../../utils/index.js');

function count_down(that, total_micro_second) {
  var second = Math.floor(total_micro_second / 1000);
  var days = second / 3600 / 24;
  var daysRound = Math.floor(days);
  var hours = second / 3600 - (24 * daysRound);
  var hoursRound = Math.floor(hours);
  var minutes = second / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
  var minutesRound = Math.floor(minutes);
  var seconds = second - (24 * 3600 * daysRound) - (3600 * hoursRound) - (60 * minutesRound);

  that.setData({
    endtime: {
      days: daysRound,
      hours: fill_zero_prefix(hoursRound),
      minutes: fill_zero_prefix(minutesRound),
      seconds: fill_zero_prefix(seconds),
      show_detail: 1
    }
  });

  if (total_micro_second <= 0) {
    that.setData({
      changeState: 1,
      endtime: {
        days: "00",
        hours: "00",
        minutes: "00",
        seconds: "00",
      }
    });
    return;
  }

  setTimeout(function() {
    total_micro_second -= 1000;
    count_down(that, total_micro_second);
  }, 1000)

}
// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}

Page({
  data: {
    endtime: {
      days: "00",
      hours: "00",
      minutes: "00",
      seconds: "00",
    },
    cancelOrderVisible: false,
    orderSkuResps: [],
    tablebar: 4,
    navState: 0,
    theme_type: '',
    loadover: false,
    pingtai_deal: 0,
    is_show: false,
    order: {},
    common_header_backgroundimage: '',
    isShowModal: false,
    userInfo: {},
    groupInfo: {
      group_name: '社区',
      owner_name: '团长'
    }
  },
  is_show_tip: '',
  timeOut: function() {
    console.log('计时完成')
  },
  options: '',
  onLoad: function(options) {
    var that = this;
    that.options = options;
    var userInfo = wx.getStorageSync('userInfo');
    userInfo && (userInfo.shareNickName = userInfo.nickName.length > 3 ? userInfo.nickName.substr(0, 3) + "..." : userInfo.nickName);
    status.setGroupInfo().then((groupInfo) => {
      that.setData({
        groupInfo
      })
    });
    util.check_login() ? this.setData({
      needAuth: false
    }) : this.setData({
      needAuth: true
    });

    that.setData({
      common_header_backgroundimage: app.globalData.common_header_backgroundimage,
      userInfo
    });
    var token = wx.getStorageSync('token');
    wx.hideShareMenu();

    wx.showLoading();
    var is_show_tip = options && options.is_show || 0;
    let isfail = options && options.isfail || '';
    this.is_show_tip = is_show_tip;
    if (is_show_tip != undefined && is_show_tip == 1) {
      //todo 弹出分享 
    } else {
      wx.showLoading();
    }

    if (isfail != undefined && isfail == 1) {
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      })
    }

    app.util.request({
      'url': 'entry/wxapp/index',
      'data': {
        controller: 'order.order_info',
        token: token,
        id: options.id
      },
      dataType: 'json',
      method: 'POST',
      success: function(res) {
        wx.hideLoading();
        if(res.data.code==0){
          if (is_show_tip != undefined && is_show_tip == 1 && res.data.data.order_info.type == 'integral') {
            wx.showToast({
              title: '兑换成功'
            })
          } else if (is_show_tip != undefined && is_show_tip == 1) {
            if (res.data.order_pay_after_share == 1) {
              let share_img = res.data.data.share_img;
              that.setData({
                share_img,
                isShowModal: true
              })
            } else {
              wx.showToast({
                title: '支付成功'
              })
            }
          }

          if (res.data.data.order_info.order_status_id == 3) {
            var seconds = (res.data.data.order_info.over_buy_time - res.data.data.order_info.cur_time) * 1000;
            if (seconds > 0) {
              count_down(that, seconds);
            } else {
              res.data.data.order_info.open_auto_delete == 1 && that.setData({
                changeState: 1
              })
            }
          }
          that.setData({
            order: res.data.data,
            pingtai_deal: res.data.pingtai_deal,
            order_refund: res.data.order_refund,
            loadover: true,
            is_show: 1,
            hide_lding: true
          })
          that.hide_lding();
        } else if(res.data.code==2){
          that.setData({ needAuth: true })
        }
      }
    })

  },
  /**
   * 授权成功回调
   */
  authSuccess: function() {
    this.onLoad(this.options);
  },

  reload_data: function() {
    var that = this;
    var token = wx.getStorageSync('token');

    app.util.request({
      'url': 'entry/wxapp/index',
      'data': {
        controller: 'order.order_info',
        'token': token,
        //"id": 30
        "id": that.data.order.order_info.order_id
      },
      dataType: 'json',
      method: 'POST',
      success: function(res) {

        if (res.data.data.order_info.order_status_id == 3) {
          var seconds = (res.data.data.order_info.over_buy_time - res.data.data.order_info.cur_time) * 1000;
          if (seconds > 0) {
            count_down(that, seconds);
          } else {
            that.setData({
              changeState: 1
            })
          }
        }
        that.setData({
          order: res.data.data,
          pingtai_deal: res.data.pingtai_deal,
          order_refund: res.data.order_refund,
          loadover: true,
          is_show: 1,
          hide_lding: true
        })

      }
    })
  },
  receivOrder: function(event) {
    let id = event.currentTarget.dataset.type;
    var token = wx.getStorageSync('token');
    var that = this;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认收到',
      confirmColor: '#F75451',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
              controller: 'order.receive_order',
              token: token,
              order_id: id
            },
            dataType: 'json',
            success: function(res) {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '收货成功',
                  icon: 'success',
                  duration: 1000
                })
                that.reload_data();
              }
            }
          })
        }
      }
    })
  },
  cancelSubmit: function(e) {
    var that = this;
    var from_id = e.detail.formId;
    var token = wx.getStorageSync('token');
    app.util.request({
      'url': 'entry/wxapp/user',
      'data': {
        controller: 'user.get_member_form_id',
        'token': token,
        "from_id": from_id
      },
      dataType: 'json',
      success: function(res) {}
    })
  },
  payNowSubmit: function(e) {
    var that = this;
    var from_id = e.detail.formId;
    var token = wx.getStorageSync('token');
    app.util.request({
      'url': 'entry/wxapp/user',
      'data': {
        controller: 'user.get_member_form_id',
        'token': token,
        "from_id": from_id
      },
      dataType: 'json',
      success: function(res) {}
    })
  },
  callDialog: function(e) {
    var order_id = e.currentTarget.dataset.type;
    var token = wx.getStorageSync('token');
    var that = this;
    wx.showModal({
      title: '取消支付',
      content: '好不容易挑出来，确定要取消吗？',
      confirmColor: '#F75451',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
              controller: 'order.cancel_order',
              token: token,
              order_id: order_id
            },
            dataType: 'json',
            success: function(res) {
              wx.showToast({
                title: '取消成功',
                icon: 'success',
                complete: function() {
                  wx.redirectTo({
                    url: '/lionfish_comshop/pages/order/index'
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  callTelphone: function(e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  applyForService: function(e) {
    var order_id = e.currentTarget.dataset.type;
    var order_goods_id = e.currentTarget.dataset.order_goods_id;

    console.log(order_goods_id);

    var that = this;
    var token = wx.getStorageSync('token');

    wx.redirectTo({
      url: '/lionfish_comshop/pages/order/refund?id=' + order_id + '&order_goods_id=' + order_goods_id
    })

  },
  payNow: function(e) {
    var order_id = e.currentTarget.dataset.type;
    var that = this;
    var token = wx.getStorageSync('token');

    app.util.request({
      'url': 'entry/wxapp/index',
      'data': {
        controller: 'car.wxpay',
        'token': token,
        "order_id": order_id
      },
      dataType: 'json',
      method: 'POST',
      success: function(res) {
        if (res.data.code == 0) {
          wx.requestPayment({
            "appId": res.data.appId,
            "timeStamp": res.data.timeStamp,
            "nonceStr": res.data.nonceStr,
            "package": res.data.package,
            "signType": res.data.signType,
            "paySign": res.data.paySign,
            'success': function(wxres) {
              wx.redirectTo({
                url: '/lionfish_comshop/pages/order/order?id=' + order_id + '&is_show=1'
              })
            },
            'fail': function(res) {
              console.log(res);
            }
          })
        } else if (res.data.code == 2) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  hide_lding: function() {
    wx.hideLoading();
    this.setData({
      is_show: true
    })
  },
  call_mobile: function(event) {
    let mobile = event.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },

  goComment: function(event) {
    let id = event.currentTarget.dataset.type;
    let order_goods_id = event.currentTarget.dataset.order_goods_id;
    var goods_id = event.currentTarget.dataset.goods_id;

    //lionfish_comshop/pages/order / evaluate
    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: '/lionfish_comshop/pages/order/evaluate?id=' + id + '&goods_id=' + goods_id + '&order_goods_id=' + order_goods_id
      })
    } else {
      wx.navigateTo({
        url: '/lionfish_comshop/pages/order/evaluate?id=' + id + '&goods_id=' + goods_id + '&order_goods_id=' + order_goods_id
      })
    }

  },
  goGoods: function(event) {
    let id = event.currentTarget.dataset.type;

    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: '/Snailfish_shop/pages/goods/index?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/Snailfish_shop/pages/goods/index?id=' + id
      })
    }


  },
  orderRefund: function(event) {
    let order_id = event.currentTarget.dataset.type;

    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: '/Snailfish_shop/pages/order/refund?id=' + order_id
      })
    } else {
      wx.navigateTo({
        url: '/Snailfish_shop/pages/order/refund?id=' + order_id
      })
    }


  },
  orderRefunddetail: function(event) {
    let order_id = event.currentTarget.dataset.type;

    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: '/Snailfish_shop/pages/order/refunddetail?id=' + order_id
      })
    } else {
      wx.navigateTo({
        url: '/Snailfish_shop/pages/order/refunddetail?id=' + order_id
      })
    }


  },
  gokefu: function(event) {
    let id = event.currentTarget.dataset.s_id;

    var goods = this.data.goods;
    var seller_info = this.data.seller_info;

    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: '/pages/im/index?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/im/index?id=' + id
      })
    }


  },
  goLink: function(event) {
    let link = event.currentTarget.dataset.link;

    wx.reLaunch({
      url: link
    })
  },
  goLink2: function(event) {
    let link = event.currentTarget.dataset.link;

    var pages_all = getCurrentPages();
    if (pages_all.length > 3) {
      wx.redirectTo({
        url: link
      })
    } else {
      wx.navigateTo({
        url: link
      })
    }

  },
  navShow: function() {
    this.setData({
      navState: 1
    })
  },
  navHide: function() {
    this.setData({
      navState: 0
    })
  },
  orderPay: function(event) {
    var that = this;
    var token = wx.getStorageSync('token');
    let id = event.currentTarget.dataset.type;

    app.util.request({
      'url': 'entry/wxapp/index',
      'data': {
        controller: 'car.wxpay',
        'token': token,
        "order_id": id
      },
      dataType: 'json',
      method: 'POST',
      success: function(res) {
        var is_pin = res.data.is_pin;

        wx.requestPayment({
          appId: res.data.appId,
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function(wxres) {
            if (is_pin == 0) {
              wx.redirectTo({
                url: '/Snailfish_shop/pages/order/order?id=' + id + '&is_show=1'
              })
            } else {
              wx.redirectTo({
                url: '/Snailfish_shop/pages/share/index?id=' + id
              })
            }

          },
          'fail': function(res) {
            console.log(res);
          }
        })

      }
    })


  },
  closeModal: function() {
    this.setData({
      isShowModal: false
    })
  },
  onShareAppMessage: function(res) {
    var order_id = this.data.order.order_info.order_id;
    let goods_share_image = this.data.order.order_goods_list[0].goods_share_image;
    let share_img = this.data.share_img;
    if (order_id && this.is_show_tip == 1) {
      return {
        title: `@${this.data.order.order_info.ziti_name}${this.data.groupInfo.owner_name}，我是${this.data.userInfo.shareNickName}，刚在你这里下单啦！！！`,
        path: "lionfish_comshop/pages/order/shareOrderInfo?order_id=" + order_id,
        imageUrl: share_img ? share_img : goods_share_image
      };
    }
  },
})