// pages/recruit/referto/referto.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        paySuccess   : false,
        experienceId: '',
        typesArr   : [],
        typesIndex : 0,
        refertoStatus: false,
        checkStatus : false,
                
        // 市级选择
        cityArr    : [],
        cityId     : 0,
        cityIndex  : 0,

        // 区域选择
        regiArr    : [],
        regiId     : 0,
        regiIndex  : 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            experienceId: options.id
        })
        // 获取申请前置接口
        this.recruitInfo(options.id);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() { },

     /**
     * 申请前置接口
     */
    recruitInfo(id) {
        wx.$api.recruit.Enroll(id, {
            experience_area_id: getApp().globalData.experienceAreaId
        }).then(res => {
            let areas = [
                {
                    city: "请选择城市",
                    city_id: null,
                    experience_area_id: null,
                    surplus: null
                }
            ]
            this.setData({
                typesArr   : [{experience_type_id: null, name: "请选择体验内容"}, ...res.data.types],
                cityArr    : [...areas, ...res.data.areas],
                paySuccess : false
            })

            // 获取区级列表
            this.regilist(res.data.areas[this.data.cityIndex].city_id)
        }).catch(err => {})
    },
    
    /**
     * 市级下拉筛选
     */
    cityDrop(e) {
        let city     = this.data.cityArr,
            index    = e.detail.value,
            citycode = city[index].city_id
        if (index != this.data.cityIndex) {
            this.setData({
                cityIndex : index,
                cityId   : citycode
            })
            
            // 获取市级列表
            this.regilist(citycode)
        }
    },

    /**
     * 区列表
     */
    regilist(areaId) {
        wx.$api.site.create({
            parent_id: areaId
        }).then(res=>{
            this.setData({
                regiArr   : [{id: null, name: "请选择区域", parent_id: null}, ...res.data],
                regiId    : null,
                regiIndex : 0
            })
        })
    },
    
    /**
     * 区下拉筛选
     */
    regiDrop(e) {
        let newIndex = e.detail.value
        this.setData({
            regiIndex  : newIndex,
            regiId     : this.data.regiArr[newIndex].id
        })
    },

    /**
     * 体验内容筛选
     */
    tasteDrop(e) {
        let newIndex = e.detail.value
        this.setData({
            typesIndex : newIndex
        })
    },

    /**
     * 提交表单
     */
    siteform(e) {
        let value  = e.detail.value
        let errMsg = ''
        // if(this.data.regiId === null) errMsg = "请选择区域"
        if(this.data.cityId === null || this.data.cityArr[this.data.cityIndex].experience_area_id === null) errMsg = "请选择城市"
        if(this.data.typesArr[this.data.typesIndex].experience_type_id === null) errMsg = "请选择体验内容"
        
        if(errMsg != ''){
            wx.showToast({
                title: errMsg,
                icon : 'none'
            })
            return
        }

        let data = {
            name                : value.name,
            address             : value.address,
            city_id             : this.data.cityId,	
            // district_id         : this.data.regiId,
            experience_id       : this.data.experienceId,
            experience_area_id  : this.data.cityArr[this.data.cityIndex].experience_area_id,
            experience_type_id  : this.data.typesArr[this.data.typesIndex].experience_type_id,
            invite              : getApp().globalData.inviteText
        }
        this.setData({
            disabled: true
        })
        wx.$api.recruit.recruitAdd(data).then(res => {
            this.setData({
                checkStatus: true
            })
        }).catch(() =>{
            this.setData({
                disabled: false
            })
        })
    },

    /**
     * 关闭弹框，跳转首页
     */
    refertoTap() {
        this.setData({
            refertoStatus: false
        })
        wx.switchTab({
            url: '/pages/recruit/index'
        })
    },

    /**
     * 申请体验官成功后，弹出
     */
    checkTap() {
        this.setData({
            refertoStatus: true,
            checkStatus: false
        })
    }
})