<template>
    <div class='container'>
        <div class='handle-box'>
            <el-form :inline='true'>
                <el-form-item>
                    <el-input
                        v-model='searchForm.title'
                        placeholder='请输入公告标题'
                        clearable>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button @click='loadData'>搜索</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type='primary' @click='dialogVisible = true' v-if="hasAuth('sys:user:save')">新增
                    </el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
            ref='noticeTable'
            :data='tableData'
            tooltip-effect='dark'
            style='width: 100%'
            border
            stripe>

            <el-table-column
                type='index'
                label='序号'
                width='50'>
            </el-table-column>

            <el-table-column
                prop='title'
                label='公告标题'
                :show-overflow-tooltip='true'>
            </el-table-column>

            <el-table-column
                prop='content'
                label='公告内容'
                width='500'
                :show-overflow-tooltip='true'>
                <template slot-scope='scope'>
                    <span v-html='scope.row.content'></span>
                </template>
            </el-table-column>

            <el-table-column
                prop='createTime'
                label='创建时间'>
            </el-table-column>

            <el-table-column
                prop='icon'
                width='180px'
                fixed='right'
                label='操作'>
                <template slot-scope='scope'>
                    <el-button type='text' @click='editHandle(scope.row)'>编辑</el-button>
                    <el-divider direction='vertical'></el-divider>
                    <template>
                        <el-popconfirm title='确定删除这条数据吗？' @confirm='delHandle(scope.row.id)'>
                            <el-button type='text' slot='reference'>删除</el-button>
                        </el-popconfirm>
                    </template>
                </template>
            </el-table-column>

        </el-table>

        <el-pagination
            @size-change='handleSizeChange'
            @current-change='handleCurrentChange'
            layout='total, sizes, prev, pager, next, jumper'
            :page-sizes='[10, 20, 50, 100]'
            :current-page='current'
            :page-size='size'
            :total='total'>
        </el-pagination>


        <!--新增对话框-->
        <el-dialog
            title='提示'
            :visible.sync='dialogVisible'
            width='60%'
            :before-close='handleClose'>

            <el-form :model='editForm' :rules='editFormRules' ref='editForm'>
                <el-row>
                    <el-col :span='12'>
                        <el-form-item label='公告标题:' prop='title' label-width='100px'>
                            <el-input v-model='editForm.title' autocomplete='off' placeholder='请输入公告标题'></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span='12'>

                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span='24'>
                        <el-form-item label='公告内容:' prop='content' label-width='100px'>
                            <Toolbar
                                style='border-bottom: 1px solid #ccc'
                                :editor='editor'
                                :defaultConfig='toolbarConfig'
                            />
                            <!-- 编辑器 -->
                            <Editor
                                style='height: 300px; overflow-y: hidden;'
                                :defaultConfig='editorConfig'
                                v-model='editForm.content'
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot='footer' class='dialog-footer'>
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type='primary' @click="submitForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

export default {
    name: 'noticeManage',
    components: {
        Toolbar, Editor
    },
    data() {
        return {
            editor: null,
            html: '<p>hello&nbsp;world</p>',
            toolbarConfig: {
                 //toolbarKeys: [],
                // excludeKeys: [ /* 隐藏哪些菜单 */ ],
            },
            editorConfig: {
                placeholder: '请输入内容...',
                MENU_CONF: {}
            },
            searchForm: {
                title: ''
            },
            delBtlStatu: true,
            total: 0,
            size: 10,
            current: 1,
            dialogVisible: false,
            editForm: {
                title: '',
                content: ''
            },
            tableData: [],
            editFormRules: {
                title: [
                    { required: true, message: '请输入公告标题', trigger: 'blur' }
                ],
                content: [
                    { required: true, message: '请输入公告内容', trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.loadData();
    },
    methods: {
        loadData() {
            this.$axios.get('/api/notice/list', {
                params: {
                    title: this.searchForm.title,
                    current: this.current,
                    size: this.size
                }
            }).then(res => {
                this.tableData = res.data.data.records;
                this.size = res.data.data.size;
                this.current = res.data.data.current;
                this.total = res.data.data.total;
            });
        },
        handleClose() {
            this.resetForm('editForm');
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
            this.editForm = {};
        },
        submitForm(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.$axios.post('/api/notice/' + (_this.editForm.id ? 'updateBean' : 'addBean'), _this.editForm)
                        .then(res => {
                            _this.loadData();
                            _this.$notify({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success'
                            });
                            _this.resetForm(formName);
                        });
                } else {
                    return false;
                }
            });
        },
        editHandle(row) {
            this.editForm = {
                id: row.id,
                title: row.title,
                content: row.content
            };
            this.dialogVisible = true;
        },
        delHandle(id) {
            this.$axios.post('/api/notice/deleteById?id=' + id).then(res => {
                this.loadData();
                this.$notify({
                    showClose: true,
                    message: '恭喜你，操作成功',
                    type: 'success'
                });
            });
        },
        handleSizeChange(val) {
            this.size = val;
            this.loadData();
        },
        handleCurrentChange(val) {
            this.current = val;
            this.loadData();
        }
    }
};
</script>

<style src='@wangeditor/editor/dist/css/style.css'></style>
<style scoped>

</style>
