<template>
    <div class="container">

        <div class="pageTitle">
            我的余额
        </div>

        <div class="point">

            <div class="point-item">
                <div class="title">
                    账户余额
                </div>
                <div class="val">
                    {{userInfo.moneyCoin}}
                </div>
            </div>

            <el-button type="primary" class="apply" @click="imageDialogVisible=true" v-if="userInfo.storeFlag=='Y'">申请提现</el-button>

        </div>

        <el-table class="pointTable" :data="tableData" row-key="id" ref="table" style="margin-top:10px">

            <el-table-column prop="remark" label="来源/用途" min-width="320" align="center">
            </el-table-column>

            <el-table-column prop="money" label="余额变化" min-width="100" align="center">
            </el-table-column>

            <el-table-column prop="createTime" label="日期" min-width="200" align="center">
                <template slot-scope="scope">
                    {{getTime(scope.row.createTime)}}
                </template>
            </el-table-column>

            <!-- <el-table-column prop="typeFlag" label="备注" min-width="100" align="center">
            </el-table-column> -->

        </el-table>
        <div class="pagination-wrapper" style="padding:20px 0;">

            <el-pagination v-if="totalNumber>pageSize" background @current-change="currentPageChange" :page-size="pageSize" layout="total, prev, pager, next" :total="totalNumber">
            </el-pagination>
        </div>

        <el-dialog class="applyContent" title="申请提现" width="30%" :visible.sync="imageDialogVisible">
            <el-tabs v-model="activeName">
                <el-tab-pane label="微信" name="0">
                    <el-form :model="formData" ref="form" :rules="rules" label-width="80px" label-position="right" style="max-width:300px;">
                        <el-form-item prop="money" label="提现金额">
                            <el-input-number v-model="formData.money" :step="100" :min="0"></el-input-number>
                        </el-form-item>

                        <el-form-item prop="wechat" label="微信号">
                            <el-input v-model="formData.wechat" placeholder="提现到的微信账号"></el-input>
                        </el-form-item>

                    </el-form>

                </el-tab-pane>
                <el-tab-pane label="支付宝" name="1">
                    <el-form :model="formData" ref="form1" :rules="rules1" label-width="80px" label-position="right" style="max-width:300px;">
                        <el-form-item prop="money" label="提现金额">
                            <el-input-number v-model="formData.money" :step="100" :min="0"></el-input-number>
                        </el-form-item>
                        <el-form-item prop="alipay" label="支付宝">
                            <el-input v-model="formData.alipay" placeholder="提现到的支付宝账号"></el-input>
                        </el-form-item>

                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="银行卡" name="2">
                    <el-form :model="formData" ref="form2" :rules="rules2" label-width="80px" label-position="right" style="max-width:300px;">
                        <el-form-item prop="money" label="提现金额">
                            <el-input-number v-model="formData.money" :step="100" :min="0"></el-input-number>
                        </el-form-item>
                        <el-form-item prop="bankCard" label="银行卡">
                            <el-input v-model="formData.bankCard" placeholder="提现到的银行卡"></el-input>
                        </el-form-item>
                        <el-form-item prop="bankName" label="银行名称">
                            <el-input v-model="formData.bankName" placeholder="银行名称"></el-input>
                        </el-form-item>
                        <el-form-item prop="openName" label="开户行">
                            <el-input v-model="formData.openName" placeholder="开户行名称"></el-input>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>

            </el-tabs>
            <span slot="footer" class="dialog-footer">
                <el-button @click="imageDialogVisible = false">取 消</el-button>
                <el-button @click="onsave" type="primary">确认</el-button>
            </span>

        </el-dialog>

    </div>
</template>
<script>
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

