<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/bootstrap-table.css">
<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/table-fix-column.css">

<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/bootstrap-table.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/table-fix-column.js"></script>

<div class="content_wrapper">
    <div id="msg-tip"></div>
    <div class="container-fluid ">
        <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">
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/share" role="button">分享点</a>
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/share/2" role="button">分享奖励配置</a>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
                <aside>
                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm" action="#" style="margin-top: 20px">
                    <section>
                        <div class="container-fluid">
                            <button type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-danger" id="commitBtn">提交</button>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="table-responsive">
                                        <table id="roleTable" class="table table-bordered">
                                            <thead>
                                            <tr>
                                                <th>类型</th>
                                                <th>最大分享次数</th>
                                                <th>奖励</th>
                                                <th>间隔时间</th>
                                                <th>游戏内展示</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td>
                                                            <input type="text" class="form-control" readonly name="good[<?php echo $key;?>][type]" value="<?php echo $vl['type'];?>">
                                                        </td>
                                                        <td>
                                                            <input type="text" class="form-control" name="good[<?php echo $key;?>][maxTimes]" value="<?php echo $vl['maxTimes'];?>">
                                                        </td>
                                                        <td width="800px">
                                                            <div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;">
                                                                <?php foreach ($vl['reward'] as $k => $reward) { ?>
                                                                    <div class="form-group">
                                                                        <label class="col-sm-2 control-label">道具*: </label>
                                                                        <div class="col-sm-3">
                                                                            <select class="form-control typeControl" name="good[<?php echo $key;?>][reward][<?php echo $k;?>][propsId]" id="type_<?php echo $k;?>">
                                                                                <?php foreach ($idData as $x => $y) { ?>
                                                                                    <option value="<?php echo $x;?>" <?php if($reward['propsId'] == $x) echo 'selected'; ?>><?php echo $y['name'];?></option>
                                                                                <?php } ?>
                                                                            </select>
                                                                        </div>
                                                                        <label class="col-sm-2 control-label">数量*: </label>
                                                                        <div class="col-sm-2">
                                                                            <input type="text" class="form-control"  name="good[<?php echo $key;?>][reward][<?php echo $k;?>][num]" value="<?php echo $reward['num'];?>">
                                                                        </div>
                                                                        <button type="button" class="btn btn-danger deleteParent" >删除</button>
                                                                    </div>
                                                                <?php } ?>
                                                                <button type="button" class="btn btn-success addParent" gid="<?php echo $key;?>" k="<?php echo count($vl['reward']);?>" style="margin-left: 12px;margin-bottom: 10px;">增加</button>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <input type="text" class="form-control" name="good[<?php echo $key;?>][intervalTime]" value="<?php echo $vl['intervalTime'];?>">
                                                        </td>
                                                        <td>
                                                            <input class='mySwitch' type="checkbox" name="good[<?php echo $key;?>][show]" value="<?php echo $vl['show'];?>" <?php if($vl['show'] == 1){ echo 'checked';}?> >
                                                        </td>
                                                    </tr>
                                                <?php } } else{?>
                                                <tr>
                                                    <td colspan="14">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                        <!--                                        <button type="button"  class="btn btn-primary"  id="addBtn"><i class="fa fa-plus-square"></i>添加</button>-->
                                    </div>
                                </div>

                            </div>
                        </div>
                    </section>
                    </form>
                </aside>
            </div>
        </div>
    </div>


</div>


<script>
    var idData = JSON.parse('<?php echo json_encode($idData); ?>');

    $('.addOrDelControl').on("click",'.deleteParent',function(){
        $(this).parent().remove();
    });

    $('.addParent').on("click",function(){
        var gid = $(this).attr('gid');
        var k = $(this).attr('k');

        var str = "";
        str += '<div class="form-group"><label class="col-sm-2 control-label">道具*: </label><div class="col-sm-3"><select class="form-control" name="good['+gid+'][reward]['+k+'][propsId]">';
        for(var x in idData) {
            str += '<option value="'+x+'">'+idData[x]['msg']+'</option>';
        }
        str += '</select></div><label class="col-sm-2 control-label">数量*: </label><div class="col-sm-2"><input type="text" class="form-control"  name="good['+gid+'][reward]['+k+'][num]" value="1">'+
            '</div><button type="button" class="btn btn-danger deleteParent" >删除</button></div>';
        $(this).before(str);

        k++;
        $(this).attr("k",k);
    });

    //提交表格数据
    $('#commitBtn').click(function () {
        var data = new FormData(document.getElementById("goodForm"));
        $.ajax({
            url: rootPath + 'config/actionShareConfig/2',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                alert(data.msg);
                // window.location.reload();
            },
            error: function (msg) {
                alert(data.msg);
            }
        });
    });

    $(".mySwitch").bootstrapSwitch({
        onSwitchChange:function(event, state){
            if(state){
                var state = 1;
            }else{
                var state = 0;
            }
            $(this).val(state);
        }
    });
</script>

