// packageI/handwrittenEsign/handwrittenEsignIndex/handwrittenEsignIndex.js
const app = getApp();
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
Page({
  behaviors: [yz_pay],
  /**
   * 页面的初始数据
   */
  data: {
    status: 1, //1||已签名状态
    resultImg: "",
    content: null,
    buttons: [],
    pay_type: null,
    showPay: false,
    money: "",
    ordersn: "",
    loading: true,
    fieldStatus:0,
    field: [],
    form_data: null,
    thumb: '',
    description:''
  },

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

  },
  getData() {
    this.setData({
      resultImg: null
    });
    let urlStr = app.getNetAddresss("plugin.hand-sign-protocol.frontend.controllers.protocol.getDetail");
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.options.id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            content: res.data.protocol.content,
            money: res.data.protocol.amount,
            status: res.data.protocol.sign_status,
            resultImg: res.data.protocol.sign_status ? res.data.protocol.img_url : null,
            loading: false,
            fieldStatus: res.data.form.status,
            field: res.data.form.fields,
            thumb: res.data.form.thumb,
            description: res.data.form.description
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });

  },
  submit(e){
    if(e) this.data.form_data = e.detail;
    this.singConfirm();
  },

  singConfirm(e) {
    if(e) this.data.resultImg = e.detail;
    if(Object.keys(this.data.field).length != 0&&!this.data.form_data){
      let childComponent = this.selectComponent('#diyform');
      childComponent.submit();
      return;
    }
    if (this.data.payMethods && this.data.payMethods.length > 0 && this.data.resultImg) {
      this.setData({
        showPay: true
      });
      return;
    }
    this.getPayButton();
  },
  getPayButton() {
    let urlStr = app.getNetAddresss("plugin.hand-sign-protocol.frontend.controllers.protocol.getButtons");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          for (const methodItem of res.data.buttons) {
            methodItem["_iconClassName"] = this.btnclass(
              methodItem["value"]
            );
          }
          this.setData({
            payMethods: res.data.buttons,
            showPay: true
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  payFee({
    currentTarget: {
      dataset: {
        type
      },
    },
  }) {
    let urlStr = app.getNetAddresss("plugin.hand-sign-protocol.frontend.controllers.protocol.pay");
    app._postNetWork({
      url: urlStr,
      data: {
        pay_type: type,
        img_url: this.data.resultImg,
        id: this.options.id,
        form_data: this.data.form_data,
        client_type: 2
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          const {
            config,
            order_sn,
          } = res.data;
          if (!order_sn) {
            wx.showToast({
              icon: "none",
              title: "参数错误",
              duration: 1500,
            });
          }
          this.rechargePay(type, config, order_sn, 'credit');
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  onClose() {
    this.setData({
      showPay: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});