<template>
  <div>
      <div class="HeadLine">
          <div class="HeadSquare"></div>
          <div class="TitleFont">标签管理</div>
      </div>
      <div style="height:50px;width:100%;"></div>
      <div style="display: flex;flex-direction: row;justify-content: flex-start">
          <el-button type="primary" icon="el-icon-plus" @click="openAddCategoryFun">新增</el-button>
      </div>

      <el-card  style="width:1000px;margin-left:200px;" shadow="always" class="card_style">
          <div style="width:900px;margin-left:30px;">
              <el-table width="400"  :data="List" :header-row-style="{height:'20px'}"  :cell-style="{padding:'2px'}" border style=";cursor: pointer">
                  <el-table-column label="标签id" key="categoryId" prop="categoryId" width="100" align="center" />
                  <el-table-column label="标签名" key="categoryName" prop="categoryName" width="200" align="center" :show-overflow-tooltip="true"/>
                  <el-table-column label="标签图标" key="category" prop="category" align="center" :show-overflow-tooltip="true">
                      <template slot-scope="scope">
                          <span v-html="scope.row.category"></span>
                      </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.categoryId)"
                          >
                          </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-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="标签名" prop="categoryName">
                          <el-input v-model="form.categoryName"  placeholder='标签名' :disabled="isEdit" clearable></el-input>
                      </el-form-item>
                      <el-form-item label="标签id" prop="categoryId">
                          <el-input v-model="form.categoryId" placeholder='ID' :disabled="isEdit" ></el-input>
                      </el-form-item>
                      <el-form-item label="标签图标" prop="category" >
                          <el-input v-model="form.category"  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="editForm()">编辑</el-button>
                              <el-button @click="submit('ruleForm')" type="primary" style=";margin-top:10px">提交</el-button>
                          </div>
                      </el-form-item>
                  </el-form>
              </el-dialog>



              <el-dialog title="添加标签" :visible.sync="openAddCategory" 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="标签名" prop="categoryName">
                          <el-input v-model="form.categoryName"  placeholder='标签名' :disabled="isEdit" clearable></el-input>
                      </el-form-item>
                      <el-form-item label="标签图标" prop="category" >
                          <el-input v-model="form.category"  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="editForm()">编辑</el-button>
                              <el-button @click="addCategory()" type="primary" style=";margin-top:10px">添加</el-button>
                          </div>
                      </el-form-item>
                  </el-form>
              </el-dialog>



          </div>
      </el-card>



  </div>
</template>

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

export default {
    name: "categoryManage",
    data(){
        return{
            isEdit:true,
            openAddCategory:false,
            List:[],
            dialogVisible:false,
            form:{
                categoryId:'',
                category:'',
                categoryName:'',
                isValid:'',
            }
        }
    },
    mounted() {
        this.GetData();
    },
    methods:{
        openAddCategoryFun(){
          this.openAddCategory=true;
        },
        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_category',
                            // URL 中的查询参数
                            params: {
                                categoryId: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_category',
                            // URL 中的查询参数
                            params: {
                                categoryId: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/selectAllCategoryByAdmin',
                params:{
                }
            }).then(function(res){
                that.List = res.data;
            }).catch((error)=>{
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: error,
                    offset: 50
                });
            })
            // /selectAllArticleByadmin
        },
        closeDialog() {
            // 先重置
            this.GetData();
            // 后关闭
            this.dialogVisible = false;
            this.openAddCategory=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/deleteCategoryByAdmin',
                    // URL 中的查询参数
                    params: {
                        categoryId: index.categoryId,
                    }
                }).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/updateCategoryByAdmin',
                // URL 中的查询参数
                params: {
                    articleId: this.form.categoryId,
                    category: this.form.category,
                    isValid: this.form.isValid,
                    categoryName: this.form.categoryName,
                }
            }).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
                });
            })
        },
        addCategory(){
            var _this = this;
            //调用方法提交
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/insertCategory',
                // URL 中的查询参数
                params: {
                    url: this.form.category,
                    isValid: this.form.isValid,
                    name: this.form.categoryName,
                }
            }).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
                });
            })
        }
    }
}

</script>

<style scoped>

</style>