// packageH/toker/tokerMerchant/tokerMerchant.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    id: "",
    many_form: [],
    once_form: false,
    member: [],
    order: [],
    card: [],
    checked: "",
    manyStatus: false,
    merchant: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.id = options.id;
    }
    let scene = decodeURIComponent(options.scene);
    if (scene) {
      console.log(scene);
      var info_arr = [];
      info_arr = scene.split(",");
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        console.log(chil_arr);
        if (chil_arr[0] == "id") {
          this.data.id = chil_arr[1];
        }
      }
    }
    this.getData();
  },
  stepChange(evt) {
    let { index } = evt.currentTarget.dataset;
    console.log(evt.detail);
    if (this.data.many_form[index]) {
      this.data.many_form[index].num = evt.detail;
    } else {
      //this.data.info.member_many[index].num = evt.detail;
      //let i="info.member_many["+index+"].num";
      let info = this.data.info;
      info.member_many[index].num = evt.detail;
      this.setData({
        info,
      });
    }
  },
  onceChange(event) {
    this.setData({
      once_form: event.detail,
    });
  },

  manyChange(evt) {
    let { index, item } = evt.currentTarget.dataset;
    console.log(item, item.checked);
    if (evt.detail) {
      this.data.manyStatus = true;
      let arr = {
        id: item.id,
        num: item.num,
      };
      this.data.many_form[index] = arr;
      console.log(this.many_form, "true");
    } else {
      this.data.many_form.splice(index, "1");
    }
    //let i = "info.member_many["+index+"].checked";
    let info = this.data.info;
    info.member_many[index].checked = evt.detail;
    this.setData({
      info,
    });
  },

  getData() {
    let urlStr = app.getNetAddresss("plugin.customer-development.Frontend.controllers.write-off.store");
    app._getNetWork({
      url: urlStr,
      data: { id: this.data.id },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let info = res.data;
        if (info.member_card.member_many) {
          for (let i = 0; i < info.member_card.member_many.length; i++) {
            info.member_card.member_many[i].num = 1;
          }
        }
        this.setData({
          info:info.member_card,
          member: info.member_card.member,
          order: info.member_card.order,
          card: info.member_card.card.goods,
        });
        wx.setNavigationBarTitle({
          title: info.plugin_name?info.plugin_name:'拓客卡',
        });
      },
    });
  },

  submit() {
    if (!this.data.once_form && !this.data.manyStatus) return app.tips("请选择核销内容");
    wx.showModal({
      content: "是否确认核销",
      success: (res) => {
        if (res.confirm) {
          this.submitGet();
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  submitGet() {
    let json = {
      id: this.data.id,
      once: this.data.once_form ? this.data.once_form : "",
      many: this.data.manyStatus ? this.data.many_form : "",
    };
    let urlStr = app.getNetAddresss("plugin.customer-development.Frontend.controllers.write-off.store-handle");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          merchant: true,
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
