<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改活动</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/bootstrap/bootstrap.min.css" >
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/css/bootstrap-datepicker.standalone.min.css">
    <link rel="stylesheet" href="<?php echo base_url();?>public/css/jquery.timepicker.min.css">
    <!-- Select2 -->
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/select2/select2.min.css">

    <style type="text/css">
        .thumbnail {
            margin:10px;
        }

        .lang-name {
            margin-left:42px;
        }

        .upimg {
            cursor: pointer;
        }

    </style>
</head>
<body>
<div id="msg-tip">

</div>
<div class="content_wrapper">
    <div class="container-fluid">
        <aside>
        	
        	<?php 
            foreach($data as $sid => $list){ 
            ?>
            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm<?php echo $sid; ?>" action="#" style="margin-top: 20px">
                <header><i class="fa fa-fw fa-file"></i>邀请奖励配置 <?php echo $sid==1?'安卓':'IOS'; ?></header>
                <section>
                	<input type="hidden" name="sid" value="<?php echo $sid; ?>">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <button type="button" class="btn btn-success" id="goodsubmitBtn<?php echo $sid; ?>">提交</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <div class="col-sm-12">
	                        <?php foreach ($list as $key => $vl) { ?>
	                            <div class="row AllTypeControl" style="border: 1px solid #CCC;margin:10px 5px;padding: 5px 0;">
	                                <div class="form-group" >
	                                    <label class="col-sm-1 control-label">区间ID*: </label>
	                                    <div class="col-sm-1">
	                                        <input type="text" class="form-control" readonly name="list[<?php echo $vl['section_id'];?>][section_id]" value="<?php echo $vl['section_id'];?>">
	                                    </div>
	                                    <label class="col-sm-1 control-label">开始等级*: </label>
	                                    <div class="col-sm-1">
	                                        <input type="text" class="form-control"  name="list[<?php echo $vl['section_id'];?>][level]" value="<?php echo $vl['level'];?>">
	                                    </div>
	                                    <label class="col-sm-1 control-label">邀请者为游客奖励体力*: </label>
	                                    <div class="col-sm-1">
	                                        <input type="text" class="form-control"  name="list[<?php echo $vl['section_id'];?>][spins_guest]" value="<?php echo $vl['spins_guest'];?>">
	                                    </div>
	                                    <label class="col-sm-1 control-label">邀请者为FB奖励体力*: </label>
	                                    <div class="col-sm-1">
	                                        <input type="text" class="form-control"  name="list[<?php echo $vl['section_id'];?>][spins_fb]" value="<?php echo $vl['spins_fb'];?>">
	                                    </div>
	                                    <label class="col-sm-1 control-label">被邀请者绑定FB奖励体力*: </label>
	                                    <div class="col-sm-1">
	                                        <input type="text" class="form-control"  name="list[<?php echo $vl['section_id'];?>][bind_fb]" value="<?php echo $vl['bind_fb'];?>">
	                                    </div>
	                                </div>
	                            </div>
	                        <?php } ?>
                    	</div>
                    </div>
                        
                    </section>
            </form>
            
            <?php } ?>

            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm3" action="#" style="margin-top: 20px">
                <header><i class="fa fa-fw fa-file"></i>邀请进度奖励配置</header>
                <section>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <button type="button" class="btn btn-success" id="goodsubmitBtn3">提交</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <div class="col-sm-12" id="form_3">

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

        </aside>
    </div>
</div>
</body>

<script>
    var stageReward;
    var stageReward_arr=[];
    var reward_type;
    var reward_type_arr=[];
    var prizeArr =[];
    var coins_list =["coins01","coins02","coins03","coins04","coins05","coins06"];
    $(function () {
        $('#goodsubmitBtn1').click(function () {
            var data = new FormData(document.getElementById("goodForm1"));
            $.ajax({
                url: rootPath + 'config/actionInviteRewardConfig',
                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);
                }
            });
        });
        
        $('#goodsubmitBtn2').click(function () {
            var data = new FormData(document.getElementById("goodForm2"));
            $.ajax({
                url: rootPath + 'config/actionInviteRewardConfig',
                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);
                }
            });
        });

        $('#goodsubmitBtn3').click(function () {
            // var data = new FormData(document.getElementById("goodForm3"));
            var data = $('#goodForm3').serializeArray();
            // console.log(data);
            $.ajax({
                url: rootPath + 'config/saveIniteStageReward',
                type: 'POST',
                data: data,
                dataType: 'json',
                success: function (ret) {
                    console.log(ret);
                    alert(ret.msg);
                    // window.location.reload();
                },
                error: function (ret) {
                    console.log(ret);
                    alert(ret.msg);
                }
            });
        });


        var reward_type_str ='<?php echo $info['propsList'];?>';
        reward_type =JSON.parse(reward_type_str);//奖励类型
        $.each(reward_type,function(k,v){
            reward_type_arr[k]=v;
        });
        var stageReward_str ='<?php echo $info['stageReward'];?>';
        stageReward =JSON.parse(stageReward_str);//阶段奖励

        $.each(stageReward,function(k,v){
            stageReward_arr[k]=v;
        });
        init();

    });
    $(document).on("change",".prize_sel",function () {
       var prizeId =  $(this).children('.form-control').val();
       if(prizeId==14){
           $(this).next().remove();
       }else{
        $(this).append().children('.coins_sel').remove();
       }
    })
