// packageI/personRed/personRedIssue/personRedIssue.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customBalance: '', // 自定义余额字样
    balance: '', // 余额
    valid_hours: '', // 红包时间
    setting_child_receive: 0,
    showSheet: false,
    form: {
      number: '', // 红包个数
      amount_total: '', // 红包金额
      title: '', // 红包标题
      redpack_type: 1, // 红包类型
      unit_amount: '', // 单个红包金额
      child_receive: 0, // 团队领取
    },
    actions: [
      { name: '拼手气红包', value: 1 },
      { name: '固定金额红包', value: 0 },
    ],
    fileList: [],
    uploadUrl: "upload.uploadPic", // 图片上传url
  },

  onLoad: function (options) {
    let yz_basic_info = wx.getStorageSync('yz_basic_info');
    this.setData({
      customBalance: yz_basic_info.balance.balance?yz_basic_info.balance.balance: "余额"
    });
  },

  onShow: function () {
    this.reserve();
  },

  reserve () {
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.redpack.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        this.setData({
          balance: res.data.data.balance,
          valid_hours: res.data.data.valid_hours,
          need_password: res.data.data.need_password,
          setting_child_receive: res.data.data.setting_child_receive
        });
      }
    });
  },

  showSheet () {
    this.setData({
      showSheet: true,
    });
  },
  closeSheet () {
    this.setData({
      showSheet: false,
    });
  },

  inpNumber (event) {
    let num = event.detail;
    num = (num).replace(/[^\d]/g,'');
    if (this.data.form.redpack_type == 0) {
      let total = (num * this.data.form.unit_amount).toFixed(2);
      this.setData({
        'form.amount_total': total
      });
    }
    this.setData({
      'form.number': num
    });
    
  },
  inpTotal (event) {
    let num = event.detail;
    if (isNaN(num)) {
      return;
    }
    this.setData({
      'form.amount_total': num
    });
  },
  inpUnit (event) {
    let num = event.detail;
    if (isNaN(num)) {
      return;
    }
    let total = (num * this.data.form.number).toFixed(2);
    this.setData({
      'form.unit_amount': num,
      'form.amount_total': total
    });
  },
  inpTitle (event) {
    let text = event.detail;
    this.setData({
      'form.title': text
    });
  },


  // 红包模式
  checkGiftType (event) {
    if (event.detail.value !== this.data.form.redpack_type) {
      this.setData({
        [`form.amount_total`]: "",
        [`form.unit_amount`]: "",
      });
    }
    this.setData({
      [`form.redpack_type`]: event.detail.value
    });
  },
  
  // 是否只限团队成员领取
  checkChange (event) {
    let child_receive = event.detail ? 1 : 0;
    this.setData({
      ['form.child_receive']: child_receive
    });
  },

  // 上传图片
  upLoadImg(event) {
    const { file } = event.detail;
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success(resdata) {
        let res = JSON.parse(resdata.data);
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url: res.data.img_url, status:"done", message: '上传完成'});
        that.setData({ fileList });
      },
      fail (err) {
        let fileList = [];
        fileList.push({ ...file, url: '', status:"failed", message: '上传失败'});
        that.setData({ fileList });
      }
    });
  },
  deletaLoadImg (event) {
    let fileList = this.data.fileList;
    fileList[event.detail.index] && fileList.pop(event.detail.index);
    this.setData({
      fileList
    });
  },

  // 检验参数
  paramsIsEmpty () {
    let params = this.data.form;
    if (!params.number) {
      wx.showToast({
        title: '红包个数不能为0',
        icon: "none"
      });
      return true;
    }
    if (app._isTextEmpty(params.amount_total * 1) || params.amount_total < 0) {
      if (params.redpack_type == 1) {
        wx.showToast({
          title: '红包总额不能为空或小于0',
          icon: "none"
        });
        return true;
      } else  if (params.redpack_type == 0){
        wx.showToast({
          title: '单个红包金额不能为空或小于0',
          icon: "none"
        });
        return true;
      }
    }
    return false;
  },

  // 发红包
  grantGift () {
    if (this.paramsIsEmpty()) {
      return;
    }
    // 红包图标
    if (this.data.fileList.length > 0) {
      this.data.form.thumb = this.data.fileList[0].url;
    }
    // 总金额
    if (this.data.form.amount_total > 0) {
      let amount_total = Number(this.data.form.amount_total).toFixed(2);
      this.setData({
        'form.amount_total': amount_total,
      });
    }
    // 红包标题
    if (app._isTextEmpty(this.data.form.title)) {
      this.setData({
        'form.title': "红包来袭~"
      });
    }

    let redpack_date = JSON.stringify(this.data.form);
    let member = JSON.stringify({balance: this.data.balance, need_password: this.data.need_password });
    wx.navigateTo({
      url: `/packageI/personRed/personRedPay/personRedPay?info=${redpack_date}&member=${member}`,
    });
  },
});