text: var Api = require('../../utils/api.js');
  var util = require('../../utils/util.js');
  var WxParse = require('../../wxParse/wxParse.js');
  var wxApi = require('../../utils/wxApi.js')
  var wxRequest = require('../../utils/wxRequest.js')
  import config from '../../utils/config.js'
  var pageCount = config.getPageCount;
  Page({
    data: {  
      hidden: false,
      pageMain:true,
      postsList: [],
      detail: {},
      postsShowSwiperList:[],
      isLastPage:false,    
      page: 1,
      vertical:true,
      search: '',
      categories: 0,
      showerror:"none",
      showCategoryName:"",
      categoryName:"",
      showallDisplay:"block", 
      displayHeader:"none",
      displaySwiper: "none",
      floatDisplay: "none",
      displayfirstSwiper:"none",
      topNav: [],
    },
    formSubmit: function (e) {
      var url = '../list/list'
      var key ='';
      if (e.currentTarget.id =="search-input")
      {
          key = e.detail.value;
      }
      else{
          key = e.detail.value.input;
      }
      if (key != '') {
        url = url + '?search=' +key;
        wx.navigateTo({
          url: url
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '请输入内容',
          showCancel: false,
        });
      }
    },
    onShareAppMessage: function () {
      return {
        title: '“' + config.getWebsiteName+'”猫小镇-带你看到更大的世界',
        path: 'pages/index/index',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    },
    onPullDownRefresh: function () {
      var self = this;
      self.setData({
        showerror: "none",
        showallDisplay:"none",
        displaySwiper:"none",
        floatDisplay:"none",
        isLastPage:false,
        page:0,
        postsShowSwiperList:[]
      });
      this.fetchTopFivePosts(); 
    },
    onReachBottom: function () {  
        var self = this;
        if (!self.data.isLastPage) {
            self.setData({
                page: self.data.page + 1
            });
            console.log('当前页' + self.data.page);
            this.fetchPostsData(self.data);
        }else {
            console.log('最后一页');
        }
    },
    onLoad: function (options) {
      var self = this; 
      this.fetchTopFivePosts();
      self.setData({
          topNav: config.getIndexNav,
      });
    },
    onShow: function (options){
        wx.setStorageSync('openLinkCount', 0);
    },  
    navigateToMiniProgram() {
      wx.navigateToMiniProgram({
        appId: 'wx637f964549aa452a',
        path: 'pages/index/index',
        extraData: {
          from: 'xxxxx'
        },
        envVersion: 'release',
        success(res) {
          // 打开其他小程序成功同步触发
          wx.showToast({
            title: '跳转成功'
          })
        }
      })
    },
    fetchTopFivePosts: function () {
      var self = this;
      //取置顶的文章
      var getPostsRequest = wxRequest.getRequest(Api.getSwiperPosts());
      getPostsRequest.then(response => {
          if (response.data.status =='200' && response.data.posts.length > 0) {
                  self.setData({
                      postsShowSwiperList: response.data.posts,
                      postsShowSwiperList: self.data.postsShowSwiperList.concat(response.data.posts.map(function (item) {
                          //item.firstImage = Api.getContentFirstImage(item.content.rendered);
                          if (item.post_medium_image_300 == null || item.post_medium_image_300 == '') {
                              if (item.content_first_image != null && item.content_first_image != '') {
                                  item.post_medium_image_300 = item.content_first_image;
                              }
                              else {
                                  item.post_medium_image_300 = "../../images/logo700.png";
                              }
  
                          }
                          return item;
                      })),
                      showallDisplay: "block",
                      displaySwiper: "block"
                  });
                  
              }
              else {
                  self.setData({
                      displaySwiper: "none",
                      displayHeader: "block",
                      showallDisplay: "block",
                  });
              }
      })
          .then(response=>{
              self.fetchPostsData(self.data);
          })
          .catch(function (response){
              console.log(response); 
              self.setData({
                  showerror: "block",
                  floatDisplay: "none"
              });
  
          })
          .finally(function () {
          
      });            
     
    },
  
    //获取文章列表数据
    fetchPostsData: function (data) {
      var self = this;    
      if (!data) data = {
        hidden: true
      };
      if (!data.page) data.page = 1;
      if (!data.categories) data.categories = 0;
      if (!data.search) data.search = '';
      if (data.page === 1) {
  			self.setData({
          
  			});
        self.setData({
          postsList: []
        });
      }
      var getPostsRequest = wxRequest.getRequest(Api.getPosts(data));
      getPostsRequest
          .then(response => {
              if (response.statusCode === 200) {
  
                  if (response.data.length < pageCount) {
                      self.setData({
                          isLastPage: true
                      });
                  }
                  self.setData({
                      floatDisplay: "block",
                      postsList: self.data.postsList.concat(response.data.map(function (item) {
                        var strdate = item.date
                        var excerpt = item.excerpt.rendered
                        item.excerpt.rendered = excerpt.replace(/\[&hellip;\]/ig, '......');
                        item.excerpt.rendered = item.excerpt.rendered.replace(/&#8221;/g, '"');
                        item.excerpt.rendered = item.excerpt.rendered.substring(0,32)+"......"
                          if (item.category_name != null) {
  
                              item.categoryImage = "../../images/category.png";
                          }
                          else {
                              item.categoryImage = "";
                          }
                          if (item.post_thumbnail_image == null || item.post_thumbnail_image == '') {
                              item.post_thumbnail_image = "../../images/logo700.png";
                          }
                          item.date = util.cutstr(strdate, 10, 1);
                          return item;
                      })),
  
                  });
                  setTimeout(function () {
                      wx.hideLoading();
                      self.setData({
                        hidden: true,
                        pageMain:false
                      })
                  }, 900);
              }
              else {
                  if (response.data.code == "rest_post_invalid_page_number") {
                      self.setData({
                          isLastPage: true
                      });
                      wx.showToast({
                          title: '没有更多内容',
                          mask: false,
                          duration: 1500
                      });
                  }
                  else {
                      wx.showToast({
                          title: response.data.message,
                          duration: 1500
                      })
                  }
              }
  
  
          })
          .catch(function (response)
          {
              if (data.page == 1) {
  
                  self.setData({
                      showerror: "block",
                      floatDisplay: "none"
                  });
  
              }
              else {
                  wx.showModal({
                      title: '加载失败',
                      content: '加载数据失败,请重试.',
                      showCancel: false,
                  });
                  self.setData({
                      page: data.page - 1
                  });
              }
  
          })
          .finally(function (response) {
              wx.hideLoading();
              wx.stopPullDownRefresh();
          });
    },
    //加载分页
    loadMore: function (e) {
      
      var self = this;
      if (!self.data.isLastPage)
      {
        self.setData({
          page: self.data.page + 1
        });
        //console.log('当前页' + self.data.page);
        this.fetchPostsData(self.data);
      }
      else
      {
        wx.showToast({
          title: '没有更多内容',
          mask: false,
          duration: 1000
        });
      }
    },
    redictDetail: function (a) {
      var t = "../detail/detail?id=" + a.currentTarget.id;
      wx.navigateTo({
        url: t
      });
    },
    //首页图标跳转
    onNavRedirect:function(e){      
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '' : e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;
        var extraData = e.currentTarget.dataset.extraData == null ? '' : e.currentTarget.dataset.extraData;
        if (redicttype == 'apppage') {//跳转到小程序内部页面         
            wx.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'webpage')//跳转到web-view内嵌的页面
        {
            url = '../webpage/webpage?url=' + url;
            wx.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'miniapp')//跳转到其他app
        {
            wx.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                extraData: extraData,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    console.log(res);
                }
            })
        }
        
    },
    // 跳转至查看小程序列表页面或文章详情页
    redictAppDetail: function (e) {
        // console.log('查看文章');
        var id = e.currentTarget.id;
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '':e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;
        
        if (redicttype == 'detailpage')//跳转到内容页
        {
            url = '../detail/detail?id=' + id;
            wx.navigateTo({
                url: url
            })
        }
        if (redicttype == 'apppage') {//跳转到小程序内部页面         
            wx.navigateTo({
                url: url
            })
        }
        else if (redicttype == 'webpage')//跳转到web-view内嵌的页面
        {
            url = '../webpage/webpage?url=' + url;
            wx.navigateTo({
                url: url
            })          
        }
        else if (redicttype == 'miniapp')//跳转到其他app
        {
            wx.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    //console.log(res);
                }
            })
        }
    },
    //返回首页
    redictHome: function (e) {
      //console.log('查看某类别下的文章');  
      var id = e.currentTarget.dataset.id,
        url = '/pages/index/index';
      wx.switchTab({
        url: url
      });
    }
  })
  