// pages/reportDetail/reportDetail.js
import { getReport, getReportUserInfo } from '../../api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reportid: '',
    name: '评估说明', // 默认展示: 评估说明 小结 ReportTotal, 系统:用具体的中文名称
    systemNameList: [
      {
        index: 0,
        viewName: '评估说明',
        reqName: '评估说明',
        toview: 'shuoming',
        response: []
      },
      {
        index: 1,
        viewName: '检测小结',
        reqName: 'ReportTotal',
        toview: 'xiaojie',
        response: []
      },
      {
        index: 2,
        viewName: '循环系统',
        reqName: '循环系统',
        toview: 'xunhuan',
        response: []
      },
      {
        index: 3,
        viewName: '呼吸系统',
        reqName: '呼吸系统',
        toview: 'huxi',
        response: []
      },
      {
        index: 4,
        viewName: '消化系统',
        reqName: '消化系统',
        toview: 'xiaohua',
        response: []
      },
      {
        index: 5,
        viewName: '泌尿系统',
        reqName: '泌尿与生殖系统',
        toview: 'miniao',
        response: []
      },
      {
        index: 6,
        viewName: '血液系统',
        reqName: '血液系统',
        toview: 'xuexie',
        response: []
      },
      {
        index: 7,
        viewName: '内分泌系统',
        reqName: '内分泌系统',
        toview: 'neifenmi',
        response: []
      },
      {
        index: 8,
        viewName: '营养代谢',
        reqName: '营养与代谢',
        toview: 'yinyangdaixie',
        response: []
      },
      {
        index: 9,
        viewName: '神经系统',
        reqName: '神经系统',
        toview: 'shenjin',
        response: []
      },
      {
        index: 10,
        viewName: '免疫系统',
        reqName: '免疫系统',
        toview: 'mianyi',
        response: []
      },
      {
        index: 11,
        viewName: '运动系统',
        reqName: '运动系统',
        toview: 'yundong',
        response: []
      },
      {
        index: 12,
        viewName: '感官系统',
        reqName: '感官系统',
        toview: 'ganguan',
        response: []
      },
      {
        index: 13,
        viewName: '理化因素',
        reqName: '理化因素',
        toview: 'lihuayinshu',
        response: []
      },
      {
        index: 14,
        viewName: '情绪压力',
        reqName: '精神及心理',
        toview: 'qinxuyali',
        response: []
      }
    ],
    systemNameList1Res:[],
    systemNameList2Res:[],
    systemNameList3Res:[],
    systemNameList4Res:[],
    systemNameList5Res:[],
    systemNameList6Res:[],
    systemNameList7Res:[],
    systemNameList8Res:[],
    systemNameList9Res:[],
    systemNameList10Res:[],
    systemNameList11Res:[],
    systemNameList12Res:[],
    systemNameList13Res:[],
    systemNameList14Res:[],
    leftNavActive: 0, // 0 小结 1 系统
    summaryList: [],
    systemList: [],
    realName: '',
    addTime: '',
    toView: 'shuoming',
    scrollIntoViewIndex: 0
  },

  onUnload() {
    this.setData({
      scrollIntoViewIndex: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      reportId: options.reportid,
      leftNavActive: 0,
      name: '评估说明'
    })
    this.getAllReportInfo(options.reportid)
    //this.getReportUserInfoFn(options.reportid)
  },

  bindscroll () {
    this.returnTop()
  },

  returnTop () {
    const query = wx.createSelectorQuery()
    const that = this
    query.select("#shuoming").boundingClientRect()
    query.select("#xiaojie").boundingClientRect()
    query.select("#xunhuan").boundingClientRect()
    query.select("#huxi").boundingClientRect()
    query.select("#xiaohua").boundingClientRect()
    query.select("#miniao").boundingClientRect()
    query.select("#xuexie").boundingClientRect()
    query.select("#neifenmi").boundingClientRect()
    query.select("#yinyangdaixie").boundingClientRect()
    query.select("#shenjin").boundingClientRect()
    query.select("#mianyi").boundingClientRect()
    query.select("#yundong").boundingClientRect()
    query.select("#ganguan").boundingClientRect()
    query.select("#lihuayinshu").boundingClientRect()
    query.select("#qinxuyali").boundingClientRect()
    query.exec(function(res){
      that.setLeftNavActive(res)
    })
  },

  setLeftNavActive (TopArr) {
    let res = []
    TopArr.map((item, index) => {
      res.push({
        index: index,
        id: item.id,
        top: Math.abs(item.top)
      })
    })
    const fItem = res.find(item => {
      return item.top < 200
    })
    if (fItem && this.data.scrollIntoViewIndex != fItem.index) {
      this.setData({
        scrollIntoViewIndex: fItem.index,
        leftNavActive: fItem.index
      })
    }

  },

  // 获取所有节点的返回信息
  async getAllReportInfo (reportid) {
    const that = this
    // wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中...',
    })
    await this.getReportUserInfoFn(reportid)
    await this.getReportFn(this.data.systemNameList[1].reqName).then(response => {
      that.setData({
        systemNameList1Res: response || []
      })
    })
    await this.getReportFn(this.data.systemNameList[2].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList2Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[3].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList3Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[4].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList4Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[5].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList5Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[6].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList6Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[7].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList7Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[8].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList8Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[9].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList9Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[10].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList10Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[11].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList11Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[12].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList12Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[13].reqName).then(response => {
      const handleRespose = that.handleRespose(response)
      that.setData({
        systemNameList13Res: handleRespose
      })
    })
    await this.getReportFn(this.data.systemNameList[14].reqName).then(response => {
      that.setData({
        systemNameList14Res: response
      })
    })
    // wx.hideNavigationBarLoading()
    wx.hideLoading()
  },

  // 获取当前报告人的信息
  getReportUserInfoFn (reportid) {
    var that = this
    return new Promise((reslove,reject) => {
      getReportUserInfo({
        reportId: reportid
      }).then(res => {
        wx.hideLoading()
        that.setData({
          addTime: res.data.addTime,
          realName: res.data.realName
        })
        reslove(res)
      })
    })
  },

  // 获取具体的报告信息
  getReportFn (name, index = 0) {
    var that = this
    var data = {
      reportId: this.data.reportId,
      name: name
    }
    return new Promise((resolve, reject) => {
      getReport(data).then(res => {
        /* if (name == 'ReportTotal' || name == '精神及心理') {
          that.setData({
            summaryList: res.data || []
          })
        } else {
          that.handleRespose(res.data, index)
        } */
        resolve(res.data)
      }).catch(e => {
        reject(e)
        wx.showModal({
          content: e,
          confirmColor: '#333',
          showCancel: false
        })
      })
    })
  },

  // 处理系统返回的数据
  handleRespose (response, index) {
    var outKey = Object.keys(response)
    var result = []
    for (var index = 0; index < outKey.length; index++) {
      const element = outKey[index];
      const innetList = Object.keys(response[element])
      var tempArr = []
      for (var i = 0; i < innetList.length; i++) {
        const innerElement = innetList[i];
        tempArr.push({
          fatherName: element,
          name: innerElement,
          value: response[element][innerElement]
        })
      }
      result.push(
        {
          name: element,
          childNode: tempArr
        }
      )
    }
    var handleResult = [...result]
    for (var index = 0; index < handleResult.length; index++) {
      const element = handleResult[index];
      hanldSort(element.childNode)
    }
    function hanldSort (childNode) {
      childNode.sort(function(a,b){
        return b.value - a.value
      })
    }
    return handleResult
  },

  // 排序函数
  handleSortFn (result) {
    var handleResult = [...result]
    for (var index = 0; index < handleResult.length; index++) {
      const element = handleResult[index];
      hanldSort(element.childNode)
    }
    function hanldSort (childNode) {
      childNode.sort(function(a,b){
        return b.value - a.value
      })
    }
    this.setData({
      systemList: handleResult
    })
  },

  /**
   * 用户点击左侧系统分类
   */
  handleSwitchNav(e) {
    var navIndex = e.currentTarget.dataset.index
    var toview = e.currentTarget.dataset.toview
    this.setData({
      leftNavActive: navIndex,
      toView: toview
    })
  }
})