<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"
                        @change="handleChangeStatus($event,scope.row.adminId)"
                    >
                    </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="adminID">
                    <el-input v-model="form.adminId"  placeholder='ID' clearable disabled></el-input>
                </el-form-item>
                <el-form-item label="账 户" prop="username">
                    <el-input v-model="form.adminName"  placeholder='请输入用户名' :disabled="isEdit"></el-input>
                </el-form-item>

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

            }
        }
    },
    mounted() {
        this.getAllAdmin();
    },
    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_admin',
                            // URL 中的查询参数
                            params: {
                                adminId: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_admin',
                            // URL 中的查询参数
                            params: {
                                adminId: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(() => {})
            }
        },
        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(){
            var _this=this;
            if(this.form.adminName!==''|| this.form.adminPassword!==''){
                axios({
                    method: 'post',
                    // 请求的地址
                    url: 'http://localhost:8082/helloGithub_war_exploded/adminRegister',
                    // URL 中的查询参数
                    params: {
                        adminname: this.form.adminName,
                        password: this.form.adminPassword,
                    }
                }).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.isEdit=true;
                        _this.dialogVisible=false;
                        _this.getAllAdmin();
                    } else {
                        Vue.prototype.$notify.error({
                            title: '错误',
                            message: res.data.msg,
                            offset: 50
                        });
                    }
                });
            }else {
                Vue.prototype.$notify.error({
                    title: '提示',
                    message: '请填写相关信息',
                    offset: 50
                });
            }

        },
        resetImg() {
            this.imageUrl = "http://localhost:8082/helloGithub_war_exploded/VerifycodeServlet?" + new Date().getTime();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        addAdmin(){
            this.modalType=0;
            this.dialogVisible=true;
        },
        handleEdit(index) {
            this.modalType = 1;

            this.dialogVisible=true;
            // 深拷贝
            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/deleteAdmin',
                    // URL 中的查询参数
                    params: {
                        adminId: index.adminId,
                    }
                }).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'}, res.data.msg),
                            type: 'success',
                            offset: 50
                        });
                        that.getAllAdmin();
                    }

                });
            }).catch(() => {
                // 点击取消:不删除了
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        closeDialog() {
            // 先重置
            this.getAllAdmin();
            // 后关闭
            this.dialogVisible = false;
            this.isEdit=true;
        },
        editForm(){
            this.isEdit=false;
        },
    }
}
</script>

<style scoped>

</style>