const Http = require('../../utils/HttpBasics.js')
const config = require('../../config/config.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tihuoWay: '微信商户号',
    accountData: "",
    accountTypeValue: 0,
    isFlag: false,
    accountCode: false,
    cOpendId: ''
  },
  /**
   * 查询收款账户状态
   */
  findAccountById(merchant_id) {
    Http.get({
      url: config.api.findAccountById,
      data: {
        id: merchant_id
      }
    }).then(res => {
      /**
       * receiverAccount
       * 如有：已设置收款账户
       * 没有；未设置收款账户
       */
      if (res && res.data && res.data.receiver) {
        this.setData({
          isSet: true,
          receiverAccount: res.data.receiver.receiverAccount,
          accountData: res.data.receiver
        })
      } else {
        this.setData({
          isSet: false
        })
      }
    }).catch(error => {
      wx.showToast({
        title: error.message,
        icon: "none"
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (options && options.merchant_id) {
      that.findAccountById(options.merchant_id)
      that.setData({
        merchant_id: options.merchant_id,
        phone: options.phone
      })
    }
    if (options && options.merchant_name) {
      that.setData({
        merchant_name: options.merchant_name,
        phone: options.phone
      })
    }
    this.getAccount(options.phone);
  },
  //查询商户C端授权状态
  getAccount(phone) {
    let that = this;
    Http.get({
      url: `${config.api.getOpenIdByPhone}?phone=${phone}`,
      data: {
        phone
      }
    }).then(res => {
      console.log(res, 111)//
      this.setData({
        haveAccount: res.message,
        cOpendId: res.data
      })
    }).catch(error => {
      console.log(error, 222)
      this.setData({
        haveAccount: error.message

      })
    })
  },
  // 提交
  formSubmit: function (e) {
    let that = this;
    let accountTypeValue = that.data.accountTypeValue;
    let accountId = e.detail.value.id;
    let accountName = e.detail.value.name;
    let id = that.data.merchant_id;
    let name = that.data.merchant_name;
    that.setData({
      select: false
    })
    if (!accountId) {
      if (accountTypeValue == 0) {
        wx.showToast({
          title: '商户号不能为空',
          icon: "none"
        })
      }
      if (accountTypeValue == 1) {
        wx.showToast({
          title: '微信号不能为空',
          icon: "none"
        })
      }
    }
    if (!accountName) {
      if (accountTypeValue == 0) {
        wx.showToast({
          title: '商户名不能为空',
          icon: "none"
        })
      }
      if (accountTypeValue == 1) {
        wx.showToast({
          title: '姓名不能为空',
          icon: "none"
        })
      }
    }
    let data = {
      accountTypeValue: that.data.accountTypeValue == 1 ? 3 : that.data.accountTypeValue,
      accountId: (that.data.accountTypeValue == 0 ? e.detail.value.id : this.data.cOpendId).replace(/\s+/g, ""),
      accountName: (e.detail.value.name).replace(/\s+/g, ""),
      id: that.data.merchant_id,
      name: that.data.merchant_name
    }
    console.log(data)
    if (!data.accountId) {
      wx.showToast({
        title: '当前状态不能提交',
        icon: "none"
      })
      return;
    }
    if (accountId && accountName && id && name) {
      Http.post({
        url: config.api.updateAccount,
        data: data
      }).then(res => {
        wx.navigateTo({
          url: '/pages/accountManagement/result/success',
        })
      }).catch(error => {
        wx.showToast({
          title: error.message,
          icon: "none"
        })
      })
    }
  },
  submitAccount() {
    let that = this;
    let data = {
      accountTypeValue: 3,
      accountId: (this.data.cOpendId).replace(/\s+/g, ""),
      accountName: (this.data.accountData.trueName).replace(/\s+/g, ""),
      id: this.data.accountData.merchantId,
      name: that.data.merchant_name
    }
    console.log(data)
    if (data.accountId && data.accountName && data.id && data.name) {
      Http.post({
        url: config.api.updateAccount,
        data: data
      }).then(res => {
        wx.navigateTo({
          url: '/pages/accountManagement/result/success',
        })
      }).catch(error => {
        wx.showToast({
          title: error.message,
          icon: "none"
        })
      })
    }
  },
  bindShowMsg(e) {
    this.setData({
      select: !this.data.select
    })
  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay: name,
      accountTypeValue: e.currentTarget.dataset.accounttypevalue,
      select: false
    })
  },
  //申请变更
  reset() {
    let that = this;
    that.setData({
      isFlag: true
    })
  },
  //删除账号得信息
  deleteByReceiverId(e) {
    Http.get({
      url: config.api.deleteByReceiverId,
      data: {
        id: e.currentTarget.dataset.id
      }
    }).then(res => {
      wx.navigateTo({
        url: '/pages/accountManagement/result/success?type=delete',
      })
    }).catch(error => {
      wx.showToast({
        title: `${error.message}`,
        icon: "none"
      })
    })
  }
})