<template>
    <div class="mainBox">
        <!--搜索功能 start------------------------------------------------------------>
        <div class="layerBox_search">
            <div class="layerBoxLine">
                <el-row>
                    <el-col :span="8">
                        <div class="searchBox">
                            <div class="searchTitle">留言标题：</div>
                            <el-input placeholder="请输入留言标题" autocomplete="off" v-model="getApiData.title" />
                        </div>
                    </el-col>
                    <el-col :span="15">
                        <div class="tag_webSite_head_btn_box">
                            <div class="searchTitle"><span></span></div>
                            <el-button type="primary" @click="getData('search')">搜索</el-button>
                            <el-button type="info" @click="clearSearchList">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </div>
        </div>
        <div class="layerBoxNoBg">
            <div>
                <el-button type="primary" @click="gotoCreat">发布留言</el-button>
            </div>
        </div>
        <!--搜索功能 end------------------------------------------------------------>
        <!--表格内容 start------------------------------------------------------------>
        <div class="layerBox">
            <tableTitle :name="tableDivTitle" />
            <el-row>
                <template>
                    <el-table :data="tableData" style="width: 100%">
                        <el-table-column fixed prop="id" label="编号" width="50"></el-table-column>
                        <el-table-column prop="title" label="留言标题" width="240"></el-table-column>
                        <el-table-column prop="created_at" label="创建时间" width="240"></el-table-column>
                        <el-table-column prop="updated_at" label="修改时间"></el-table-column>
                        <el-table-column prop="status" label="状态">
                            <template slot-scope="scope">
                                <span v-if="scope.row.status == 0" class="status0">待审核</span>
                                <span v-if="scope.row.status == 1" class="status1">已通过</span>
                                <span v-if="scope.row.status == 2" class="status2">已拒绝</span>
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="350" header-align="center">
                            <template slot-scope="scope">
                                <div class="listBtnBox">
                                    <div class="listDeleteBtn" @click="deleteData(scope.row.id)">
                                        <i class="el-icon-delete"></i>删除
                                    </div>
                                    <div class="listUpBtn" @click="upData(scope.row.id)">
                                        <i class="el-icon-refresh-left"></i>撤回 
                                    </div>
                                    <!--非管理员且该留言已通过申请，则无法再次编辑-->
                                    <div class="listMainBtn" @click="getDataMain(scope.row.id)" v-if="creatNews_user_type != 10000 && scope.row.is_update == 0">
                                        <i class="el-icon-view"></i>查看
                                    </div>
                                    <div class="listEditBtn" @click="gotoEdit(scope.row.id)" v-if="creatNews_user_type != 10000 && scope.row.is_update == 1">
                                        <i class="el-icon-edit-outline"></i>编辑
                                    </div>
                                    <div class="listEditBtn" @click="gotoEdit(scope.row.id)" v-if="creatNews_user_type == 10000">
                                        <i class="el-icon-edit-outline"></i>编辑
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-row>
        </div>
        <div class="alignBox">
            <el-row>
                <el-col :span="24">
                    <el-pagination :current-page="getApiData.page" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" :page-size="10" layout="total, prev, pager, next, jumper"
                        :total="allCount"></el-pagination>
                </el-col>
            </el-row>
        </div>
        <!--表格内容 end------------------------------------------------------------>
    </div>
</template>

<script>
//获得用户身份
import { getWebSiteId, getUseType } from '@/utils/auth'
//表格标题
import tableTitle from './components/tableTitle';
//引入公用样式
import '@/styles/global.less';

