<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-message"></i> {{ $t('sidebar.mailboxManagement') }} </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box" style="overflow: hidden">
                <!-- <el-input v-model="query.username" placeholder="Account / Realname / Email" style="width: 240px; margin-right: 10px">
                </el-input>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="
                        query.pageIndex = 1;
                        getDate();
                    "
                    >Search</el-button
                > -->
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd" style="float: right">Add Mailbox </el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" empty-text="">
                <el-table-column prop="user" label="userName" width="200"></el-table-column>
                <el-table-column prop="email" label="Email"></el-table-column>

                <el-table-column label=" " width="120" align="center">
                    <template slot-scope="scope">
                        <!-- <el-button plain type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">Edit Message</el-button> -->
                        <el-button plain type="primary" icon="el-icon-discount" @click="handleEdit(scope.row)">Edit</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 添加用户弹出框 -->
        <el-dialog :title="`${!addForm.id ? 'Add Mailbox' : 'Edit Password'}`" :close-on-click-modal="false" :visible.sync="addVisible" width="550px">
            <el-form ref="addTab" :model="addForm" :rules="rules" label-width="150px">
                <el-form-item label="User :" prop="user"  v-if="!addForm.id">
                    <el-input v-model="addForm.user" clearable></el-input>
                  
                </el-form-item>
                <el-form-item label="User :" prop="user1"  v-else>
                    <p >{{ addForm.user }}</p>
                  
                </el-form-item>
                <el-form-item label="email :" prop="email" v-if="addForm.id">
                    
                   <p >{{ addForm.email?addForm.email:'' }}</p>
                </el-form-item>

                <!-- autocomplete="new-password" -->
                <el-form-item label="Password :" prop="password" class="no-autofill-pwd">
                    <el-input v-model="addForm.password"  clearable autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="Confirm password :" prop="repassword" class="no-autofill-pwd">
                    <el-input v-model="addForm.repassword"  clearable autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">Cancel</el-button>
                <el-button type="primary" @click="saveAdd">Save</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'partyList',
    data() {
        return {
            randomName: `password_${Math.random().toString(36).substr(2, 9)}`,
            urlList: {
                list: 'api/Email/getEmailAccounts',
                add: 'api/Email/addEmailAccount',
                edit: 'api/Email/changePassword'
            },
            mediaUrl: this.Common.mediaUrl,
            userrole: localStorage.getItem('U_status'),
            guest_id: localStorage.getItem('U_id'),
            tableData: [],
            blackForm: {},
            emailForm: {
                mark_new: ''
            },
            roleList: [
                {
                    value: 0,
                    title: 'All Role'
                },
                {
                    value: 1,
                    title: 'Editor'
                },
                {
                    value: 2,
                    title: 'Reviewer'
                },
                {
                    value: 3,
                    title: 'Editorial Board Member'
                }
            ],
            addForm: {},
            editForm: {},
            query: {
                role: 0,
                pageIndex: 1,
                pageSize: 15,
                username: ''
            },
            link_Total: 0,
            email_num: 0,
            blackVisible: false,
            emailVisible: false,
            addVisible: false,
            editVisible: false,
            rules: {
                user: [
                    {
                        required: true,
                        message: 'Please input user',
                        trigger: 'blur'
                    }
                ],

                password: [
                    {
                        required: true,
                        validator: 'Please input password',
                        trigger: 'blur'
                    }
                ],
                repassword: [
                    {
                        required: true,
                        validator: 'Please input password again',
                        trigger: 'blur'
                    }
                ]
            }
        };
    },
    mounted() {},
    created() {
        this.getDate();
    },
    methods: {
        // 获取数据
        getDate() {
            this.$api
                .post(this.urlList.list)
                .then((res) => {
                    if (res.code == 0) {
                        this.tableData = res.data.list;
                    } else {
                        this.$message.error(res.msg);
                    }
                })
                .catch((err) => {
                    this.$message.error(err);
                });
        },

        // 指数颜色
        colorIndex(num, time) {
            if (time != 0) {
                let date = new Date(parseInt(time * 1000));
                let Y = date.getFullYear() + '-';
                let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
                let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
                let H = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
                let U = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
                time = Y + M + D;
            }

            let str = '';
            if (num < 10) {
                str =
                    '<b style="color:#cb160a">' +
                    num +
                    '</b><br/><span style="color:#aaa;font-size:14px;margin-left:5px;"> (' +
                    time +
                    ')</span>';
            } else if (num < 15) {
                str =
                    '<b style="color:#cbb504">' +
                    num +
                    '</b><br/><span style="color:#aaa;font-size:14px;margin-left:5px;"> (' +
                    time +
                    ')</span>';
            } else {
                str =
                    '<b style="color:#0cbc15">' +
                    num +
                    '</b><br/><span style="color:#aaa;font-size:14px;margin-left:5px;"> (' +
                    time +
                    ')</span>';
            }
            if (time == 0) {
                str = '<b style="color:#aaa;">0 </b><br/><span style="color:#aaa;font-size:14px;margin-left:5px;">(No time)</span>';
            }
            return str;
        },

        // 检测邮箱弹出框
        handleAdd() {
            this.addForm = {
                user: '',
                password: '',
                repassword: ''
            };
            this.addVisible = true;
            this.$nextTick(() => {
                this.$refs.addTab.resetFields();
            });
        },

        // 添加用户弹出框
        saveEmail() {
            this.$refs.emailTab.validate((valid) => {
                if (valid) {
                    let Email_mark = 0;
                    let Accnt_mark = 0;
                    this.$api
                        .post('api/User/checkUserByEmail', this.emailForm)
                        .then((res) => {
                            if (res.code == 0) {
                                if (res.data.has == 0) {
                                    Email_mark = 0;
                                } else {
                                    Email_mark = 1;
                                }
                            } else {
                                this.$message.error(res.msg);
                            }
                            this.$api
                                .post('api/User/checkUserByAccount', this.emailForm)
                                .then((res) => {
                                    if (res.code == 0) {
                                        if (res.data.has == 0) {
                                            Accnt_mark = 0;
                                        } else {
                                            Accnt_mark = 1;
                                        }
                                    } else {
                                        this.$message.error(res.msg);
                                    }
                                    if (Email_mark == 1 && Accnt_mark == 1) {
                                        this.email_num = 1;
                                        this.emailForm.mark_new = 'Email and account already exist!';
                                    } else if (Email_mark == 1 && Accnt_mark != 1) {
                                        this.email_num = 1;
                                        this.emailForm.mark_new = 'Email already exist!';
                                    } else if (Email_mark != 1 && Accnt_mark == 1) {
                                        this.email_num = 1;
                                        this.emailForm.mark_new = 'Account already exist!';
                                    } else {
                                        this.addForm.email = this.emailForm.email;
                                        this.addForm.account = this.emailForm.account;
                                        this.email_num = 2;
                                        setTimeout(() => {
                                            this.emailVisible = false;
                                            this.addVisible = true;
                                        }, 1000);
                                    }
                                })
                                .catch((err) => {
                                    this.$message.error(err);
                                });
                        })
                        .catch((err) => {
                            this.$message.error(err);
                        });
                } else {
                    this.$message.error('error submit!!');
                    return false;
                }
            });
        },
        // 保存添加用户
        saveAdd() {
            this.$refs.addTab.validate((valid) => {
                if (valid) {
                    if (this.addForm.password == this.addForm.repassword) {
                        var data = {};
                        if (!this.addForm.id) {
                            data = {
                                user: this.addForm.user,
                                password: this.addForm.password
                            };
                        } else {
                            data = {
                                id: this.addForm.id,
                                password: this.addForm.password
                            };
                        }
                        this.$api
                            .post(this.addForm.id ? this.urlList.edit : this.urlList.add, data)
                            .then((res) => {
                                if (res.code == 0) {
                                    this.addVisible = false;

                                    this.getDate();
                                    this.$message.success('User added successfully!');
                                } else {
                                    this.$message.error(res.msg);
                                }
                            })
                            .catch((err) => {
                                this.$message.error(err);
                            });
                    } else {
                        this.$message.error('The two passwords are inconsistent!');
                    }
                } else {
                    this.$message.error('error submit!!');
                    return false;
                }
            });
        },
        // 编辑用户弹出框
        handleEdit(row) {
            this.addForm = { id: row.id, password: '', repassword: '',user:row.user,email:row.email };
            this.addVisible = true;
            this.$nextTick(() => {
                this.$refs.addTab.resetFields();
            });
        },
        // 保存编辑用户
        saveEdit() {},
        // 编辑身份跳页面

        // 拉黑弹窗
        handleDark(row) {
            this.blackForm = Object.assign({}, row);
            this.blackVisible = true;
        },
        // 移除拉黑操作
        handleBlack(blackForm) {
            // 二次确认更改状态
            this.$confirm('Are you sure you want ' + blackForm.realname + ' to join the blacklist？', 'Tip', {
                type: 'warning'
            })
                .then(() => {
                    this.$api
                        .post('api/User/pushUserToBlack', this.blackForm)
                        .then((res) => {
                            if (res.code == 0) {
                                this.$message.success('Join the blacklist successful!');
                                this.blackForm.reason = '';
                                this.blackVisible = false;
                                this.getDate();
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((err) => {
                            this.$message.error(err);
                        });
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getDate();
        }
    },
    computed: {},
    watch: {}
};
</script>

<style scoped >
.handle-box {
    margin-bottom: 20px;
}

.el-button--primary.is-plain:hover {
    background-color: #409eff !important;
    color: #fff !important;
}

.ro_li_ku {
    border: 1px solid #419fcf;
    border-radius: 5px;
    color: #419fcf;
    display: inline-block;
    margin: 5px;
    padding: 0 5px;
    white-space: nowrap;
}


::v-deep.no-autofill-pwd .el-input--suffix .el-input__inner {
        -webkit-text-security: disc !important;
    }
</style>
