<?php
/**
 * 添加等级
 */
if (!empty($_GET['mid'])) {
    $title = '编辑等级规则-' . $_GET['mid'];
} else {
    $title = '添加等级规则';
}
include "header.php";
if (!empty($_QET['mid'])) {
    $PRICE = $DB->get_row("SELECT * FROM `sky_price` WHERE `mid` = '{$_QET['mid']}' ");
    if (!$PRICE) show_msg('等级规则->' . $_QET['mid'] . '不存在', '等级规则->' . $_QET['mid'] . '不存在请检查是否访问有误?', 3);
}
?>

<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12" id="title" data-mid="<?= empty($_QET['mid']) ? '-1' : $_QET['mid'] ?>">
                        <h3 class="mb-3 header-title text-success"><?= $title ?></h3>
                        <form class="form-horizontal layui-form">
                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">等级名称</label>
                                <input type="text" name="name" lay-verify="required"
                                       class="form-control" value="<?= $PRICE['name'] ?>" placeholder="请输入等级名称">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">等级售价 / 元</label>
                                <input type="text" name="money"
                                       class="form-control" value="<?= $PRICE['money'] ?>"
                                       placeholder="请输入等级购买价格,用户可补差价升级">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">订单利润百分比 <a
                                            href="javascript:layer.alert('如何填写：<br>填写10就是赚10%的利润，填写20就是赚20%的利润，以此类推！<br>Ps：利润是商品售价和成本之间的金额！如100元成本，售价110元，中间的10元就是你赚的利润!',{title:'订单利润百分比说明'})">[说明]</a></label>
                                <input type="text" name="priceis"
                                       class="form-control" value="<?= $PRICE['priceis'] ?>"
                                       placeholder="填10就等于赚该等级用户百分之十的订单利润，以此类推">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">货币兑换倍数 <a
                                            href="javascript:layer.alert('如何填写：<br>如填写1000，就相当于成本的1000倍等于<?= $conf['currency'] ?>兑换价格！，以此类推！',{title:'货币兑换倍数说明'})">[说明]</a></label>
                                <input type="text" name="pointsis"
                                       class="form-control" value="<?= $PRICE['pointsis'] ?>"
                                       placeholder="如填1000,<?= $conf['currency'] ?>兑换价格就是成本的1000倍!">
                            </div>

                            <div class="form-group mb-3" style="">
                                <label for="example-input-normal"
                                       style="position: relative;bottom: 0;left: 0">等级介绍</label>
                                <textarea class="form-control" name="content" id="content"
                                          rows="6"
                                          placeholder="请输入等级介绍,纯文字即可,无需输入代码！"><?= $PRICE['content'] ?></textarea>
                            </div>

                            <button type="submit" lay-submit lay-filter="hy"
                                    class="btn btn-block btn-xs btn-success">保存等级内容
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<?php include "bottom.php"; ?>
<link href="<?= $cdnpublic ?>summernote/0.8.12/summernote.css" rel="stylesheet">
<script src="<?= $cdnpublic ?>summernote/0.8.12/summernote.min.js"></script>
<script src="<?= $cdnpublic ?>summernote/0.8.12/lang/summernote-zh-CN.min.js"></script>
<script>
    $(document).ready(function () {
        $('#content').summernote({
            lang: 'zh-CN',
            placeholder:'请输入等级介绍内容,可编辑代码哦！',
            minHeight:200,
            callbacks:{
                onImageUpload: function(files, editor, $editable) {
                    UploadFiles(files,'content');
                }
            },
        });
        function UploadFiles(files,id){
            var imageData = new FormData();
            $.each( files, function( key, val ) {
                imageData.append("imageData"+key, val);
            });
            $.ajax({
                data:imageData,
                type: "POST",
                url: "ajax.php?act=image_content",
                cache: false,
                contentType: false,
                processData: false,
                success: function(imageUrl) {
                    if(imageUrl.code==1){
                        var content = '';
                        $.each( imageUrl['SrcArr'], function( key, val ) {
                            $('#'+id).summernote('editor.insertImage',val['src']);
                            content += '图片：<font color=red>'+val['name']+'</font>大小为：<font color=red>'+val['size']+'</font><br>';
                        });
                        layer.alert(content + '<hr>Ps:图片可一次上传多张！',{title:imageUrl.msg});
                    }else layer.msg(imageUrl.msg);
                },
                error: function() {
                    layer.msg('图片上传接口异常，上传失败！');
                }
            })
        }
    });

    layui.use('form', function () {
        var form = layui.form;
        form.on('submit(hy)', function (data) {
            data.field['content'] = $("#content").summernote('code');
            data.field['state'] = $("#title").attr('data-mid');
            layer.alert('是否要执行提交操作？', {
                icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                    var index = layer.msg('数据保存中,请稍后...', {icon: 16, time: 999999});
                    $.post('ajax.php?act=level_add', data.field, function (res) {
                        if (res.code == 1) {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn: ['继续', '返回列表'],
                                btn1: function (layero, index) {
                                    location.reload();
                                },
                                btn2: function (layero, index) {
                                    location.href = 'admin.level.list.php'
                                }
                            });
                        } else {
                            layer.close(index);
                            layer.alert(res.msg, {
                                icon: 2,
                                anim: 4
                            });
                        }
                    });
                }
            });
            return false;
        });
    })

</script>


