<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增徽章')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-badge-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">徽章名称：</label>
            <div class="col-sm-8">
                <input name="name" class="form-control" type="text" maxlength="50" required placeholder="最多50个字">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">徽章副标题：</label>
            <div class="col-sm-8">
                <input name="subTitle" class="form-control" type="text" maxlength="20" placeholder="最多20个字">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">徽章图标：</label>
            <div class="col-sm-8">
                <input type="hidden" name="icon" id="badgeIconUrl" value="">
                <div id="badgeIconPreviewWrap" class="m-b" style="display:none;">
                    <img id="badgeIconPreview" src="" alt="预览" style="max-height:96px;border-radius:4px;border:1px solid #eee;"/>
                </div>
                <div class="file-loading">
                    <input id="fileinput-badge-icon" type="file" name="file" data-browse-on-zone-click="true">
                </div>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 支持 jpg、png、gif、webp，单张最大 5M；上传后自动保存为图片地址。</span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">徽章类型：</label>
            <div class="col-sm-8">
                <select name="type" class="form-control m-b" required onchange="typeChange(this.value)">
                    <option value="">请选择</option>
                    <option value="1">护照纪念徽章</option>
                    <option value="2">演出纪念徽章</option>
                    <option value="4">签证页</option>
                    <!-- <option value="3">特殊徽章</option> -->
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input name="sort" class="form-control" type="number" min="0" value="0" placeholder="数值越大越靠前">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分享文案：</label>
            <div class="col-sm-8">
                <textarea name="shareText" class="form-control" rows="3" maxlength="255" placeholder="请输入徽章分享文案"></textarea>
            </div>
        </div>
        <div class="form-group" id="ticketTimesDiv" style="display: none;">
            <label class="col-sm-3 control-label is-required">关联演出：</label>
            <div class="col-sm-8">
                <input name="performanceId" id="performanceId" class="form-control" type="text" placeholder="请输入演出ID">
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 演出纪念徽章、签证页必填</span>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script type="text/javascript" th:inline="javascript">
    var prefix = ctx + "adam/caomei/badge";
    var platformUrl = /*[[${platformUrl}]]*/ '';

    function typeChange(val) {
        if (val == 2 || val == 4) {
            $("#ticketTimesDiv").show();
            $("#performanceId").prop("required", true);
        } else {
            $("#ticketTimesDiv").hide();
            $("#performanceId").prop("required", false);
            $("#performanceId").val("");
        }
    }

    $("#form-badge-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if (!$("#badgeIconUrl").val()) {
            $.modal.msgWarning("请先上传徽章图标");
            return;
        }
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-badge-add').serialize());
        }
    }

    $(function () {
        $("#fileinput-badge-icon").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': platformUrl + "/platform/basicServices/alOss/upload/unsm",
            "uploadExtraData": {
                "pathName": "other",
                "buckType": 1
            },
            autoReplace: true,
            dropZoneTitle: "点击或拖拽上传徽章图标",
            maxFileCount: 1,
            maxFileSize: 5120,
            allowedFileExtensions: ['jpg', 'jpeg', 'png', 'gif', 'webp'],
            msgSizeTooLarge: '文件 "{name}" ({size} KB) 超过了允许大小 {maxSize} KB，最大支持上传5M文件'
        }).on("filebatchselected", function () {
            $(this).fileinput("upload");
        }).on("fileuploaded", function (event, data) {
            if (!data.response || !data.response.data || !data.response.data.ossPath) {
                $.modal.alertWarning((data.response && data.response.msg) ? data.response.msg : "图片上传失败");
                return;
            }
            var fullUrl = "https://img.zhengzai.tv/" + data.response.data.ossPath;
            $("#badgeIconUrl").val(fullUrl);
            $("#badgeIconPreview").attr("src", fullUrl);
            $("#badgeIconPreviewWrap").show();
        });
    });
</script>
</body>
</html>
