<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../common.jsp" />
    <style>
        .layui-form-item{
            margin-top: 10px;
        }
        .layui-form{
            width: 75%;
            margin-left: 10%;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" id="addForm">
    <input name="id" value="${entity.id}" hidden/>
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input name="title" type="text" value="${entity.title}" autocomplete="off" maxlength="11" lay-verify="required" placeholder="标题" class="layui-input"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <input name="describe" type="text" value="${entity.describe}" lay-verify="required" autocomplete="off" maxlength="50"  placeholder="简介" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" id="content" name="content">${entity.content}</textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">url</label>
        <div class="layui-input-block">
            <input name="url" type="text" value="${entity.url}" autocomplete="off"  placeholder="url" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="upload"><i class="layui-icon">&#xe67c;</i>上传图片</button>
            <div class="prev-image" style="margin-top: 5px;display: ${empty entity || empty entity.photo ? 'none' : 'block'}">
                <img src="${entity.photo}" id="prevImg" alt="图片" style="height: 100px;border: 1px solid #e1e1e1;" />
            </div>
            <input name="photoBaseStr" id="photoBaseStr" type="hidden" value="${entity.photoBaseStr}"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input name="sort" type="number" value="${entity.sort}" autocomplete="off" maxlength="8"  placeholder="排序" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <select name="status" id="status" lay-verify="required">
               <option value="">请选择</option>
                <option value="1" ${empty entity || entity.status eq 1 ? 'selected' : ''}>是</option>
                <option value="-1" ${entity.status eq -1 ? 'selected' : ''}>否</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="button" id="close" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','layer','upload'], function(){
        let layer = layui.layer
            , form = layui.form, upload = layui.upload;
        $('#close').click(function() {
            parent.layer.closeAll();
        });
        var up = upload.render({
            elem: '#upload' //绑定元素
            ,url: '/api/upload/' //上传接口
            ,auto:false
            ,acceptMime:"image/*"
            ,accept:"images"
            ,size : 1024 //最大上传1M的图片
            ,choose: function(obj){
                //将每次选择的文件追加到文件队列
                var files = obj.pushFile();
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                obj.preview(function(index, file, result){
                    $(".prev-image").show();
                    $("#prevImg").attr("src",result)
                    $("#photoBaseStr").val(result);
                });
            }
            ,done: function(res){
                //上传完毕回调
            }
            ,error: function(){
                //请求异常回调
            }
        });
        //监听提交
        form.on('submit(formDemo)', function(data){
            var loading = layer.msg('数据提交中...', {icon: 16,shade: 0.3,time: false});
            let params = data.field;
            params.content = um.getContent();
            $.ajax({
                url: '${ctx}/jftSmallClass/save',
                method: 'post',
                data: JSON.stringify(params),
                dataType: 'JSON',
                contentType:"application/json",
                success: function(res) {
                    layer.close(loading);
                    if(res.code == 200) {
                        layer.msg(res.message, { icon: 1, time: 1000 }, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layui.table.reload('jftSmallClass');
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.message, {
                            icon: 2
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>