</script>
<script>
    var idData = JSON.parse('<?php echo json_encode($idData); ?>');
    var chestSkin = JSON.parse('<?php echo json_encode($chestSkin); ?>');
    $(function () {
        $('body').on("click",'.deleteParent',function(){
            $(this).parent().remove();
        });

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

        $(document).on("change",".propsSelect",function(){
            var value = $(this).val();
            var gid = $(this).attr("gid");
            var str = '';
            if(value == 19){
                str = '<label class="col-sm-1 control-label">皮肤*: </label>';
                str += '<div class="col-sm-2">';
                str += '<select class="col-sm-3 form-control" name="reward['+gid+'][prize][skin][]">';
                for(var x in chestSkin) {
                    str += '<option value="'+x+'">'+chestSkin[x]+'</option>';
                }
                str += '</select></div>';
            }else{
                str = '<input type="hidden" name="name" value="">';
            }
            $(this).parent("div").parent("div").find(".skinSelect").html(str);
        });

    });

    function init(){
        var html ='';
        $.each(stageReward,function(k,v){
            html += initStageReward(v);
            // return false;
        });
        html += '<div class="text-center"><botton class="btn btn-primary" id="addStage" onclick="addStage()">添加</botton><botton class="btn btn-danger del-tr" onclick="delStage()">删除</botton></div>';
        $("#form_3").append(html);
    }

    function addStage() {
        index = $('#form_3').children('.AllTypeControl').length;
        index = index != undefined ? parseInt(index) : 0;
        index = index + 1;
        //初始化数据
        stageReward[index] = {
            num: 1,
            stage: index,
            prize: [{propsId: 15, num: 1, rtype: 0}],
        };
        html = initStageReward(stageReward[index]);
        $('#form_3').children('.AllTypeControl:last').after(html);
    }

    function delStage() {
        index = $('#form_3').children('.AllTypeControl').length;
        index = index != undefined ? parseInt(index) : 0;
        if(index <= 0){
            return;
        }
        $('#form_3').children('.AllTypeControl:last').remove();
        delete(stageReward[index]);
    }

    function initStageReward(data) {
        var html='';
        html+='<div class="row AllTypeControl" style="border: 1px solid #CCC;margin:10px 5px;padding: 5px 0;">';
        html+='<div class="form-group" >';

        html+='<label class="col-sm-1 control-label">进度*: </label><div class="col-sm-1"><input type="text" class="form-control" readonly name="reward['+data.stage+'][stage]" value="'+data.stage+'"></div>';
        html+='<label class="col-sm-1 control-label">邀请人数*: </label><div class="col-sm-1"> <input type="text" class="form-control"  name="reward['+data.stage+'][num]" value="'+data.num+'"></div>';
        html+='<label class="col-sm-1 control-label">奖品*: </label>';
        html+='<div class="col-sm-8"><div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;" id="prizes_'+data.stage+'">';
        prizeArr[data.stage]=0;
        for(var i=0;i<data.prize.length;i++){
            html += prizeLine(data.stage,data.prize[i],i);
            prizeArr[data.stage]++;
        }
        html+='<button type="button" class="btn btn-success addParent" style="margin-left: 12px;margin-bottom: 10px;" gid="'+data.stage+'">增加</button>';

        html+='</div></div>';

        html+='</div></div>';
        return html;
    }
    function prizeLine(stage,data,pid) {
        var html = "";
        html += '<div class="form-group"><label class="col-sm-1 control-label">道具*: </label><div class="col-sm-3"><select class="form-control propsSelect" gid="'+stage+'" name="reward['+stage+'][prize][propsId][]"">';
        for(var x in idData) {
            selected = data.propsId == x ? ' selected' : '';
            html += '<option value="'+x+'"'+selected+'>'+idData[x]['msg']+'</option>';
        }
        html += '</select></div><label class="col-sm-1 control-label">数量*: </label><div class="col-sm-2"><input type="text" class="form-control"  name="reward['+stage+'][prize][num][]" value="'+data.num+'"></div>';
        html += '<div class="skinSelect">';
        if(data.propsId == 19){
            html += '<label class="col-sm-1 control-label">皮肤*: </label>';
            html += '<div class="col-sm-2">';
            html += '<select class="col-sm-3 form-control" name="reward['+stage+'][prize][skin][]">';
            for(var x in chestSkin) {
                var selected = (data.skin !== undefined && x == data.skin) ? ' selected' : '';
                html += '<option value="'+x+'"'+selected+'>'+chestSkin[x]+'</option>';
            }
            html += '</select></div>';
        }else{
            html += '<input type="hidden" name="reward['+stage+'][prize][skin][]" value="">';
        }
        html += '</div>';
        html += '<button type="button" class="btn btn-danger deleteParent" >删除</button></div>';
        return html;
    }

</script>
</html>
