<template>
    <div>
        <div class="HeadLine">
            <div class="HeadSquare"></div>
            <div class="TitleFont">文章管理</div>
        </div>
        <div style="height:50px;width:100%;"></div>

        <el-card  shadow="always" class="card_style" >
            <el-table :data="List" :header-row-style="{height:'20px'}"  :cell-style="{padding:'2px'}" border @row-dblclick="toDetailPage" style=";cursor: pointer">
                <el-table-column label="文章id" key="articleId" prop="articleId" width="100" align="center" />
                <el-table-column label="文章标题" key="articleTitle" prop="articleTitle" width="200" align="center" :show-overflow-tooltip="true"/>
                <el-table-column label="发布者id" key="userId" prop="userId" width="100" align="center" />
                <el-table-column label="阅读量" key="readCount" prop="readCount" width="100" align="center" :show-overflow-tooltip="true"/>
                <el-table-column label="发布时间" key="publishTime" prop="publishTime" width="100" align="center" />
                <el-table-column label="文章图标" key="articleIco" prop="articleIco" width="150" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-image :src="scope.row.articleIco"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="文章内容" key="articleContent" prop="articleContent" width="250" align="center">
                    <template slot-scope="scope">
                        <el-tooltip  placement="top" width="150"
                                     trigger="hover"
                                     effect="dark" close-delay="2000">
                            <div class="MyPopover" slot="content">{{scope.row.articleContent}}</div>
                            <div>
                                <span>{{ scope.row.articleContent.substr(0,12)}}</span>
                                <span v-if="(scope.row.articleContent).length > 12">...</span>
                            </div>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="是否有效" key="isValid" prop="isValid"  align="center">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.isValid"
                            :active-value="1"
                            :inactive-value="0"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            @change="handleChangeStatus($event,scope.row.articleId)"
                        >
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column width="150" label="操作" fixed="right" align="center">
                    <template slot-scope="scope">
                        <div class="ButtonBlock">
                            <img class="MyButton" @click="handleEdit(scope.row)"
                                 src="../../../assets/svg/EditButton.svg" style="width:60px">
                            <img class="MyButton" @click="handleDelete(scope.row)"
                                 src="../../../assets/svg/DeleteButton.svg" style="width:60px">
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>


        <el-dialog title="编辑" :visible.sync="dialogVisible" width="26%"
                   append-to-body
                   :close-on-click-modal="false"
                   :before-close="closeDialog"
        >
            <el-form ref="ruleForm" class="login_container" :model="form" status-icon  label-width="100px">
                <el-form-item label="文章ID" prop="articleId">
                    <el-input v-model="form.articleId"  placeholder='ID' clearable disabled></el-input>
                </el-form-item>
                <el-form-item label="发布用户ID" prop="userId">
                    <el-input v-model="form.userId"  disabled></el-input>
                </el-form-item>
                <el-form-item label="文章标题" prop="articleTitle" >
                    <el-input v-model="form.articleTitle"  placeholder='标题' clearable :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="文章内容" prop="articleContent" >
                    <el-input v-model="form.articleContent"
                              type="textarea"
                              rows="10"
                              placeholder='文章内容'
                              :disabled="isEdit"
                              clearable></el-input>
                </el-form-item>
                <el-form-item label="阅读量" prop="readCount" >
                    <el-input v-model="form.readCount"  placeholder='阅读量' :disabled="isEdit" clearable></el-input>
                </el-form-item>
                <el-form-item label="文章图标" prop="articleIco" >
                    <el-input v-model="form.articleIco"  placeholder='图标' :disabled="isEdit" clearable></el-input>
                </el-form-item>
                <el-form-item label="是否有效" prop="isValid">
                    <el-radio-group v-model="form.isValid" :disabled="isEdit">
                        <el-radio :label="1">有效</el-radio>
                        <el-radio :label="0">无效</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item>
                    <div style="display: flex;flex-direction: row;align-items: center;flex-wrap: nowrap;justify-content: space-between">
                        <el-button @click="editForm()">编辑</el-button>
                        <el-button @click="submit()" type="primary" style=";margin-top:10px">提交</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>

    </div>
</template>

<script>
import axios from "axios";
import Vue from "vue";

