<template>
  <div>
      <el-table
              border
              stripe
              :data="dataTable"
              style="width: 100%;margin-top: 20px">
          <el-table-column
                  prop="userId"
                  label="ID"
                  sortable
                  align="center"
          >
          </el-table-column>
          <el-table-column
                  prop="userName"
                  align="center"
                  label="用户名">
          </el-table-column>
          <el-table-column
                  prop="userPassword"
                  align="center"
                  label="密码">
          </el-table-column>
          <el-table-column
                  prop="creatTime"
                  align="center"
                  label="注册时间">
          </el-table-column>
          <el-table-column
                  prop="userLevel"
                  align="center"
                  label="等级">
          </el-table-column>
          <el-table-column
                  prop="projectNum"
                  align="center"
                  label="项目数">
          </el-table-column>
          <el-table-column
                  prop="contributionValue"
                  align="center"
                  label="贡献值">
          </el-table-column>
          <el-table-column
                  prop="searchHistory"
                  align="center"
                  label="搜索历史">
          </el-table-column>
          <el-table-column
                  prop="userAvatar"
                  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"
                  >
                  </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="26%"
                 append-to-body
                 :close-on-click-modal="false"
      >
          <el-form ref="ruleForm" class="login_container" :model="form" status-icon  label-width="70px">
              <el-form-item label="ID" prop="username">
                  <el-input v-model="form.userId"  placeholder='ID' clearable disabled></el-input>
              </el-form-item>
              <el-form-item label="用户名" prop="username">
                  <el-input v-model="form.userName"  placeholder='用户名' clearable></el-input>
              </el-form-item>

              <el-form-item label="密 码" prop="password">
                  <el-input type="password" v-model="form.userPassword"
                            placeholder='请输入密码'
                            clearable></el-input>
              </el-form-item>

              <el-form-item label="项目数" prop="projectNum">
                  <el-input v-model="form.projectNum"  placeholder='项目数量' clearable></el-input>
              </el-form-item>

              <el-form-item label="贡献值" prop="contributionValue">
                  <el-input v-model="form.contributionValue"  placeholder='贡献值' clearable></el-input>
              </el-form-item>
              <el-form-item label="等级" prop="userLevel">
                  <el-input v-model="form.userLevel"  placeholder='用户等级' clearable></el-input>
              </el-form-item>

              <el-form-item label="搜索历史" prop="searchHistory">
                  <el-input v-model="form.searchHistory"  placeholder='搜索历史' clearable></el-input>
              </el-form-item>
              <el-form-item label="头像" prop="userAvatar">
                  <el-input v-model="form.userAvatar"  placeholder='头像' clearable></el-input>
              </el-form-item>
              <el-form-item label="是否有效" prop="isValid">
                  <el-radio-group v-model="form.isValid">
                      <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: "userManage",
    data(){
        return {
            dataTable:[],
            dialogVisible:false,
            form:{
                userId:'',
                userName:'',
                userPassword:'',
                creatTime:'',
                userLevel:'',
                projectNum:'',
                contributionValue:'',
                searchHistory:'',
                userAvatar:'',
                isValid:'',
            },
        }
    },
    mounted() {
        this.getAllUser();
    },
    methods:{
        getAllUser(){
            var that=this;
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/selectUser',
                // URL 中的查询参数
                params: {
                }
            }).then(function (res) {
                that.dataTable=res.data;
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        openDialog(){
            this.dialogVisible=true;
        },
        submit() {
            var _this = this;
            //调用方法提交
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/updateUser',
                // URL 中的查询参数
                params: {
                    userId: this.form.userId,
                    userName: this.form.userName,
                    userPassword: this.form.userPassword,
                    creatTime: this.form.creatTime,
                    userLevel: this.form.userLevel,
                    projectNum: this.form.projectNum,
                    contributionValue: this.form.contributionValue,
                    searchHistory: this.form.searchHistory,
                    userAvatar: this.form.userAvatar,
                    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.dialogVisible = false;
                    _this.getAllUser();
                    Vue.prototype.$notify({
                        title: '成功',
                        message: ('i', {style: 'color: teal'}, "更新成功！"),
                        type: 'success',
                        offset: 50
                    });
                }
            });
        },

        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/deleteUser',
                    // URL 中的查询参数
                    params: {
                        id: index.userId,
                    }
                }).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.getAllUser();
                    }

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

<style scoped>

</style>