const request = require('../../utils/request.js');
import WxCountUp from '../../utils/WxCountUp'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标，消息中心   1表示显示    0表示不显示
      showBack: 0, //返回
      title: '我的主页', //导航栏 中间的标题
      // 此页面 页面内容距最顶部的距离
      height: app.globalData.height * 2 + 20,
    },
    // 此页面 页面内容距最顶部的距离
    active: 0,
    icon: {
      normal: '/pages/images/home.png',
      active: '/pages/images/home_fill.png',
      bankNormal: '/pages/images/duijie.png',
      bankActive: '/pages/images/duijie_fill.png',
      marketNormal: '/pages/images/recharge.png',
      marketActive: '/pages/images/recharge_fill.png',
      policyNormal: '/pages/images/news_hot.png',
      policyActive: '/pages/images/news_hot_fill.png',
      myNormal: '/pages/images/people.png',
      myActive: '/pages/images/people_fill.png'
    },
    guidanceUnitList:[
      {
        image:"../../images/part1.jpg",
        name:""
      },
      {
        image:"../../images/part2.jpg",
        name:""
      }
    ],
    // 成功对接
    mainOneInfo: {
      "registerCompany": 0,
      "solutionFinancing": 0,
      "demandCount": 0,
      "demandMoney": 0,
      "financialProduct": 0,
      "financialInstitution": 0,
      "helpCompany": 0,
      "finalsummoney":0,
    },
    // 热门产品
    productList: [],
    // 政策解读
    policyList: [
      {title:'抓好六稳工作 落实六保任务 火炬中心 开展吸纳高校毕业生就业创业工作'},
        {title:'抓好六稳工作 落实六保任务 火炬中心 开展吸纳高校毕业生就业创业工作'}
    ],
    // 轮播
    backgrounds: [{"pic": "/images/banner1.png"},{"pic": "/images/banner1.png"},{"pic": "/images/banner1.png"},{"pic": "/images/banner1.png"}],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500
  },
  goResult(){
    wx.navigateTo({
      url: '/pages/docking/docking'
    })
  },
  // 我要融资
  tongjiBtn: function () {
    wx.navigateTo({
      url: '/pages/bank/index'
    })
  },
  // 政策解读页面列表跳转
  policyListTap: function () {
    wx.navigateTo({
      url: '/pages/policy/index'
    })
  },
  // 政策解读页面详情跳转
  // 判断id
  policyListDetailTap: function (e) {
    wx.navigateTo({
      url: '/pages/policyNews/policyNews' // 希望跳转过去的页面
    })
  },
  // 热门产品跳转
  toshow(e) {
    let claimid = e.currentTarget.dataset.claimid;
    let img = e.currentTarget.dataset.img;
    console.log(e);
    // console.log(JSON.stringify(e.currentTarget.dataset))
    wx.navigateTo({
      url: '/pages/marketLoanDetail/index?claimid=' + claimid + '&img=' + img,
    })
  },
  // 热门产品更多
  clickMore(){},
  // 切换菜单
  onChange(event) {
    this.setData({
      active: event.detail
    });
    if (event.detail == 0) {
      wx.redirectTo({
        url: '/pages/home/index'
      })
    } else if (event.detail == 1) {
      wx.redirectTo({
        url: '/pages/bank/index'
      })
    } else if (event.detail == 2) {
      wx.redirectTo({
        url: '/pages/market/index'
      })
    } else if (event.detail == 3) {
      wx.redirectTo({
        url: '/pages/policy/index'
      })
    } else if (event.detail == 4) {
      wx.redirectTo({
        url: '/pages/my/index'
      })
    }
  },
  topNews: function () {
    wx.request({
      method: "POST",
      url: app.globalData.publicUrl + 'api/financial/financeclaims/top',
      success: (e) => {
        if (e.data.errcode == 0) {
          let arr = [];
          e.data.data.forEach((item) => {
            let nikeName  = '';
            if(item.name.length > 3){
              nikeName =   item.name.substring(0, 3) + '....'
            }else{
              nikeName = item.name
            }
            arr.push({
              _id: item._id,
              pic: app.globalData.imageUrl + item.innew.logo,
              mongey_min_rate: item.mongey_min_rate,
              mongey_max_rate: item.mongey_max_rate,
              claims_min_money: item.claims_min_money,
              claims_max_money: item.claims_max_money,
              claims_min_term:item.claims_min_term,
              claims_max_term: item.claims_max_term,
              introduction: item.innew.introduction,
              // title: item.name.substring(0, 5) + '...'
              title: nikeName
            });
          });
          this.setData({
            productList: arr
          })
        }
      }
    })
  },
  getDate(val, formatStr) {
    if (val) {
      return this.format(new Date(Number(val)), formatStr);
    }
    return '';
  },
  format(date, formatStr) {
    formatStr = formatStr.replace(/yyyy|YYYY/, date.getFullYear());
    formatStr = formatStr.replace(/MM/, (date.getMonth() + 1) > 9 ? (date.getMonth() + 1).toString() : '0' + (date.getMonth() + 1));
    formatStr = formatStr.replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : '0' + date.getDate());
    return formatStr;
  },
  loadPolicyList: function () {
    request.query({
      url: 'api/financial/tPolicyInterpretation',
      data: {
        skip: 0,
        limit: 2
      }
    }).then((res) => {
      let temp = res.data.data.map((item) => {
        if (item.image) {
          item.image = request.imageUrl + item.image
        }
        if (item.create_time) {
          item.create_time = this.getDate(item.create_time, 'yyyy.MM.dd')
        }
        return item;
      });
      this.setData({
        policyList: temp
      });
    }).catch((err) => {
      console.log(err);
    });
  },
  loadGuidanceUnitList: function () {
    request.query({
      url: 'api/financial/tGuidanceUnit',
      data: {
        skip: 0,
        limit: 3
      }
    }).then((res) => {
      let temp = res.data.data.map((item) => {
        if (item.image) {
          item.image = request.imageUrl + item.image
        }
        return item;
      });
      this.setData({
        guidanceUnitList: temp
      });
    }).catch((err) => {
      console.log(err);
    });
  },
  loadStatistics: function () {
    var that = this;
    request.query({
      url: 'api/financial/tGovernmentStatistics/pageSelect',
    }).then((res) => {
      this.setData({
        mainOneInfo: res.data.data
      });
      that.countUp = new WxCountUp('mainOneInfo.registerCompany', res.data.data.zcqynum, {}, that);
      that.countUp.start();
      that.countUp1 = new WxCountUp('mainOneInfo.financialInstitution', res.data.data.finalsummoney, {}, that);
      that.countUp1.start();
      that.countUp2 = new WxCountUp('mainOneInfo.demandCount', res.data.data.demandCount, {}, that);
      that.countUp2.start();
      that.countUp3 = new WxCountUp('mainOneInfo.finalsummoney', res.data.data.finalsummoney, {}, that);
      that.countUp3.start();
    }).catch((err) => {
      console.log(err);
    });
  },
  //轮播图
  getbanner() {
    wx.request({
      method: "GET",
      url: app.globalData.publicUrl + 'api/financial/banner',
      data: {
        skip: 0,
        limit: 3,
      },
      success: (res) => {
        if (res.data.errcode == 0) {
          this.setData({
            background: res.data.data
          })
        }
      }
    })
  },
  onLoad: function (options) {
    // this.topNews();
    // this.loadPolicyList();
    // this.loadGuidanceUnitList();
    // this.loadStatistics();
    // this.getbanner();
  }
})
