<!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">
</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" value="price" name="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>
                                <button type="button" style="margin:5px;" class="btn btn-primary" onclick="refreshActivityMd5()" role="button">刷新活动图片MD5配置</button>
                                <button type="button" style="margin:5px;" class="btn btn-primary" onclick="refreshCDNAll()" role="button">刷新CDN根目录</button>
                            </div>
                        </div>
                    </div>
                    <div class="container-fluid">
                        <?php foreach ($data as $key => $vl) { ?>
                            <div class="row" style="border: 1px solid #CCC;margin:10px 5px;padding: 5px 0;">
                                <div class="form-group">
                                    <label class="col-sm-1 control-label">类型*: </label>
                                    <div class="col-sm-1">
                                        <input type="text" class="form-control" name="name[<?php echo $vl['id']; ?>]" value="<?php echo $vl['name']; ?>">
                                    </div>
                                    <div class="col-sm-10">
                                        <?php
                                        if (!empty($vl['content'])) {
                                            foreach ($vl['content'] as $k => $v) {
                                                ?>
                                                <div class="col-sm-12 addOrDelControl" style="margin-top: 1rem;padding-left:0">
                                                    <label class="col-sm-1 control-label">名称*: </label>
                                                    <div class="col-sm-2">
                                                        <input type="text" class="form-control" name="content[<?php echo $vl['id']; ?>][name][]" value="<?php echo $v['name']; ?>">
                                                    </div>
                                                    <label class="col-sm-1 control-label">文件夹名称*: </label>
                                                    <div class="col-sm-3">
                                                        <input type="text" class="form-control" name="content[<?php echo $vl['id']; ?>][type][]" value="<?php echo $v['type']; ?>">
                                                    </div>
                                                    <div class="col-sm-1">
                                                        <button type="button" class="btn btn-primary"
                                                                onclick="openTitleEdit('<?=$vl['name']?>','<?=$v['type']?>')"
                                                                data-toggle="modal"
                                                                data-target="#titleEditBtn">标题配置
                                                        </button>
                                                    </div>
                                                    <div class="col-sm-1">
                                                        <button type="button" class="btn btn-danger deleteParent">删除</button>
                                                    </div>
                                                    <?php if ($debug) { ?>
                                                        <div class="col-sm-2">
                                                            <button type="button" class="btn btn-warning syncParent" data-env="pro" data-type="<?= $v['type'] ?>" data-name="<?= $v['name'] ?>" data-parent="<?= $vl['name'] ?>">同步目录 到线上服务器</button>
                                                        </div>
                                                    <?php } else { ?>
                                                        <div class="col-sm-1">
                                                            <button type="button" class="btn btn-warning refreshCDN" data-type="<?= $v['type'] ?>" data-name="<?= $v['name'] ?>" data-parent="<?= $vl['name'] ?>">刷新CDN</button>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                                <?php
                                            }
                                            ?>
                                            <div class="col-sm-12" style="margin-top: 1rem;">
                                                <button type="button" class="btn btn-success syncTitleConf" data-parent="<?=$vl['name']?>">同步标题配置</button>
                                                <button type="button" class="btn btn-success addParent" bid="<?php echo $vl['id']; ?>">新增文件夹</button>
                                            </div>
                                            <?php
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="col-sm-12" style="margin-top: 1rem;">
                            <button type="button" class="btn btn-success addParent2">新增类型</button>
                        </div>
                    </div>
                </section>
            </form>
        </aside>
    </div>
</div>
</body>

<div class="modal fade" id="titleEditBtn" style="overflow-y: auto" 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="titleConfForm" action="">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">活动类型:</label>
                        <div class="col-sm-3">
                            <input type="text" readonly class="form-control" name="type">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">活动皮肤key值:</label>
                        <div class="col-sm-3">
                            <input type="text" readonly class="form-control" name="key">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文字上部颜色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="textUpColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文字下部颜色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" style="margin-bottom: " name="textBottomColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">过渡状态:</label>
                        <div class="col-sm-3">
                            <select class="form-control" name="transitionStatus">
                                <option value="1">强过渡</option>
                                <option value="2">软过渡</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文字阴影颜色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="textShadowColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文字描边颜色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="textBorderColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">投影色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="shadowColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">高光色:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="highlightColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">多语言key:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="langKey">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">英文标题:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="en">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">中文标题:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="zh">
                        </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="titleConfSubmitBtn">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">

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

        $('.addParent').on("click", function () {
            var id = $(this).attr("bid");
            var str = `
                    <div class="col-sm-12 addOrDelControl" style="margin-top: 1rem;padding-left:0">
                        <label class="col-sm-1 control-label">名称*: </label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" name="content[${id}][name][]" value="">
                        </div>
                        <label class="col-sm-1 control-label">文件夹名称*: </label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control"  name="content[${id}][type][]" value="">
                        </div>
                    </div>
            `

            $(this).parent().before(str);
        });

        var maxId = <?php echo $maxId; ?>;
        $('.addParent2').on("click", function () {
            maxId++
            var str = `
                    <div class="row" style="border: 1px solid #CCC;margin:10px 5px;padding: 5px 0;">
                        <div class="form-group" >
                            <label class="col-sm-1 control-label">类型*: </label>
                            <div class="col-sm-1">
                                <input type="text" class="form-control" name="name[${maxId}]" value="">
                            </div>
                            <div class="col-sm-10">
                                <div class="col-sm-12 addOrDelControl" style="margin-top: 1rem;padding-left:0">
                                    <label class="col-sm-1 control-label">名称*: </label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="content[${maxId}][name][]" value="">
                                    </div>
                                    <label class="col-sm-1 control-label">文件夹名称*: </label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control"  name="content[${maxId}][type][]" value="">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
            `

            $(this).parent().before(str);
            maxId++;
        });

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

        $('.syncParent').click(function () {
            if (!confirm('是否确定同步到线上？\n同步之后，会直接在正式服务器上发布相关资源。')) return false;
            var data = $(this).data()
            showLoading()

            $.post('/config/syncDir', data, function (data) {
                hideLoading()
                if (typeof data == 'string') data = JSON.parse(data)
                alert(data.msg)
            });
        })
        $('.syncTitleConf').click(function () {
            if (!confirm('是否确定将该类型的标题配置同步到线上？\n同步之后，需要在正式服务器上保存才能生效。')) return false;
            var data = $(this).data()
            showLoading()

            $.post('/config/syncTitleConf', data, function (data) {
                hideLoading()
                if (typeof data == 'string') data = JSON.parse(data)
                alert(data.msg)
            });
        })
    });

    $('.refreshCDN').click(function () {
        var _this = $(this)
        var data = _this.data();
        var uri = '/config/refreshActivityCDN/' + data.type
        console.log(uri)
        if (confirm('确认刷新活动图片CDN吗?')) {
            showLoading()
            $.post(uri, {}, function (data) {
                hideLoading()
                console.log(data)
                alert("刷新成功")
            });
        }
    })

    function refreshCDNAll() {
        var _this = $(this)
        var data = _this.data();
        var uri = '/config/refreshActivityCDNAll'
        console.log(uri)
        if (confirm('确认刷新CDN根目录吗?')) {
            showLoading()
            $.post(uri, {}, function (data) {
                hideLoading()
                console.log(data)
                alert("刷新成功")
            });
        }
    }

    function refreshActivityMd5() {
        if (confirm('确认刷新活动图片Md5配置吗?')) {
            $.post('/activity/refreshActivityMd5', {
                id: 1
            }, function (data) {
                alert("刷新成功")
            });
        }
    }

    function openTitleEdit(type, subType) {
        let formData = {}
        $.ajax({
            type: "POST", // 数据提交类型
            url: "config/themeTitleConf",
            data: {type: type,subType: subType},
            async: false,
            success: function (data) {
                data = JSON.parse(data);
                if (Number(data.ret) !== 0) {
                    alert(data.msg);
                } else {
                    formData = data.data
                }
            }
        });

        let elemVal
        $('#titleConfForm .form-group .form-control').each((key, elem) => {
            elemVal = formData.hasOwnProperty(elem.name) ? formData[elem.name] : ""
            $(elem).val(elemVal)
        })
    }

    $('#titleConfSubmitBtn').click(function () {
        const data = $('#titleConfForm').toObject()

        $.ajax({
            url: rootPath + 'config/themeTitleConfUpdate',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function (data) {
                alert(data.msg);

                if (data.ret == 0) {
                    window.location.reload();
                }
            },
            error: function (data) {
                alert(data.msg);
            }
        });
    });

</script>
</html>
