// pages/home/home.js
const {
  getHomeStatistics,
  getCapital
} = require("../../api/api")
const app = getApp()
Page({
  data: {
    titleList:[],
    userType:'',
    capitalList:[],//资方列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      // userType: wx.getStorageSync('userType') == "TRADER" ? 0 : 1, //0:代理商 1:平台
      userType: wx.getStorageSync('userType'),
      permission: wx.getStorageSync('permission')
    })
    var titleList = [{
        title: "信息采集",
        perm:'HYXCX_XXCJ_T',
        titleColumns: [{
          title: "客户管理",
          titleImages: "/img/plan-4.png",
          titleType: 1,
          redpointNum: 0,
          perm:'HYXCX_XXDJ_T'
        }, {
          title: "信息授权",
          titleImages: "/img/plan-4.png",
          titleType: 2,
          redpointNum: 0,
          perm:'HYXCX_XXSQ_T'
        }, {
          title: "开二类卡",
          titleImages: "/img/plan-4.png",
          titleType: 4,
          redpointNum: 0,
          perm:'HYXCX_KELK_T'
        },  {
          title: "农户合同",
          titleImages: "/img/plan-4.png",
          titleType: 5,
          redpointNum: 0,
          perm:'HYXCX_NHHT_T'
        }]
      },
      {
        title: "勘测设计",
        perm:'HYXCX_KCSJ_T',
        titleColumns: [{
          title: "勘测设计",
          titleImages: "/img/plan-4.png",
          titleType: 3,
          redpointNum: 0,
          perm:'HYXCX_DZCK_T'
        }, {
          title: "设计审核",
          titleImages: "/img/plan-2.png",
          titleType: 6,
          redpointNum: 0,
          perm:'HYXCX_SJSH_T'
        },{
          title: "电站档案",
          titleImages: "/img/home_dzda.png",
          titleType: 17,
          redpointNum: 0,
          perm:'HYXCX_DZDA_T'
        },{
            title: "档案审核",
            titleImages: "/img/home_dash.png",
            titleType: 18,
            redpointNum: 0,
            perm:'HYXCX_DASH_T'
          }
        ]
      },
      {
        title: "安装阶段",
        perm:'HYXCX_AZJD_T',
        titleColumns: [
          // {
          //   title: "物料下单",
          //   titleImages: "/img/fix-1.png",
          //   titleType: 7,
          //   redpointNum: 0,
          //   perm:'HYXCX_WLXD_T'
          // },
          {
            title: "电站安装",
            titleImages: "/img/fix-2.png",
            titleType: 8,
            redpointNum: 0,
            perm:'HYXCX_DZAZ_T'
          },
          {
            title: "接入逆变器",
            titleImages: "/img/jrnbq.png",
            titleType: 20,
            redpointNum: 0,
            perm:'HYXCX_JRNBQ_T'
          },
          {
            title: "安装验收",
            titleImages: "/img/interconnection-2.png",
            titleType: 9,
            redpointNum: 0,
            perm:'HYXCX_AZYS_T'
          }
        ]
      }, {
        title: "并网阶段",
        perm:'HYXCX_BWJD_T',
        titleColumns: [
        {
            title: "电站并网",
            titleImages: "/img/fix-4.png",
            titleType: 16,
            redpointNum: 0,
            perm:'HYXCX_DZBW_T'
          },
          {
            title: "国网授权",
            titleImages: "/img/home_gwsq.png",
            titleType: 19,
            redpointNum: 0,
            perm:'HYXCX_GWSQ_T'
          },
          // {
          //   title: "合同签署",
          //   titleImages: "/img/interconnection-3.png",
          //   titleType: 11,
          //   redpointNum: 0,
          //   perm:'HYXCX_HTQS_T'
          // }, 
          {
            title: "并网验收",
            titleImages: "/img/fix-3.png",
            titleType: 12,
            redpointNum: 0,
            perm:'HYXCX_BWYS_T'
          },
          // {
          //   title: "项目存档",
          //   titleImages: "/img/over-1.png",
          //   titleType: 13,
          //   redpointNum: 0,
          //   perm:'HYXCX_XMCD_T'
          // },
          {
            title: "项目查询",
            titleImages: "/img/over-2.png",
            titleType: 14,
            redpointNum: -1,
            perm:'HYXCX_XMCX_T'
          }, 
          // {
          //   title: "电站整改",
          //   titleImages: "/img/plan-3.png",
          //   titleType: 15,
          //   redpointNum: 0,
          //   perm:'HYXCX_DZZG_T'
          // },
        ]
      }, 
      // {
      //   title: "其它",
      //   perm:'HYXCX_BWJD_T',
      //   titleColumns: [{
      //       title: "待办任务",
      //       titleImages: "/img/fix-4.png",
      //       titleType: 16,
      //       redpointNum: 0,
      //       perm:'HYXCX_DZBW_T'
      //     },          
      //     {
      //       title: "消息通知",
      //       titleImages: "/img/fix-3.png",
      //       titleType: 12,
      //       redpointNum: 0,
      //       perm:'HYXCX_BWYS_T'
      //     },          
      //     {
      //       title: "竣工验收",
      //       titleImages: "/img/over-2.png",
      //       titleType: 14,
      //       redpointNum: -1,
      //       perm:'HYXCX_XMCX_T'
      //     }, {
      //       title: "电站整改",
      //       titleImages: "/img/plan-3.png",
      //       titleType: 15,
      //       redpointNum: 0,
      //       perm:'HYXCX_DZZG_T'
      //     }
      //   ]
      // }
    ]
    this.setData({
      titleList: titleList
    })
  },  
  onShow(){
    this.setData({
      traderName: wx.getStorageSync('traderName'),
      capitalName:wx.getStorageSync('capitalName'),
      capitalId:wx.getStorageSync('capitalId')
    })
    if(this.data.userType == 'PLATFORM_EMP'){
      this.getHomeStatistics()
    }else{
      this.getCapitalList(()=>{
        this.getHomeStatistics()
      })
    }
    //自定义tab图标选中设置
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected:0, 
      })
    }
  },
  /**
     * 
     * 获取投资方
     * 
     * @param {*} e 
     */
    getCapitalList(callback) {
      callback = callback || function(){};
      getCapital({
        traderId: wx.getStorageSync('traderId'),      
      }).then((res) => {
        if(res.code == 200){
          this.setData({
            capitalList: res.data
          })
          //为空默认显示第一个资方
          if(!wx.getStorageSync('capitalId')){
            if(res.data){
              wx.setStorageSync('capitalId', res.data[0].id)
              wx.setStorageSync('capitalName', res.data[0].name)
              this.setData({
                capitalName:res.data[0].name,
                capitalId:res.data[0].id
              });
            }
          }
        }
        callback();
      })
    },

  /**
   * 
   * 下拉刷新
   * 
   * @param {*} e 
   */
  onPullDownRefresh(e) {
    this.getHomeStatistics()
  },

  /**
   * 
   * 获取首页统计
   * 
   * @param {*} e 
   */
  getHomeStatistics(e) {
    getHomeStatistics({
      userId: wx.getStorageSync('userId'),
      traderId: wx.getStorageSync('userType')=='PLATFORM_EMP'?wx.getStorageSync('traderId'):'',
      partner: wx.getStorageSync('capitalId'),
    }).then((res) => {
      if (res.code == 200) {
        wx.stopPullDownRefresh()
        var data = JSON.parse(res.data)

        this.setData({
          //信息采集
          [`titleList[0].titleColumns[0].redpointNum`]: data.columns.xxdjTotal < 100 ? data.columns.xxdjTotal : '99+', //客户管理
          [`titleList[0].titleColumns[1].redpointNum`]: data.columns.xxsqTotal < 100 ? data.columns.xxsqTotal : '99+', //信息授权
          [`titleList[0].titleColumns[2].redpointNum`]: data.columns.erlkTotal < 100 ? data.columns.erlkTotal : '99+', //二类卡
          [`titleList[0].titleColumns[3].redpointNum`]: data.columns.nhhtTotal < 100 ? data.columns.nhhtTotal : '99+', //农户合同

          //勘测设计
          [`titleList[1].titleColumns[0].redpointNum`]: data.columns.dzckTotal < 100 ? data.columns.dzckTotal : '99+', //电站初勘
          [`titleList[1].titleColumns[1].redpointNum`]: data.columns.sjshTotal < 100 ? data.columns.sjshTotal : '99+', //设计审核
          [`titleList[1].titleColumns[2].redpointNum`]: data.columns.dzdaTotal < 100 ? data.columns.dzdaTotal : '99+', //电站档案
          [`titleList[1].titleColumns[3].redpointNum`]: data.columns.dashTotal < 100 ? data.columns.dashTotal : '99+', //档案审核

          //安装阶段
          // [`titleList[2].titleColumns[0].redpointNum`]: data.columns.wlxdTotal < 100 ? data.columns.wlxdTotal : '99+',
          [`titleList[2].titleColumns[0].redpointNum`]: data.columns.dzazTotal < 100 ? data.columns.dzazTotal : '99+', //电站安装
          [`titleList[2].titleColumns[1].redpointNum`]: data.columns.nbqNum < 100 ? data.columns.nbqNum : '99+', //接入逆变器
          [`titleList[2].titleColumns[2].redpointNum`]: data.columns.azysTotal < 100 ? data.columns.azysTotal : '99+', //安装验收

          //并网阶段
          [`titleList[3].titleColumns[0].redpointNum`]: data.columns.dzbwTotal < 100 ? data.columns.dzbwTotal : '99+', //电站并网
          [`titleList[3].titleColumns[1].redpointNum`]: data.columns.ifsgsNum < 100 ? data.columns.ifsgsNum : '99+', //国网授权
          [`titleList[3].titleColumns[2].redpointNum`]: data.columns.bwysTotal < 100 ? data.columns.bwysTotal : '99+', //并网验收
          [`titleList[3].titleColumns[4].redpointNum`]: data.columns.dzzgTotal < 100 ? data.columns.dzzgTotal : '99+', //项目查询

        })

      }
    })
  },



})