<template>
    <div class="messageList flex-display">
        <div class="head">
            <div class="action">
                <el-button plain type="success" icon="el-icon-circle-plus-outline" @click="dialog.visible=true">新增</el-button>
                <el-button plain type="warning" icon="el-icon-edit" @click="updateMessage">确认变更</el-button>
            </div>
        </div>

        <div class="list">
            <el-table :data="messageList" border stripe :fit="false" height="100%">
                <el-table-column label="消息类型" prop="msgType" width="150" show-overflow-tooltip/>
                <el-table-column label="文本消息内容" prop="content" width="150" show-overflow-tooltip/>

                <el-table-column label="图片消息id" prop="mediaId" width="150" show-overflow-tooltip/>

                <el-table-column label="图文消息标题" prop="title" width="150" show-overflow-tooltip/>
                <el-table-column label="图文消息描述" prop="description" width="150" show-overflow-tooltip/>
                <el-table-column label="图文消息图片地址" prop="picUrl" width="150" show-overflow-tooltip/>
                <el-table-column label="图文消息地址" prop="url" width="150" show-overflow-tooltip/>
                <el-table-column label="关键字" prop="hinge" width="150" show-overflow-tooltip/>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <div class="headrow">
                            <el-button size="mini" @click="handelMessage(scope.row.id)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handelDelete(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <el-dialog :title="dialog.title" :visible.sync="dialog.visible" center @close="handelCancel">
            <div class="content">

                <el-form label-width="100px" :model="messageDto" ref="buttonForm">

                    <el-form-item label="消息类型:" prop="msgType">
                        <template>
                            <el-select v-model="messageDto.msgType" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </template>
                    </el-form-item>
                    <el-form-item label="文本消息内容:" prop="content" v-show="messageDto.msgType == 'text'">
                        <el-input
                                type="textarea"
                                :autosize="{ minRows: 2, maxRows: 4}"
                                placeholder="请输入内容"
                                v-model="messageDto.content">
                        </el-input>
                    </el-form-item>

                    <el-form-item label="图片id:" prop="mediaId" v-show="messageDto.msgType == 'image'">
                        <el-input v-model="messageDto.mediaId" placeholder="请输入关键字"></el-input>
                    </el-form-item>

                    <el-form-item label="图文标题:" prop="title" v-show="messageDto.msgType == 'news'">
                        <el-input v-model="messageDto.title" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="图文描述:" prop="description" v-show="messageDto.msgType == 'news'">
                        <el-input v-model="messageDto.description" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="图片地址:" prop="picUrl" v-show="messageDto.msgType == 'news'">
                        <el-input v-model="messageDto.picUrl" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="图文地址:" prop="url" v-show="messageDto.msgType == 'news'">
                        <el-input v-model="messageDto.url" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="关键字:" prop="hinge" >
                        <el-input v-model="messageDto.hinge" placeholder="请输入关键字"></el-input>
                    </el-form-item>
                </el-form>

            </div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="handelCancel">取 消</el-button>
                <el-button type="primary" @click="handelClickConform">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import messageList from "@/api/messageList"
    export default {
        name: "messageList",
        data(){
            return{
                messageList:[],
                isUpdate:false,
                dialog:{
                    title:'新增',
                    visible:false
                },
                messageDto:{},
                options:[{value:'text',label:'文本消息'},
                    {value:'news',label:'图文消息'},
                        {value:'image',label:'图片消息'}],
            }
        },
        mounted(){
            this.getMessageList()
        },
        methods:{
            getMessageList(){
                messageList.getMessageList().then(res =>{
                    this.messageList=res
                    console.log(res)
                })
            },
            refresh(){
                this.getMessageList();
                this.handelCancel();
            },
            handelCancel() {
                this.isUpdate=false;
                this.dialog.visible=false;
                this.messageDto={}
            },
            handelDelete(id){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    messageList.deleteMessage(id).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.refresh();
                    });

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handelMessage(id){
                this.isUpdate=true;
                messageList.getMessageById(id).then(res => {
                    this.messageDto = res
                    this.dialog.visible=true;
                })

            },
            handelClickConform(){

                this.isUpdate ? messageList.updateMessage(this.messageDto).then(this.refresh) :
                    messageList.addMessage(this.messageDto).then(this.refresh)
            },
            updateMessage(){
                messageList.refreshMessage().then(this.refresh)
            }

        }
    }
</script>

<style lang="less" scoped>
    .messageList{

        /deep/ .el-dialog {
            width: 500px;
        }
    }
</style>
