<!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>
            <header><i class="fa fa-fw fa-file"></i>配置缓存时间：<?php echo date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('d')-date('w')+6,date('Y')));?></header>
            <section>
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-sm-12">
                            <button style="margin:5px;" class="btn btn-success" onclick="refreshConf()" role="button">刷新配置</button>
                        </div>
                    </div>
                </div>
            </section>
        </aside>
        <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/luckydraw/0" role="button">普通用戶</a>
                            <a style="margin:5px;" class="btn btn-primary " href="#config/luckydraw/vip" role="button">VIP</a>
                            <a style="margin:5px;" class="btn btn-primary " href="#config/luckydraw/0/1" role="button">1.7.2+普通用戶</a>
                            <a style="margin:5px;" class="btn btn-primary " href="#config/luckydraw/vip/1" role="button">1.7.2+VIP</a>
                        </div>
                    </div>
                </div>
            </section>
        </aside>
        <aside>
            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="rewardForm" action="#" style="margin-top: 20px">
                <input type="hidden" value="<?php echo $newVersion;?>" name="newVersion"/>
                <input type="hidden" value="<?php echo $flag;?>" name="flag"/>
                <input type="hidden" value="reward" name="actionType"/>
                <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="rewardsubmitBtn">提交</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid AllTypeControl">
                            <div class="row" style="border: 1px solid #CCC;margin:10px 5px;padding: 10px 0;">
                                <?php 
                                	for ($i=0;$i<11;$i++) { 

                                ?>
                                <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="configId[<?php echo $config[$i]['id'];?>]" value="<?php echo $config[$i]['id']+1;?>">
                                    </div>
                                    <label class="col-sm-1 control-label">奖品大类*: </label>
                                    <div class="col-sm-2">
                                        <select class="form-control typeControl" name="type[]" id="type_<?php echo $i;?>">
                                            <?php foreach ($idData as $key => $vl) { ?>
                                                <option value="<?php echo $key;?>" <?php if($config[$i][$flag.'reward']['type'] == $key) echo 'selected'; ?>><?php echo $vl['name'];?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <label class="col-sm-1 control-label">奖品*: </label>
                                    <div class="col-sm-2">
                                        <select class="form-control" name="rewardid[]" id="id_<?php echo $i;?>" selectedId="<?php echo $config[$i][$flag.'reward']['id'];?>" selectedGid="<?php echo $config[$i][$flag.'reward']['gid'];?>">

                                        </select>
                                    </div>
                                    <label class="col-sm-1 control-label">奖品数量*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control"  name="num[]" value="<?php echo $config[$i][$flag.'reward']['num'];?>">
                                    </div>
                                    <label class="col-sm-1 control-label">排序*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control"  name="sort[]" value="<?php echo $config[$i]['sort'];?>">
                                    </div>
                                </div>
                                <?php } ?>
                                <div class="form-group">
                                    <div class="col-sm-2" style="margin: 20px 0px;">
                                        <label class="col-sm-6 control-label">奖品ID*: </label>
                                        <div class="col-sm-6">
                                            <input type="text" readonly class="form-control" name="configId[11]" value="12">
                                        </div>
                                    </div>
                                    <div class="col-sm-8">
                                        <div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;">
                                            <?php 
                                            	foreach ($config[count($config)-1][$flag.'reward']['data'] as $k => $reward) { 
                                            	
                                            ?>
                                                <div class="form-group">
                                                    <label class="col-sm-1 control-label">大类*: </label>
                                                    <div class="col-sm-3">
                                                        <select class="form-control typeControl" name="type[<?php echo count($config)-1;?>][]" id="type_<?php echo count($config)-1;?>_<?php echo $k;?>">
                                                            <?php foreach ($idData as $key => $vl) { ?>
                                                                <option value="<?php echo $key;?>" <?php if($reward['type'] == $key) echo 'selected'; ?>><?php echo $vl['name'];?></option>
                                                            <?php } ?>
                                                        </select>
                                                    </div>
                                                    <label class="col-sm-1 control-label">奖品*: </label>
                                                    <div class="col-sm-3">
                                                        <select class="form-control" name="rewardid[<?php echo count($config)-1;?>][]" id="id_<?php echo count($config)-1;?>_<?php echo $k;?>" selectedId="<?php echo $reward['id'];?>" selectedGid="<?php echo $reward['gid'];?>">

                                                        </select>
                                                    </div>
                                                    <label class="col-sm-1 control-label">数量*: </label>
                                                    <div class="col-sm-2">
                                                        <input type="text" class="form-control"  name="num[<?php echo count($config)-1;?>][]" 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" pro="<?php echo count($config)-1;?>" k="<?php echo count($config[count($config)-1][$flag.'reward']['data']);?>" style="margin-left: 12px;margin-bottom: 10px;">增加</button>
                                        </div>
                                    </div>
                                    <div style="margin: 20px 0px;">
                                        <label class="col-sm-1 control-label">排序*: </label>
                                        <div class="col-sm-1">
                                            <input type="text" class="form-control"  name="sort[]" value="<?php echo $config[$i]['sort'];?>">
                                        </div>
                                    </div>
                                </div>
                            </div>
                    </div>
                </section>
            </form>
        </aside>
    </div>
    <div class="container-fluid">
        <aside>
            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="progressForm" action="#" style="margin-top: 20px">
            	<input type="hidden" value="<?php echo $newVersion;?>" name="newVersion"/>
                <input type="hidden" value="0" name="flag"/>
                <input type="hidden" value="progress" name="actionType"/>
                <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="progresssubmitBtn">提交</button>
                            </div>
                        </div>
                    </div>
                    <?php foreach ($progressconfig as $pro => $row) { ?>
                        <div class="container-fluid AllTypeControl" style="border: 1px solid #CCC;margin: 20px 15px;">
                            <div class="form-group">
                                <div class="col-sm-2" style="margin: 20px 0px;">
                                    <label class="col-sm-6 control-label">进度值*: </label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" name="progress[]" value="<?php echo $pro;?>">
                                    </div>
                                </div>
                                <div class="col-sm-10">
                                    <div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;">
                                        <?php 
                                        	foreach ($row as $k => $reward) { 
                                        ?>
                                            <div class="form-group">
                                                <label class="col-sm-1 control-label">奖品大类*: </label>
                                                <div class="col-sm-2">
                                                    <select class="form-control typeControl" name="type[<?php echo $pro;?>][]" id="type_<?php echo $pro;?>_<?php echo $k;?>">
                                                        <?php foreach ($idData as $key => $vl) { ?>
                                                            <option value="<?php echo $key;?>" <?php if($reward['type'] == $key) echo 'selected'; ?>><?php echo $vl['name'];?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                                <label class="col-sm-1 control-label">奖品*: </label>
                                                <div class="col-sm-2">
                                                    <select class="form-control" name="rewardid[<?php echo $pro;?>][]" id="id_<?php echo $pro;?>_<?php echo $k;?>" selectedId="<?php echo $reward['id'];?>" selectedGid="<?php echo $reward['gid'];?>">

                                                    </select>
                                                </div>
                                                <label class="col-sm-1 control-label">奖品数量*: </label>
                                                <div class="col-sm-2">
                                                    <input type="text" class="form-control"  name="num[<?php echo $pro;?>][]" 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" pro="<?php echo $pro;?>" k="<?php echo count($row);?>" style="margin-left: 12px;margin-bottom: 10px;">增加</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                </section>
            </form>
        </aside>
    </div>
    <div class="container-fluid">
        <aside>
            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="proForm" action="#" style="margin-top: 20px">
            	<input type="hidden" value="<?php echo $newVersion;?>" name="newVersion"/>
                <input type="hidden" value="<?php echo $flag;?>" name="flag"/>
                <input type="hidden" value="pro" name="actionType"/>
                <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="prosubmitBtn">提交</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid" style="border: 1px solid #CCC;margin: 20px 15px;">
                        <div class="row">
                            <div class="form-group" style="padding:5px 0">
                                <label style="text-align: center" class="col-sm-1 control-label">幸运值*: </label>
                                <div class="col-sm-11">
                                    <?php for ($i=0;$i<12;$i++) { ?>
                                        <label style="text-align: center" class="col-sm-1 control-label">奖品<?php echo $i+1;?>*: </label>
                                    <?php } ?>
                                </div>
                            </div>
                            <?php foreach ($proconfig as $key => $vl) { ?>
                                <div class="form-group" style="padding:5px 0">
                                    <div class="col-sm-1" style="padding-left: 2%;">
                                        <input type="text" class="form-control" name="lucky[]" placeholder="0" value="<?php echo $vl['lucky']?>">
                                    </div>
                                    <div class="col-sm-11">
                                        <?php for ($i=0;$i<12;$i++) { ?>
                                            <div class="col-sm-1">
                                                <input type="text" class="form-control" name="pro[<?php echo $vl['lucky']?>][]" placeholder="0" value="<?php echo $vl[$flag.'pro'][$i]?>">
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                </section>
            </form>
        </aside>
    </div>
</div>
</body>

<script>
    $(function () {
        var idData = JSON.parse('<?php echo json_encode($idData); ?>');
        function tyepToId(name) {
            var type = $("#type"+name).val();
            var str  = '';
            var selectId = $("#id"+name).attr('selectedId');
            var selectedGid = $("#id"+name).attr('selectedGid');
            console.log("name",name);
            console.log("type",type);
            for(var x in idData[type]['data']) {
                if(x == selectId){
                    str += '<option value="'+x+'" selected>'+idData[type]['data'][x]+'</option>';
                }else if(type == 1 && selectedGid == x){
                	str += '<option value="'+x+'" selected>'+idData[type]['data'][x]+'</option>';
                }else{
                    str += '<option value="'+x+'" >'+idData[type]['data'][x]+'</option>';
                }

            }
            $("#id"+name).html(str);
        }
        function typeChange(name) {
            var tap = name.replace('type','');
            var type = $("#"+name).val();
            var str  = '';
            for(var x in idData[type]['data']) {
                str += '<option value="'+x+'" >'+idData[type]['data'][x]+'</option>';
            }
            $("#id"+tap).html(str);
        }

        for(i=0;i<11;i++){
            tyepToId("_"+i);
        }


        <?php foreach ($progressconfig as $pro => $row) {
            foreach ($row as $k => $reward){ ?>
            tyepToId("_<?php echo $pro;?>_<?php echo $k;?>");
        <?php }}?>
        <?php foreach ($config[count($config)-1][$flag.'reward']['data'] as $k => $reward) { ?>
            tyepToId("_<?php echo count($config)-1;?>_<?php echo $k;?>");
        <?php }?>
        $('.AllTypeControl').on("click",'.typeControl',function(){
            typeChange($(this).attr('id'));
        });

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

        $('.addParent').on("click",function(){
            var pro = $(this).attr('pro');
            var k   = $(this).attr('k');
            var str = '';
            str += '<div class="form-group"><label class="col-sm-1 control-label">奖品大类*: </label><div class="col-sm-2"><select class="form-control typeControl" name="type['+pro+'][]" id="type_'+pro+'_'+k+'">';
            for(var x in idData) {
                str += '<option value="'+x+'">'+idData[x]['name']+'</option>';
            }
            str += '</select></div><label class="col-sm-1 control-label">奖品*: </label><div class="col-sm-2"><select class="form-control" name="rewardid['+pro+'][]" id="id_'+pro+'_'+k+'">';
            for(var x in idData[1]['data']) {
                str += '<option value="'+x+'">'+idData[1]['data'][x]+'</option>';
            }
            str += '</select></div><label class="col-sm-1 control-label">奖品数量*: </label><div class="col-sm-2"><input type="text" class="form-control"  name="num['+pro+'][]" value="0">'+
                   '</div><button type="button" class="btn btn-danger deleteParent" >删除</button></div>';
            $(this).before(str);
            $(this).attr('k',Number(k)+1);
        });

        var formName = ['reward','pro','progress'];
        formName.forEach(function (value,index) {
            $('#'+value+'submitBtn').click(function () {
                var data = new FormData(document.getElementById(value+"Form"));
                $.ajax({
                    url: rootPath + 'Config/luckydrawConfig',
                    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);
                    }
                });
            });
        });
    });

    function refreshConf() {
        if( confirm('确认刷配置缓存吗?') ) {
            $.post('/Config/refreshLuckyDrawConf', {
                id: 1
            }, function (data) {
                alert("刷新成功")
            });
        }
    }

</script>
</html>
