<link rel="stylesheet" href="<?php echo base_url();?>public/js/date/datedropper.min.css">
<script src="<?php echo base_url();?>public/js/date/datedropper.min.js"></script>

<!-- Select2 -->
<link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/select2/select2.min.css">
<script src="<?php echo base_url();?>public/js/vendor/select2/select2.full.min.js"></script>

<div class="content_wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-1">
                                    <a style="margin:5px;" class="btn btn-success" href="#debug/file/firebase/firebase" role="button">配图上传</a>
                                </div>
                                <div class="col-sm-1">
                                    <a style="margin:5px;" class="btn btn-success" target="_blank" href="/export/pushConfig" role="button">导出语言配置</a>
                                </div>
                                <div class="col-sm-1">
                                    <button style="margin:5px;" class="btn btn-info"  onclick="selectFile()" role="button">导入语言配置</button>
                                    <input id="importFile" type="file" accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style="display: none" name="importFile" >
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>推送点配置</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="table-responsive">
                                        <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                            <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>大类</th>
                                                <th>小类</th>
                                                <th>配置</th>
                                                <th style="width: 10%;">配图(格式：{id}.png)</th>
                                                <th style="width: 10%;">操作</th>
                                            </tr>
                                            </thead>
                                            <form class="form-horizontal" role="form" method="post" action="#" data-callBack="" id="contentForm">
                                                <tbody class="content-body">
                                                <?php foreach($list as $v) { ?>
                                                    <tr>
                                                        <td><input name="id[]" type="hidden"  value="<?=$v['id']?>"><?php echo $v['id']?></td>
                                                        <td><input name="name[]" type="hidden"  value="<?=$v['name']?>"><?php echo $v['name']?></td>
                                                        <td><input name="class[]" type="hidden"  value="<?=$v['class']?>"><?php echo $v['class']?></td>
                                                        <td><input name="type[]" type="hidden"  value="<?=$v['type']?>"><?php echo $v['type']?></td>
                                                        <td>
                                                            <textarea readonly="readonly"  name="conf[]"  class="form-control show" rows="5" pid="<?php echo $v['id']?>" id="conf_<?php echo $v['id']?>"><?php echo $v['conf']?></textarea>
                                                        </td>
                                                        <td style="width:10%">
                                                            <img style="height: 60px;" src="/public/game/image/firebase/<?php echo $v['id']; ?>.png?time=<?php echo time();?>" alt="">
                                                        </td>
                                                        <td>
                                                            <button data-id="<?=$v['id']?>" type="button" class="btnDel  btn btn-danger">删除</button>
                                                        </td>
                                                    </tr>
                                                <?php } ?>

                                                </tbody>
                                            </form>
                                            <tbody class="botton-body">
                                                <tr>
                                                    <td colspan="7">
                                                        <button type="button" class="btnAdd btn btn-info">增加</button>
