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

      <el-card  shadow="always" class="card_style">
          <el-table
              @row-dblclick="toDetailPage"
                  :header-row-style="{height:'20px'}"  :cell-style="{padding:'2px'}" border
                  :data="List"
                  style="width: 100%;margin-top: 20px;cursor: pointer">
              <el-table-column
                      prop="id"
                      label="ID"
                      sortable
                      align="center"
              >
              </el-table-column>
              <el-table-column
                      prop="userId"
                      align="center"
                      label="用户ID"
              >
              </el-table-column>
              <el-table-column
                  prop="oneFileName"
                  width="120"
                  :show-overflow-tooltip="true"
                  align="center"
                  label="文件名">
              </el-table-column>
              <el-table-column
                      prop="language"
                      align="center"
                      label="语言种类">
              </el-table-column>
              <el-table-column
                      prop="content"
                      width="150"
                      align="center"
                      label="内容"
                      :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                      <el-tooltip  placement="top" width="150"
                                   trigger="hover"
                                   effect="dark" close-delay="2000">
                          <div class="MyPopover" slot="content">{{scope.row.content}}</div>
                          <div>
                              <span>{{ scope.row.content.substr(0,12)}}</span>
                              <span v-if="(scope.row.content).length > 12">...</span>
                          </div>
                      </el-tooltip>
                  </template>
              </el-table-column>
              <el-table-column
                      prop="url"
                      width="150"
                      align="center"
                      label="URL路径"
                      :show-overflow-tooltip="true"
              >
              </el-table-column>
              <el-table-column
                      prop="description"
                      width="120"
                      :show-overflow-tooltip="true"
                      align="center"
                      label="描述">
              </el-table-column>
              <el-table-column
                      prop="lookNum"
                      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.id)"
                      >
                      </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="46%"
                 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="id">
                  <el-input v-model="form.id"  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="oneFileName">
                  <el-input v-model="form.oneFileName" :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="URL" prop="url">
                  <el-input v-model="form.url" :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="描述" prop="description" >
                  <el-input v-model="form.description"  placeholder='描述' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="内容" prop="content" >
                  <el-input v-model="form.content"

                            type="textarea"
                            rows="10"
                            placeholder='内容'
                            disabled
                            clearable></el-input>
              </el-form-item>
              <el-form-item label="阅读量" prop="lookNum" >
                  <el-input v-model="form.lookNum"  placeholder='阅读量' :disabled="isEdit" clearable></el-input>
              </el-form-item>
              <el-form-item label="语言种类" prop="articleIco" >
                  <el-input v-model="form.language"  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: "oneFileManage",
    data(){
        return{
            isEdit:true,
            List:[],
            dialogVisible:false,
            form:{
                id:null,
                userId:null,
                lookNum:null,
                oneFileName:'',
                content:'',
                language:'',
                url:'',
                isValid:'',
                description:''
            }
        }
    },
    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_onefile',
                            // URL 中的查询参数
                            params: {
                                id: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_onefile',
                            // URL 中的查询参数
                            params: {
                                id: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/selectAllFileByAdmin',
                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/deleteFileByAdmin',
                    // URL 中的查询参数
                    params: {
                        id: index.id,
                    }
                }).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/updateFileByAdmin',
                // URL 中的查询参数
                params: {
                    id: this.form.id,
                    userId: this.form.userId,
                    lookNum: this.form.lookNum,
                    oneFileName: this.form.oneFileName,
                    // Content: '',
                    language: this.form.language,
                    url: this.form.url,
                    isValid: this.form.isValid,
                    description: this.form.description,
                }
            }).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.id){
                this.$router.push({
                    path:'/oneFileDetail',
                    query: {
                        id: row.id,
                        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">
.el-tooltip__popper{
  effect:"light";
  max-width: 250px;
  //overflow: clip;
}
</style>