var app = getApp();
const config = require('../../../../config/config.js')
const util = require('../../../../utils/util.js')
const Http = require('../../../../utils/HttpBasics.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    organizationType: '',
    picturesUrl: [],
    industryList: [],
    industryIndex: 0,
    qualification_id: "",
    settlement_id: "",
    qualification_type: "",
    message: "",
    flagImgUrl: false,
  },
  setIndustryIndexValue(e) {
    let value = e.detail.value
    console.log(this.data.industryList[value])
    this.setData({
      settlement_id: this.data.industryList[value].settlementId,
      qualification_id: this.data.industryList[value].code,
      qualification_type: this.data.industryList[value].name,
      flagImgUrl: this.data.industryList[value].flag,
      message: this.data.industryList[value].message,
      industryIndex: value,
    })
  },
  getQualification() {
    Http.get({
      url: config.api.getQualification,
      data: {
        organizationType: this.data.organizationType
      }
    }).then(res => {
      console.log(res.data)
      this.setData({
        industryList: res.data,
        qualification_id: res.data[0].code,
        settlement_id: res.data[0].settlementId,
        qualification_type: res.data[0].name,
        message: res.data[0].message
      })
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })
  },

  getReceiverApply() {
    wx.showLoading({
      title: '加载中...',
    })
    Http.get({
      url: config.api.getReceiverApply,
      data: {
        merchantId: app.globalData.merchant.merchant_id

      }
    }).then(res => {

      if (res.data && res.data.organizationType) {
        this.setData({
          organizationType: res.data.organizationType
        })
      }

      if (res.data && res.data.id) {
        this.setData({
          id: res.data.id
        })
      }
      if (res.data && res.data.settlementInfo) {
        this.setData({
          qualification_id: JSON.parse(res.data.settlementInfo).qualification_id, //选择行业id
          settlement_id: JSON.parse(res.data.settlementInfo).settlement_id, //结算规则id
          qualification_type: JSON.parse(res.data.settlementInfo).qualification_type, //所属行业
          picturesUrl: JSON.parse(res.data.qualifications),
          industryIndex: JSON.parse(res.data.settlementInfo).industryIndex
        })
      }


      this.getQualification()


      wx.hideLoading();

    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })
  },

  upPictures() {
    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.api.merchantUpload, // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'token': app.globalData.token
          },
          success(res) {
            // let tempObj = that.data.businessLicenseInfo
            let tempUrl = ""
            let tempArr = that.data.picturesUrl
            tempArr.push(JSON.parse(res.data).data)
            console.log(tempArr)
            that.setData({
              picturesUrl: tempArr
            })

          }
        })
      }
    })
  },
  delPicturesUrl(e) {
    let tempArr = this.data.picturesUrl
    let index = e.currentTarget.dataset.index
    tempArr.splice(index, 1)
    this.setData({
      picturesUrl: tempArr,
    })
  },

  save(e) {
    let flag = e.currentTarget.dataset.flag
    let errMeg = "";
    let errFlag = false;

    if (this.data.flagImgUrl) {
      if (this.data.picturesUrl.length == 0) {
        errMeg = "请上传图片"
        errFlag = true
      }
    }

    if (errFlag) {
      wx.showToast({
        title: errMeg,
        icon: "none",
        duration: 2000
      })
      return
    }



    let settlementInfo = {
      qualification_id: this.data.qualification_id, //选择行业id
      settlement_id: this.data.settlement_id, //结算规则id
      qualification_type: this.data.qualification_type, //所属行业
      industryIndex: this.data.industryIndex
    }

    let parame = {};

    parame = {
      id: this.data.id,
      organizationType: this.data.organizationType,
      settlementInfo: JSON.stringify(settlementInfo),
      qualifications: JSON.stringify(this.data.picturesUrl)

    }


    console.log(parame, "parame")

    Http.post({
      url: config.api.receiverAdd,
      data: parame
    }).then(res => {
      console.log(res, "res")
      if (flag == "save") {
        wx.reLaunch({
          url: '/pages/warehouse/warehouse',
        })
      } else if (flag == "next") {
        wx.navigateTo({
          url: '/pages/warehouse/detail/clearingFirm/clearingFirm',
        })
      }

    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})