<!--                                                        <button type="button" class="btnSubmit btn btn-success">提交</button>-->
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="actionShow" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="top: 20%;width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">推送信息</h4>
            </div>
            <form class="form-horizontal" role="form" method="post" action="#" data-callBack="" id="conf_set">
                <div class="modal-body">
                    <table id="langDivShow" class="table table-bordered">
                        <thead><tr><th>语言</th><th>标题</th><th>内容</th></tr></thead>
                        <tbody id="table_body">
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="saveSub">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var classes = {
        'activityOnline' :'activityOnline'
        ,'other' :'other'
        // ,'activityReward' :'activityReward'
    };
    var acts = '<?php echo json_encode($acts);?>'
    var langList = '<?php echo json_encode($langList);?>'
    if(typeof acts == 'string')acts = JSON.parse(acts)
    if(typeof langList == 'string')langList = JSON.parse(langList)
    console.log(langList)
    $('.show').click(function () {
        $("#table_body").children("tr").remove();
        var data = $(this).val();
        var html='';
        data = JSON.parse(data);
        var id=$(this).attr("pid");
        html+='<tr><input type="hidden" name="id" value="'+id+'"></tr>';
        for (key in data){
            html +='<tr>' +
                '<td>'+key+':'+langList[key]+'</td>' +
                '<td><input type="text" name="conf['+key+'][title]" value="'+data[key]['title']+'" class="form-control"></td>' +
                '<td style="width:300px"><input type="text" name="conf['+key+'][content]" value="'+data[key]['content']+'" class="form-control" style="width:800px"></td>' +
                '</tr>';
        }
        $("#table_body").append(html);
        $('#actionShow').modal('show')
    });
    $('#saveSub').click(function(){
        var datas = $('#conf_set').serializeArray();
        var id = datas[0].value;
        // console.log(id,datas);
        $.ajax({
            url: rootPath + 'push/saveConfig',
            type: 'POST',
            data: datas,
            dataType:'json',
            success: function (data) {
                alert(data.msg);
                $('#conf_'+id).html(JSON.stringify(data.data));
            },
            error: function (data) {
                alert(data.msg);
            }
        });
    });

    $('.btnAdd').click(function(){
        var html = ''+
            " <tr>\n" +
            "   <td>-</td>\n" +
            "   <td><input name=\"name\" value=\"\"></td>\n" +
            "   <td class='classTd'><select name=\"class\" class='select2 inputField'>"+getClassOptions()+"</select> </td>\n" +
            "   <td class='typeTd'><select name=\"type\" class='select2 inputField'>"+getActOptions()+"</select></td>\n" +
            "   <td>\n" +
            "       <textarea readonly=\"readonly\"  name=\"conf\"  class=\"form-control show\" rows=\"5\" >{}</textarea>\n" +
            "   </td>\n" +
            "   <td style=\"width:10%\">\n" +
            "       <img style=\"height: 60px;\" src=\"\" alt=\"\">\n" +
            "   </td>\n" +
            "   <td style=\"width:10%\">\n" +
            "       <button data-id=\"0\" type=\"button\" class=\"btnDel  btn btn-danger\" onclick='delRecord(this)'>删除</button>\n" +
            "       <button data-id=\"0\" type=\"button\" class=\"btnSaveNew  btn btn-danger\" onclick='saveRecord(this)'>保存</button>\n" +
            "   </td>\n" +
            "</tr>";
        $('.content-body').append(html)
        $('.select2').select2({})
    })

    $('body').on('change', 'select[name=class]', function(){
        var typeTd = $(this).parents('tr').find('.typeTd')
        typeTd.empty();
        switch ($(this).val()){
            case 'activityOnline':
                typeTd.html("<select name=\"type\" class='inputField select2'>"+getActOptions()+"</select>");
                return;
            case 'other':
                typeTd.html("<input name=\"type\" class='inputField form-control' >");
                return;
        }
        console.log()
    })
    function getActOptions(){
        var html = '<option value="-1">选择推送点小类</option>';
        $.each(acts, function(i, o){
            html += "<option value='"+i+"'>" + o.tips+ "--"+ i + "</option>"
        })
        return html
    }
    function getClassOptions(){
        var html = '<option value="-1">选择推送点大类</option>';
        $.each(classes, function(i, o){
            html += "<option value='"+i+"'>" + o + "</option>"
        })
        return html
    }

    function saveRecord(obj){
        var _this = $(obj)
        var data = {
            'name' : _this.parents('tr').find('input[name=name]').val(),
            'class' : _this.parents('tr').find('.classTd').find('.inputField').val(),
            'type' : _this.parents('tr').find('.typeTd').find('.inputField').val(),
            'conf' : _this.parents('tr').find('textarea[name=conf]').val(),
        }
        console.log(data)

        $.post('/push/addConfig',data, function(res){
            if(typeof res == 'string')res = JSON.parse(res)
            alert(res.msg)
            if(res.ret > 0){
                window.location.reload()
            }
        })
    }

    function delRecord(obj){
        var _this = $(obj)
        var id = (_this.data('id'))
        if(id >= 1){
            if(confirm('是否确定要删除改推送点？')){
                $.post('/push/delConfig/'+id,{}, function(res){
                    if(typeof res == 'string')res = JSON.parse(res)
                    alert(res.msg)
                    if(res.ret > 0){
                        _this.parents('tr').remove()
                    }
                })
            }
        }else{
            _this.parents('tr').remove()
        }

    }
    $('.btnDel').click(function(){
        delRecord(this);
    })

    function selectFile(){
        $('#importFile').click();
    }

    $("#importFile").change(function() {
        var _this = $(this)
        var filePath = _this.val();
        if (filePath) {
            var formData = new FormData();
            formData.append("file",document.getElementById('importFile').files[0]);
            showLoading()
            $.ajax({
                type: "POST", // 数据提交类型
                url: 'import/pushConfig',//"activity/importSerialPack", // 发送地址
                data: formData, //发送数据
                processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
                contentType: false, //
                success: function(data) { // 成功回调
                    hideLoading()
                    _this.val('');
                    data = JSON.parse(data);
                    console.log(data)
                    alert(data.msg)
                    if(data.ret >=0){
                        window.location.reload();
                    }
                },
                error:function(e,x,h){
                    hideLoading()
                    _this.val('');
                    alert(h)
                }
            });
        } else {
            return false
        }
    });
</script>