<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>
                    <header><i class="fa fa-fw fa-file"></i>惊喜事件配置-
                        <?php switch ($type){
                            case 'spins': echo '体力'; break;
                            case 'card' : echo '卡片'; break;
                         }?>
                    </header>
                    <section>
                        <form id="clubConfigForm">
                            <input name="id" type="hidden" value="<?php echo $config['id'] ?? 0;?>">
                            <div class="container-fluid">
                                <?php foreach ($columns as $key => $html){ ?>
                                    <?=$html?>
                                <?php }?>

                                <?php if($type == 'card'){?>
                                <div class="row rewardDiv">
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">索要者奖励</label>
                                    </div>
                                    <div class="col-sm-3">
                                        <select class="propSelector form-control" name="receiverReward[0][type]">
                                            <?php foreach ($props as $k => $item){?>
                                                <option value="<?=$k?>" <?php if(!empty($config['receiverReward'][0]['type']) && $k == $config['receiverReward'][0]['type'])echo 'selected'; ?> ><?=$item['name']?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-3">
                                        <select class="skinSelector form-control" name="receiverReward[0][skin]">
                                            <?php if (!empty($config['receiverReward'][0]['type'] && !empty($props[$config['receiverReward'][0]['type']]['skinList']))){ ?>
                                                <?php foreach ($props[$config['receiverReward'][0]['type']]['skinList'] as  $skin){?>
                                                    <option value="<?=$skin['id']?>" <?php if(!empty($config['receiverReward'][0]['skin']) && $skin['id'] == $config['receiverReward'][0]['skin'])echo 'selected'; ?> ><?=$skin['name']?></option>
                                                <?php } ?>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">数量</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input class=" form-control" name="receiverReward[0][num]" value="<?=intval($config['receiverReward'][0]['num'])?>">
                                    </div>
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">取值类型</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <select class=" form-control" name="receiverReward[0][rtype]">
                                            <option value="1" <?php if(!empty($config['receiverReward'][0]['rtype']) && 1 == $config['receiverReward'][0]['rtype'])echo 'selected'; ?> >直接取值</option>
                                            <option value="2" <?php if(!empty($config['receiverReward'][0]['rtype']) && 2 == $config['receiverReward'][0]['rtype'])echo 'selected'; ?> >等级取值</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row rewardDiv">
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">帮助者奖励</label>
                                    </div>
                                    <div class="col-sm-3">

                                        <select class="propSelector form-control" name="helperReward[0][type]">
                                            <?php foreach ($props as $k => $item){?>
                                                <option <?=$k?>  <?=$config['helperReward'][0]['type']?>  value="<?=$k?>" <?php if(!empty($config['helperReward'][0]['type']) && $k == $config['helperReward'][0]['type']){echo 'selected'; }?> ><?=$item['name']?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-3">
                                        <select class="skinSelector form-control" name="helperReward[0][skin]">
                                            <?php if (!empty($config['helperReward'][0]['type'] && !empty($props[$config['helperReward'][0]['type']]['skinList']))){ ?>
                                                <?php foreach ($props[$config['helperReward'][0]['type']]['skinList'] as  $skin){?>
                                                    <option value="<?=$skin['id']?>" <?php if(!empty($config['helperReward'][0]['skin']) && $skin['id'] == $config['helperReward'][0]['skin']){echo 'selected'; }?> ><?=$skin['name']?></option>
                                                <?php } ?>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">数量</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input class=" form-control" name="helperReward[0][num]" value="<?=intval($config['helperReward'][0]['num'])?>">
                                    </div>
                                    <div class="col-sm-1 " style="text-align: right;vertical-align: center">
                                        <label class="form-control " style="border: none">取值类型</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <select class=" form-control" name="helperReward[0][rtype]">
                                            <option value="1" <?php if(!empty($config['helperReward'][0]['rtype']) && 1 == $config['helperReward'][0]['rtype']){echo 'selected';} ?> >直接取值</option>
                                            <option value="2" <?php if(!empty($config['helperReward'][0]['rtype']) && 2 == $config['helperReward'][0]['rtype']){echo 'selected';} ?> >等级取值</option>
                                        </select>
                                    </div>
                                </div>
                                <?php }?>
                                <div class="row">
                                    <button type="button" class="btn btn-success btnSubmit">提交</button>
                                </div>
                            </div>
                        </form>
                    </section>
                </aside>
            </div>
        </div>
    </div>
</div>
<script>
    var props = '<?php echo json_encode($props);?>';
    if(typeof props == 'string')props = JSON.parse(props);
    console.log(props)
    // $('.select2').select2({})
    $('.btnSubmit').click(function(){
        var datas = $('#clubConfigForm').serializeArray();
        console.log(datas);
        $.post('/club/saveSupriseConfig/<?=$type?>', datas, function(res){
            if(typeof res == 'string')res = JSON.parse(res)
            alert(res.msg)
        })
    });
    $('.propSelector').change(function(){
        var propId = $(this).val()
        var skinList = props[propId].skinList
        var skinSelector = $(this).parents('.rewardDiv').find('.skinSelector')
        skinSelector.empty()
        if(skinList){
            $.each(skinList, function(i, o){
                console.log(o)
                skinSelector.append("<option value='"+o.id+"'>"+o.name+"</option>")
            })
        }
    })
</script>