const Parse = getApp().Parse;
const company = getApp().globalData.company
Page({

    /**
     * 页面的初始数据
     */
    data: {
        length: 0,
        earnings: 0,
        list: [
            { 'num': '提现到银行卡' },
        ],
        idx: null,
        id: null,
        name: null,
        price: null,
        checked: true,
        merchant: null,
        names: null,
        bankName: null,
        accountId: null,
        activeColor: getApp().globalData.activeColor,
        titleColor: getApp().globalData.titleColor,
    },
    async getUserAgentWithdraw() {
        let merchant = wx.getStorageSync("merchant");
        if (merchant == "") {
            wx.redirectTo({
                url: '/nova-bzzb/pages/my/login/index'
            });
            return;
        }
        if (this.data.length > 2) {
            wx.showToast({
                title: '您本月的提现次数已到上限',
                icon: 'none',

            });
            return;
        }
        if (!this.data.price) {
            wx.showToast({
                title: '请输入提现金额',
                icon: 'none',

            });
            return
        }
        console.log(this.data.price, this.data.earnings);
        console.log(this.data.price > this.data.earnings)
        if (Number(this.data.price) > Number(this.data.earnings)) {
            wx.showToast({
                title: '您输入的金额超过了您的可提现金额',
                icon: 'none',
            });
            return
        }
        if (this.data.price < 1000) {
            wx.showToast({
                title: '最低提现金额为1000元',
                icon: 'none',

            });
            return
        }

        if (!this.data.name) {
            wx.showToast({
                title: '请选择提现方式',
                icon: 'none',

            });
            return
        }
        if (!this.data.names || !this.data.bankName || !this.data.accountId) {
            wx.showToast({
                title: '请输入完整银行卡信息',
                icon: 'none',

            });
            return
        }
        let UserAgentWithdraw = Parse.Object.extend("UserAgentWithdraw")
        let userAgentWithdraw = new UserAgentWithdraw()
        userAgentWithdraw.set("count", Number(this.data.price))
        userAgentWithdraw.set("type", "bank")
        userAgentWithdraw.set("isVerified", false)
        userAgentWithdraw.set("company", {
            __type: 'Pointer',
            className: 'Company',
            objectId: company
        })
        userAgentWithdraw.set("user", {
            __type: 'Pointer',
            className: '_User',
            objectId: this.data.merchant

        })
        userAgentWithdraw.set("info", [{
            name: this.data.names,
            accountId: this.data.accountId,
            bankName: this.data.bankName,
            storeName: this.data.id,
        }]

        )

        userAgentWithdraw.save().then(res => {
            console.log(res)
            wx.showToast({
                title: '提现成功',
                icon: 'none',

            });
            wx.navigateBack({
                delta: 1

            })

        })


    },
    goIndex(e) {
        let index = e.currentTarget.dataset.index;
        let name = e.currentTarget.dataset.item.num
        if (index == this.data.idx) {
            this.setData({
                idx: null,
                name: null
            })
        } else {
            this.setData({
                idx: index,
                name: name
            })
        }
    },
    blur(e) {
        let name = e.currentTarget.dataset.name
        this.setData({
            [name]: e.detail.value
        })
    },
    async getTotal(startTime, endTime) {
        let UserAgentWithdraw = new Parse.Query('UserAgentWithdraw')
        UserAgentWithdraw.equalTo('user', this.data.merchant)
        UserAgentWithdraw.equalTo('type', 'bank')
        UserAgentWithdraw.greaterThan('createdAt', startTime)
        UserAgentWithdraw.lessThan('createdAt', endTime)
        let userAgentWithdraw = await UserAgentWithdraw.find()
        let length
        if (userAgentWithdraw) {
            console.log(userAgentWithdraw.length);
            length = userAgentWithdraw.length
        }
        this.setData({
            length: length
        })
        console.log(this.data.length);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(Parse.User.current().id);
        let earnings = options.earnings
        let id = options.id
        let merchant = wx.getStorageSync('merchant'); //用户
        console.log(merchant);
        console.log(earnings);
        this.setData({
            earnings: earnings,
            merchant: merchant.objectId,
            id: id
        })
        let d = new Date();
        this.date = d
        let data = new Date(d.getFullYear(), d.getMonth() + 1, 0);
        let day = data.getDate();
        this.data.year = d.getFullYear()
        this.data.month = d.getMonth() + 1 > 9 ? d.getMonth() + 1 : "0" + (d.getMonth() + 1)
        let dayArr = []
        for (let i = 0; i < day; i++) {
            if (i < 9) {
                dayArr.push("0" + (i + 1))
            } else {
                dayArr.push((i + 1))
            }

        }
        let startTime = this.data.year + "-" + this.data.month + "-" + "01" + " 00:00:00"
        let endTime = this.data.year + "-" + this.data.month + "-" + dayArr.length + " 23:59:57"
        console.log(startTime, endTime);
        this.getTotal(startTime, endTime)
    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

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

    }
})