export default {
    name: "articleManage",
    data(){
        return{
            isEdit:true,
            List:[],
            dialogVisible:false,
            form:{
                articleId:'',
                userId: '',
                articleContent: '',
                articleTitle: '',
                articleIco: '',
                readCount: '',
                isValid: '',
            }
        }
    },
    mounted() {
        this.GetData();
    },
    methods:{
        handleChangeStatus($event, id){
            if ($event === 1) { // 这里判断一下
                // 启用
                this.$confirm('确认启用吗？', '操作确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        axios({
                            method: 'post',
                            // 请求的地址
                            url: 'http://localhost:8082/helloGithub_war_exploded/updateisValid_article',
                            // URL 中的查询参数
                            params: {
                                articleId:id,
                                isValid:$event
                            }
                        }).then(function (res) {
                            if (res.data.code === 200) {
                                Vue.prototype.$notify({
                                    title: '成功',
                                    message: ('i', {style: 'color: teal'}, res.data.msg),
                                    type: 'success',
                                    offset: 50
                                });
                            } else {
                                Vue.prototype.$notify.error({
                                    title: '错误',
                                    message: res.data.msg,
                                    offset: 50
                                });
                            }
                        })
                    })
                    .catch((error)=>{
                        Vue.prototype.$notify.error({
                            title: '错误',
                            message: error,
                            offset: 50
                        });
                    })
            } else {
                // 禁用
                this.$confirm('确认禁用吗？', '操作确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        axios({
                            method: 'post',
                            // 请求的地址
                            url: 'http://localhost:8082/helloGithub_war_exploded/updateisValid_article',
                            // URL 中的查询参数
                            params: {
                                articleId:id,
                                isValid:$event
                            }
                        }).then(function (res) {
                            if (res.data.code === 200) {
                                Vue.prototype.$notify({
                                    title: '成功',
                                    message: ('i', {style: 'color: teal'}, res.data.msg),
                                    type: 'success',
                                    offset: 50
                                });
                            } else {
                                Vue.prototype.$notify.error({
                                    title: '错误',
                                    message: res.data.msg,
                                    offset: 50
                                });
                            }
                        })
                    })
                    .catch((error)=>{
                        Vue.prototype.$notify.error({
                            title: '错误',
                            message: error,
                            offset: 50
                        });
                    })
            }
        },
        GetData(){
            var that = this;
            axios({
                method:'post',
                url:'http://localhost:8082/helloGithub_war_exploded/selectAllArticleByadmin',
                params:{
                }
            }).then((res)=>{
                that.List = res.data;
            }).catch((error)=>{
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: error,
                    offset: 50
                });
            })
            // /selectAllArticleByadmin
        },
        closeDialog() {
            // 先重置
            this.GetData();
            // 后关闭
            this.dialogVisible = false;
            this.isEdit=true;
        },
        editForm(){
            this.isEdit=false;
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        openDialog(){
            this.dialogVisible=true;
        },
        handleEdit(index) {
            this.openDialog();
            // 深拷贝
            this.form = index;
        },
        handleDelete(index) {
            var that=this;
            this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios({
                    method: 'post',
                    // 请求的地址
                    url: 'http://localhost:8082/helloGithub_war_exploded/deleteArticleByAdmin',
                    // URL 中的查询参数
                    params: {
                        articleId: index.articleId,
                    }
                }).then(function (res) {
                    if (res.data.code === 500) {
                        Vue.prototype.$notify.error({
                            title: '错误',
                            message: res.data.msg,
                            offset: 50
                        });
                    } else {
                        Vue.prototype.$notify({
                            title: '成功',
                            message: ('i', {style: 'color: teal'}, "删除成功"),
                            type: 'success',
                            offset: 50
                        });
                        that.GetData();
                    }

                });
            }).catch(() => {
                // 点击取消:不删除了
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        submit() {
            var _this = this;
            //调用方法提交
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/updateArticleByAdmin',
                // URL 中的查询参数
                params: {
                    articleId: this.form.articleId,
                    userId: this.form.userId,
                    articleContent: this.form.articleContent,
                    articleTitle: this.form.articleTitle,
                    articleIco: this.form.articleIco,
                    readCount: this.form.readCount,
                    isValid: this.form.isValid,
                }
            }).then(function (res) {
                if (res.data.code === 500) {
                    Vue.prototype.$notify.error({
                        title: '错误',
                        message: res.data.msg,
                        offset: 50
                    });

                } else {
                    _this.isEdit=true;
                    _this.dialogVisible = false;
                    _this.GetData();
                    Vue.prototype.$notify({
                        title: '成功',
                        message: ('i', {style: 'color: teal'}, "更新成功！"),
                        type: 'success',
                        offset: 50
                    });
                }
            }).catch((error)=>{
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: error,
                    offset: 50
                });
            })
        },
        toDetailPage(row){
            if(row.articleId){
                this.$router.push({
                    path:'/ArticleDetail',
                    query: {
                        id: row.articleId,
                        refresh: true
                    }})
            }else {
                return false;
            }

        }


    }
}
</script>
<style>
.MyPopover{
    max-width:200px;
    max-height:100px;
    overflow: auto;
}
.MyPopover::-webkit-scrollbar {
    width: 10px;
    background-color: #ffce9a;
    border-radius: 5px;
}
.MyPopover::-webkit-scrollbar-thumb {
    background-color: #676767;
    border-radius: 5px;
}
</style>


<style scoped lang="scss">
.formData{
    border:3px solid gray;
}

.el-tooltip__popper{
    effect:"light";
    max-width: 250px;
//overflow: clip;
}
</style>