<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 langtag="page-hostlist"></h5>

                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="content">
                    <div class="table-responsive">
                        <div id="toolbar">
                            <a class="btn btn-primary dim" href="{{.web_base_url}}/index/addhost?vkey={{.task_id}}&client_id={{.client_id}}">
                                <i class="fa fa-fw fa-lg fa-plus"></i>
                                <span langtag="word-add"></span>
                            </a>
                        </div>
                        <table class="table-striped table-hover" data-mobile-responsive="true" id="taskList_table"></table>
                    </div>
                </div>
                <div class="ibox-content">

                    <table id="table"></table>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
    /*bootstrap table*/
    $('#table').bootstrapTable({
        toolbar: "#toolbar",
        method: 'post', // 服务器数据的请求方式 get or post
        url: window.location, // 服务器数据的加载地址
        queryParams: function (params) {
            return {
                "offset": params.offset,
                "limit": params.limit,
                "client_id": {{.client_id}},
                "search": params.search,
                "sort": params.sort,
                "order": params.order
            }
        },
        search: true,
        contentType: "application/x-www-form-urlencoded",
        striped: true, // 设置为true会有隔行变色效果
        showHeader: true,
        showColumns: true,
        showRefresh: true,
        pagination: true,//分页
        sidePagination: 'server',//服务器端分页
        pageNumber: 1,
        pageList: [5, 10, 20, 50, 100, 'all'],
        cookie: true,
        cookieIdTable: 'host',
        cookieStorage: 'localStorage',
        detailView: true,
        smartDisplay: true, // 智能显示 pagination 和 cardview 等
        onExpandRow: function () { $('body').setLang('.detail-view'); },
        onLoadSuccess:function (data) { $('body').setLang('.detail-view'); },
        onPostBody: function (data) { if ($(this)[0].locale != undefined ) $('body').setLang('#table'); },
        detailFormatter: function (index, row, element) {
            tmp = '<b langtag="word-exportflow"></b>: <span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow\'})">' + changeunit(row.Flow.ExportFlow) + '</span>&emsp;'
                + '<b langtag="word-inletflow"></b>: <span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow\'})">' + changeunit(row.Flow.InletFlow) + '</span>&emsp;'
                + '<b langtag="word-crypt"></b>: <span langtag="word-' + row.Client.Cnf.Crypt + '"></span>&emsp;'
                + '<b langtag="word-compress"></b>: <span langtag="word-' + row.Client.Cnf.Compress + '"></span>&emsp;'
                + '<b langtag="word-flowlimit"></b>: <span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow_limit\'})">' + row.Flow.FlowLimit + 'MB</span>&emsp;'
                + '<b langtag="word-timelimit"></b>: <span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'time_limit\'})">' + row.Flow.TimeLimit + '</span>&emsp;<br/><br>'
                + '<b langtag="word-autohttps"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'auto_https\'})" langtag="word-' + row.AutoHttps + '"></span>&emsp;'
                + '<b langtag="word-autocors"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'auto_cors\'})" langtag="word-' + row.AutoCORS + '"></span>&emsp;'
                + '<b langtag="word-compatmode"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'compat_mode\'})" langtag="word-' + row.CompatMode + '"></span>&emsp;'
                + '<b langtag="word-proxyprotocoltitle"></b>: <span langtag="word-proxyprotocolv' + row.Target.ProxyProtocol + '"></span>&emsp;'
                + '<b langtag="word-httpsjustproxytitle"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'https_just_proxy\'})" langtag="word-' + row.HttpsJustProxy + '"></span>&emsp;'
                + '<b langtag="word-tlsoffloadtitle"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'tls_offload\'})" langtag="word-' + row.TlsOffload + '"></span>&emsp;'
                + '<b langtag="word-targettype"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'target_is_https\'})" langtag="word-ishttps' + row.TargetIsHttps + '"></span>&emsp;<br/><br>'
            if (row.Client.Cnf.U || row.Client.Cnf.P) {
                tmp += '<b langtag="word-basicusername"></b>: <span onclick="oCopy(this)">' + row.Client.Cnf.U + '</span>&emsp;'
                + '<b langtag="word-basicpassword"></b>: <span onclick="oCopy(this)">' + row.Client.Cnf.P + '</span>&emsp;<br/><br>'
            }
            if (row.UserAuth && row.UserAuth.Content) {
                tmp += '<b langtag="word-auth"></b>: ' + (row.UserAuth && row.UserAuth.Content ? row.UserAuth.Content : '') + '&emsp;<br/><br>'
            }
            if (row.HostChange) {
                tmp += '<b langtag="word-requesthost"></b>: <span onclick="oCopy(this)">' + row.HostChange + '</span>&emsp;<br/><br>'
            }
            if (row.HeaderChange) {
                tmp += '<b langtag="word-requestheader"></b>: ' + row.HeaderChange + '&emsp;<br/><br>'
            }
            if (row.RespHeaderChange) {
                tmp += '<b langtag="word-responseheader"></b>: ' + row.RespHeaderChange + '&emsp;<br/><br>'
            }
            if (row.RedirectURL) {
                tmp += '<b langtag="word-redirecturl"></b>: <span onclick="oCopy(this)">' + row.RedirectURL + '</span>&emsp;<br/><br>'
            }
            tmp += '<b langtag="word-autossl"></b>: <span onclick="submitform(\'turn\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'auto_ssl\'})" langtag="word-' + row.AutoSSL + '"></span>&emsp;'
            if (row.Scheme != "http" && row.HttpsJustProxy == false && row.AutoSSL == false && (row.CertFile || row.KeyFile)) {
                return tmp + "<br/><br>"
                + '<b langtag="word-httpscert"></b>: <div onclick="oCopy(this)" style="height:60px; max-width:75vw; overflow:auto; white-space:nowrap; border:1px solid #ccc; padding:5px; box-sizing:border-box;">' + row.CertFile + '</div>&emsp;<br/>'
                + '<b langtag="word-httpskey"></b>: <div onclick="oCopy(this)" style="height:60px; max-width:75vw; overflow:auto; white-space:nowrap; border:1px solid #ccc; padding:5px; box-sizing:border-box;">' + row.KeyFile + '</div>&emsp;<br/><br>'
            }
            return tmp
        },
        // 表格的列
        columns: [
            {
                field: 'Id', //域值
                title: '<span langtag="word-id"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'Client.Id', //域值
                title: '<span langtag="word-clientid"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    var text = row.Client.Remark ? (row.Client.Id + ' · ' + row.Client.Remark) : row.Client.Id
                    return '<span onclick="copyText(\'' + row.Client.VerifyKey + '\')">' + text + '</span>'
                }
            },
            {
                field: 'Remark', //域值
                title: '<span langtag="word-remark"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'Client.VerifyKey', //域值
                title: '<span langtag="word-verifykey"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'Host', //域值
                title: '<span langtag="word-host"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    hosturl = ((row.Scheme == 'http') ? 'http://' : 'https://') + row.Host + ((row.Scheme == 'http') ? ":{{.httpProxyPort}}" : ":{{.httpsProxyPort}}") + row.Location
                    return '<a href="' + hosturl + '" target="_blank">' + row.Host + '</a>'
                }
            },
            {
                field: 'Scheme', //域值
                title: '<span langtag="word-scheme"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span langtag="word-' + value + '"></span>'
                }
            },
            {
                field: 'TargetIsHttps', //域值
                title: '<span langtag="word-targettype"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-ishttps' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'target_is_https\'})"></span>'
                    } else {
                        return '<span langtag="word-ishttps' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'target_is_https\'})"></span>'
                    }
                }
            },
            {
                field: 'Target.TargetStr', //域值
                title: '<span langtag="word-target"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'Location', //域值
                title: '<span langtag="word-path"></span>', //标题
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'PathRewrite', //域值
                title: '<span langtag="word-pathrewrite"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'HttpsJustProxy', //域值
                title: '<span langtag="word-httpsjustproxytitle"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'https_just_proxy\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'https_just_proxy\'})"></span>'
                    }
                }
            },
            {
                field: 'TlsOffload', //域值
                title: '<span langtag="word-tlsoffloadtitle"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'tls_offload\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'tls_offload\'})"></span>'
                    }
                }
            },
            {
                field: 'AutoSSL', //域值
                title: '<span langtag="word-autossl"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'auto_ssl\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'auto_ssl\'})"></span>'
                    }
                }
            },
            {
                field: 'AutoHttps', //域值
                title: '<span langtag="word-autohttpstitle"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'auto_https\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'auto_https\'})"></span>'
                    }
                }
            },
            {
                field: 'AutoCORS', //域值
                title: '<span langtag="word-autocorstitle"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'auto_cors\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'auto_cors\'})"></span>'
                    }
                }
            },
            {
                field: 'CompatMode', //域值
                title: '<span langtag="word-compatmode"></span>', //标题
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'stop\', \'{{.web_base_url}}/index/stophost\', {\'id\':' + row.Id + ', \'mode\': \'compat_mode\'})"></span>'
                    } else {
                        return '<span langtag="word-' + value + '" onclick="submitform(\'start\', \'{{.web_base_url}}/index/starthost\', {\'id\':' + row.Id + ', \'mode\': \'compat_mode\'})"></span>'
                    }
                }
            },
            {
                field: 'InletFlow', //域值
                title: '<span langtag="word-inletflow"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow\'})">' + changeunit(row.Flow.InletFlow) + '</span>'
                }
            },
            {
                field: 'ExportFlow', //域值
                title: '<span langtag="word-exportflow"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow\'})">' + changeunit(row.Flow.ExportFlow) + '</span>'
                }
            },
            {
                field: 'TotalFlow', //域值
                title: '<span langtag="word-totalflow"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow\'})">' + changeunit(row.Flow.InletFlow + row.Flow.ExportFlow) + '</span>'
                }
            },
            {
                field: 'FlowRemain', //域值
                title: '<span langtag="word-flowremain"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (row.Flow.FlowLimit === 0) {
                        return '∞'
                    }
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow_limit\'})">' + changeunit(row.Flow.FlowLimit*1024*1024 - row.Flow.InletFlow - row.Flow.ExportFlow) + '</span>'
                }
            },
            {
                field: 'TimeRemain', //域值
                title: '<span langtag="word-timeremain"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'time_limit\'})">' + getRemainingTime(row.Flow.TimeLimit).formatted + '</span>'
                }
            },
            {
                field: 'Flow.FlowLimit', //域值
                title: '<span langtag="word-flowlimit"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return '<span langtag="word-false"></span>'
                    }
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'flow_limit\'})">' + changeunit(value * 1024 * 1024) + '</span>'
                }
            },
            {
                field: 'Flow.TimeLimit', //域值
                title: '<span langtag="word-timelimit"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: false, //false表示不显示
                sortable: true, //启用排序
                formatter: function (value, row, index) {
                    if (value === "0001-01-01T00:00:00Z") {
                        return '<span langtag="word-false"></span>'
                    }
                    return '<span onclick="submitform(\'clear\', \'{{.web_base_url}}/index/clearhost\', {\'id\':' + row.Id + ', \'mode\': \'time_limit\'})">' + value + '</span>'
                }
            },
            {
                field: 'NowConn', //域值
                title: '<span langtag="word-nowconn"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    return '<span onclick="oCopy(this)">' + value + '</span>'
                }
            },
            {
                field: 'IsClose', //域值
                title: '<span langtag="word-runstatus"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    if (value) {
                        return '<span class="badge badge-badge" langtag="word-close"></span>'
                    } else {
                        return '<span class="badge badge-primary" langtag="word-open"></span>'
                    }
                }
            },
            {
                field: 'Client.IsConnect', //域值
                title: '<span langtag="word-clientstatus"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: true, //false表示不显示
                sortable: true,//启用排序
                formatter: function (value, row, index) {
                    hosturl = ((row.Scheme == 'http') ? 'http://' : 'https://') + row.Host + row.Location
                    if (value) {
                        return '<a href="' + hosturl + '" target="_blank"><span class="badge badge-primary" langtag="word-online"></span></a>'
                    } else {
                        return '<span class="badge badge-badge" langtag="word-offline"></span>'
                    }
                }
            },
            {
                field: 'option', //域值
                title: '<span langtag="word-option"></span>', //内容
                align: 'center',
                halign: 'center',
                visible: true, //false表示不显示
                formatter: function (value, row, index) {
                    btn_group = '<div class="btn-group">'
                    if (row.IsClose) {
                        btn_group += "<a onclick=\"submitform('start', '{{.web_base_url}}/index/starthost', {\'id\':" + row.Id
                        btn_group += '})" class="btn btn-outline btn-primary"><i class="fa fa-play"></i></a>'
                    } else {
                        btn_group += "<a onclick=\"submitform('stop', '{{.web_base_url}}/index/stophost', {'id':" + row.Id
                        btn_group += '})" class="btn btn-outline btn-warning"><i class="fa fa-pause"></i></a>'
                    }
                    btn_group += "<a onclick=\"submitform('delete', '{{.web_base_url}}/index/delhost', {'id':" + row.Id
                    btn_group += '})" class="btn btn-outline btn-danger"><i class="fa fa-trash"></i></a>'
                    btn_group += '<a href="{{.web_base_url}}/index/edithost?id=' + row.Id
                    btn_group += '" class="btn btn-outline btn-success"><i class="fa fa-edit"></i></a></div>'
                    return btn_group
                }
            }
        ]
    });
</script>
