<template>
  <div>
      <el-table
              border
              stripe
              :data="dataTable"
              style="width: 100%;margin-top: 20px">
          <el-table-column
                  prop="commentId"
                  label="ID"
                  sortable
                  width="50"
                  align="center"
          >
          </el-table-column>
          <el-table-column
                  prop="userId"
                  align="center"
                  width="70"
                  label="用户ID">
          </el-table-column>
          <el-table-column
                  prop="projectId"
                  align="center"
                  show-overflow-tooltip
                  label="项目ID">
          </el-table-column>
          <el-table-column
                  prop="content"
                  align="center"
                  show-overflow-tooltip
                  label="评论内容">
          </el-table-column>
          <el-table-column
                  prop="isUsed"
                  align="center"
                  show-overflow-tooltip
                  label="是否使用过">
          </el-table-column>
          <el-table-column
                  prop="commentTime"
                  align="center"
                  show-overflow-tooltip
                  label="评论时间">
          </el-table-column>
          <el-table-column
                  prop="star"
                  show-overflow-tooltip
                  align="center"
                  label="项目评分">
          </el-table-column>
          <el-table-column
                  prop="likeNum"
                  align="center"
                  label="点赞数">
          </el-table-column>
          <el-table-column
                  prop="commentTime"
                  align="center"
                  label="评论时间">
          </el-table-column>


          <el-table-column
                  prop="isValid"
                  align="center"
                  label="是否有效">
              <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.commentId)"
                  >
                  </el-switch>
              </template>
          </el-table-column>

          <el-table-column
                  align="center"
                  fixed="right"
                  label="操作">
              <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" size="small" circle></el-button>
                  <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" circle size="small"></el-button>
              </template>
          </el-table-column>
      </el-table>


      <el-dialog title="编辑" :visible.sync="dialogVisible" width="45%"
                 append-to-body
                 :close-on-click-modal="false"
                 :before-close="closeDialog"
      >
          <el-form ref="ruleForm" class="login_container" :model="form" status-icon  label-width="80px">
              <el-form-item label="ID" prop="commentId">
                  <el-input v-model="form.commentId"  placeholder='ID' clearable disabled></el-input>
              </el-form-item>
              <el-form-item label="用户名ID" prop="userId">
                  <el-input v-model="form.userId"  placeholder='用户ID' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="项目ID" prop="projectId">
                  <el-input  v-model="form.projectId"
                             :disabled="isEdit"
                             placeholder='项目ID'
                             clearable></el-input>
              </el-form-item>

              <el-form-item label="评论内容" prop="content">
                  <el-input type="textarea" v-model="form.content"  placeholder="评论内容" :rows="5" :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="是否使用过" prop="isUsed">
                  <el-input v-model="form.isUsed"  placeholder='是否使用过' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="评论时间" prop="commentTime">
                  <el-input v-model="form.commentTime"  placeholder='评论时间' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="评分" prop="star">
                  <el-input v-model="form.star"  placeholder='评分' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="点赞数" prop="fileAddress">
                  <el-input v-model="form.likeNum"  placeholder='点赞数' clearable :disabled="isEdit"></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="resetForm('ruleForm')">重置</el-button>
                      <el-button @click="submit('ruleForm')"  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: "commentManage",
    data(){
        return{
            dataTable:[],
            isEdit:true,
            dialogVisible:false,
            form:{
                commentId:'',
                userId:'',
                projectId:'',
                content:'',
                isUsed:'',
                commentTime:'',
                isValid:'',
                star:'',
                likeNum:'',

            }
        }
    },
    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_comment',
                            // URL 中的查询参数
                            params: {
                                commentId: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(() => {})
            } else {
                // 禁用
                this.$confirm('确认禁用吗？', '操作确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        axios({
                            method: 'post',
                            // 请求的地址
                            url: 'http://localhost:8082/helloGithub_war_exploded/updateisValid_comment',
                            // URL 中的查询参数
                            params: {
                                commentId: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(() => {})
            }
        },
        closeDialog() {
            // 先重置
            this.getAllComment();
            // 后关闭
            this.dialogVisible = false;
            this.isEdit=true;
        },
        getAllComment(){
            var that=this;
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/selectComment',
                // URL 中的查询参数
                params: {
                }
            }).then(function (res) {
                that.dataTable=res.data;
            })
        },
        openDialog(){
            this.dialogVisible=true;
        },
        handleEdit(index) {

            this.openDialog();
            // 深拷贝
            this.form = index;
        },
        submit() {
            var _this = this;
            //调用方法提交
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/updateComment',
                // URL 中的查询参数
                params: this.form
            }).then(function (res) {
                if (res.data.code === 500) {
                    Vue.prototype.$notify.error({
                        title: '错误',
                        message: res.data.msg,
                        offset: 50
                    });
                } else {
                    _this.dialogVisible = false;
                    _this.getAllComment();
                    Vue.prototype.$notify({
                        title: '成功',
                        message: ('i', {style: 'color: teal'}, "更新成功！"),
                        type: 'success',
                        offset: 50
                    });
                }
            });
        },
        handleDelete(index) {
            var that=this;
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios({
                    method: 'post',
                    // 请求的地址
                    url: 'http://localhost:8082/helloGithub_war_exploded/deleteComment',
                    // URL 中的查询参数
                    params: {
                        id: index.commentId,
                    }
                }).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.getAllComment();
                    }

                });
            }).catch(() => {
                // 点击取消:不删除了
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
    },
    mounted() {
        this.getAllComment();
    },


}
</script>

<style scoped>

</style>