
<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">
<link rel="stylesheet" href="<?php echo base_url(); ?>public/css/vendor/bootstrap/bootstrap-table.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>public/css/vendor/bootstrap/table-fix-column.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>public/node_modules/bootstrap-select/dist/css/bootstrap-select.min.css">
<script src="<?php echo base_url(); ?>public/node_modules/bootstrap-select/dist/js/bootstrap-select.min.js"></script>

<style type="text/css">
    table {
        table-layout: fixed !important;
    }

    th {
        background: rgb(146, 208, 80);
    }

    td, th {
        max-width: 400px;
        word-wrap: break-word;
    }

    .table-hover > tbody > tr:hover {
        background-color: #c8bd6d;
    }

    input {
        width: 60px;
    }

    #roleTable tr:nth-child(5n) {
        background: rgb(198, 239, 206);
    }

    /**隔五行*/

    .fixed-table-toolbar {
        display: none;
    }

    .fileinput-button {
        position: relative;
        top: -5px;
    }

    .fileinput-button input {
        position: absolute;
        right: 0px;
        top: 0px;
        opacity: 0;
        -ms-filter: 'alpha(opacity=0)';
    }
</style>

<div class="content_wrapper">
    <div id="msg-tip"></div>
    <div class="container-fluid ">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>回归奖励开放时间配置</header>
                    <section>
                        <div class="container-fluid">

                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-sm-12">
                                        <button type="button" class="btn btn-success" id="commonConfSubmitBtn">提交</button>
                                    </div>
                                </div>
                            </div>

                            <div class="ibox-content" style="padding:25px 0;">
                                <div class="col-sm-8">
                                    <form class="form-horizontal" method="post" id="commonConfForm" action="">
                                        <input name="id" type="hidden" value="<?=$info['id']??0?>">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">开放时间：</label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control" id="datetimepicker" placeholder=" 时间选择 " name="time_range" value="<?php echo date("Y-m-d H:i:s",$info['start'])." - ".date("Y-m-d H:i:s",$info['end']);?>">
                                            </div>
                                        </div>
                                    </form>

                                </div>
                            </div>

                        </div>
                    </section>
                    <section>
                        <div class="container-fluid">
                            <div class="ibox-title" style="margin-bottom: 20px">
                                <h5 style="font-weight: bold">奖励内容配置：</h5>
                            </div>
                        </div>
                        <div class="container-fluid">
                            <div>
                                <span id="filespanXml" class="btn btn-success fileinput-button">
                                    <span>导入</span>
                                    <input type="file" id="uploadXml">
                                </span>
                                <input type="button" style="display: none;margin-bottom: 10px;width: 80px;"
                                       class="btn btn-success" value="上传" id="uploadConfigFileBtn"/>
                                <!--                                <a type="button" href="/Export/skucoin" style="margin-bottom: 10px;width: 80px;"-->
                                <!--                                   class="btn btn-success">导出</a>-->
                            </div>

                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="table-responsive ">
                                        <table id="roleTable" class="table table-bordered table-hover"
                                               data-toggle="table"
                                               data-classes="table table-hover"
                                               data-show-columns="true"
                                               data-striped="true"

                                               data-height="700">
                                            <thead>
                                            <tr>
<!--                                                <th>ID</th>-->
                                                <th>等级下限</th>
                                                <th>等级上限</th>
                                                <th>奖励配置</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if (is_array($rewards) && !empty($rewards)) {
                                                foreach ($rewards as $key => $vl) { ?>
                                                    <tr>
<!--                                                        <td>--><?php //echo $vl['id']; ?><!--</td>-->
                                                        <td><?php echo $vl['minLv']; ?></td>
                                                        <td><?php echo $vl['maxLv']; ?></td>
                                                        <td><?php echo $vl['prizeHtml']; ?></td>

                                                        <td>
                                                            <button type="button" class="btn btn-primary active"
                                                                    onclick="openEdit(this)"
                                                                    data-toggle="modal"
                                                                    data-target="#goodsEditBtn"
                                                                    data-value='<?php echo json_encode($vl); ?>'>编辑
                                                            </button>
                                                        </td>
                                                    </tr>
                                                <?php }
                                            } else { ?>
                                                <tr>
                                                    <td colspan="14">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>
</div>

<div class="modal" id="goodsEditBtn" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 60%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                            class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">修改配置</h4>
            </div>
            <form class="form-horizontal" method="post" id="goodForm" action="">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">ID:</label>
                        <div class="col-sm-2">
                            <input type="text" readonly class="form-control" name="id">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">等级下限:</label>
                        <div class="col-sm-3">
                            <input type="number" class="form-control" placeholder="等级下限" name="minLv">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">等级上限:</label>
                        <div class="col-sm-3">
                            <input type="number" class="form-control" placeholder="等级上限" name="maxLv">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="propsList" id="prizeJson">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="goodsSubmitBtn">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="<?php echo base_url(); ?>static/common/js/props.js"></script>
<script src="<?php echo base_url(); ?>public/js/vendor/bootstrap/src/bootstrap-table.js"></script>
<script src="<?php echo base_url(); ?>public/js/vendor/bootstrap/src/table-fix-column.js"></script>

<script>

</script>
<script>
    laydate.render({
        elem: '#datetimepicker',
        type: 'datetime',
        range: true
    });

    // 编辑提交
    $('#goodsSubmitBtn').click(function () {
        showLoading()
        var data = new FormData(document.getElementById("goodForm"));
        $.ajax({
            url: 'returns/saveRewards',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                hideLoading()
                alert(data.msg);
                window.location.reload();
            },
            error: function (data, xhr) {
                hideLoading()
                console.log(data, xhr)
                alert(xhr);
            }
        });
    });

    //开放时间配置
    $('#commonConfSubmitBtn').click(function () {
        var data = new FormData(document.getElementById("commonConfForm"));
        data.append('img_name', 'sprintPay')
        $.ajax({
            url:  '/returns/saveOpenTimesConf',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                alert(data.msg);
                // confirm(data.msg);
                // window.location.reload();
            },
            error: function (data) {
                alert(data.msg);
            }
        });
    });

    //导入奖励配置
    $("#filespanXml").on("change", "input[type='file']", function () {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#uploadConfigFileBtn').click()
            // $('#filespanXml').hide();
            // $('#uploadConfigFileBtn').show();
        } else {
            return false
        }
    });
    $("#uploadConfigFileBtn").click(function () {
        var formData = new FormData();
        formData.append("file", document.getElementById("uploadXml").files[0]);
        showLoading()
        $.ajax({
            type: "POST", // 数据提交类型
            url: "returns/importRewards", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function (data) { // 成功回调
                $('#uploadXml').val('')
                hideLoading()
                data = JSON.parse(data);
                if (data.ret == 0) {
                    // console.log(data)
                    alert(data.msg);
                    window.location.reload();
                } else {
                    // msgTip = $('#msg-tip');
                    alert(data.msg);
                    // window.location.reload();
                }
            }
        });
    });

    function openEdit(even) {
        let formData = $(even).data('value')
        for (let key in formData) {
            if (formData.hasOwnProperty(key)) {
                $("[name=" + key + "]").val(formData[key]);
            }
        }
        propsClass.Init(JSON.parse(formData['rewards']), 'prizeJson');
    }

</script>