export default {
    created() {
        this.getData();
    },
    data() {
        return {
            formData: {
                money: ''
            },
            rules: {
                money: [
                    { required: true, message: '提现金额不能为空', trigger: 'blur' },
                ],
                wechat: [
                    {
                        required: true, message: '微信账号不能为空', trigger: 'blur'
                    }
                ]
            },
            rules1: {
                money: [
                    { required: true, message: '提现金额不能为空', trigger: 'blur' },
                ],
                alipay: [
                    {
                        required: true, message: '支付宝账号不能为空', trigger: 'blur'
                    }
                ]
            },
            rules2: {
                money: [
                    { required: true, message: '提现金额不能为空', trigger: 'blur' },
                ],
                bankCard: [
                    {
                        required: true, message: '银行卡号不能为空', trigger: 'blur'
                    }
                ],
                bankName: [
                    {
                        required: true, message: '银行名称不能为空', trigger: 'blur'
                    }
                ],
                openName: [
                    {
                        required: true, message: '开户行不能为空', trigger: 'blur'
                    }
                ]
            },
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 10,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '用户',
                    value: 'userId',
                    show: true
                },
                {
                    label: '订单',
                    value: 'orderId',
                    show: true
                },
                {
                    label: '变化数量',
                    value: 'money',
                    show: true
                },
                {
                    label: '剩余金额',
                    value: 'balance',
                    show: true
                },
                {
                    label: '类型',
                    value: 'typeFlag',
                    show: true
                },
                {
                    label: '备注',
                    value: 'remark',
                    show: true
                },
            ],
            multipleMode: false,
            showAdvancedQueryDialog: false,
            advancedQueryFields: [],
            showTableSortDialog: false,
            tableSortFields: [],
            searchMethods: ['=', '!=', '>', '>=', '<', '<=', 'like'],
            advancedQueryColumns: [
                {
                    label: '用户',
                    value: 'user_id'
                },
                {
                    label: '订单',
                    value: 'order_id'
                },
                {
                    label: '变化数量',
                    value: 'money'
                },
                {
                    label: '剩余金额',
                    value: 'balance'
                },
                {
                    label: '类型',
                    value: 'type_flag'
                },
                {
                    label: '备注',
                    value: 'remark'
                },
            ],
            advancedQuerySearchKey: '',
            orderByStr: '',
            imgSrc: '',
            imageDialogVisible: false,
            activeName: '0'
        }
    },
    computed: {
        ...mapState(['userInfo']),
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        onsave() {
            var data = {
                userId: this.userInfo.id,
                money: this.formData.money
            }
            if (this.activeName == '0') {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        data.wechat = this.formData.wechat
                        this.submit(data);
                    } else {
                        return false;
                    }
                });
            }
            else if (this.activeName == '1') {
                this.$refs.form1.validate((valid) => {
                    if (valid) {
                        data.alipay = this.formData.alipay
                        this.submit(data);
                    } else {
                        return false;
                    }
                });
            }
            else {
                this.$refs.form2.validate((valid) => {
                    if (valid) {
                        data.bankCard = this.formData.bankCard
                        data.bankName = this.formData.bankName
                        data.openName = this.formData.openName
                        this.submit(data);
                    } else {
                        return false;
                    }
                });
            }
        },
        submit(data) {

            this.$http.post({
                url: '/memberCoinTixian/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.imageDialogVisible = false
                    this.$alert('您的申请已提交，请耐心等待', '提交成功', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                }
            })
        },
        pageSizeChange(size) {
            this.currentPage = 1;
            this.pageSize = size;
            this.getData();
        },
        currentPageChange(page) {
            this.currentPage = page;
            this.getData();
        },
        getData() {

            var data = {
                currentPage: this.currentPage,
                pageNumber: this.pageSize,
                userId: this.userInfo.id
            }



            this.$http.get({
                url: '/memberCoin/page',
                data: data
            }).then(res => {
                if (res.success) {
                    this.totalNumber = res.data.page.totalNumber;
                    this.tableData = res.data.pp;
                }
            })
        },
        isColumnShow(column) {
            var row = this.tableColumns.find(i => i.value === column);
            return row ? row.show : false;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        editRow(row) {
            this.$router.push({
                path: '/memberPoint',
                query: {
                    id: row.id,
                    column: this.$route.query.column,
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        addField() {
            this.advancedQueryFields.push({
                link: 'AND',
                name: '',
                searchMethod: '=',
                value: '',
            });
        },
        removeField(i) {
            if (this.advancedQueryFields.length > 0) {
                this.advancedQueryFields.splice(i, 1);
            }
        },
        advancedQuery() {

            this.advancedQuerySearchKey = '';

            if (this.advancedQueryFields.length > 0) {

                var templist = [];

                this.advancedQueryFields.forEach(item => {
                    if (item.link && item.name && item.searchMethod && item.value) {
                        var tempItem = item.link + '_,' + item.name + '_,' + item.searchMethod + '_,' + item.value;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.advancedQuerySearchKey = templist.join('_;');
                }
            }

            this.getData();
            this.showAdvancedQueryDialog = false;
        },
        addSortField() {
            this.tableSortFields.push({
                name: '',
                order: 'asc',
            });
        },
        removeSortField(i) {
            if (this.tableSortFields.length > 0) {
                this.tableSortFields.splice(i, 1);
            }
        },
        tableSortQuery() {

            this.orderByStr = '';

            if (this.tableSortFields.length > 0) {

                var templist = [];

                this.tableSortFields.forEach(item => {
                    if (item.name && item.order) {
                        var tempItem = item.name + '_,' + item.order;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.orderByStr = templist.join('_;');
                }
            }

            this.getData();
            this.showTableSortDialog = false;
        },
        exportExcel() {
            window.location.href = this.$baseUrl + "/memberPoint/exportExcel?searchKey="
                + this.filter1 + "&advancedQuery=" + this.advancedQuerySearchKey + "&orderByStr=" + this.orderByStr;
        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/memberPoint/del',
                    data: { id: row.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        DateFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD', { locale: zh })
            }

        },
        showImg(img) {
            this.imgSrc = img;
            this.imageDialogVisible = true;
        },

    }
}
</script>
<style lang="less" scoped>
.container {
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    border: 1px solid rgba(234, 234, 234, 1);
    min-height: 612px;
    padding: 0 25px 0 39px;

    .pageTitle {
        font-size: 14px;
        font-family: MicrosoftYaHei;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
        line-height: 19px;
        padding: 21px 0 9px;
        border-bottom: 1px solid #ebebeb;
    }

    .point {
        // width: 953px;
        height: 106px;
        background: rgba(255, 253, 236, 1);
        line-height: 106px;
        margin-top: 5px;
        box-sizing: border-box;
        font-size: 0;
        text-align: center;

        position: relative;

        .apply {
            position: absolute;
            right: 100px;
            top: 30px;
        }

        .point-item {
            display: inline-block;

            vertical-align: middle;
            text-align: center;

            .title {
                font-size: 14px;
                font-family: MicrosoftYaHei;
                color: rgba(51, 51, 51, 1);
                line-height: 19px;
            }

            .val {
                font-size: 36px;
                font-family: MicrosoftYaHei;
                color: rgba(251, 110, 8, 1);
                line-height: 47px;
            }
        }
    }
}
</style>
