/*
 * @Descripttion: 签署管理页面
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: chenminggui
 * @lastAuthor: chenminggui
 * @Date: 2022-02-28 15:45:51
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-01 09:30:14
 */
import { fhPatientSignList } from '../../api/sign/index'
import { tabsArr, statusObj } from './utils'
Page({
    data: {
        currentTab: 0,
        tabArray: tabsArr,
        listArr: [],
        statusObj: statusObj,
        curTabName: 'communication',
        pageNumber: 1,
        pageCount: 1,
        // 用户是否登录状态
        anflag:true
    },
    onLoad() { },
    onShow() {
        // 获取用户是否登录状态
        const ifAuth = wx.getStorageSync('anflag')
        this.setData({
            anflag:ifAuth,
            listArr: []
        })
        this.featureGetSignData(this.data.curTabName)
    },

    //上拉加载更多
    handleScrollTolower() {
        if (this.data.pageNumber < this.data.pageCount) {
            this.featureGetSignData(this.data.curTabName, this.data.pageNumber + 1)
        }
    },

    // 去详情
    handleGoDetail(event) {
        console.log(event)
        const id = event.currentTarget.dataset.id
        const remoteVisitRecordId = event.currentTarget.dataset.remotevisitrecordid
        wx.navigateTo({
            url: `/pages/sign-detail/index?id=${id}&remoteVisitRecordId=${remoteVisitRecordId}`
        })
    },

    // 用户登录
    handleUserLoad() {
         if (this.data.anflag) {
            wx.$routerTo('/pages/authorization/authorization')
            return
        }
    },

    /**
     * 切换tab栏的监听事件
     * @event 切换tab参数
     */
    handleTabChange(event) {
        const arr = this.data.listArr
        try {
            const name = event.detail.name
            this.setData({
                listArr: [],
                curTabName: name
            })
            this.featureGetSignData(name)
        } catch (e) {
            this.setData({
                listArr: arr
            })
        }
    },

    /**
     * 获取列表的数据
     * @state 状态参数
     */
    async featureGetSignData(state, curPage = 1) {
        this.handleLoading()
        const params = {
            memberId: wx.getStorageSync('memberId'),
            state: state,
            pageSize: 10,
            pageNumber: curPage
        }
        const { results } = await fhPatientSignList(params)
        const listArr = this.data.listArr.concat(results.results)
        this.setData({
            listArr: listArr,
            pageNumber: results.curPage,
            pageCount: results.pageCount
        })
        wx.stopPullDownRefresh()
        this.handleCloseLoad()
    },

    /**
     * Loading
     */
    handleLoading() {
        wx.showLoading({
            title: '加载中...'
        })
    },

    handleCloseLoad() {
        wx.hideLoading()
    },

    /**
     * 整理数组结构
     * @dataArr 需要处理的数组
     * @return 返回处理好的数组
     */
    conputedArr(dataArr) {
        const newArr = []

        dataArr.forEach((item, index) => {
            const startDate = item.startTime.split(' ')
            const targetData = newArr.find(target => {
                return target.startDate === startDate[0]
            })
            if (targetData) {
                targetData.dataList.push(item)
            } else {
                const listObj = {
                    startDate: startDate[0],
                    dataList: [item]
                }
                newArr.push(listObj)
            }
        })

        return newArr
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            listArr: []
        })
        this.featureGetSignData(this.data.curTabName)
    },
    // 上拉加载更多
    onReachBottom() {
        if (this.data.pageNumber < this.data.pageCount) {
            this.featureGetSignData(this.data.curTabName, this.data.pageNumber + 1)
        }
    }
})