export default {
    components: {
        tableTitle,//表格标题
    },
    data() {
        //0.全局操作 start ------------------------------------------------------------>
        let self = this;
        //0.全局操作 end ------------------------------------------------------------>
        return {
            //1.列表和分页相关 start ------------------------------------------------------------>
            tableDivTitle: "留言列表",
            tableData: [],//内容
            getApiData: {
                website_id:0,//站点id
                is_master: 1,//是否为审核列表 0=待审核列表 1=已审核列表
                title: "",//留言标题
                page: 1,//当前是第几页
                page_size: 10,//一共多少条
            },
            allCount: 0,//总条数
            //分页相关 end ------------------------------------------------------------>
            
            //用户身份
            creatNews_user_type:0
        }
    },
    methods: {
        //1.列表和分页相关 start ------------------------------------------------------------>
        //1.1 开始请求列表信息方法
        getData(type) {
            //如果是搜索，重新加载第一页==搜索
            if (type == "search") {
                this.getApiData.page = 1;
            }
            this.$store.dispatch('news/getMessageList', this.getApiData).then(res => {
                //列表
                this.tableData = res.data.data;
                //页数
                this.allCount = res.data.count;
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '网络错误，请重试！'
                });
            })
        },
        //1.2 删除内容
        deleteData(id) {
            this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                console.log("当前删除：" + id)
                this.$store.dispatch('news/delMessage', {id:id}).then(res => {
                    if(res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    } else {
                        this.$message.error(res.message);
                    }
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消删除操作！'
                });
            });
        },
        //1.3 拒绝消息
        upData(id) {
            //拒绝消息
            this.$confirm('此操作将撤回该条数据至待审核列表, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                console.log("当前撤回：" + id)
                let data = {
                    id:id,
                    status:0,
                    reason:""
                }
                this.$store.dispatch('news/upMessageStatus', data).then(res => {
                    if(res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '撤回成功!'
                        });
                    } else {
                        this.$message.error(res.message);
                    }
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消撤回操作！'
                });
            });
            
        },
        //1.4 列表内容分页
        handleSizeChange(val) {
            this.getApiData.page = val;
            this.getData();
        },
        //1.5 点击分页
        handleCurrentChange(val) {
            this.getApiData.page = val;
            this.getData();
        },
        //1.6 重置按钮
        clearSearchList() {
            this.getApiData.is_master = 1;
            this.getApiData.title = "";
            this.getApiData.page = 1;
            this.getApiData.page_size = 10;
            this.getData();
        },
        //列表和分页相关 end ------------------------------------------------------------>
        //跳转到添加留言页面 start ------------------------------------------------------------>
        //创建
        gotoCreat() {
            this.$router.push({
                path: '/messageCreat',
                query: {
                    type: 'add',
                    page: 'messageList'
                }
            });
        },
        //编辑
        gotoEdit(id) {
            this.$router.push({
                path: '/messageCreat',
                query: {
                    id: id,
                    type: 'edit',
                    page: 'messageList'
                }
            });
        },
        //查看
        getDataMain(id) {
            this.$router.push({
                path: '/messageCreat',
                query: {
                    id: id,
                    type: 'view',
                    page: 'messageList'
                }
            });
        },
        //跳转到添加留言页面 end ------------------------------------------------------------>
    },
    mounted() {
        //获得第一页
        this.getData();
        //1.获得用户身份
        this.creatNews_user_type = getUseType()
        //2.获得站点id
        this.getApiData.website_id = getWebSiteId();
    },
}
</script>

<style scoped lang="less">
.status0 {
    color: #5570F1;
}
.status1 {
    color: #519C66;
}
.status2 {
    color: #CC5F5F;
}
//执行v-deep穿透scope选择器 start------------------------------------------------------------>*/
::v-deep .custom-form-item>.el-form-item__label {
    line-height: 140px !important;
}
::v-deep .custom-textarea .el-textarea__inner {
    resize: none;
    /* 禁止用户拖拽调整大小 */
}
::v-deep .custom-align-right .el-form-item__label {
    text-align: right;
    /* 设置标签文字右对齐 */
}
//执行v-deep穿透scope选择器 end------------------------------------------------------------>*/
.tag_webSite_head_btn_box {
    overflow: hidden;
}
.tag_webSite_head_btn_box .searchTitle {
    opacity: 0;
    span {
        display: block;
        height: 16px;
    }
}
.tag_webSite_head_btn_box button {
    float: right;
}
.tag_webSite_head_btn_box button:nth-of-type(1) {
    margin-left: 10px;
}
</style>
