// pages/temp/cash/index.js
const app = getApp()
const util = require("../../../utils/jmsUtil.js");
const util2 = require("../../../utils/util.js");
const api = require('../../../api/jms.js');
const user = require('../../../utils/user.js');
const check = require('../../../utils/check.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        workerFee: {},
        worker: '',
        workerFeeList: [],
        pageIndex: 1,
        showAuth: false,
        code: '',
        checked: false
    },
    userProtocol() {
        wx.navigateTo({
            url: '/pages/temp/cash/protocol'
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       this.getBlance();
        this.getWorkerFeeList();
        this.getLoginCode();
    },
    getBlance(){
        util.request(api.getWorkerFee, {}, 'GET').then(res => {
            this.setData({
                workerFee: res.data.fee,
                worker: res.data.worker
            })
        });
    },
    goCancel() {
        this.setData({
            showAuth: false
        })
    },
    goCashout() { //去提现
        let that = this
        //是否认证过
        if (!this.data.worker.signStatus || this.data.worker.signStatus == false) {
            this.setData({
                showAuth: true
            })
            return;
        }
        if(that.data.workerFee.blance<=0){
            util.showErrorToast("无可提现金额");
            return;
        }

        wx.showModal({
            title: '提示',
            confirmText:'提现',
            content: '提现金额为' + that.data.workerFee.blance + '，平台通过授权第三方（“云账户”）向您的微信钱包打款',
            success: function (sm) {
                if (sm.confirm) {
                    util.request(api.Cashout, {
                        amount: that.data.workerFee.blance
                    }).then(function (res) {
                        if (res.errno === 0) {
                            wx.showToast({
                                title: '提现申请成功,请查看【微信支付】',
                                icon: 'none',
                                duration: 2000
                            });
                            that.setData({
                                showAuth: false
                            })
                            that.getBlance();
                        }else{
                            util.showErrorToast(res.errmsg);
                        }
                    });

                } else if (sm.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    goDeductDetail() {
        wx.navigateTo({
            url: '/pages/temp/cash/cashList',
        })
    },
    getWorkerFeeList(e) { //扣款明细
        let that = this;
        util.request(api.getWorkerFeeList, {
            pageIndex: that.data.pageIndex,
            pageSize: 10
        }, 'GET').then(res => {
            if (res.data.list.length == 0) {
                util2.showErrorToast("没有更多数据");
            } else {
                let list = that.data.workerFeeList;
                list = list.concat(res.data.list);
                that.setData({
                    workerFeeList: list,
                    pageIndex: that.data.pageIndex + 1
                })

                console.info(that.data.workerFeeList)
            }

        });
    },

    bindinputName(e) {
        let val = e.detail.value;
        this.setData({
            ['worker.name']: val
        });

    },
    bindinputNid(e) {
        let val = e.detail.value;
        this.setData({
            ['worker.idCard']: val
        })
    },
    onChange(e) {
        console.log(e);
        this.setData({
            checked: e.detail,
        });
    },
    cashoutAuth() {
        if (this.data.worker.name == '') {
            wx.showToast({
                title: '请输入真实姓名',
                icon: 'none',
                duration: 2000
            });
            return false;

        } else if (!check.isCardCheck(this.data.worker.idCard)) {
            wx.showToast({
                title: '请输入正确的身份证号',
                icon: 'none',
                duration: 2000
            });
            return false;

        } else if (!this.data.checked) {
            wx.showToast({
                title: '请阅读《用户条款》并勾选',
                icon: 'none',
                duration: 2000
            });
            return false;

        }
        let that = this;
        util.request(api.getSign, {
            tempWorkerId: this.data.worker.id,
            name: this.data.worker.name,
            idCard: this.data.worker.idCard,
            phone: this.data.worker.tel,
            code: this.data.code
        }, "POST").then(res => {
            if (res.errno == 0) {
                that.setData({
                    showAuth: false,
                    'worker.signStatus':true
                })
                that.goCashout();
            } else {
                that.getLoginCode();
                util.showErrorToast(res.errmsg);
            }
        });
    },
    getLoginCode() {
        let that = this;
        user.login().then(res => {
            console.info(res)
            that.setData({
                code: res.code
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getWorkerFeeList();
    },

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

    }
})