const api = require('../../../utils/api.js');
const login = require('../../../utils/index.js');
const app = getApp(), {
  globalData: {
    util: {
      regeneratorRuntime,
      navigate_auth_native,
      getWeatherInfo
    }
  }

} = app;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    district: "洛龙"
  },
  navigateTo: function (e) {
    let that = this
    let getUserInfo = login.default.getUserInfo();
    getUserInfo.then((resolve) => {
      that.data.userInfo = resolve
      if (!that.data.userInfo.id) {
        return false
      } else {
        if (that.data.userInfo.isRealname != 1) {
          wx.showToast({
            title: '请先完善用户信息',
            icon: 'none'
          })
          return false
        }

        switch (that.flag) {
          case '1':
            if (!that.data.userInfo) {
              login.default.auth()
              return false
            } else if (that.data.userInfo.identity != 0) {
              wx.showToast({
                title: '企业身份不能办理个人事项',
                icon: 'none'
              })
              return false
            }
            break;
          case '2':
            if (!that.data.userInfo) {
              login.default.auth()
              return false
            } else if (that.data.userInfo.identity != 1) {
              wx.showToast({
                title: '个人身份不能办理企业事项',
                icon: 'none'
              })
              return false
            }
            break
        }

        getApp().globalData.util.navigate_auth_native(e);
      }
    })
  },
  nav(e) {
    let that = this
    switch (that.flag) {
      case '1':
        if (!that.data.userInfo) {
          login.default.auth()
          return false
        } else if (that.data.userInfo.identity != 0) {
          wx.showToast({
            title: '请切换到个人',
            icon: 'none'
          })
          return false
        }
        break;
      case '2':
        if (!that.data.userInfo) {
          login.default.auth()
          return false
        } else if (that.data.userInfo.identity != 1) {
          wx.showToast({
            title: '请切换到法人',
            icon: 'none'
          })
          return false
        }
        break
    }
    if (that.data.userInfo.isRealname != 1) {
      wx.showToast({
        title: '请先实名',
        icon: 'none'
      })
      return false
    }
    getApp().globalData.util.navigate_auth_native(e);
  },
  inText: function (e) {
    let that = this
    let arr = []
    if (that.data.params) {
      that.data.param = that.data.params
    } else {
      that.data.params = that.data.param
    }
    for (var i = 0; i < that.data.param.length; i++) {
      if (String(that.data.param[i].serviceName).indexOf(String(e.detail.value).trim()) != -1) {
        arr.push(that.data.param[i])
      }
    }
    that.setData({
      param: arr
    })
  },
  nullText: function (e) {
    let that = this
    if (!String(e.detail.value).trim()) {
      that.setData({
        param: that.data.params ? that.data.params : that.data.param
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad({
    id,
    scene = "",
    flag,
    preService,
    code,
    unid,
    titleName
  }) {
    let that = this
    wx.setNavigationBarTitle({
      title: titleName,
    })
    let options = {}
    options.flag = flag
    options.preService = preService
    options.code = code
    options.unid = unid
    let [pid = id, cid = 0] = scene.match(/\d{1,}/g) || []
    this.data = {
      pid,
      cid
    }
    that.req(options)

    this.setData({ weather: await getWeatherInfo() })


  },
  req: function (options) {
    let that = this
    that.flag = options.flag
    wx.showLoading({
      title: '正在加载',
    })
    wx.setStorageSync('flag', options.flag)
    var requestData = ''
    if (options.preService == 'PERSON_ZT' & options.flag == 1) {
      requestData = 'personZt'
    } else if (options.preService == 'PERSON_RSSJ' & options.flag == 1) {
      requestData = 'personRssj'
    } else if (options.preService == 'PERSON_TDRQ' & options.flag == 1) {
      requestData = 'personTdrq'
    } else if (options.preService == 'ENTERPRISE_ZT' & options.flag == 2) {
      requestData = 'enterpriseZt'
    } else if (options.preService == 'ENTERPRISE_TDDX' & options.flag == 2) {
      requestData = 'enterpriseTddx'
    } else if (options.preService == 'ENTERPRISE_JYHD' & options.flag == 2) {
      requestData = 'enterpriseJyhd'
    }
    if (options.unid) {
      wx.request({
        url: api.handles,
        data: {
          'flag': options.flag,
          deptUnid: options.unid
        },
        method: 'GET',
        success(res) {
          wx.hideLoading()
          that.data.param = res.data.data
          that.setData({
            param: res.data.data,
            flag: options.flag
          })
        }
      })
      return false
    } else {
      wx.request({
        url: api.handles,
        data: {
          [requestData]: options.code,
          'flag': options.flag
        },
        method: 'GET',
        success(res) {
          wx.hideLoading()
          that.data.param = res.data.data
          that.setData({
            param: res.data.data,
            flag: options.flag
          })
        }
      })
    }

  },

  nav: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      param: this.data.params,
      searchValue: ''
    })
    wx.stopPullDownRefresh()

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({
    from,
    target
  }) {
    let cid = 0;
    if (from == 'button') {
      ({
        dataset: {
          cid
        }
      } = target)
    }
    return {

      title: '弹出分享时显示的分享标题',

      //imageUrl: '',

      path: `pages/handle/details/index?scene=${this.data.pid}_${cid}`


    }
  },


  setcur({
    target: {
      dataset: {
        sindex,
        url,
        docCount
      }
    }
  }) {
    sindex && this.setData({
      cur: sindex
    })
  },
  navigate_auth_native,


})