<!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>
            <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm" action="#" style="margin-top: 20px">
                <input type="hidden" class="form-control" name="type" value="<?php echo $type;?>">
                <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="goodsubmitBtn">提交</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <?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">商品GID*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control" readonly name="good[<?php echo $vl['gid'];?>][gid]" value="<?php echo $vl['gid'];?>">
                                    </div>
                                    <label class="col-sm-1 control-label">商品名称*: </label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gname]" value="<?php echo $vl['gname'];?>">
                                    </div>
                                    <label class="col-sm-1 control-label">商品SKU*: </label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gsku]" value="<?php echo $vl['gsku'];?>">
                                    </div>
                                    <label class="col-sm-1 control-label">商品简介*: </label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gdesc]" value="<?php echo $vl['gdesc'];?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label">商品价格*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gprice]" value="<?php echo $vl['gprice'];?>">
                                    </div>
                                    <label class="col-sm-1 control-label">商品限购*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gMax]" value="<?php echo $vl['gMax'];?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label">商品货物*: </label>
                                    <div class="col-sm-8">
                                        <div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;">
                                            <?php foreach ($vl['gbaseReward'] 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="good[<?php echo $vl['gid'];?>][gbaseReward][type][]" id="type_<?php echo $vl['gid'];?>_<?php echo $k;?>">
                                                            <?php foreach ($idData as $x => $y) { ?>
                                                                <option value="<?php echo $x;?>" <?php if($reward['type'] == $x) echo 'selected'; ?>><?php echo $y['name'];?></option>
                                                            <?php } ?>
                                                        </select>
                                                    </div>
                                                    <label class="col-sm-1 control-label">奖品*: </label>
                                                    <div class="col-sm-3">
                                                        <select class="form-control" name="good[<?php echo $vl['gid'];?>][gbaseReward][id][]" id="id_<?php echo $vl['gid'];?>_<?php echo $k;?>" selectedId="<?php echo $reward['id'];?>">

                                                        </select>
                                                    </div>
                                                    <label class="col-sm-1 control-label">数量*: </label>
                                                    <div class="col-sm-2">
                                                        <input type="text" class="form-control"  name="good[<?php echo $vl['gid'];?>][gbaseReward][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" pro="<?php echo $vl['gid'];?>" k="<?php echo count($reward);?>" style="margin-left: 12px;margin-bottom: 10px;">增加</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </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');
            console.log(type);
            for(var x in idData[type]['data']) {
                if(x == selectId){
                    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);
        }

        <?php foreach ($list as $key => $vl) { ?>
            <?php foreach ($vl['gbaseReward'] as $k => $reward) { ?>
            tyepToId("_<?php echo $vl['gid'];?>_<?php echo $k;?>");
            <?php }?>
        <?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-3"><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-3"><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);
        });


        $('#goodsubmitBtn').click(function () {
            var data = new FormData(document.getElementById("goodForm"));
            $.ajax({
                url: rootPath + 'shop/actionConfig',
                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);
                }
            });
        });
    });
</script>
</html>
