<template>
    <div class="list-view">
        <page-title>
            <el-button
                @click="addRow"
                type="primary"
                icon="el-icon-plus"
                :disabled="fetchingData || downloading"
                class="filter-item"
            >
                新增
            </el-button>
            <el-button
                @click="download('/adapayMerchant/excel')"
                icon="el-icon-upload2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                导出
            </el-button>
        </page-title>
        <div class="filters-container">
            <el-button class="filter-item" @click="showQueryDialog = true">查询</el-button>
            <el-button class="filter-item" @click="showRefundDialog = true">退款</el-button>
            <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>
        <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="name" label="name"> </el-table-column>
            <el-table-column prop="appId" label="appId" width="330"> </el-table-column>
            <el-table-column prop="status" label="status"> </el-table-column>
            <el-table-column prop="selected" label="selected" align="center">
                <template v-slot="{ row }">
                    <el-tag :type="row.selected ? 'success' : 'info'">{{ row.selected ? '是' : '否' }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="right" fixed="right" width="150">
                <template v-slot="{ row }">
                    <el-button @click="select(row)" type="primary" size="mini" plain v-if="!row.selected"
                        >切换</el-button
                    >
                    <el-button @click="editRow(row)" type="primary" size="mini" plain>编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <!-- <div class="multiple-mode-wrapper">
                <el-button v-if="!multipleMode" @click="toggleMultipleMode(true)">批量编辑</el-button>
                <el-button-group v-else>
                    <el-button @click="operation1">批量操作1</el-button>
                    <el-button @click="operation2">批量操作2</el-button>
                    <el-button @click="toggleMultipleMode(false)">取消</el-button>
                </el-button-group>
            </div> -->
            <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="showQueryDialog" width="650px">
            <el-form label-position="right" label-width="100px">
                <el-form-item label="商户">
                    <el-select v-model="merchantId">
                        <el-option
                            v-for="item in tableData"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="订单流水号">
                    <el-input v-model="orderId"></el-input>
                </el-form-item>
                <el-form-item v-if="queryResult" label="结果">
                    <pre v-html="queryResult" style="line-height: 1.5"></pre>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="query" :loading="querying">查询</el-button>
            </div>
        </el-dialog>

        <el-dialog title="订单退款" :visible.sync="showRefundDialog" width="650px">
            <el-form label-position="right" label-width="100px">
                <el-form-item label="商户">
                    <el-select v-model="merchantId">
                        <el-option
                            v-for="item in tableData"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="订单流水号">
                    <el-input v-model="orderId"></el-input>
                </el-form-item>
                <el-form-item v-if="refundResult" label="结果">
                    <pre v-html="refundResult" style="line-height: 1.5"></pre>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="refund" :loading="querying">退款</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'AdapayMerchantList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/adapayMerchant/all',
            downloading: false,
            merchantId: null,
            orderId: null,
            showQueryDialog: false,
            showRefundDialog: false,
            querying: false,
            queryResult: null,
            refundResult: null
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        beforeGetData() {
            return { search: this.search, query: { del: false } };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/adapayMerchantEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.$router.push({
                path: '/adapayMerchantEdit',
                query: {
                    id: row.id
                }
            });
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/adapayMerchant/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        select(row) {
            this.$alert('确认切换？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/adapayMerchant/select`, { id: row.id });
                })
                .then(() => {
                    this.$message.success('切换成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        query() {
            this.querying = true;
            this.$http
                .post('/adapayMerchant/query', {
                    id: this.merchantId,
                    orderId: this.orderId
                })
                .then(res => {
                    this.querying = false;
                    console.log(res);
                    this.queryResult = JSON.stringify(res, null, 4);
                })
                .catch(e => {
                    this.querying = false;
                    this.$message.error(e.error || '失败');
                });
        },
        refund() {
            this.$alert('确认退款？', '警告', { type: 'error' })
                .then(() => {
                    this.querying = true;
                    this.$http
                        .post('/adapayMerchant/refund', {
                            id: this.merchantId,
                            orderId: this.orderId
                        })
                        .then(res => {
                            this.querying = false;
                            console.log(res);
                            this.refundResult = JSON.stringify(res, null, 4);
                        })
                        .catch(e => {
                            this.querying = false;
                            this.$message.error(e.error || '失败');
                        });
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
