{{template "layout" .}}
<!-- iCheck for checkboxes and radio inputs -->
<link rel="stylesheet" href="/assets/plugins/iCheck/all.css">
{{define "content"}}
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                {{if eq .AppID ""}}
                    新增租户
                {{else}}
                    修改租户
                {{end}}
                <small>service manage</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin/index"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">服务管理</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <!-- right column -->
                <div class="col-md-12">
                    <a name="alert-warning"></a>
                    <div id="alert-warning" class="alert alert-warning alert-dismissible" style="display: none;">
                        <h4><i class="icon fa fa-warning"></i> Alert!</h4>
                        <label></label>
                    </div>
                    <form class="form-horizontal" method="post">
                        <div class="box box-info">
                            <div class="box-header with-border">
                                <h3 class="box-title">基本信息</h3>
                            </div>
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">租户id
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        {{if eq .AppID ""}}
                                            <input type="text" class="form-control" name="app_id" value="{{.AppID}}">
                                        {{else}}
                                            <input type="hidden" class="form-control" name="app_id" value="{{.AppID}}">
                                            <label class="form-control-static">{{.AppID}}</label>
                                        {{end}}
                                    </div>
                                    <div class="col-sm-3">
                                        唯一标识，支持：英文、数字、-、_
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">租户名称
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="name" value="{{.Name}}">
                                    </div>
                                    <div class="col-sm-3">
                                        租户名称
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">32位密钥
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="secret" value="{{.Secret}}">
                                    </div>
                                    <div class="col-sm-3">
                                        支持：英文、数字
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">请求方法
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        {{if eq .AppID ""}}
                                        <input type="text" class="form-control" name="method" value="any">
                                        {{else}}
                                        <input type="text" class="form-control" name="method" value="{{.Method}}">
                                        {{end}}
                                    </div>
                                    <div class="col-sm-3">
                                        选项有：any、get、post
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">本地与服务器时差
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="timeout" value="{{.Timeout}}">
                                    </div>
                                    <div class="col-sm-3">
                                        （s）计算签名时间与服务器时差
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">接口列表 </label>
                                    <div class="col-sm-7">
                                                        <textarea type="text" class="form-control" style="height: 100px;"
                                                                  name="open_api">{{.OpenAPI}}</textarea>
                                    </div>
                                    <div class="col-sm-3"> 支持前缀匹配<br/>每个接口一行 如：<br/>/gatekeeper/data_service/ping
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">客户端ip白名单
                                    </label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="white_ips" value="{{.WhiteIps}}">
                                    </div>
                                    <div class="col-sm-3">
                                        多条用以英文逗号分隔
                                    </div>
                                </div>
                                <div class="form-group" style="display: none">
                                    <label class="col-sm-2 control-label">城市ID
                                    </label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="city_ids" value="{{.CityIDs}}">
                                    </div>
                                    <div class="col-sm-3">

                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">日请求总量
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="total_query_daily" value="{{.TotalQueryDaily}}">
                                    </div>
                                    <div class="col-sm-3">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Qps限流
                                        <span class="text-red">*</span></label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="qps" value="{{.QPS}}">
                                    </div>
                                    <div class="col-sm-3">
                                        QPS （单机），0为不限制
                                    </div>
                                </div>
                                <div class="form-group" style="display: none">
                                    <label class="col-sm-2 control-label">group_id
                                    </label>
                                    <div class="col-sm-7">
                                        <input type="text" class="form-control" name="group_id" value="{{.GroupID}}">
                                    </div>
                                    <div class="col-sm-3">
                                    </div>
                                </div>
                            </div>
                            <div class="box-footer">
                                {{if eq .AppID ""}}
                                    <button type="button" id="add-btn" class="btn btn-primary">Submit</button>
                                {{else}}
                                    <button type="button" id="edit-btn" class="btn btn-primary">Submit</button>
                                {{end}}
                            </div>
                        </div>
                        <input name="id" type="hidden" value="{{.ID}}"/>
                    </form>
                </div>
                <!--/.col (right) -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
{{end}}
{{define "script"}}
    <!-- page script -->
    <script src="/assets/plugins/iCheck/icheck.min.js"></script>
    <script>
        $(function () {
            //iCheck for checkbox and radio inputs
            $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
                checkboxClass: 'icheckbox_minimal-blue',
                radioClass: 'iradio_minimal-blue'
            })
            //Red color scheme for iCheck
            $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
                checkboxClass: 'icheckbox_minimal-red',
                radioClass: 'iradio_minimal-red'
            })
            //Flat red color scheme for iCheck
            $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
                checkboxClass: 'icheckbox_flat-green',
                radioClass: 'iradio_flat-green'
            })
        })
        $("#add-btn").click(function(){
            $("#add-btn").attr("disabled",true);
            $.ajax({
                type: "post",
                url: "/admin/save_app",
                data: {
                    "app_id": $("input[name='app_id']").val(),
                    "name": $("input[name='name']").val(),
                    "secret": $("input[name='secret']").val(),
                    "method": $("input[name='method']").val(),
                    "timeout": $("input[name='timeout']").val(),
                    "open_api": $("textarea[name='open_api']").val(),
                    "white_ips": $("input[name='white_ips']").val(),
                    "city_ids": $("input[name='city_ids']").val(),
                    "total_query_daily": $("input[name='total_query_daily']").val(),
                    "qps": $("input[name='qps']").val(),
                    "group_id": $("input[name='group_id']").val(),
                    "id": $("input[name='id']").val(),
                },
                dataType: "json",
                complete: function (data) {
                    $("#alert-warning").show();
                    if(data.responseJSON.errno==0){
                        location.href="/admin/app_list";
                    }else{
                        console.log(data.responseJSON.errno);
                        $("#alert-warning label").html(data.responseJSON.errmsg);
                        location.href="#alert-warning";
                    }
                    $("#add-btn").attr("disabled",false);
                }
            });
            return false;
        });
        $("#edit-btn").click(function(){
            $("#edit-btn").attr("disabled",true);
            $.ajax({
                type: "post",
                url: "/admin/save_app",
                data: {
                    "app_id": $("input[name='app_id']").val(),
                    "name": $("input[name='name']").val(),
                    "secret": $("input[name='secret']").val(),
                    "method": $("input[name='method']").val(),
                    "timeout": $("input[name='timeout']").val(),
                    "open_api": $("textarea[name='open_api']").val(),
                    "white_ips": $("input[name='white_ips']").val(),
                    "city_ids": $("input[name='city_ids']").val(),
                    "total_query_daily": $("input[name='total_query_daily']").val(),
                    "qps": $("input[name='qps']").val(),
                    "group_id": $("input[name='group_id']").val(),
                    "id": $("input[name='id']").val(),
                },
                dataType: "json",
                complete: function (data) {
                    if(data.responseJSON.errno==0){
                        alert("保存成功!");
                        window.location.reload();
                    }else{
                        $("#alert-warning").show();
                        $("#alert-warning label").html(data.responseJSON.errmsg);
                        location.href="#alert-warning";
                    }
                    $("#edit-btn").attr("disabled",false);
                }
            });
            return false;
        });
    </script>
{{end}}