var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    cart_list: [],//订单页数据
    address_list:[],
    param:{
      cart_ids: '',
      type: 14,
      send_type: 2,
      temp: '',
      delivery_id: 0,
      goods_id: 0,
      number: 0,
      goods_attr: '',
      pid: 0,
      pt_id: 0
    },
    is_showshop:false,//是否显示自提门店
    is_showCoupon:false,//显示优惠券
    is_showmask:false,
    coupon_cont:'',
    total_shipping_fee:0,
    is_pwd:false,//是否显示密码
    is_payment:false,
    is_invoice:0,//是否开启发票
    //配送时间
    multiIndex: [0, 0],
    service_time_desc: '请选择配送时间',
    best_time_day: 0,
    best_time_hour: 0,
    
    //配送时间end
    invoice_type:"不需要",
    bonus_msg:"立即使用",
  },
  onLoad: function (options) {
    var self = this;
    var param = self.data.param;
    param.type = options.type;
    // this.setData({
    //   param: {
    //     cart_ids: 273,
    //     type: 14
    //   },
    // });
    if (options.cart_ids) {
      param.cart_ids = options.cart_ids;
    }
    if (options.goods_id) {
      param.goods_id = options.goods_id;
    }
    if (options.number) {
      param.number = options.number;
    }
    if (options.goods_attr) {
      param.goods_attr = options.goods_attr;
    }
    if (options.type) {
      param.type = options.type;
    }
    if (options.pid) {
      param.pid = options.pid;
    }
    if (options.pt_id) {
      param.pt_id = options.pt_id;
    }
    this.setData({
      param: param
    });
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function (ret) {
      self.getIndex();
    });
  },
  //修改收货地址
  changeData: function (data) {
    if (data.invoice_type){
      var param = this.data.param;
      param.invoice_type = data.invoice_type;
      param.invoice_title = data.invoice_title
      this.setData({
        param: param
      })
      if (data.invoice_type==1){
        this.setData({
          invoice_type:"普通发票"
        })
      } else if (data.invoice_type == 3){
        this.setData({
          invoice_type: "增值税发票"
        })
      }
    }else{
      var param = this.data.param;
      if (data.id ==0) {
        param.delivery_id = 0;
      } else {
        param.delivery_id = data.id;
      }

      this.setData({
        param: param
      })
      this.setData({
        address_list: data,
        invoice_type: "不需要发票"
      });
    }
    this.getIndex();
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Order&a=index';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      // console.log(ret);
      // console.log(app.globalData.HCshopid);
      if (ret && ret.status == 1) {
        var shop_id = app.globalData.HCshopid;
        // var service_time_format = ret.data.service_time_format;
        // var service_time_format_arr = new Array;
        // service_time_format_arr.push(service_time_format[0]);
        // service_time_format_arr.push(service_time_format[1]);
        self.setData({
            cart_list: ret.data.data,
            address_list: ret.data.addr_info.address_info,
            addres_status: ret.data.addr_info.status,
            shop_info: ret.data.data.list[shop_id]['shop_info'],
            coupons: ret.coupons,
            coupon_cont:ret.coupons.total+"张优惠券",
            user_info:ret.member_info,
            total_price: ret.data.data.all_price,
            pay_price: ret.data.data.all_price,
            // service_time_format: service_time_format_arr,
            // service_time_format_tmp: ret.data.service_time_format
            is_invoice:ret.is_invoice,//是否开启发票
          })
        if (ret.data.addr_info && ret.data.addr_info.address_info){
          self.setData({
            address_list: ret.data.addr_info.address_info
          })
          var param = self.data.param;
          param.delivery_id = self.data.address_list.id;
          self.setData({
            param: param
          })
        }
        var param = self.data.param;
        param.addres_status = self.data.addres_status;
        self.setData({
          param:param
        })
        //计算价格
        self.count_total();
      }else{
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  //选择配送方式
  radioChange:function(e){
    var send_type =e.detail.value;
    var param = this.data.param;
    param.send_type = send_type;
    
    this.setData({
      param: param,
    });
    if(send_type==1){
      this.setData({
        is_showshop: true,
      });
    }else{
      this.setData({
        is_showshop: false,
      });
    }
    this.count_total();
  },
  //优惠券显示
  showCoupon:function(){
    this.setData({
      is_showCoupon: true,
      is_showmask:true,
    })
  },
  //关闭遮罩
  cloce_mask:function(){
    if (this.data.is_showCoupon==true){
      this.setData({
        is_showCoupon: false,
      })
    }
    if (this.data.is_pwd==true){
      this.setData({
        is_pwd: false,
      })
    }
    if (this.data.is_payment==true){
      this.setData({
        is_payment:false
      })
    }
    this.setData({
      is_showmask: false,
    })
  },
  //获取订单留言
  getOrderTemp:function(event){
    var temp = event.detail.value;
    var param = this.data.param;
    param.temp = temp;
    this.setData({
      param: param,
    });
  },
  //选择优惠券
  bindbonusChange:function(event){
    var money = event.currentTarget.dataset.index;
    var bonus_id = event.currentTarget.dataset.id;
    var param = this.data.param;
    param.bonus_id = bonus_id;
    param.bonus_money = money;
    this.setData({
      param: param,
      coupon_cont: "￥" + money,
    });
    this.count_total();
  },
  //选择余额
  switch1Change:function(e){
    var use_account;
    if (e.detail.value==true){
      use_account=1;
    }else{
      use_account=0;
    }
    var param = this.data.param;
    param.use_account = use_account;
    param.payment_id=1;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //选择积分
  switch2Change: function (e) {
    var score;
    if (e.detail.value == true) {
      score = 1;
    } else {
      score = 0;
    }
    var param = this.data.param;
    param.score = score;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //计算价格
  count_total:function(){
    var self = this;
    var action = 'c=Order&a=count_total';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data.param, function (ret) {
      if (ret.status == 1) {
        self.setData({
          total_info: ret.total_info,
          total_price: ret.total_info.total_price,
          pay_price: ret.total_info.pay_price,
          total_shipping_fee: ret.total_info.total_shipping_fee
        });
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  //选择收货地址
  goTonearby: function (event) {
    app.HCnavigateTo('../../userinfo/address/address?type=1');
  },
  //选择支付方式
  radiopayChange:function(e){
    var payment_id = e.detail.value;
    var param = this.data.param;
    param.payment_id = payment_id;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //设置支付密码
  setpwd:function(e){
    var param = this.data.param;
    param.pay_pwd = e.detail.value;
    this.setData({
      param: param
    });
  },
  //提交订单
  sub_order: function () {
    var self = this;
    if (self.data.pay_price > 0 && (self.data.param.use_account > 0 || self.data.param.score > 0)) {
      self.setData({
        is_pwd: true,
        is_payment: true,
        is_showmask: true
      });
    } else if (self.data.pay_price > 0 && self.data.param.use_account == 0 ) {
      self.setData({
        is_payment: true,
        is_showmask: true
      });
    } else if (self.data.param.use_account > 0 || self.data.param.score > 0 && self.data.pay_price == 0 ) {
      self.setData({
        
        is_pwd: true,
        is_showmask: true
      });
    } else {
      self.balance();
    }
  },
  balance: function (e) {
    var self = this;
    if (e && e.detail&&e.detail.formId){
      self.data.param.formId = e.detail.formId;
    }
    var action = 'c=Order&a=sub_order';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data.param, function (ret) {
      if (ret.status == 1) {
        if (ret.data && ret.data.order_id && self.payment_id != 16) {
          self.goToDetaile(ret.data.order_id);
        } else if (ret.data && ret.data.pay_id) {
          self.setData({
            id: ret.data.pay_id,
          });
          self.web_pay(ret.data.pay_id);
        }

      } else {
        if (ret.data.url == 3) {
          // console.log(3);
          app.HCerror('提示', ret.msg, true, function (ret) {
            if (ret.confirm) {
              self.jumprespwd();
            }
          }); return;
        }
        if (ret.data.app_pay == 1 && self.data.param.use_account > 0 || self.data.param.score > 0){
          app.HCerror('提示', ret.msg);
          self.setData({
            is_pwd: true,
            is_payment: true,
            is_showmask: true
          });
          return;
        }
        if (ret.data.app_pay == 1){
          app.HCerror('提示', ret.msg); 
          self.setData({
            is_payment: true,
            is_showmask: true
          });
          return;
        }
        // console.log(1);
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
   jumprespwd:function(){
    app.HCnavigateTo('../../userinfo/reset_pwd/reset_pwd');
  },
  web_pay: function (pay_id) {
    var self = this;
    //console.log(self.data.param.payment_id);
    // if(self.data.param.payment_id==17){
    //   var action = 'c=Wxsmallpay&a=pay_moneydone';
    // }else{
      var action = 'c=Wxsmallpay&a=pay_done';
    //}
    var param=self.data.param;
    param.id = pay_id;
    self.setData({
      param:param
    })
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data.param, function (ret) {
     // console.log(ret);
      if (ret.status == 1) {
        app.HCrequestPayment(ret.wxsmallcode, function (ret) {
          if (ret.status == 1) {
            app.HCswitchTab('../../userinfo/index/index');
          } else {
            app.HCerror('提示', '支付失败'); 
            app.HCswitchTab('../../userinfo/index/index'); 
            return;
          }
        });
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  bindMPChange:function(event){
    var self = this;
    var value = event.detail.value;
    var service_time_format_tmp = self.data.service_time_format_tmp;
    var best_time_day = 0;
    var best_time_hour = 0;
    var service_time_desc = service_time_format_tmp[0][value[0]].n;
    if (service_time_format_tmp[1][value[1]].s == 1) {//立即配送
      var service_time_desc = service_time_format_tmp[1][value[1]].n;
    }else if(value[0]==0){
      service_time_desc += service_time_format_tmp[1][value[1]].n;
      best_time_hour = service_time_format_tmp[1][value[1]].k;
    }else{
      service_time_desc += service_time_format_tmp[2][value[1]].n;
      best_time_hour = service_time_format_tmp[2][value[1]].k;
    }
    best_time_day = service_time_format_tmp[0][value[0]].k;
    
    self.setData({
      service_time_desc:service_time_desc,
      best_time_day: best_time_day,
      best_time_hour: best_time_hour,
      multiIndex: value
    });
    var param = self.data.param;
    param.best_time_day = best_time_day;
    param.best_time_hour=best_time_hour,
      self.setData({
      param: param
    });
  }, 
  bindMPColumnChange: function (event) {
    var self = this;
    var detail = event.detail;
    var service_time_format = self.data.service_time_format;
    var service_time_format_tmp = self.data.service_time_format_tmp;
    if (detail.column == 0){
      if (detail.value > 0) {
        service_time_format[1] = service_time_format_tmp[2];
      } else {
        service_time_format[1] = service_time_format_tmp[1];
      }
    }
    self.setData({
      service_time_format: service_time_format
    });
  },
  //跳转
  goToDetaile: function (e) {
    app.HCredirectTo('../../buycart/order_success/order_success?order_id=' + e);
  }, 
  //跳转
  goToInvoice: function (e) {
    app.HCnavigateTo('../../invoice/invoice?cart_ids=' + this.data.param.cart_ids+'&goods_id=' + this.data.param.goods_id+'&number=' + this.data.param.number+'&goods_attr='+this.data.param.goods_attr+'&pt_id='+this.data.param.pt_id+'&pid='+this.data.param.pid);
  }, 
})