var app = getApp();
var $ = require('../../js/util.js');
Page({
  data:{
    list:[],
    tabTxt: ['不需要发票', '普通发票'],
    tab: [false, true],
    ttype: [0, 1, 3],
    param:{
      is_invoice:1,
    },
    is_show:false,
    is_show_zeng:false,
    is_coun:false,
    is_use:true
  },
  onLoad: function (options) {
    var self = this;
    var param = self.data.param;
    param.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();
    });
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Order&a=order_invoice';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      if(ret.status==1){
        self.setData({
          list: ret.data.shop_goodsinfo,
          invoice: ret.data.invoce_info
        })
      }
      },true)
  },
  change_title:function(e){
    var self=this;
    var data = [true, true, true], index = e.currentTarget.dataset.index, o_type = e.currentTarget.dataset.type;
    if (this.data.tab[index] == true) {
      data[index] = false;
      if(o_type!=0){
        this.setData({
          is_show:true,
          is_use:false
        })
      }
      if (o_type == 0) {
        this.setData({
          is_show: false,
          is_use: true
        })
      }
      if (o_type==3){
        this.setData({
          is_show_zeng: true
        })
      }
      if (o_type != 3) {
        this.setData({
          is_show_zeng: false
        })
      }
      this.setData({
        tab: data,
        invoice_type: o_type,
        param: {
          invoice_type: o_type,
        }
      });
    }
  },
  radioChange:function(e){
    var send_type = e.detail.value;
    if(send_type==3){
      this.setData({
        is_coun:true,
        is_use: false
      })
    }else{
      this.setData({
        is_coun: false,
        is_use:true
      })
    }
    var param = this.data.param;
    param.invoice_title = send_type;
    param.id=0;
    this.setData({
      param:param
    })
  },
  formSubmit:function(event){
    var self=this;
    var value = event.detail.value;
    value.invoice_type = self.data.invoice_type;
    value.invotce_title=self.data.param.invoice_title;
    var action = 'c=Order&a=sub_invoice';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', $.ObjectToStr(value), function (ret) {
      if(ret.status==1){
        app.HCtoast('操作成功！');
        setTimeout(function () {
          app.HCchangeData(1, self.data.param, function () {
            app.HCnavigateBack();
          })
        }, 2000);
      }else{
        app.HCerror('提示', ret.msg, false); return;
      }
    },true)
  }
})