<!-- 模态框（Modal） -->
<div>
<div class="modal fade bs-example-modal-lg" id="configModel" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">更改配置</h4>
            </div>
            <div class="modal-body">
                <ul class="nav nav-tabs" id="config_model_group_id"></ul>
                <div class="tab-content" id="config_model_group_content_id"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn btn-link" id="but_initConfigId">初始化配置信息</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
</div>
<div>
    <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" id="configValueModel_id">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <h4 class="modal-title">查看/修改</h4>
                </div>
                <div class="modal-body">
                    <form class="bs-example bs-example-form" role="form" id="configValueModelForm" method="post" action="${ctx!}/config/post/save">
                        <div class="input-group">
                            <span class="input-group-addon">配置项：</span>
                            <input type="text" class="form-control" value="哈哈">
                        </div>
                        <br>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="submitForm();">提交并使用</button>
                </div>
            </div>
        </div>
    </div>
</div>
<#include "/share/configure/premium_config.html">
<script>
    /**
     * 注入表单元素
     **/
    function preForm(groupKey,franId){
        console.log("C端计费保单元素",franId)
        $("#table_"+groupKey).bootstrapTable({
            url: '${ctx}/config/get/list/group',
            pagination: true,
            uniqueId:'id',
            queryParams:function (params){return {
                "groupKey":groupKey,
                "franId":franId,
                pageSize: params.limit, // 每页要显示的数据条数
                offset: params.offset, // 每页显示数据的开始行号
                sort: params.sort, // 要排序的字段
                sortOrder: params.order, // 排序规则,
                showRefresh: true
            }},
            columns: [{
                field: 'id',
                title: 'ID',
                align:'center'
            },{
                field: 'franName',
                title: '加盟商',
                align:'center'
            },{
                field: 'configName',
                title: '配置项名称',
                align:'center'
            }, {
                field: 'configKey',
                title: '配置项名Key',
                align:'center'
            },{
                field: 'configStatus',
                title: '状态',
                align:'center'
            },/*{
                field: 'configRmk',
                title: '备注',
                align:'center'
            },*/{
                title: "操作",
                formatter:function (value, row, index){
                    return '<a href="javascript:showConfigValueModel(\''+groupKey+'\','+row.id+');"> 更改</a>&nbsp;'+
                        '<a href="javascript:deleteConfig(\''+groupKey+'\','+row.id+');"> 删除</a>&nbsp;'+
                        '<a href="javascript:cloneNewConfig(\''+groupKey+'\','+row.id+');"> 克隆</a>';
                },
                align:'center'
            }, {
                title: "排序",
                formatter:function (value, row, index){
                    return '<a href="javascript:sortConfig(\''+groupKey+'\','+row.id+',\'up\');" class="mdi mdi-arrow-up-bold mdi-18px" style="color: #006cfa" title="上移一行">&nbsp;&nbsp;'+
                        '<a href="javascript:sortConfig(\''+groupKey+'\','+row.id+',\'down\');" class="mdi mdi-arrow-down-bold mdi-18px" style="color: #eea236" title="下移一行"';
                },
                align:'center'
            }
            ]
        });
    }
    /**
     * 打开修改配置项值的模态框
     **/
    function showConfigValueModel(table,id){
        $('#configModel').modal('hide');
        $("#configValueModel_id").modal('show');
        $('#configValueModel_id').on('hidden.bs.modal', function () {
            $('#configModel').modal('show');
        });
        let configValue=$('#table_'+table).bootstrapTable('getRowByUniqueId',id).configValue;
        let valueStr='<input type="hidden" name="id" value="'+id+'">'+
                     '<input type="hidden" name="configGroup" value="'+table+'" id="configFormActiveId">';
        let i=0;
        for(let key in configValue){
            valueStr=valueStr+'<div class="input-group">' +
                '             <span class="input-group-addon">'+key+'</span>' +
                '             <input type="hidden" name="keys" value="'+key+'">'+
                '             <input type="text" class="form-control" name="values" value="'+configValue[key]+'">' +
                '         </div><br>';
            i++;
        }
        document.getElementById("configValueModelForm").innerHTML=valueStr;
    }

    /**
     * 删除配置项
     * @param id
     */
    function deleteConfig(table,id){
        $.confirm({
            title: '删除确认',
            content: '您即将删除该条配置项，是否继续？',
            type: 'red',
            typeAnimated: true,
            buttons: {
                tryAgain: {
                    text: '继续',
                    btnClass: 'btn-blue',
                    action: function(){
                        document.getElementById("js_load").style.display = "inline";
                        $.ajax({
                            type: "GET",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "${ctx!}/config/get/delete" ,
                            data: {"configId":id},
                            success: function (res) {
                                if (res.status==200){
                                    lightyear.notify('操作成功!', 'success');
                                    document.getElementById("js_load").style.display = "none";
                                    $('#table_'+table).bootstrapTable('refresh',{url: "${ctx}/config/get/list/group"});
                                    return;
                                }
                                document.getElementById("js_load").style.display = "none";
                                lightyear.notify(res.message, 'danger');
                            },
                            error : function() {
                                document.getElementById("js_load").style.display = "none";
                                alert("异常！");
                            }
                        })
                    }
                },
                close: {
                    text: '放弃'
                }
            }
        });
        return;
    }
    /**
     * 克隆一个配置项目
     * */
    function cloneNewConfig(table,id){
        document.getElementById("js_load").style.display = "inline";
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "${ctx!}/config/get/clone" ,
            data: {"configId":id},
            success: function (res) {
                if (res.status==200){
                    lightyear.notify('克隆成功!', 'success');
                    document.getElementById("js_load").style.display = "none";
                    $('#table_'+table).bootstrapTable('refresh',{url: "${ctx}/config/get/list/group"});
                    return;
                }
                document.getElementById("js_load").style.display = "none";
                lightyear.notify(res.message, 'danger');

            },
            error : function() {
                document.getElementById("js_load").style.display = "none";
                alert("异常！");
            }
        });

    }

    /**
     * 排序
     **/
    function sortConfig(table,id,type){
        document.getElementById("js_load").style.display = "inline";
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "${ctx!}/config/get/sort" ,
            data: {"configId":id,"type":type},
            success: function (res) {
                if (res.status===200){
                    document.getElementById("js_load").style.display = "none";
                    $('#table_'+table).bootstrapTable('refresh',{url: "${ctx}/config/get/list/group"});
                    return;
                }
                document.getElementById("js_load").style.display = "none";
                lightyear.notify(res.message, 'danger');
            },
            error : function() {
                document.getElementById("js_load").style.display = "none";
                lightyear.notify("链接失败，请检查网络~", 'danger');
            }
        });

    }

    /**
     * 提交表单
     **/
    function submitForm(){
        document.getElementById("js_load").style.display = "inline";
        $.ajax({
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "${ctx!}/config/post/save" ,
            data: $('#configValueModelForm').serialize(),
            success: function (res) {
                if (res.status==200){
                    lightyear.notify('操作成功!', 'success');
                    document.getElementById("js_load").style.display = "none";
                    $("#configValueModel_id").modal('hide');
                    $('#table_'+$("#configFormActiveId").val()).bootstrapTable('refresh',{url: "${ctx}/config/get/list/group"});
                    return;
                }
                document.getElementById("js_load").style.display = "none";
                lightyear.notify(res.message, 'danger');

            },
            error : function() {
                document.getElementById("js_load").style.display = "none";
                alert("异常！");
            }
        });
    }
    /**
     * 添加一个配置项
     * @param groupKey
     * @param franId
     * @param templateCode
     */
    function addNewConfig(groupKey,franId){
        $.ajax({
            data:{
                "groupKey":groupKey,
                "franId":franId,
            },
            type: "GET",
            url: "${ctx}/config/add/configure",
            async: false,
            success :function(res){
                if (res.status==200){
                    lightyear.notify('新增成功!', 'success');
                    document.getElementById("js_load").style.display = "none";
                    $('#table_'+table).bootstrapTable('refresh',{url: "${ctx}/config/get/list/group"});
                    return;
                }
                document.getElementById("js_load").style.display = "none";
                lightyear.notify(res.message, 'danger');
            },
            error:function(res){
                lightyear.notify("网络连接异常，请检查网络设置", 'danger');
            }
        });
    }


    /**
     * 初始化配置
     * */
    function initConfig(group,franId){
        $.confirm({
            title: '初始化配置确认',
            content: '您即将执行初始化操作，将为会您创建一套默认配置，是否继续？',
            type: 'blue',
            typeAnimated: true,
            buttons: {
                tryAgain: {
                    text: '继续',
                    btnClass: 'btn-blue',
                    action: function(){
                        runAjax("${ctx!}/config/get/init","GET",{"groups":Object.keys(group).toString(),"franId":franId},function (res){
                            lightyear.notify('操作成功!', 'success');
                            $('#configModel').modal('hide');
                        });
                    }
                },
                close: {
                    text: '放弃'
                }
            }
        });

    }

    /**
     * 获取对象第一个元素key
     * @param obj
     * @returns {*}
     */
    function getOne(obj){
        for(let key in obj){
            return key;
        }
    }

</script>