<template>
    <div class="list-view">
        <page-title>
            <el-button
                @click="download('/rechargeOrder/excel')"
                icon="el-icon-upload2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                导出
            </el-button>
        </page-title>
        <div class="filters-container">
            <el-form :model="formData" :inline="true" @keyup.enter.native="initData">
                <el-form-item label="创建时间">
                    <created-at-picker v-model="formData.createdAt" @input="initData" name="创建"></created-at-picker>
                </el-form-item>

                <el-form-item label="订单编号">
                    <el-input v-model="formData.orderNo" placeholder="订单编号" clearable=""></el-input>
                </el-form-item>

                <el-form-item label="用户">
                    <user-search v-model="formData.userId"></user-search>
                </el-form-item>

                <el-form-item label="充值状态">
                    <el-select v-model="formData.status" :clearable="true" @change="initData">
                        <el-option
                            v-for="(value, key) in rechargeStatusMap"
                            :label="value"
                            :value="key"
                            :key="key"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div class="tool-wrapper">
                <div class="tool-right">
                    <el-tag>总金额({{ totalAmount || 0 }}￥)</el-tag>
                </div>
                <el-input
                    placeholder="搜索..."
                    v-model="search"
                    clearable
                    class="filter-item search"
                    @keyup.enter.native="initData"
                >
                    <el-button @click="initData" slot="append" icon="el-icon-search"> </el-button>
                </el-input>
            </div>
        </div>
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            header-row-class-name="table-header-row"
            header-cell-class-name="table-header-cell"
            row-class-name="table-row"
            cell-class-name="table-cell"
            :height="tableHeight"
            v-loading="fetchingData"
        >
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
            <el-table-column prop="id" label="ID" width="100"> </el-table-column>
            <el-table-column prop="userId" label="用户ID"> </el-table-column>
            <el-table-column prop="orderNo" label="订单编号" min-width="160" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="title" label="标题" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="payTool" label="充值方式" width="100"> </el-table-column>
            <el-table-column prop="orderAmount" label="充值金额"> </el-table-column>
            <!-- <el-table-column prop="userFeeAmt" label="服务费" > </el-table-column> -->
            <el-table-column prop="amount" label="到账金额"> </el-table-column>
            <el-table-column prop="status" label="状态" width="100">
                <template v-slot="scope">
                    <el-tooltip placement="top" :content="scope.row.result || '没有问题'">
                        <el-tag
                            :type="rechargeStatusMap[scope.row.status].type"
                            v-if="rechargeStatusMap[scope.row.status]"
                            >{{ rechargeStatusMap[scope.row.status] }}</el-tag
                        >
                    </el-tooltip>
                </template>
            </el-table-column>

            <el-table-column prop="transactionId" label="交易流水" min-width="160" show-overflow-tooltip>
            </el-table-column>

            <el-table-column prop="finishTime" label="完成时间" width="140">
                <template v-slot:header="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>
            <el-table-column prop="notifyTime" label="通知时间" width="140"> </el-table-column>

            <el-table-column prop="createdAt" label="创建时间" width="140">
                <template v-slot:header="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>

            <el-table-column label="操作" min-width="100" fixed="right">
                <template v-slot="scope">
                    <el-button type="primary" size="mini" @click="checkRechargeResult(scope.row.orderNo)"
                        >检查</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-pagination
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
            >
            </el-pagination>
        </div>

        <el-dialog title="检查充值结果" :visible.sync="rechargeResultDialog">
            <div class="desc-wrapper">
                <div class="desc-line">
                    <div class="desc-label">订单编号</div>
                    <div class="desc-content">{{ rechargeResultData.orderNo }}</div>
                </div>

                <div class="desc-line">
                    <div class="desc-label">交易流水</div>
                    <div class="desc-content">{{ rechargeResultData.transactionNo }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">商品标题</div>
                    <div class="desc-content">{{ rechargeResultData.goodsTitle }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">充值金额</div>
                    <div class="desc-content">{{ rechargeResultData.totalAmount }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">充值状态</div>
                    <div class="desc-content">
                        <el-tag
                            :type="rechargeStatusMap[rechargeResultData.status].type"
                            v-if="rechargeStatusMap[rechargeResultData.status]"
                            >{{ rechargeStatusMap[rechargeResultData.status] }}</el-tag
                        >
                    </div>
                </div>

                <div class="desc-line">
                    <div class="desc-label">状态描述</div>
                    <div class="desc-content">{{ rechargeResultData.orderMsg }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">支付时间</div>
                    <div class="desc-content">{{ rechargeResultData.payTime }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">付款账号</div>
                    <div class="desc-content">{{ rechargeResultData.accNo }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">付款人用户号</div>
                    <div class="desc-content">{{ rechargeResultData.accLogonNo }}</div>
                </div>
                <div class="desc-line">
                    <div class="desc-label">支付平台</div>
                    <div class="desc-content">
                        <el-tag
                            :type="payPlatformMap[rechargeResultData.payPlatform].type"
                            v-if="payPlatformMap[rechargeResultData.payPlatform]"
                            >{{ payPlatformMap[rechargeResultData.payPlatform] }}</el-tag
                        >
                    </div>
                </div>
            </div>
            <div slot="footer">
                <el-button type="primary" @click="rechargeResultDialog = false">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';
import SortableHeader from '../components/SortableHeader.vue';
import { payPlatformMap, rechargeStatusMap } from '@/utils/constant.js';

export default {
    components: { SortableHeader },
    name: 'RechargeOrderList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/rechargeOrder/all',
            downloading: false,
            formData: {
                createdAt: '',
                orderNo: '',
                userId: null,
                status: null
            },
            totalAmount: 0,
            payPlatformMap,
            rechargeStatusMap,
            rechargeResultDialog: false,
            rechargeResultData: {}
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        beforeGetData() {
            return {
                search: this.search,
                query: { del: false, ...this.formData }
            };
        },
        afterGetData(res) {
            this.totalAmount = res.totalAmount;
        },
        checkRechargeResult(orderNo) {
            this.fetchingData = true;
            this.$http
                .post('/rechargeOrder/checkRechargeResult', { orderNo: orderNo })
                .then(res => {
                    this.fetchingData = false;
                    this.rechargeResultDialog = true;
                    this.rechargeResultData = res;
                    this.initData();
                })
                .catch(e => {
                    this.fetchingData = false;
                    this.$message.error(e.error);
                });
        }
    }
};
</script>
<style lang="less" scoped>
.tool-wrapper {
    display: block;
    overflow: hidden;

    .tool-right {
        float: left;
    }
}

.desc-wrapper {
    padding: 0px;
    overflow: hidden;
    .desc-line {
        margin-bottom: 10px;
        float: left;
        width: 50%;
        height: 30px;
        display: flex;
        .desc-label {
            font-weight: 800;
            width: 140px;
        }

        .desc-label:after {
            content: ':';
        }

        .desc-content {
            padding-left: 10px;
        }
    }
}
</style>
