<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
              :header-row-style="{height:'20px'}"  :cell-style="{padding:'2px'}" border @row-dblclick="toDetailPage"
              :data="dataTable"
              style="width: 100%;margin-top: 20px;cursor: pointer">
              <el-table-column
                  prop="projectId"
                  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="projectName"
                  align="center"
                  width="100"
                  :show-overflow-tooltip="true"
                  label="项目名称">
              </el-table-column>
              <el-table-column
                  prop="projectUrl"
                  align="center"
                  width="100"
                  show-overflow-tooltip
                  label="项目地址">
              <template slot-scope="scope">
                  <el-link :href="scope.row.projectUrl" :underline="false">{{scope.row.projectUrl}}</el-link>
              </template>
          </el-table-column>
          <el-table-column
                  prop="projectIco"
                  align="center"
                  width="100"
                  show-overflow-tooltip
                  label="图标">
              <template   slot-scope="scope">
                  <el-image :src="scope.row.projectIco" lazy />
              </template>
          </el-table-column>

              <el-table-column
                  prop="projectTitle"
                  align="center"
                  show-overflow-tooltip
                  label="项目标题">
              </el-table-column>
              <el-table-column
                  prop="projectDescription"
                  show-overflow-tooltip
                  align="center"
                  label="项目描述">
              </el-table-column>
              <el-table-column
                  prop="startNum"
                  align="center"
                  label="点赞数">
              </el-table-column>
              <el-table-column
                  prop="submitTime"
                  align="center"
                  width="100"
                  show-overflow-tooltip
                  label="提交时间">
              </el-table-column>

              <el-table-column
                  prop="categoryId"
                  align="center"
                  label="主要标签">
              </el-table-column>

              <el-table-column
                  prop="lookCount"
                  align="center"
                  label="查看次数">
              </el-table-column>

              <el-table-column
                  prop="periodicals"
                  align="center"
                  width="50"
                  label="期刊">
              </el-table-column>
              <el-table-column
                  prop="fileAddress"
                  align="center"
                  show-overflow-tooltip
                  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.projectId)"
                      >
                      </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="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="70px">
              <el-form-item label="ID" prop="projectId">
                  <el-input v-model="form.projectId"  placeholder='ID' clearable disabled></el-input>
              </el-form-item>
              <el-form-item label="用户名" prop="userId">
                  <el-input v-model="form.userId"  placeholder='用户ID' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="项目名称" prop="projectTitle">
                  <el-input  v-model="form.projectName"
                             :disabled="isEdit"
                            placeholder='项目名称'
                            clearable></el-input>
              </el-form-item>
              <el-form-item label="项目地址" prop="projectIco">
                  <el-input v-model="form.projectUrl"  placeholder='项目地址' clearable :disabled="isEdit"></el-input>
              </el-form-item>

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

              <el-form-item label="标题" prop="projectTitle">
                  <el-input v-model="form.projectTitle"  placeholder='标题' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="项目描述" prop="projectDescription">
                  <el-input type="textarea" v-model="form.projectDescription"  placeholder="项目描述" :rows="5" :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="点赞数" prop="startNum">
                  <el-input v-model="form.startNum"  placeholder='点赞数' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="提交时间" prop="submitTime">
                  <el-input v-model="form.submitTime"  placeholder='提交时间' clearable :disabled="isEdit"></el-input>
              </el-form-item>

              <el-form-item label="标签" prop="categoryId">
                  <el-input v-model="form.categoryId"  placeholder='标签' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="文件地址" prop="fileAddress">
                  <el-input v-model="form.fileAddress"  placeholder='文件地址' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="期刊" prop="periodicals">
                  <el-input v-model="form.periodicals"  placeholder='期刊' clearable :disabled="isEdit"></el-input>
              </el-form-item>
              <el-form-item label="查看数" prop="lookCount">
                  <el-input v-model="form.lookCount"  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>


  </div>
</template>

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

export default {
    name: "projectManage",
    data(){
        return {
            dataTable:[],
            isEdit:true,
            dialogVisible:false,
            form:{
                projectId:'',
                userId:'',
                projectName:'',
                projectUrl:'',
                projectIco:'',
                projectTitle:'',
                projectDescription:'',
                startNum:'',
                submitTime:'',
                categoryId:'',
                isValid:'',
                fileAddress:'',
                periodicals:'',
                lookCount:'',

            }
        }
    },
    methods:{
        toDetailPage(row){
            this.$router.push({
                path:'/ProjectDetail',
                query: {
                    id: row.projectId,
                    refresh: 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_project',
                            // URL 中的查询参数
                            params: {
                                projectId: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_project',
                            // URL 中的查询参数
                            params: {
                                projectId: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(() => {})
            }
        },
        editForm(){
            this.isEdit=false;
        },
        closeDialog() {
            // 先重置
            this.getAllProject();
            // 后关闭
            this.dialogVisible = false;
            this.isEdit=true;
        },
        getAllProject(){
            var that=this;
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/selectProject',
                // URL 中的查询参数
                params: {
                }
            }).then(function (res) {
                that.dataTable=res.data;
            })
        },
        submit() {
            var _this = this;
            //调用方法提交
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/updateProject',
                // 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.getAllProject();
                    Vue.prototype.$notify({
                        title: '成功',
                        message: ('i', {style: 'color: teal'}, "更新成功！"),
                        type: 'success',
                        offset: 50
                    });
                }
            });
        },
        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/deleteProject',
                    // URL 中的查询参数
                    params: {
                        id: index.projectId,
                    }
                }).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.getAllProject();
                    }

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

<style scoped>

</style>