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

    /**
     * 页面的初始数据
     */
    data: {
        enrollData   : '',
        paySuccess   : false,
        experienceId: '',
        typesArr   : [],
        typesIndex : 0,
        refertoStatus: false,
        checkStatus : false,

        //省份选择
        areas      : [],
        areaId     : '',
        areaIndex  : 0,

        //市级选择
        cityList   : [],
        cityId     : 0,
        cityIndex  : 0,

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            experienceId: options.id
        })
        // 编辑报名前置接口
        this.eitInfo(options.id);
    },

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

     /**
     * 申请前置接口
     */
    eitInfo(id) {
        wx.$api.recruit.Edit(id).then(res => {
            let typeValue    = res.data.types.findIndex(val=> val.experience_type_id == res.data.enroll.experience_type.experience_type_id)
            let areasValue = res.data.provinces.findIndex(val=> val.name == res.data.express.province.name),
                cityValue  = res.data.cities.findIndex(val=> val.name == res.data.express.city.name),
                regiValue  = res.data.districts.findIndex(val=> val.name == res.data.express.district.name)
            this.setData({
                enrollData  : res.data.enroll,
                typesIndex  : typeValue,
                typesArr    : res.data.types,
                areas       : res.data.provinces,
                cityList    : res.data.cities,
                regiList    : res.data.districts,
                areaIndex   : areasValue,
                cityIndex   : cityValue,
                regiIndex   : regiValue,
                areaId      : res.data.express.province.region_id,
                cityId      : res.data.express.city.region_id,
                regiId      : res.data.express.district.region_id
            })
        }).catch(err => {})
    },
    
    /**
     * 省市区列表
     */
    createInfo() {
        wx.$api.site.create().then(res => {
            let areas         =  res.data,
                areaIndex     =  this.data.areaIndex
            this.setData({
                areas    : areas,
                areaId   : areas[areaIndex].id,
            })

            this.citylist(areas[areaIndex].id)
        }).catch(err => {})
    },

    /**
     * 所在省份-下拉
     */
    areasChange(e) {
        let area     = this.data.areas,
            index    = e.detail.value,
            atcode   = area[index].id
        if (index != this.data.areaIndex) {
            this.setData({
                areaIndex : index,
                areaId    : atcode
            })
            // 获取市级列表
            this.citylist(atcode)
        }
    },

    /**
     * 市级列表
     */
    citylist(cityId) {
        wx.$api.site.create({
            parent_id: cityId
        }).then(res=>{
            let cityArr = res.data
            this.setData({
                cityId    : cityArr[0].id,
                cityIndex : 0,
                cityList  : cityArr
            })
            
            // 获取区级列表
            this.regilist(cityArr[0].id)
        })
    },

    /**
     * 市级下拉筛选
     */
    cityDrop(e) {
        let city     = this.data.cityList,
            index    = e.detail.value,
            citycode = city[index].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({
                regiList  : res.data,
                regiId    : res.data[0].id,
                regiIndex : 0
            })
        })
    },
    
    /**
     * 区下拉筛选
     */
    regiDrop(e) {
        let newIndex = e.detail.value
        this.setData({
            regiIndex  : newIndex,
            regiId     : this.data.regiList[newIndex].id
        })
    },


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

    /**
     * 提交表单
     */
    siteform(e) {
        let value  = e.detail.value

        let data = {
            name                : value.name,
            address             : value.address,
            experience_type_id  : this.data.typesArr[this.data.typesIndex].experience_type_id,
            province_id         : this.data.areaId,
            city_id             : this.data.cityId,	
            district_id         : this.data.regiId,
        }
        this.setData({
            disabled: true
        })
        wx.$api.recruit.EditPut(this.data.enrollData.experience_area_enroll_id,data).then(res => {
            this.setData({
                checkStatus: true,
                disabled: false
            })
        }).catch(() =>{
            this.setData({
                disabled: false
            })
        })
    },

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

    /**
     * 申请体验官成功后，弹出
     */
    checkTap() {
        this.setData({
            checkStatus: false
        })
        wx.switchTab({
            url: '/pages/recruit/index'
        })
    }
})