// packageF/storeManagement/withdrawalDetail/withdrawalDetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    status: '',
    recordData: {},
    incomes: [],
    showPopup: false,

    allCheck: false,
    isOperation: false,
    isShowSheet: false,
    statusActions: [
      {status: 1, name: "通过"},
      {status: 3, name: "驳回"},
      {status: -1, name: "无效"},
    ],
    batchIncomes: [], // 批量操作的收入id
    batchIncomesAll:[],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.data.id = options.id;
    }
    this.getData();
  },

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

  },

  changeType(evt) {
    let name = evt.currentTarget.dataset.name;
    this.setData({
      [name]: evt.detail
    });
    if(name=="allCheck"){
      if (evt.detail) {
        let levels = this.data.batchIncomesAll.map((item) => {
          return item+"";
        });
        this.setData({
          batchIncomes: levels
        });
      } else {
        this.setData({
          batchIncomes: []
        });
      }
    }
  //  console.log(this.data.allCheck)
  },

  setShowPopup () {
    this.setData({
      showPopup:!this.data.showPopup
    });
  },

  showSheet (event) {
    let index = event.currentTarget.dataset.index
    if (this.data.status == 0) {
      this.setData({
        setIndex: index,
        isShowSheet: true
      })
    }
  },
  closeSheet () {
    this.setData({ isShowSheet: false});
  },
  changeStatus (event) {
    this.setData({
      isShowSheet: false,
      [`incomes[${this.data.setIndex}].auditStatus`]: event.detail.status,
      [`incomes[${this.data.setIndex}].auditStatusText`]: event.detail.name,
    })
  },

  getData(){
    let urlStr = app.getNetAddresss('plugin.shop-assistant.frontend.withdraw.withdraw-detail');
    app._postNetWork({
      url: urlStr,
      data: {id:this.data.id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let changeData={};
        changeData.recordData = res.data;
        changeData.status = res.data.status;
        changeData.incomes = res.data.type_data ? res.data.type_data.incomes : [];
        if (changeData.status == 0 || changeData.status == -1) {
          changeData.isOperation = true;

        } else {
          changeData.isOperation = false;
        }
        changeData.batchIncomesAll = changeData.incomes.map(item=>item.id);
        this.setData(changeData);
        
      }
    });
  },

  clickBatchBotton(event){
    if(this.data.batchIncomes.length<=0) return app.tips("请选择需要修改的记录");
    this.data.incomes.forEach((ele, index) => {
      let id = ele.id + '';
      if (this.data.batchIncomes.includes(id)) {
        let params = event.currentTarget.dataset.params;
        this.setData({
          [`incomes[${index}].auditStatus`]: params.status,
          [`incomes[${index}].auditStatusText`]: params.name,
        })
      }
    });
  },

  clickButton (evt,incomeList=[]) {
    let {type,opt} = evt.currentTarget.dataset;
    let json = {
      id: this.data.id,
      submit_operation: type
    };
    let tips = '';
    if (type == 1) {
      // 审核
      if (this.data.recordData.type == "balance") {
        json.status = opt;
      } else {
        //收入审核，默认操作全部记录
        json.audit = {};
        this.data.incomes.forEach((item) => {
          if (item.auditStatus) {
            json.audit[item.id] = item.auditStatus;
          } else {
            json.audit[item.id] = this.data.statusActions[0].status;
          }
        });
      }
    } else if (type == 2) {
      tips = "是否需要" + this.data.recordData.payment_button_name;
      // 打款
    } else if (type == 3) {
      // 重新审核
      tips = "将重新审核记录,确认是否执行该操作!";
    } else if (type == 5) {
      // 重新打款
      tips = "重新打款一次，确认是否执行该操作!";
    } else if (type == 6) {
      // 审核驳回
      tips = "驳回后，需要会员重新申请提现（仅驳回审核通过提现）";
    } else if (type == 7) {
      // 线下确认打款
      tips = "本打款方式需要线下打款，系统只是完成流程!";
    }
    this.submitOperation(json, tips);
  },

  async submitOperation (data, tip) {
    if (tip) {
      let flag = await app.confirm(tip); 
      if(!flag) return;
    }
    let urlStr = app.getNetAddresss('plugin.shop-assistant.frontend.withdraw.examine');
    app._postNetWork({
      url: urlStr,
      data,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let changeData={};
        changeData.status = 'all';
        changeData.recordData = {};
        changeData.incomes = [];
        changeData.showPopup = false;
        this.setData(changeData);
        this.getData();
      }
    });
    
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});