{{define "inboundInfoModal"}}
<a-modal id="inbound-info-modal"
    v-model="infoModal.visible" title='{{ i18n "pages.inbounds.details"}}'
    :closable="true"
    :mask-closable="true"
    :class="siderDrawer.isDarkTheme ? darkClass : ''"
    :footer="null"
    width="600px"
    >
    <table style="margin-bottom: 10px; width: 100%;">
        <tr><td>
            <table>
                <tr><td>{{ i18n "protocol" }}</td><td><a-tag color="green">[[ dbInbound.protocol ]]</a-tag></td></tr>
                <tr><td>{{ i18n "pages.inbounds.address" }}</td><td><a-tag color="blue">[[ dbInbound.address ]]</a-tag></td></tr>
                <tr><td>{{ i18n "pages.inbounds.port" }}</td><td><a-tag color="green">[[ dbInbound.port ]]</a-tag></td></tr>
            </table>
        </td>
        <td v-if="dbInbound.isVMess || dbInbound.isVLess || dbInbound.isTrojan || dbInbound.isSS">
            <table>
                <tr>
                    <td>{{ i18n "transmission" }}</td><td><a-tag color="green">[[ inbound.network ]]</a-tag></td>
                </tr>
            <template v-if="inbound.isTcp || inbound.isWs || inbound.isH2">
                <tr v-if="inbound.host"><td>{{ i18n "host" }}</td><td><a-tag color="green">[[ inbound.host ]]</a-tag></td></tr>
                <tr v-else><td>{{ i18n "host" }}</td><td><a-tag color="orange">{{ i18n "none" }}</a-tag></td></tr>
            
                <tr v-if="inbound.path"><td>{{ i18n "path" }}</td><td><a-tag color="green">[[ inbound.path ]]</a-tag></td></tr>
                <tr v-else><td>{{ i18n "path" }}</td><td><a-tag color="orange">{{ i18n "none" }}</a-tag></td></tr>
            </template>
            
            <template v-if="inbound.isQuic">
                <tr><td>quic {{ i18n "encryption" }}</td><td><a-tag color="green">[[ inbound.quicSecurity ]]</a-tag></td></tr>
                <tr><td>quic {{ i18n "password" }}</td><td><a-tag color="green">[[ inbound.quicKey ]]</a-tag></td></tr>
                <tr><td>quic {{ i18n "camouflage" }}</td><td><a-tag color="green">[[ inbound.quicType ]]</a-tag></td></tr>
            </template>
            
            <template v-if="inbound.isKcp">
                <tr><td>kcp {{ i18n "encryption" }}</td><td><a-tag color="green">[[ inbound.kcpType ]]</a-tag></td></tr>
                <tr><td>kcp {{ i18n "password" }}</td><td><a-tag color="green">[[ inbound.kcpSeed ]]</a-tag></td></tr>
            </template>
            
            <template v-if="inbound.isGrpc">
                <tr><td>grpc serviceName</td><td><a-tag color="green">[[ inbound.serviceName ]]</a-tag></td></tr>
            </template>
            </table>
        </td></tr>
            <tr colspan="2" v-if="dbInbound.hasLink()">
                <td v-if="inbound.tls">
                    tls: <a-tag color="green">{{ i18n "enabled" }}</a-tag><br />
                    tls {{ i18n "domainName" }}: <a-tag :color="inbound.serverName ? 'green' : 'orange'">[[ inbound.serverName ? inbound.serverName : '' ]]</a-tag>
                </td>
                <td v-else-if="inbound.XTLS">
                    xtls: <a-tag color="green">{{ i18n "enabled" }}</a-tag><br />
                    xtls {{ i18n "domainName" }}: <a-tag :color="inbound.serverName ? 'green' : 'orange'">[[ inbound.serverName ? inbound.serverName : '' ]]</a-tag>
                </td>
                <td v-else>tls: <a-tag color="red">{{ i18n "disabled" }}</a-tag>
            </td>
        </tr>
    </table>
    <template v-if="infoModal.clientSettings">
    <a-divider>{{ i18n "pages.inbounds.client" }}</a-divider>
    <table style="margin-bottom: 10px;">
        <tr v-for="col,index in Object.keys(infoModal.clientSettings).slice(0, 3)">
            <td>[[ col ]]</td>
            <td><a-tag color="green">[[ infoModal.clientSettings[col] ]]</a-tag></td>
        </tr>
        <tr>
            <td>{{ i18n "status" }}</td>
            <td>
                <a-tag v-if="isEnable" color="blue">{{ i18n "enabled" }}</a-tag>
                <a-tag v-else color="red">{{ i18n "disabled" }}</a-tag>
                <a-tag v-if="!isActive" color="red">{{ i18n "depleted" }}</a-tag>
            </td>
        </tr>
    </table>
    <table style="margin-bottom: 10px; width: 100%;">
            <tr>
                <th>{{ i18n "usage" }}</th>
                <th>{{ i18n "pages.inbounds.totalFlow" }}</th>
                <th>{{ i18n "pages.inbounds.expireDate" }}</th>
        <tr>
            <td>
                <a-tag v-if="infoModal.clientStats" :color="statsColor(infoModal.clientStats)">
                    [[ sizeFormat(infoModal.clientStats['up']) ]] / 
                    [[ sizeFormat(infoModal.clientStats['down']) ]]
                    ([[ sizeFormat(infoModal.clientStats['up'] + infoModal.clientStats['down']) ]])
                </a-tag>
            </td>
            <td>
                <a-tag v-if="infoModal.clientSettings.totalGB > 0" :color="statsColor(infoModal.clientStats)">[[ sizeFormat(infoModal.clientSettings.totalGB) ]]</a-tag>
                <a-tag v-else color="green">{{ i18n "indefinite" }}</a-tag>
            </td>
            <td>
                <template v-if="infoModal.clientSettings.expiryTime > 0">
                    <a-tag :color="infoModal.isExpired ? 'red' : 'blue'">
                        [[ DateUtil.formatMillis(infoModal.clientSettings.expiryTime) ]]
                    </a-tag>
                </template>
                <a-tag v-else-if="infoModal.clientSettings.expiryTime < 0" color="cyan">[[ infoModal.clientSettings.expiryTime / -86400000 ]] {{ i18n "pages.client.days" }}</a-tag>
                <a-tag v-else color="green">{{ i18n "indefinite" }}</a-tag>
            </td>
        </tr>
    </table>
    <table v-if="infoModal.clientSettings.subId + infoModal.clientSettings.tgId" style="margin-bottom: 10px;">
        <tr v-if="infoModal.clientSettings.subId">
            <td>Subscription link</td>
            <td><a :href="[[ subBase + infoModal.clientSettings.subId ]]" target="_blank">[[ subBase + infoModal.clientSettings.subId ]]</a></td>
        </tr>
        <tr v-if="infoModal.clientSettings.tgId">
            <td>Telegram Username</td>
            <td><a :href="[[ tgBase + infoModal.clientSettings.tgId ]]" target="_blank">@[[ infoModal.clientSettings.tgId ]]</a></td>
        </tr>
    </table>
    </template>
    <template v-else>
        <a-divider></a-divider>
        <table v-if="inbound.protocol == Protocols.SHADOWSOCKS" style="margin-bottom: 10px; width: 100%;">
            <tr>
                <th>{{ i18n "encryption" }}</th>
                <th>{{ i18n "password" }}</th>
                <th>{{ i18n "pages.inbounds.network" }}</th>
            </tr><tr>
                <td><a-tag color="green">[[ inbound.settings.method ]]</a-tag></td>
                <td><a-tag color="blue">[[ inbound.settings.password ]]</a-tag></td>
                <td><a-tag color="green">[[ inbound.settings.network ]]</a-tag></td>
            </tr>
        </table>
        <table v-if="inbound.protocol == Protocols.DOKODEMO" style="margin-bottom: 10px; width: 100%;">
            <tr>
                <th>{{ i18n "pages.inbounds.targetAddress" }}</th>
                <th>{{ i18n "pages.inbounds.destinationPort" }}</th>
                <th>{{ i18n "pages.inbounds.network" }}</th>
                <th>FollowRedirect</th>
            </tr><tr>
                <td><a-tag color="green">[[ inbound.settings.address ]]</a-tag></td>
                <td><a-tag color="blue">[[ inbound.settings.port ]]</a-tag></td>
                <td><a-tag color="green">[[ inbound.settings.network ]]</a-tag></td>
                <td><a-tag color="blue">[[ inbound.settings.followRedirect ]]</a-tag></td>
            </tr>
        </table>
        </table>
        <table v-if="inbound.protocol == Protocols.SOCKS" style="margin-bottom: 10px; width: 100%;">
            <tr>
                <th>{{ i18n "password" }} Auth</th>
                <th>{{ i18n "pages.inbounds.enable" }} udp</th>
                <th>IP</th>
            </tr><tr>
                <td><a-tag color="green">[[ inbound.settings.auth ]]</a-tag></td>
                <td><a-tag color="blue">[[ inbound.settings.udp]]</a-tag></td>
                <td><a-tag color="green">[[ inbound.settings.ip ]]</a-tag></td>
            </tr><tr v-if="inbound.settings.auth == 'password'">
                <td> </td>
                <td>{{ i18n "username" }}</td>
                <td>{{ i18n "password" }}</td>
            </tr><tr v-for="account,index in inbound.settings.accounts">
                <td><a-tag color="green">[[ index ]]</a-tag></td>
                <td><a-tag color="blue">[[ account.user ]]</a-tag></td>
                <td><a-tag color="green">[[ account.pass ]]</a-tag></td>
            </tr>
        </table>
        </table>
        <table v-if="inbound.protocol == Protocols.HTTP" style="margin-bottom: 10px; width: 100%;">
            <tr>
                <th> </th>
                <th>{{ i18n "username" }}</th>
                <th>{{ i18n "password" }}</th>
            </tr><tr v-for="account,index in inbound.settings.accounts">
                <td><a-tag color="green">[[ index ]]</a-tag></td>
                <td><a-tag color="blue">[[ account.user ]]</a-tag></td>
                <td><a-tag color="green">[[ account.pass ]]</a-tag></td>
            </tr>
        </table>
        </table>
    </template>
    <div v-if="dbInbound.hasLink()">
        <a-divider>URL</a-divider>
        <p>[[ infoModal.link ]]</p>
        <button class="ant-btn ant-btn-primary" id="copy-url-link"><a-icon type="snippets"></a-icon>{{ i18n "copy" }}</button>
    </div>
