/*
 * 手太欠
 * 愿这世界都如故事里一样 美好而动人~
*/
Page({

    /**
     * 页面的初始数据
     */
    data: {
        semesters           : [],
        formData            : [{
            name	        : '',
            mobile	        : '',
            index	        : 0
        }],
        empower	            : {},
        allPrice            : '0.00',

        semestersIndex      : 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            empowerId: options.id
        })
        this.indexInfo(options.id)
    },

    /**
     * 增收赋能-报名
     */
    indexInfo(id) {
        wx.$api.empower.buyInit(id).then(res => {
            let { empower, semesters } = res.data;
            this.setData({
                empower   : empower,
                semesters : semesters
            })
            this.getTotalPrice()
        }).catch(err => {})
    },

    // 计算价格
    getTotalPrice(){
        let totalPrice = 0
        this.data.formData.map(val => {
            totalPrice += Number(this.data.semesters[val.index].price)
        })
        this.setData({
            allPrice : totalPrice.toFixed(2)
        })
    },

    /**
     * 选择学期
     */
    onChange(e) {
        let that = this
        var val = e.detail.value;
        let index = e.currentTarget.dataset.index  // 获取数据的索引
        let reward = 'formData[' + index +'].index'  // 获取lists[index].reward
        that.setData({
            [reward]: val
        })
    },

    // 添加新用户
    onAddUser(){
        var  lists = this.data.formData;
        var newData = {
            name	: '',
            mobile	: '',
            index	: 0
        };
        lists.push(newData);
        this.setData({
            formData: lists,
        })  
        this.getTotalPrice()
    },


    // 删除用户
    onRemoveUser (e) {
        var lists = this.data.formData;
        let index = e.currentTarget.dataset.index
        lists.splice(index,1)
        this.setData({
            formData: lists,
        })
    },

    // 真实姓名
    bindName(e) {
        let that = this
        var val = e.detail.value;
        let index = e.currentTarget.dataset.index  // 获取数据的索引
        let reward = 'formData[' + index +'].name'  // 获取lists[index].reward
        that.setData({
            [reward]: val
        })
    },

    // 手机号码
    bindTel(e) {
        let that = this
        var val = e.detail.value;
        let index = e.currentTarget.dataset.index  // 获取数据的索引
        let reward = 'formData[' + index +'].mobile'  // 获取lists[index].reward
        that.setData({
            [reward]: val
        })
    },

    // 提交报名
    onSubmit(){
        let submitData = this.data.formData.map(val => {
            return {
                name		: val.name,
                mobile		: val.mobile,
                semester_id	: this.data.semesters[val.index].id
            }
        })
        wx.$api.empower.buy({
            empower_id  : this.data.empowerId,
            data        : submitData
        }).then(res => {
            wx.redirectTo({
                url: '/pages/pay/index?params=' + encodeURIComponent(JSON.stringify(res.data))
            })
        }).catch(err => {})
    }
})