const api = require('../../../utils/api.js');
const login = require('../../../utils/index.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    mask:false,
    showText:'显示更多',
    dis:false,
    keepImg:'../../../images/keep.png'
  },
  previewimg:function(e){
    var imgList=[]
    imgList.push(e.currentTarget.dataset.url)
    wx.previewImage({
      urls: imgList,
    })
  },
  // 底部跳转
  navigator:function(e){
    let that=this
    let getUserInfo = login.default.getUserInfo();
    getUserInfo.then((resolve, reject) => {
      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
        }
        if (!that.data.userInfo) {
          login.default.auth()
          return false
        }
        let frIsYesNo = that.data.guide.preService.forUser.indexOf('法人')
        let grIsYesNo = that.data.guide.preService.forUser.indexOf('个人')
        if(!e.currentTarget.dataset.type){
          switch (this.data.flag) {
            case '1':
              if (that.data.userInfo.identity != 0) {
                wx.showToast({
                  title: '企业身份不能办理个人事项',
                  icon: 'none'
                })
                return false
              }
              break;
            case '2':
              if (that.data.userInfo.identity != 1) {
                wx.showToast({
                  title: '个人身份不能办理企业事项',
                  icon: 'none'
                })
                return false
              }
              break
          }
          if (that.data.search) {
            if (that.data.userInfo.identity != 1 && grIsYesNo == -1) {
              wx.showToast({
                title: '个人身份不能办理企业事项',
                icon: 'none'
              })
              return false
            } else {
              if (that.data.userInfo.identity != 0 && frIsYesNo == -1) {
                wx.showToast({
                  title: '企业身份不能办理个人事项',
                  icon: 'none'
                })
                return false
              }
            }
          }
          if (that.data.collecct) {
            if (that.data.userInfo.identity != 1 && grIsYesNo == -1) {
              wx.showToast({
                title: '个人身份不能办理企业事项',
                icon: 'none'
              })
              return false
            } else {
              if (that.data.userInfo.identity != 0 && frIsYesNo == -1) {
                wx.showToast({
                  title: '企业身份不能办理个人事项',
                  icon: 'none'
                })
                return false
              }
            }
          }
        }
        if (e.currentTarget.dataset.type =='switchTab') {
          wx.setStorageSync('unid', e.currentTarget.dataset.unid)
          wx.switchTab({
            url: e.currentTarget.dataset.url,
          })
          return false
        }
        wx.navigateTo({
          url: e.currentTarget.dataset.url,
        })
      }
    })
  },
  nav(e){
    
  },
  // 拨打咨询电话
  call:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  // 收藏
  keep:function(){
    if(!wx.getStorageSync('userId')){
      let getUserInfo = login.default.getUserInfo();
    }else{
      let that = this
      wx.showLoading({
        title: '请稍后',
      })
      var zhllCollect = {}
      zhllCollect.type = 1
      zhllCollect.serviceUnid = that.data.guide.preService.unid
      zhllCollect.serviceName = that.data.guide.preService.serviceName
      zhllCollect.userId = wx.getStorageSync('userId')
      wx.request({
        url: api.addCollect,
        data: zhllCollect,
        method: 'POST',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res)
          // if (that.data.keep) {
          //   that.setData({
          //     keepImg: '../../../images/keep.png'
          //   })
          //   that.data.keep = false;
          //   wx.showToast({
          //     title: '已取消收藏',
          //     icon: 'none'
          //   })
          // } else {
          that.setData({
            keepImg: '../../../images/keep_check.png'
          })
          that.data.keep = true
          that.setData({
            ['guide.isCollect']: 1
          })
          wx.showToast({
            title: '收藏成功',
            icon: 'none'
          })
          // }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  kindToggle:function(e){
    let that=this
    if (that.data.expand === e.currentTarget.dataset.value) {
      that.setData({
        dis: false,
        expand: e.currentTarget.dataset.value,
      })
      that.data.expand = ''
    } else {
      that.setData({
        dis: true,
        expand: e.currentTarget.dataset.value,
      })
    }
  },
  // 左右滑动切换
  swiper:function(e){
    this.setData({
      index:e.detail.current
    })
  },
  // 显示更多
  show:function(){
    let that=this
    if(that.data.ifshow){
      that.data.ifshow=false
      that.setData({
        show: that.data.ifshow,
        showText: '显示更多'
      })
    }else{
      that.data.ifshow=true
      that.setData({
        show:that.data.ifshow,
        showText:'收起'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.data.flag=options.flag
    that.data.search = options.search
    that.data.collecct = options.collect
    wx.removeStorageSync('collecct')
    wx.showLoading({
      title: '正在加载',
    })
    wx.request({
      url: api.guide,
      data: { unid: options.unid, userId: wx.getStorageSync('userId'),type:1 },
      success(res) {
        wx.hideLoading();
        that.setData({
          guide: res.data.data,
          search: options.search == 1 ? options.search:''
        })
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight-100
        });
      }
    })
  },
  // tab切换
  tab:function(e){
    let that=this
    that.setData({
      index:e.currentTarget.dataset.index,
      current_index: e.currentTarget.dataset.index,
    })
  },
  // 打开遮罩层
  mask:function(e){
    let that=this;
    that.data.docUrl=e.currentTarget.dataset.url
    that.data.docName=e.currentTarget.dataset.docname
    that.setData({
      mask:true,
      docName: that.data.docName
    })
  },
  // 关闭遮罩层
  clear:function(){
    let that=this;
    that.setData({
      mask:false
    })
  },
  // 拷贝
  copy:function(){
    let that=this
    wx.setClipboardData({
      data: that.data.docUrl,
      success: function (res) {
        
      }
    })
  },
  c:function(){
    this.setData({
      webUrl:false
    })
  },
  //预览文件
  preview:function(){
    let that=this
    console.log(wx.getSystemInfoSync())
    var arr=String(wx.getSystemInfoSync().system).split(' ')
    console.log(that.data.docUrl)
    if(arr[0]=='iOS'){
      wx.navigateTo({
        url: '/pages/handle/serverDetails/web/web?web=' + that.data.docUrl,
      })
      return false;
    }
    console.log(arr)
    
    wx.downloadFile({
      url: that.data.docUrl,
      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
      },
      fail: function (res) {
        console.log('文件下载失败');
        wx.showToast({
          title: '预览失败',
          icon:'none'
        })
      },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})