import Notify from '@vant/weapp/notify/notify';

import {
    selectVisitorRecordByIntervieweeIdRq,
    selectVisitorRecordByUserIdRq,
    updateVisitorPersonStatusRq
} from "../../../../api/meeting/visitorIinvitation.js"

import {
    selfFormatTimeYMDHM
} from "../../../../utils/util.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabTitle: '预约记录',
        userDetail: {},
        changeData: false,
        // 预约
        reservationPageNum: 1,
        reservationPageSize: 10,
        reservationDataList: [],
        reservationIsAll: false,
        // 审核
        verifyPageNum: 1,
        verifyPageSize: 10,
        verifyDataList: [],
        verifyIsAll: false,
        //
        dialogShow: false,
        dialogId: null,
        dialogContent: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        let userDetail = wx.getStorageSync('user')
        _this.setData({
            ...options,
            userDetail,
        })

        // 获取-预约记录 数据
        _this.getReservationData();
        // 获取-预约审核 数据
        _this.getVerifyData();
    },

    // 获取数据
    getDataList() {
        let _this = this;
        let tabTitle = _this.data.tabTitle
        if (tabTitle == '预约记录') {
            _this.getReservationData()
        } else if (tabTitle == '预约审核') {
            _this.getVerifyData()
        } else {
            wx.showToast({
                title: `tab 切换错误(${tabTitle})`,
                icon: 'none',
            });
            return;
        }
    },

    // 获取-预约记录 数据
    getReservationData() {
        let _this = this;
        // 是否全部加载完成
        if (_this.data.reservationIsAll) {
            return;
        }
        selectVisitorRecordByIntervieweeIdRq({
            pageNum: _this.data.reservationPageNum,
            pageSize: _this.data.reservationPageSize,
            intervieweeId: _this.data.userDetail.id
        }).then(res => {
            console.log('getReservationData', res);
            let queryDataList = res.data.records;
            if (queryDataList && queryDataList.length > 0) {
                let reservationDataList = _this.data.reservationDataList.concat(_this.formartData(queryDataList))
                _this.setData({
                    reservationDataList,
                    reservationPageNum: _this.data.reservationPageNum + 1
                })
            } else {
                _this.setData({
                    reservationIsAll: true
                })
            }
        })
    },

    // 获取-预约审核 数据
    getVerifyData() {
        let _this = this;
        // 是否全部加载完成
        if (_this.data.verifyIsAll) {
            return;
        }
        selectVisitorRecordByUserIdRq({
            pageNum: _this.data.verifyPageNum,
            pageSize: _this.data.verifyPageSize,
            userId: _this.data.userDetail.id
        }).then(res => {
            console.log('getVerifyData', res);
            let queryDataList = res.data.records;
            if (queryDataList && queryDataList.length > 0) {
                let verifyDataList = _this.data.verifyDataList.concat(_this.formartData(queryDataList))
                _this.setData({
                    verifyDataList,
                    verifyPageNum: _this.data.verifyPageNum + 1
                })
            } else {
                _this.setData({
                    verifyIsAll: true
                })
            }
        })
    },

    // 格式化数据
    formartData(list) {
        return list.map(item => {
            let color = "#3794FF"
            let verifyShow = false;
            if (item.status == 0) { // 待审核
                color = "#3794FF"
                verifyShow = true;
            } else if (item.status == 1) { // 审核通过
                color = "#62c855"
            } else if (item.status == 2) { // 审核驳回
                color = "red"
            }
            item.fontColor = color;
            item.verifyShow = verifyShow;
            item.visitTimeFormat = selfFormatTimeYMDHM(item.visitTime);
            return item;
        })
    },

    // tab 切换
    tabClickSwitch(event) {
        this.setData({
            tabTitle: event.detail.title,
        })
    },

    // 显示-弹出框
    showDialog(e) {
        console.log('showDialog', e);
        let _this = this
        _this.setData({
            dialogShow: true,
            dialogId: e.currentTarget.dataset.id,
            dialogContent: null
        })
    },

    // 弹出框-审核
    dialogVerify(e) {
        console.log('dialogSuccess', e);
        let status = e.currentTarget.dataset.status;
        let _this = this
        // 驳回 需要输入描述信息
        if (status == 2 && !_this.data.dialogContent) {
            Notify({
                type: 'danger',
                message: '请输入描述信息!'
            });
            return
        }
        updateVisitorPersonStatusRq({
            id: _this.data.dialogId,
            status,
            rejectContent: _this.data.dialogContent,
        }).then(res => {
            console.log('updateVisitorPersonStatusRq', res);
            if (res.code != 0) {
                Notify({
                    type: 'danger',
                    message: res.msg
                });
            }
            _this.setData({
                // 预约
                reservationPageNum: 1,
                reservationPageSize: 10,
                reservationDataList: [],
                reservationIsAll: false,
                // 审核
                verifyPageNum: 1,
                verifyPageSize: 10,
                verifyDataList: [],
                verifyIsAll: false,
                //
                dialogShow: false,
                dialogId: null,
                dialogContent: null
            })
            // 获取-预约记录 数据
            _this.getReservationData();
            // 获取-预约审核 数据
            _this.getVerifyData();
        })
    },

    // 弹出框-输入框：输入内容时触发
    dialogInput(e) {
        let _this = this
        console.log('dialogInput', e.detail);
        _this.setData({
            dialogContent: e.detail,
        })
    },

    // 跳转-添加
    goAdd() {
        wx.navigateTo({
            url: '/pages/meeting/visitorIinvitation/add/add?title=预约',
        })
    },
    
    // 跳转-详情、修改
    goDetail(e) {
        console.log('goDetail', e);
        let id = e.currentTarget.dataset.id
        let type = e.currentTarget.dataset.type
        let urlParam = "?type=" + type;
        if (type == 'verify') {
            urlParam = urlParam + "&title=审核" + "&id=" + id
        } else if (type == 'detail') {
            urlParam = urlParam + "&title=详情" + "&id=" + id
        }
        wx.navigateTo({
            url: '/pages/meeting/visitorIinvitation/detail/detail' + urlParam,
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        if (_this.data.changeData) {
            _this.setData({
                dataChange: false,
                // 预约
                reservationPageNum: 1,
                reservationPageSize: 10,
                reservationDataList: [],
                reservationIsAll: false,
                // 审核
                verifyPageNum: 1,
                verifyPageSize: 10,
                verifyDataList: [],
                verifyIsAll: false,
            })
            // 获取-预约记录 数据
            _this.getReservationData();
            // 获取-预约审核 数据
            _this.getVerifyData();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('onReachBottom');
        let _this = this;
        // 获取数据
        _this.getDataList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
})