<template>
    <div>
        <div style="display: flex;justify-content: flex-start">
            <el-button type="primary" @click="addAdmin" icon="el-icon-plus">新增</el-button>
        </div>
        <el-table
            border
            stripe
            :data="dataTable"
            style="width: 100%;margin-top: 20px">
            <el-table-column
                prop="adminId"
                label="ID"
                sortable
                align="center"
            >
            </el-table-column>
            <el-table-column
                prop="adminName"
                align="center"
                label="管理员">
            </el-table-column>
            <el-table-column
                prop="adminPassword"
                align="center"
                label="密码">
            </el-table-column>
            <el-table-column
                prop="loginTime"
                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
                fixed="right"
                align="center"
                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="modalType === 0 ? '添加' : '编辑'" :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 :rules="rules" label-width="70px">
                <el-form-item label="账 户" prop="username">
                    <el-input v-model="form.adminName"  placeholder='请输入用户名' clearable></el-input>
                </el-form-item>

                <el-form-item label="密 码" prop="password">
                    <el-input type="password" v-model="form.adminPassword"
                              placeholder='请输入密码'
                              clearable></el-input>
                </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: "systemManage",
    data() {
        return {
            modalType: 0,
            dialogVisible:false,
            dataTable:null,
            imageUrl: "http://localhost:8082/helloGithub_war_exploded/VerifycodeServlet?" + new Date().getDate(),
            // 登陆数据
            form: {
                adminName: '',
                adminPassword: '',
            },
            // 校验规则
            rules: {
                username: [{ required: 'true', message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: 'true', message: '请输入用户名', trigger: 'blur' }],

            }
        }
    },
    mounted() {
        this.getAllAdmin();
    },
    methods: {
        getAllAdmin(){
            var that=this;
            axios({
                method: 'post',
                // 请求的地址
                url: 'http://localhost:8082/helloGithub_war_exploded/selectAdmin',
                // URL 中的查询参数
                params: {
                }
            }).then(function (res) {
                that.dataTable=res.data;
            })
        },
        submit(formName){
            var _this=this;
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    //调用方法提交
                    axios({
                        method: 'post',
                        // 请求的地址
                        url: 'http://localhost:8082/helloGithub_war_exploded/adminRegister',
                        // URL 中的查询参数
                        params: {
                            adminname: this.login.username,
                            password: this.login.password,
                        }
                    }).then(function (res) {
                        if (res.data.code === 200) {
                            Vue.prototype.$notify({
                                title: '成功',
                                message: ('i', {style: 'color: teal'}, res.data.msg),
                                type: 'success',
                                offset: 50
                            });
                            _this.dialogVisible=false;
                           _this.getAllAdmin();
                        } else {
                            Vue.prototype.$notify.error({
                                title: '错误',
                                message: res.data.msg,
                                offset: 50
                            });
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        resetImg() {
            this.imageUrl = "http://localhost:8082/helloGithub_war_exploded/VerifycodeServlet?" + new Date().getTime();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        addAdmin(){
            this.dialogVisible=true;
        },
        handleEdit(index) {
            console.log(index);
            this.modalType = 1;

            this.addAdmin();
            // 深拷贝
            this.form = index;

        },
        // 删除按钮
        handleDelete(index) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios({
                    method: 'post',
                    // 请求的地址
                    url: 'http://localhost:8082/helloGithub_war_exploded/adminRegister',
                    // URL 中的查询参数
                    params: {
                        id: index.id,
                    }
                }).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(() => {
                // 点击取消:不删除了
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        closeDialog() {
            // 先重置
            this.form={}
            // 后关闭
            this.dialogVisible = false
        },
    }
}
</script>

<style scoped>

</style>