<?php
/**
 * 新增分类
 */
if (!empty($_GET['cid'])) {
    $title = '编辑商品分类-' . $_GET['cid'];
} else {
    $title = '添加适配分类';
}
include "header.php";
if (!empty($_QET['cid'])) {
    $CLASS = $DB->get_row("SELECT * FROM `sky_class` WHERE `cid` = '{$_QET['cid']}' ");
    if (!$CLASS) {
        show_msg('分类->' . $_QET['cid'] . '不存在', '分类->' . $_QET['cid'] . '不存在请检查是否访问有误?', 3);
    }
}
?>
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="mb-3 header-title text-success"><?= $title ?></h3>
                        <p class="text-muted">
                            分类图片可使用<a href="http://cdn.vue8.cn/" target="_blank">晴天云盘</a>存储哦,每个新用户都会赠送免费存储空间！
                        </p>
                        <?php if (!empty($_QET['cid'])) { ?>
                            <div class="row mb-2">
                                <div class="col-sm-4">
                                    <a href="admin.goods.list.php?cid=<?= $_QET['cid'] ?>" class="btn btn-success mb-2"><i
                                                class="layui-icon layui-icon-light mr-2"></i>
                                        查看分类下的商品
                                    </a>
                                </div>
                            </div>
                        <?php } ?>
                        <form class="form-horizontal layui-form">
                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">分类状态</label>
                                <select class="custom-select mt-3" name="state">
                                    <option value="1" <?= $CLASS['state'] == 1 ? 'selected' : null ?>>显示分类</option>
                                    <option value="2" <?= $CLASS['state'] == 2 ? 'selected' : null ?>>掩藏分类</option>
                                </select>
                            </div>

                            <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="<?= $CLASS['name'] ?>"
                                       placeholder="分类的名称,建议不要起太长">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal">分类图片</label>
                                <div class="input-group">
                                    <input type="text" name="image" lay-verify="required" lay-verType="tips"
                                           class="form-control" id="image"
                                           value="<?= $_QET['cid'] == '' ? '/assets/img/sc.jpg' : $CLASS['image'] ?>"
                                           placeholder="分类的图片,推荐放好看的图片"/>
                                    <div class="input-group-append">
                                        <span class="input-group-text" id="upload" style="cursor: pointer">上传</span>
                                    </div>
                                    <div class="input-group-append">
                                                <span class="input-group-text"
                                                      onclick="layer.alert('<img src=\''+$('#image').val()+'\' style=width:100%  />')"
                                                      style="cursor: pointer;background-color: slateblue;color: white">预览</span>
                                    </div>
                                    <div class="input-group-append">
                                        <a href="http://cdn.vue8.cn/s/pieoksig"
                                           target="_blank" class="input-group-text"
                                           style="cursor: pointer;background-color: #a8f46b;color: white">图库</a>
                                    </div>
                                </div>
                            </div>
                            <button type="submit" lay-submit lay-filter="hy"
                                    class="btn btn-block btn-xs btn-success"><?= $_QET['cid'] == '' ? '添加一个新的分类' : '保存分类信息' ?>
                            </button>
                        </form>
                        <span class="mt-3" style="display: block">晴天免费商品图库 <a href="http://cdn.vue8.cn/s/pieoksig"
                                                                              target="_blank">进入</a>,存储海量商品图片</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include "bottom.php"; ?>

<script>
    <?php
    if (!empty($_QET['cid'])) {
        echo '
        var dis = ' . $_QET['cid'] . ';';
    } else {
        echo 'var dis = "";';
    }
    ?>
    layui.use(['upload', 'form'], function () {
        var form = layui.form;
        form.on('submit(hy)', function (data) {
            layer.alert('是否要执行当前操作？', {
                icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                    var index = layer.msg('数据保存中,请稍后...', {icon: 16, time: 999999});
                    if (dis != '') {
                        data.field['dis'] = dis;
                    }
                    $.post('ajax.php?act=add_category', 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.class.list.php'
                                }
                            });
                        } else {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn1: function (layero, index) {
                                    location.reload();
                                }
                            });
                        }
                    });
                }
            });
            return false;
        });

        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , url: 'ajax.php?act=image_up' //上传接口
            , done: function (res, index, upload) {
                layer.msg('图片上传成功');
                $("input[name='image']").val(res.src);
                $("#image_base").attr('src', res.src);
            }
            , error: function () {
                layer.msg('图片上传失败!')
            }
        });
    });
</script>
