<!DOCTYPE html>
<html lang="en">
{{template "head" .}}
<style>
    @media (min-width: 769px) {
        .ant-layout-content {
            margin: 24px 16px;
        }
    }

    .ant-col-sm-24 {
        margin-top: 10px;
    }

    .ant-tabs-bar {
        margin: 0;
    }

    .ant-list-item {
        display: block;
    }

    .ant-tabs-top-bar {
        background: white;
    }
</style>
<body>
<a-layout id="app" v-cloak>
    {{ template "commonSider" . }}
    <a-layout id="content-layout">
        <a-layout-content>
            <a-spin :spinning="spinning" :delay="500" tip="loading">
                <a-space direction="vertical">
                    <a-space direction="horizontal">
                        <a-button type="primary" :disabled="saveBtnDisable" @click="updateAllSetting">{{ i18n "pages.setting.save" }}</a-button>
                        <a-button type="danger" :disabled="!saveBtnDisable" @click="restartPanel">{{ i18n "pages.setting.restartPanel" }}</a-button>
                    </a-space>
                    <a-tabs default-active-key="1">
                        <a-tab-pane key="1" tab='{{ i18n "pages.setting.panelConfig"}}'>

                            <a-list item-layout="horizontal" style="background: white">
                                <setting-list-item type="text" title='{{ i18n "pages.setting.panelListeningIP"}}' desc='{{ i18n "pages.setting.panelListeningIPDesc"}}' v-model="allSetting.webListen"></setting-list-item>
                                <setting-list-item type="number" title='{{ i18n "pages.setting.panelPort"}}' desc='{{ i18n "pages.setting.panelPortDesc"}}' v-model.number="allSetting.webPort"></setting-list-item>
                                <setting-list-item type="text" title='{{ i18n "pages.setting.publicKeyPath"}}' desc='{{ i18n "pages.setting.publicKeyPathDesc"}}' v-model="allSetting.webCertFile"></setting-list-item>
                                <setting-list-item type="text" title='{{ i18n "pages.setting.privateKeyPath"}}' desc='{{ i18n "pages.setting.privateKeyPathDesc"}}' v-model="allSetting.webKeyFile"></setting-list-item>
                                <setting-list-item type="text" title='{{ i18n "pages.setting.panelUrlPath"}}' desc='{{ i18n "pages.setting.panelUrlPathDesc"}}' v-model="allSetting.webBasePath"></setting-list-item>
                                <a-list-item>
                                    <a-row  style="padding: 20px">
                                        <a-col :lg="24" :xl="12">
                                            <a-list-item-meta title="Language"/>
                                        </a-col>

                                        <a-col :lg="24" :xl="12">
                                            <temlate>
                                                <a-select
                                                        ref="selectLang"
                                                        v-model="lang"
                                                        @change="setLang(lang)"
                                                        style="width: 100%"
                                                >
                                                    <a-select-option  :value="l.value" label="China" v-for="l in supportLangs" >
                                                        <span role="img" aria-label="l.name" v-text="l.icon"></span>
                                                        &nbsp;&nbsp;<span v-text="l.name"></span>
                                                    </a-select-option>
                                                </a-select>
                                            </temlate>
                                        </a-col>
                                    </a-row>

                                </a-list-item>
                            </a-list>
                        </a-tab-pane>
                        <a-tab-pane key="2" tab='{{ i18n "pages.setting.userSetting"}}'>
                            <a-form style="background: white; padding: 20px">
                                <a-form-item label='{{ i18n "pages.setting.oldUsername"}}'>
                                    <a-input v-model="user.oldUsername" style="max-width: 300px"></a-input>
                                </a-form-item>
                                <a-form-item label='{{ i18n "pages.setting.currentPassword"}}'>
                                    <a-input type="password" v-model="user.oldPassword"
                                             style="max-width: 300px"></a-input>
                                </a-form-item>
                                <a-form-item label='{{ i18n "pages.setting.newUsername"}}'>
                                    <a-input v-model="user.newUsername" style="max-width: 300px"></a-input>
                                </a-form-item>
                                <a-form-item label='{{ i18n "pages.setting.newPassword"}}'>
                                    <a-input type="password" v-model="user.newPassword"
                                             style="max-width: 300px"></a-input>
                                </a-form-item>
                                <a-form-item>
