<!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">
    <script src="<?php echo base_url(); ?>public/js/common.js"></script>
    <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">
                <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" style="margin-right: 20px" class="btn btn-success" id="goodsubmitBtn">提交</button>
                                <button type="button" style="margin-right: 20px" class="btn btn-danger" id="delChestAllTokens" data-href="/config/delChestAllTokens">删除所有宝箱全部代币配置</button>

                                <button type="button" class="btn btn-info importBtn" data-href="/import/chestSkinBasic">导入宝箱基础配置</button>
                                <button type="button" style="margin-right: 20px" class="btn btn-info importBtn" data-href="/import/chestSkinProbs">导入宝箱概率配置</button>
                                <select   style="margin-right: 20px" class="btn btn-info importSelector"  data-href="/import/chestSkinProbs">
                                    <option value="-1">导入道具类型</option>
                                    <?php foreach ($propTypes as $propId => $text){?>
                                        <option value="<?=$propId?>">导入<?=$text?></option>
                                    <?php }?>
                                </select>

                                <button type="button"  class="btn btn-primary exportBtn" data-href="/export/chestSkinBasic">导出宝箱基础配置</button>
                                <button type="button" class="btn btn-primary exportBtn" data-href="/export/chestSkinProbs">导出宝箱概率配置</button>
                                <select   style="margin-right: 20px" class="btn btn-primary exportSelector" onchange="" data-href="/import/chestSkinProbs">
                                    <option value="-1">导出道具类型</option>
                                    <?php foreach ($propTypes as $propId => $text){?>
                                        <option value="<?=$propId?>">导出<?=$text?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>
                    </div>
                    <input type="file" id="importFiles" name="files" style="display: none">
                    <div class="container-fluid">
                        <?php foreach ($list as $key => $vl) { ?>
                            <div class="row AllTypeControl" style="border: 1px solid #CCC;margin:10px 5px;padding: 10px">
                                <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="good[<?php echo $vl['id']; ?>][id]" value="<?php echo $vl['id']; ?>">
                                    </div>
                                    <label class="col-sm-1 control-label">宝箱皮肤资源ID*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control" name="good[<?php echo $vl['id']; ?>][skinResourceId]" value="<?php echo $vl['skinResourceId']; ?>">
                                    </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['id']; ?>][name]" value="<?php echo $vl['name']; ?>">
                                    </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['id']; ?>][card]" value="<?php echo $vl['reward'][17]['num']; ?>">
                                    </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['id']; ?>][joker]" value="<?php echo $vl['reward'][18]['num']; ?>">
                                    </div>
                                    <label class="col-sm-1 control-label">展示大奖类型: </label>
                                    <div class="col-sm-1">
                                        <select name="good[<?php echo $vl['id']; ?>][lastRewardPid]" class="form-control m-b">
                                            <?php
                                            foreach ($lastRewardSelectTypes as $k => $v) {
                                                ?>
                                                <option value="<?php echo $k ?>" <?php if ($vl['lastRewardPid'] == $k) echo 'selected'; ?>><?php echo $v ?></option>
                                                <?php
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">

                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label"><a href="/#config/chestWeight/2/spinsNum/<?php echo $vl['id']; ?>" target="_blank" type="button" class="btn btn-success">体力配置预览</a></label>
                                    <label class="col-sm-1 control-label"><a href="/#config/chestWeight/2/petExp/<?php echo $vl['id']; ?>" target="_blank" type="button" class="btn btn-success">宠物经验预览</a></label>
                                    <label class="col-sm-1 control-label"><a href="/#config/chestWeight/2/tokens/<?php echo $vl['id']; ?>" target="_blank" type="button" class="btn btn-success">代币配置预览</a></label>
                                    <label class="col-sm-1 control-label"><button data-href="/config/delTokens/<?php echo $vl['id']; ?>"  type="button" class="btn btn-danger delTokens">删除代币配置</button></label>
                                    <label class="col-sm-1 control-label"><a href="/#config/cardTypePro/<?php echo $vl['id']; ?>" target="_blank" type="button" class="btn btn-success">宝箱概率配置</a></label>

                                    <div class="col-sm-5" style="display: block">
                                        <div class="col-sm-9">
                                            <table class="col-sm-12">
                                                <tr>
                                                    <td class="formPropsRewardsTag" data-value='<?= $vl["props"] ?>' data-name="good[<?php echo $vl['id']; ?>][props]">
                                                    </td>
                                                    <td class="text-right">
                                                        <button type="button"
                                                                class="btn btn-outline btn-success"
                                                                onclick="propsJs.initEdit(this, 'formPropsRewardsTag')">
                                                            其他奖励配置
                                                        </button>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                </section>
            </form>
        </aside>
    </div>
</div>
</body>

<script>
    $(function () {
        propsJs.initTag('formPropsRewardsTag')
        propsJs.initTag('rewardShow')
        $('.formPropsRewardsTag').change()

        $('#goodsubmitBtn').click(function () {
            var data = new FormData(document.getElementById("goodForm"));
            // console.log($("#goodForm").toObject());return;
            $.ajax({
                url: rootPath + 'config/actionChestSkin',
                type: 'POST',
                data: data,
                dataType: 'json',
                contentType: false, //必须
                processData: false, //必须
                success: function (data) {
                    // console.log(data);
                    alert(data.msg);
                    window.location.reload();
                },
                error: function (msg) {
                    // console.log(data);
                    alert(data.msg);
                }
            });
        });
    });
    let type = 0
    $('.importSelector').change(function(){
        let propType = $(this).val()
        if (propType < 0) return;
        type = propType
        let importUrl = `/import/chestSkinRewards/${propType}`
        $('#importFiles').data('href', importUrl)
        $('#importFiles').click()
    })
    $('.importBtn').click(function () {
        $('#importFiles').click()
        $('#importFiles').data('href', $(this).data('href'))
    })
    $('#importFiles').change(function () {
        var url = $(this).data('href');
        console.log(url)
        var formData = new FormData();
        formData.append("file", document.getElementById("importFiles").files[0]);
        $(this).val('');
        if(type > 20){
            if(!confirm('宝箱中的代币将被新导入配置中的代币覆盖，是否继续操作？'))return
        }
        showLoading()
        $.ajax({
            type: "POST", // 数据提交类型
            url: url, // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function (data) { // 成功回调
                hideLoading()
                data = JSON.parse(data);
                $('.importSelector').val(-1)
                alert(data.msg);
                // if (data.ret > 0) {
                //     window.location.reload();
                // }
            }
            , error: function (xhr, err) { // 成功回调
                hideLoading()
                console.log(xhr, err)
                alert('上传失败');
            }
        });
    })

    $('.exportBtn').click(function () {
        window.open($(this).data('href'))
    })

    $('.exportSelector').change(function(){
        let propType = $(this).val()
        if (propType < 0) return;
        let exportUrl = `/export/chestSkinRewards/${propType}`
        window.open(exportUrl)
        $('.exportSelector').val(-1)
    })
    $('.delTokens').click(function (){
        if(!confirm('是否删除该宝箱的代币配置？'))return;
        let url = $(this).data('href')
        showLoading()
        $.get(url,function(response){
            hideLoading()
            response = jsonParse(response)
            alert(response.msg)
        })
    })

    $('#delChestAllTokens').click(function (){
        if(!confirm('确认删除所有宝箱的代币配置？'))return;
        let url = $(this).data('href')
        showLoading()
        $.get(url,function(response){
            hideLoading()
            response = jsonParse(response)
            alert(response.msg)
        })
    })

</script>
</html>
