<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <!-- 搜索栏与表格区域 -->
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form class="table-select-main" :model="searchForm" label-position="left">
                    <el-row :gutter="15">
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="输入敏感词" v-model="searchForm.keyword"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" plain round @click="query">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    icon="el-icon-plus"
                                    @click="create"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="enabled">批量启用</el-button>
                                <el-button type="primary" plain round @click="close">批量关闭</el-button>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    @click="deleteData(ids, 'batch')"
                                >批量删除</el-button>
                                <el-button type="primary" plain round @click="textareaAdd">文本域批量添加</el-button>
                                <SensitiveWordsImport @queryListData="queryListData"></SensitiveWordsImport>
                            </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="keyword" label="敏感词" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="createBy" label="创建人" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column label="创建时间">
                        <template slot-scope="scope">
                            {{
                            scope.row.createTime | formatTimePipe('yyyy-MM-dd')
                            }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="启用状态">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.status"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="switchChange(scope.row.status, scope.row.id)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="success"
                                plain
                                round
                                @click="edit(scope.row)"
                            >编辑</el-button>
                        </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 @queryListData="queryListData" ref="addEditDialog"></AddEditDialog>
        <!--文本域批量添加弹层 -->
        <TextareaDialog @queryListData="queryListData" ref="textareaDialog"></TextareaDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import TextareaDialog from './components/textareaDialog';
import SensitiveWordsImport from './components/importExcel';
import {
    queryPagesensitiveWord,
    deletesensitiveWord,
    editsensitiveWord,
    useBath,
    nouseBath,
} from '@/apps/mobile/api/SensitiveWordLibrary/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
        TextareaDialog,
        SensitiveWordsImport,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                // 页码
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false, // table是否展示loading
            ids: [], // 选中的列
        };
    },
    methods: {
        /**
         * 新增
         */
        create() {
            this.$refs.addEditDialog.open('add');
        },
        /**
         * 编辑
         */
        edit(data) {
            this.$refs.addEditDialog.open('edit', data);
        },
        /**
         * table选择列被改变时
         * @param {Array} data 选中的列
         */
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
            console.log(this.ids);
        },
        // PageComponent变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        // 分页获取数据
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'createTime',
                },
            };
            queryPagesensitiveWord(obj)
                .then(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;
                });
        },
        delete(id) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    deletesensitiveWord(id)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(err => err);
                })
                .catch(() => {});
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            if (type === 'batch') {
                console.log(id);
                if (id.length === 0) return this.$message.warning('请至少选中一项 !');
                this.delete({ids: id});
            } else {
                this.delete({ids: [id]});
            }
        },
        // 启用状态改变时
        switchChange(val, id) {
            console.log(val, id);
            let obj = {
                status: val,
                id,
            };
            editsensitiveWord(obj)
                .then(() => {
                    this.$message.success('修改成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 查询
        query() {
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '' || this.searchForm[k] === null || this.searchForm[k] === undefined) {
                    delete this.searchForm[k];
                }
            }
            this.page.pageNum = 1;
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.page.pageNum = 1;
            this.queryListData();
        },
        // 批量启用
        enabled() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            useBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量关闭
        close() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            nouseBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        textareaAdd() {
            this.$refs.textareaDialog.open();
        },
    },
    created() {
        this.queryListData();
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
