<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>授权中心</title>
    <script type="text/javascript" src="themes/js/vue.min.js"></script>
    <script type="text/javascript" src="themes/js/index.js"></script>
    <script type="text/javascript" src="themes/js/axios.min.js"></script>
    <script type="text/javascript" src="themes/js/jquery.js"></script>
    <script type="text/javascript" src="themes/js/moment.js"></script>
    <link rel="stylesheet" href="themes/css/index.css" type="text/css">
    <link rel="stylesheet" href="themes/css/main.css" type="text/css">
</head>
<body>
<div id="app">
    <div class="header">
        <div class="logo">
            <div class="logo-image"></div>
        </div>
    </div>
    <div class="card-main">
        <div class="main">
            <div class="top">
                <div class="labe">授权企业详情</div>
            </div>
            <div class="company">
                <div class="company-det">

                    <div class="company-lint">
                        <div class="company-name">
                            <div class="company-logo">
                                <img src="http://www.crtech.cn/images/logo.png">
                            </div>
                            <div style="margin-left: 20px;">{{single.company.name}}</div>
                        </div>
                        <div style="margin: 10px;">
                            <div class="address">地址：{{single.company.address}}</div>
                            <div class="des">简介：{{single.company.introduce}}</div>
                            <div class="des">授权码：<span style="color: green">已生成</span> <span><el-link
                                    type="primary" :underline="false"
                                    style=" font-size: 12px;margin-left: 8px;" @click="dialogCodeVisible = true">查看</el-link></span></div>
                            <div class="des">授权文件：<span style="color: green">正常</span>
                                <span>
                                    <el-link type="primary" :underline="false" style=" font-size: 12px;margin-left: 8px;" @click="dialogTableVisible = true">更换授权文件</el-link></span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card-main">
        <div class="main">
            <div class="top">
                <div class="labe">{{single.group}}</div>
            </div>
            <div class="pubg">
                <div class="applist">
                    <div class="app" v-for="(item,i) in single.applist">
                        <div class="app-det">
                            <img :src="item.logo ? item.logo : 'themes/img/mk.png'">
                        </div>
                        <div class="app-name">{{item.name}}</div>
                        <div class="app-des">版本：{{item.version}}</div>
                        <div class="app-license">{{item.grade == 'custom' ? '定制授权' : item.gradeName}}</div>
                        <div class="app-time" style="color: green" v-if="new Date().getTime() < new Date(item.expiryTime).getTime()"> {{item.expiryTime | dateFilter('YYYY-MM-DD')}}到期</div>
                        <div v-else style="color: red">已过期</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <el-dialog title="授权码" :visible.sync="dialogCodeVisible">
        <div slot="header" class="clearfix">
            <span>本机授权码</span>
            <el-button style="float: right; padding: 3px 0;" icon="el-icon-document-copy" @click="copy()" type="text">复制授权码</el-button>
        </div>
        <div class="demo-input-suffix">
            <el-input type="textarea" ref="copy" autosize="true" v-model="single.code" resize="none" readonly="true"></el-input>
        </div>
    </el-dialog>
    <el-dialog title="授权文件修改" :visible.sync="dialogTableVisible">
        <div slot="header">
            <span>上传授权文件</span>
        </div>
        <span class="tip"></span>
        <div align="center">
            <el-upload class="upload-demo" accept enctype="multipart/form-data" drag action="/xinadmin/upload" multiple="false" show-file-list="false"
                       drag="true" auto-upload="true" limit="1" :on-success="Success">
                <i class="el-icon-upload" style="height: 10px;"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip" slot="tip">请将您获得的授权文件(LICENSE)传入系统进行校验</div>
            </el-upload>
        </div>
    </el-dialog>
</div>
<script>
    Vue.filter("dateFilter", function (date, formatPattern) {
        return moment(date).format(formatPattern || "YYYY-MM-DD HH:mm:ss");
    });
    new Vue({
        el: '#app',
        data() {
            return {
                single: {},
                dialogCodeVisible: false,
                dialogTableVisible: false
            }
        },
        created: function () {
            this.checkLogin();
            this.singlecode();
        },
        //绑定方法
        methods: {
            copy() {
                this.$refs.copy.select()
                document.execCommand('Copy')
                this.$message.success('已复制到剪贴板')
            },
            singlecode() {
                const that = this;
                axios.get('/xinadmin/single', {}).then(function (res) {
                    if (res.data) {
                        if(res.data.license){
                            that.single = res.data;
                        }else{
                            location.href = "index.jsp"
                        }
                    }
                }).catch(function (error) {
                    console.log(error)
                })
            },
            uploadfile() {
                this.$refs.upload.submit()
            },
            checkLogin(){
                const token = sessionStorage.getItem("token");
                if(token == null){
                    location.href = "login.jsp"
                }else{
                    const str = new Date().getTime()
                    if((+token + (2*60*60*1000) < str)){
                        location.href = "login.jsp"
                    }
                }
            },
            Success(response, file, fileList) {
                if(response.state == "success"){
                    this.dialogTableVisible = false;
                    this.$notify({
                        title: '成功',
                        duration:2000,
                        message: '校验通过',
                        type: 'success'
                    });
                    const that = this;
                    this.$confirm('授权文件已更新，是否立即重启相关应用?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.get('/xinadmin/adminstarter', {}).then(function (res) {
                            that.$notify({
                                title: '重启中',
                                duration:2000,
                                message: '正在重启相关应用，请稍侯....',
                                type: 'info'
                            });
                            setTimeout(() => {
                                location.reload();
                            }, 2000);
                        }).catch(function (error) {
                            console.log(error)
                        })
                    }).catch(() => {
                        that.$notify({
                            title: '警告',
                            duration:2000,
                            message: '应用尚未重启，授权尚未生效，请稍后手动重启',
                            type: 'warning'
                        });
                    });
                }else{
                    this.$notify.error({
                        title: '上传失败',
                        message: response.error
                    });
                }
            }
        }
    })
</script>
</body>
</html>
