// pages/recruit/index.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userLogin      : false,

        recruitData    : '',
        type           : 'flowPath',  //flowPath 参与流程  clockIn 打卡

        followState    : false,
        followStype    : '',  //是关闭还是跳转
        loginCode      : '',
        subscribe      : '', //是否关注

        calendarData   : [], //日历表
        canSign        : '', //是否可以签到
        selectMonth    : '', //当前月
        lastMonth      : '', //选择下个月
        nextMonth      : '', //选择上个月
        signDayNumber  : '', //累计打卡天数
        signDayCount   : '', //总打卡天数
        signWaterNumber: '', //总打卡给水滴
        skinData       : '', //体验对比分析
        dailyData      : [], //日常任务
        dailyId        : '',  // 日常任务列表id
        dailyTitle     : '',  // 日常任务列表标题
        dailyState     : false, //日常分享列表弹出
        nameState      : false, //粘贴地址弹框
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.invite != undefined) {
            getApp().globalData.inviteText = options.invite
        }
        if(options.is_experience != undefined) {
            getApp().globalData.isExperience = options.is_experience
        }
        if(options.experience_area_id != undefined) {
            getApp().globalData.experienceAreaId = options.experience_area_id
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            followState: false
        })
        
        wx.login({
            success: res => {
                this.setData({
                    loginCode: res.code
                })
            }
        })
        
        // 获取体验官首页
        this.recruitInfo();

        // 获取登录状态
        if(wx.getStorageSync("token") != ''){
            this.setData({
                userLogin: true
            })
            return
        }

        this.setData({
            type: 'flowPath',
            followState: false
        })
    },

    /**
     * 体验官首页
     */
    recruitInfo() {
        wx.$api.recruit.index({
            invite         : getApp().globalData.inviteText || '',
            is_experience  : getApp().globalData.isExperience || ''
        }).then(res => {
            this.setData({
                recruitData: res.data
            })
            // 0可以申请  1需要面检测  5签收-查看物流  6打卡  7第二次面  8完成
            if(res.data.can.status == 6 || res.data.can.status == 7 || res.data.can.status == 8) {
                // 显示打卡数据
                this.setData({
                    type : 'clockIn'
                })

                // 获取日历
                this.signInfo();

                // 获取日常任务
                this.dailyInfo();
            }

            if(res.data.can.status == 8) {
                // 获取两次报告对比
                this.skinInfo();
            }
        }).catch(err => {})
    },

    /**
     * 申请
     */
    applyGo() {
        if(wx.getStorageSync("token") != ''){
            this.setData({
                followStype : 'experience'
            })

            // 0可以申请  1需要面检测  5签收-查看物流  6打卡  7第二次面
            if(this.data.recruitData.can.status == 0) {
                if(!this.data.recruitData.is_subscribe) {
                    // 微信公众号入库
                    this.followCode();

                    this.setData({
                        followState: true
                    })
                    return
                }
                // 申请体验官
                wx.navigateTo({
                    url: './referto/referto?id=' + this.data.recruitData.experience_id
                })
            } else if(this.data.recruitData.can.status == 5) {
                // 查看物流
                wx.navigateTo({
                    url:  "/pages/recruit/expressTrack/expressTrack?id=" + this.data.recruitData.experience_id
                })
            } else if(this.data.recruitData.can.status == 6) {
                // 日历打卡
                if(!this.data.recruitData.is_subscribe) {
                    // 微信公众号入库
                    this.followCode();

                    this.setData({
                        followState: true
                    })
                    return
                }
                // 填写打卡心得
                wx.navigateTo({
                    url: './signWrite/signWrite?experienceId=' + this.data.recruitData.experience_id + '&signDayNumber=' + this.data.signDayNumber
                })
            } else {
                // 面部检测
                wx.navigateTo({
                    url: '/pages/index/assess/assess?type=1'
                })
            }
            return
        }
        
        // 去登录
        this.setData({
            followState: false
        })
        wx.navigateTo({
            url: "/pages/login/index"
        })
    },

    /**
     * 编辑申请
     */
    editGo() {
        wx.navigateTo({
            url: './refertoEdit/refertoEdit?id=' + this.data.recruitData.experience_id
        })
    },

    rejectGo() {
        wx.showModal({
            title: '驳回原因提示',
            showCancel: false,
            content: this.data.recruitData.can.remark,
            success: res => {}
        })
    },

    /**
     * 获取code
     */
    followCode() {
        wx.login({
            success: res => {
                this.setData({
                    loginCode: res.code
                })
            }
        })
        
        wx.getUserProfile({
            desc    : "获取你的昵称、头像、地区及性别",
            success : e => {
                if(e.errMsg == "getUserProfile:ok"){
                    wx.$api.recruit.wechatMini({
                        code         :this.data.loginCode,
                        iv           : e.iv,
                        encryptedData: e.encryptedData
                    }).then(res => {
                        // subscribe == 0未关注公众号
                        //subscribe == 1已关注公众号
                        this.setData({
                            subscribe: res.data.subscribe
                        })
                    }).catch(err => {})
                }
            },
            fail: err => {
                return;
            }
        })
    },

    /**
     * 日历
     */
    signInfo() {
        wx.$api.recruit.signCalendar({
            date         : this.data.selectMonth,
            experience_id: this.data.recruitData.experience_id
        }).then(res => {
            this.setData({
                calendarData : res.data.calendar,
                lastMonth    : res.data.lastMonth,
                nextMonth    : res.data.nextMonth,
                selectMonth  : res.data.nowMonth,
                canSign      : res.data.canSign,
                signDayNumber: res.data.signDayNumber,
                signDayCount : res.data.signDayCount,
                signWaterNumber : res.data.signWaterNumber
            })
        }).catch(err => {})
    },

    /**
     * 日历数量加减
     */
    couponNumber(e){
        let val = e.currentTarget.dataset.type
        if (val == 'plus'){
            this.setData({
                selectMonth: this.data.lastMonth
            })
        }else{
            this.setData({
                selectMonth: this.data.nextMonth
            })
        }

        // 获取日历
        this.signInfo();
    },

    /**
     * 签到弹出
    */
    tapPop() {
        this.setData({
            followStype : 'experience'
        })
        if(!this.data.recruitData.is_subscribe) {
            // 微信公众号入库
            this.followCode();

            this.setData({
                followState: true
            })
            return
        }
        wx.navigateTo({
            url: './signWrite/signWrite?experienceId=' + this.data.recruitData.experience_id + '&signDayNumber=' + this.data.signDayNumber
        })
    },

    /**
     * 签到跳走
    */
    tapGo() {
        if(this.data.followStype == 'close') {
            this.setData({
                followState: !this.data.followState
            })
            return
        }
        this.setData({
            followState: !this.data.followState
        })

        // 0可以申请  1需要面检测  5签收-查看物流  6打卡  7第二次面
        if(this.data.recruitData.can.status == 0) {
            this.setData({
                followState: false
            })
            // 申请体验官
            wx.navigateTo({
                url: './referto/referto?id=' + this.data.recruitData.experience_id
            })
        } else if(this.data.recruitData.can.status == 6) {
            // 填写打卡心得
            wx.navigateTo({
                url: './signWrite/signWrite?experienceId=' + this.data.recruitData.experience_id + '&signDayNumber=' + this.data.signDayNumber
            })
            this.setData({
                followState: false
            })
        }
    },

    /**
     * 打开日常任务弹框
     */
    dailyBind() {
        this.setData({
            dailyState: true
        })
    },

    /**
     * 日常任务
     */
    dailyInfo() {
        wx.$api.recruit.dailyCreate().then(res => {
            this.setData({
                dailyData: res.data
            })
        }).catch(err => {})
    },

    /**
     * 日常任务分享
     */
    shareTap(e) {
        this.setData({
            dailyId: e.currentTarget.dataset.id,
            dailyTitle: e.currentTarget.dataset.title,
            dailyState: false,
            nameState: true
        })
    },

    /**
     * 关闭日常任务分享
     */
    dailyClose() {
        this.setData({
            dailyState: false
        })
    },

    /**
     * 复制分享地址
     */
    freeform(val) {
        let data = {
            category_id     : this.data.dailyId,
            url             : val.detail.value.url,
            city_id         : this.data.recruitData.experience_id
        }

        wx.$api.recruit.dailyPost(data).then(res => {
            wx.showToast({
                title : '提交成功',
                icon  : "none"
            })
            this.setData({
                nameState: false
            })
            // 获取日常任务
            this.dailyInfo();
        }).catch(() =>{})

    },

    /**
     * 关闭复制分享地址
     */
    establish() {
        this.setData({
            nameState: false
        })
    },

    /**
     * 分享记录
     */
    recordTap() {
        wx.navigateTo({
            url: '/pages/recruit/dailyList/dailyList',
        })
        this.setData({
            dailyState: false
        })
    },

    /**
     * 两次报告对比
     */
    skinInfo() {
        wx.$api.recruit.skinDiff().then(res => {
            this.setData({
                skinData: res.data
            })
        }).catch(err => {})
    },

    /**
     * 公众号弹出
     */
    followHide() {
        // 微信公众号入库
        this.followCode();
        
        this.setData({
            followState: !this.data.followState,
            followStype: 'close'
        })
    },

    /**
     * 第二次免检
     */
    tapSecond() {
        // 面部检测
        wx.navigateTo({
            url: '/pages/index/assess/assess?type=2'
        })
    }
})