<!--                                    <a-button type="primary" @click="updateUser">Revise</a-button>-->
                                    <a-button type="primary" @click="updateUser">{{ i18n "confirm" }}</a-button>
                                </a-form-item>
                            </a-form>
                        </a-tab-pane>
                        <a-tab-pane key="3" tab='{{ i18n "pages.setting.xrayConfiguration"}}'>
                            <a-list item-layout="horizontal" style="background: white">
                                <setting-list-item type="switch" title='{{ i18n "pages.setting.xrayConfigTorrent"}}' desc='{{ i18n "pages.setting.xrayConfigTorrentDesc"}}'  v-model="torrentSettings"></setting-list-item>
                                <setting-list-item type="switch" title='{{ i18n "pages.setting.xrayConfigPrivateIp"}}' desc='{{ i18n "pages.setting.xrayConfigPrivateIpDesc"}}'  v-model="privateIpSettings"></setting-list-item>
                                <a-divider>{{ i18n "pages.setting.advancedTemplate"}}</a-divider>
                                <a-collapse>
                                    <a-collapse-panel header="{{ i18n "pages.setting.xrayConfigInbounds"}}">
                                        <setting-list-item type="textarea" title='{{ i18n "pages.setting.xrayConfigInbounds"}}' desc='{{ i18n "pages.setting.xrayConfigInboundsDesc"}}' v-model ="inboundSettings"></setting-list-item>
                                    </a-collapse-panel>
                                    <a-collapse-panel header="{{ i18n "pages.setting.xrayConfigOutbounds"}}">
                                        <setting-list-item type="textarea" title='{{ i18n "pages.setting.xrayConfigOutbounds"}}' desc='{{ i18n "pages.setting.xrayConfigOutboundsDesc"}}' v-model ="outboundSettings"></setting-list-item>
                                    </a-collapse-panel>
                                    <a-collapse-panel header="{{ i18n "pages.setting.xrayConfigRoutings"}}">
                                        <setting-list-item type="textarea" title='{{ i18n "pages.setting.xrayConfigRoutings"}}' desc='{{ i18n "pages.setting.xrayConfigRoutingsDesc"}}' v-model ="routingRuleSettings"></setting-list-item>
                                    </a-collapse-panel>
                                </a-collapse>
                                <a-divider>{{ i18n "pages.setting.completeTemplate"}}</a-divider>
                                <setting-list-item type="textarea" title='{{ i18n "pages.setting.xrayConfigTemplate"}}' desc='{{ i18n "pages.setting.xrayConfigTemplateDesc"}}' v-model="allSetting.xrayTemplateConfig"></setting-list-item>
                            </a-list>
                        </a-tab-pane>
                        <a-tab-pane key="4" tab='{{ i18n "pages.setting.TGReminder"}}'>
                            <a-list item-layout="horizontal" style="background: white">
                                <setting-list-item type="switch" title='{{ i18n "pages.setting.telegramBotEnable" }}' desc='{{ i18n "pages.setting.telegramBotEnableDesc" }}'  v-model="allSetting.tgBotEnable"></setting-list-item>
                                <setting-list-item type="text" title='{{ i18n "pages.setting.telegramToken"}}' desc='{{ i18n "pages.setting.telegramTokenDesc"}}'  v-model="allSetting.tgBotToken"></setting-list-item>
                                <setting-list-item type="number" title='{{ i18n "pages.setting.telegramChatId"}}' desc='{{ i18n "pages.setting.telegramChatIdDesc"}}'  v-model.number="allSetting.tgBotChatId"></setting-list-item>
                                <setting-list-item type="text" title='{{ i18n "pages.setting.telegramNotifyTime"}}' desc='{{ i18n "pages.setting.telegramNotifyTimeDesc"}}'  v-model="allSetting.tgRunTime"></setting-list-item>
                                <setting-list-item type="switch" title='{{ i18n "pages.setting.tgNotifyBackup" }}' desc='{{ i18n "pages.setting.tgNotifyBackupDesc" }}'  v-model="allSetting.tgBotBackup"></setting-list-item>
                                <setting-list-item type="number" title='{{ i18n "pages.setting.tgNotifyExpireTimeDiff" }}' desc='{{ i18n "pages.setting.tgNotifyExpireTimeDiffDesc" }}'  v-model="allSetting.tgExpireDiff" :min="0"></setting-list-item>
                                <setting-list-item type="number" title='{{ i18n "pages.setting.tgNotifyTrafficDiff" }}' desc='{{ i18n "pages.setting.tgNotifyTrafficDiffDesc" }}'  v-model="allSetting.tgTrafficDiff" :min="0"></setting-list-item>
                                <setting-list-item type="number" title='{{ i18n "pages.setting.tgNotifyCpu" }}' desc='{{ i18n "pages.setting.tgNotifyCpuDesc" }}'  v-model="allSetting.tgCpu" :min="0" :max="100"></setting-list-item>
                            </a-list>
                        </a-tab-pane>
                        <a-tab-pane key="5" tab='{{ i18n "pages.setting.otherSetting"}}'>
                            <a-list item-layout="horizontal" style="background: white">
                                <setting-list-item type="text" title='{{ i18n "pages.setting.timeZonee"}}' desc='{{ i18n "pages.setting.timeZoneDesc"}}' v-model="allSetting.timeLocation"></setting-list-item>
                            </a-list>
                        </a-tab-pane>
                    </a-tabs>
                </a-space>
            </a-spin>
        </a-layout-content>
    </a-layout>
