<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form class="table-select-main" label-position="left">
                    <el-row :gutter="15">
                        <!-- <el-col :span="4">
              <el-form-item>
                <el-button type="primary" round>查询</el-button>
                <el-button type="info" plain round>重置</el-button>
              </el-form-item>
                        </el-col>-->
                        <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    round
                                    icon="el-icon-plus"
                                    @click="showAddEditDialog(null)"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="deleteData(null)">批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{ ids.length }}</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectionChange"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <el-table-column prop="name" label="会费名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="period" label="有效期(月)" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="price" label="价格"></el-table-column>
                    <el-table-column prop="userLevelName" label="用户等级"></el-table-column>
                    <el-table-column prop="examineFlag" label="审核状态">
                        <template slot-scope="scope">
                            <span class="warn" v-show="scope.row.examineFlag == 0">待审核</span>
                            <span class="success" v-show="scope.row.examineFlag == 1">审核通过</span>
                            <RejectReason
                                :content="scope.row.notExamineReason"
                                v-show="scope.row.examineFlag == 2"
                            ></RejectReason>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createTime">
                        <template slot-scope="scope">{{ scope.row.createTime | formatTimePipe }}</template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="120px;">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                :disabled="scope.row.examineFlag === 1"
                                @click="showAddEditDialog(scope.row)"
                            >编辑</el-button>
                            <slot></slot>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <!-- 添加与编辑弹层 -->
        <AddEditDialog ref="addEditDialog" @queryListData="queryListData"></AddEditDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import { queryMemberFeeList, deleteMemberFeeById, deleteBatchMemberFee } from '@/apps/mobile/api/memberFee/index';
import RejectReason from '@/apps/mobile/components/ReasonForRejection/index';
import AddEditDialog from './components/addEditDialog';
export default {
    components: {
        TitleBar,
        RejectReason,
        AddEditDialog,
    },
    data() {
        return {
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false,
            ids: [],
        };
    },
    created() {
        this.queryListData();
    },
    methods: {
        queryListData() {
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryMemberFeeList(obj)
                .then(res => {
                    console.log(res);
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        showAddEditDialog(data) {
            if (data) {
                this.$refs.addEditDialog.open('edit', data);
            } else {
                this.$refs.addEditDialog.open('add', null);
            }
        },
        deleteData(data) {
            if (data) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteMemberFeeById({id: data.id})
                            .then(res => {
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            } else {
                if (this.ids.length === 0) {
                    this.$message({
                        type: 'warning',
                        message: '请先选择一项！',
                    });
                    return false;
                }
                deleteBatchMemberFee({ids: this.ids})
                    .then(res => {
                        this.queryListData();
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                        });
                    })
                    .catch();
            }
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