</a-modal>
<script>
    const infoModal = {
        visible: false,
        inbound: new Inbound(),
        dbInbound: new DBInbound(),
        settings: null,
        clientSettings: null,
        clientStats: [],
        upStats: 0,
        downStats: 0,
        clipboard: null,
        link: null,
        index: null,
        isExpired: false,
        show(dbInbound, index) {
            this.index = index;
            this.inbound = dbInbound.toInbound();
            this.dbInbound = new DBInbound(dbInbound);
            this.link = dbInbound.genLink(index);
            this.settings = JSON.parse(this.inbound.settings);
            this.clientSettings = this.settings.clients ? Object.values(this.settings.clients)[index] : null;
            this.isExpired = this.inbound.isExpiry(index);
            this.clientStats = this.settings.clients ? this.dbInbound.clientStats.find(row => row.email === this.clientSettings.email) : [];
            this.visible = true;
            infoModalApp.$nextTick(() => {
                if (this.clipboard === null) {
                    this.clipboard = new ClipboardJS('#copy-url-link', {
                        text: () => this.link,
                    });
                    this.clipboard.on('success', () => app.$message.success('{{ i18n "copied" }}'));
                }
            });
        },
        close() {
            infoModal.visible = false;
        },
    };

    const infoModalApp = new Vue({
        delimiters: ['[[', ']]'],
        el: '#inbound-info-modal',
        data: {
            infoModal,
            get dbInbound() {
                return this.infoModal.dbInbound;
            },
            get inbound() {
                return this.infoModal.inbound;
            },
            get isActive() {
                if(infoModal.clientStats){
                    return infoModal.clientStats.enable;
                }
                return infoModal.dbInbound.isEnable;
            },
            get isEnable() {
                if(infoModal.clientSettings){
                    return infoModal.clientSettings.enable;
                }
                return infoModal.dbInbound.isEnable;
            },
            get subBase() {
                return window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port:"") + basePath + "sub/";
            },
            get tgBase() {
                return "https://t.me/"
            },
        },
        methods: {
            copyTextToClipboard(elmentId,content) {
                this.infoModal.clipboard = new ClipboardJS('#' + elmentId, {
                        text: () => content,
                    });
                this.infoModal.clipboard.on('success', () => { 
                    app.$message.success('{{ i18n "copied" }}')
                    this.infoModal.clipboard.destroy();
                });
            },
            statsColor(stats) {
                if(!stats) return 'blue'
                if(stats['total'] === 0) return 'blue'
                else if(stats['total'] > 0 && (stats['down']+stats['up']) < stats['total']) return 'cyan'
                else return 'red'
            }
        },
        
    });

</script>
{{end}}