</a-layout>
{{template "js" .}}
{{template "component/setting"}}
<script>

    const app = new Vue({
        delimiters: ['[[', ']]'],
        el: '#app',
        data: {
            siderDrawer,
            spinning: false,
            oldAllSetting: new AllSetting(),
            allSetting: new AllSetting(),
            saveBtnDisable: true,
            user: {},
            lang : getLang()
        },
        methods: {
            loading(spinning = true) {
                this.spinning = spinning;
            },
            async getAllSetting() {
                this.loading(true);
                const msg = await HttpUtil.post("/xui/setting/all");
                this.loading(false);
                if (msg.success) {
                    this.oldAllSetting = new AllSetting(msg.obj);
                    this.allSetting = new AllSetting(msg.obj);
                    this.saveBtnDisable = true;
                }
            },
            async updateAllSetting() {
                this.loading(true);
                const msg = await HttpUtil.post("/xui/setting/update", this.allSetting);
                this.loading(false);
                if (msg.success) {
                    await this.getAllSetting();
                }
            },
            async updateUser() {
                this.loading(true);
                const msg = await HttpUtil.post("/xui/setting/updateUser", this.user);
                this.loading(false);
                if (msg.success) {
                    this.user = {};
                }
            },
            async restartPanel() {
                await new Promise(resolve => {
                    this.$confirm({
                        title: '{{ i18n "pages.setting.restartPanel" }}',
                        content: '{{ i18n "pages.setting.restartPanelDesc" }}',
                        okText: '{{ i18n "sure" }}',
                        cancelText: '{{ i18n "cancel" }}',
                        onOk: () => resolve(),
                    });
                });
                this.loading(true);
                const msg = await HttpUtil.post("/xui/setting/restartPanel");
                this.loading(false);
                if (msg.success) {
                    this.loading(true);
                    await PromiseUtil.sleep(5000);
                    location.reload();
                }
            }
        },
        async mounted() {
            await this.getAllSetting();
            while (true) {
                await PromiseUtil.sleep(1000);
                this.saveBtnDisable = this.oldAllSetting.equals(this.allSetting);
            }
        },
        computed: {
            templateSettings: {
                get: function () { return this.allSetting.xrayTemplateConfig ? JSON.parse(this.allSetting.xrayTemplateConfig) : null ; },
                set: function (newValue) { this.allSetting.xrayTemplateConfig = JSON.stringify(newValue, null, 2) },
            },
            inboundSettings: {
                get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.inbounds, null, 2) : null; },
                set: function (newValue) {
                    newTemplateSettings = this.templateSettings;
                    newTemplateSettings.inbounds = JSON.parse(newValue)
                    this.templateSettings = newTemplateSettings
                },
            },
            outboundSettings: {
                get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.outbounds, null, 2) : null; },
                set: function (newValue) {
                    newTemplateSettings = this.templateSettings;
                    newTemplateSettings.outbounds = JSON.parse(newValue)
                    this.templateSettings = newTemplateSettings
                },
            },
            routingRuleSettings: {
                get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.routing.rules, null, 2) : null; },
                set: function (newValue) {
                    newTemplateSettings = this.templateSettings;
                    newTemplateSettings.routing.rules = JSON.parse(newValue)
                    this.templateSettings = newTemplateSettings
                },
            },
            torrentSettings: {
                get: function () {
                    torrentFilter = false
                    if(this.templateSettings != null){
                        this.templateSettings.routing.rules.forEach(routingRule => {
                            if(routingRule.hasOwnProperty("protocol")){
                                if (routingRule.protocol[0] === "bittorrent" && routingRule.outboundTag == "blocked"){
                                    torrentFilter = true
                                }
                            }
                        });
                    }
                    return torrentFilter
                },
                set: function (newValue) {
                    newTemplateSettings = JSON.parse(this.allSetting.xrayTemplateConfig);
                    if (newValue){
                        newTemplateSettings.routing.rules.push(JSON.parse("{\"outboundTag\": \"blocked\",\"protocol\": [\"bittorrent\"],\"type\": \"field\"}"))
                    }
                    else {
                        newTemplateSettings.routing.rules = [];
                        this.templateSettings.routing.rules.forEach(routingRule => {
                            if (routingRule.hasOwnProperty('protocol')){
                                if (routingRule.protocol[0] === "bittorrent" && routingRule.outboundTag == "blocked"){
                                    return;
                                }
                            }
                            newTemplateSettings.routing.rules.push(routingRule);
                        });
                    }
                    this.templateSettings = newTemplateSettings
                },
            },
            privateIpSettings: {
                get: function () {
                    localIpFilter = false
                    if(this.templateSettings != null){
                        this.templateSettings.routing.rules.forEach(routingRule => {
                            if(routingRule.hasOwnProperty("ip")){
                                if (routingRule.ip[0] === "geoip:private" && routingRule.outboundTag == "blocked"){
                                    localIpFilter = true
                                }
                            }
                        });
                    }
                    return localIpFilter
                },
                set: function (newValue) {
                    newTemplateSettings = JSON.parse(this.allSetting.xrayTemplateConfig);
                    if (newValue){
                        newTemplateSettings.routing.rules.push(JSON.parse("{\"outboundTag\": \"blocked\",\"ip\": [\"geoip:private\"],\"type\": \"field\"}"))
                    }
                    else {
                        newTemplateSettings.routing.rules = [];
                        this.templateSettings.routing.rules.forEach(routingRule => {
                            if (routingRule.hasOwnProperty('ip')){
                                if (routingRule.ip[0] === "geoip:private" && routingRule.outboundTag == "blocked"){
                                    return;
                                }
                            }
                            newTemplateSettings.routing.rules.push(routingRule);
                        });
                    }
                    this.templateSettings = newTemplateSettings
                },
            },
        }
    });

</script>
</body>
</html>