<!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-edit" th:object="${badge}">
        <input name="badgeId" th:field="*{badgeId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">徽章名称：</label>
            <div class="col-sm-8">
                <input name="name" th:field="*{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" th:field="*{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" th:value="*{icon}">
                <div id="badgeIconPreviewWrap" class="m-b"
                     th:style="${badge.icon != null and (#strings.startsWith(badge.icon, 'http') or #strings.startsWith(badge.icon, '/'))} ? '' : 'display:none;'">
                    <img id="badgeIconPreview" alt="当前图标"
                         th:src="${badge.icon != null and (#strings.startsWith(badge.icon, 'http') or #strings.startsWith(badge.icon, '/'))} ? ${badge.icon} : ''"
                         style="max-height:96px;border-radius:4px;border:1px solid #eee;"/>
                </div>
                <p id="badgeIconLegacyHint" class="help-block"
                   th:if="${badge.icon != null and !(#strings.startsWith(badge.icon, 'http') or #strings.startsWith(badge.icon, '/'))}">
                    当前为 Emoji 文本：<strong th:text="${badge.icon}"></strong>，上传图片保存后将替换为图片地址。
                </p>
                <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">
                <input type="hidden" name="type" th:value="*{type}"/>
                <select class="form-control m-b" disabled>
                    <option th:selected="${badge.type == 1}" value="1">护照纪念徽章</option>
                    <option th:selected="${badge.type == 2}" value="2">演出纪念徽章</option>
                </select>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 徽章类型保存后不可修改</span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input name="sort" th:field="*{sort}" class="form-control" type="number" min="0" placeholder="数值越大越靠前">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分享文案：</label>
            <div class="col-sm-8">
                <textarea name="shareText" th:field="*{shareText}" class="form-control" rows="3" maxlength="255" placeholder="请输入徽章分享文案"></textarea>
            </div>
        </div>
        <div class="form-group" id="ticketTimesDiv" th:style="${badge.type == 2 ? 'display:block;' : 'display:none;'}">
            <label class="col-sm-3 control-label is-required">关联演出：</label>
            <div class="col-sm-8">
                <input name="performanceId" id="performanceId" th:field="*{performanceId}" class="form-control" type="text" placeholder="请输入演出ID" th:required="${badge.type == 2}">
                <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 == '2') {
            $("#ticketTimesDiv").show();
            $("#performanceId").prop("required", true);
        } else {
            $("#ticketTimesDiv").hide();
            $("#performanceId").prop("required", false);
            $("#performanceId").val("");
        }
    }

    $(function () {
        typeChange($('input[name="type"]').val());

        $("#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();
            $("#badgeIconLegacyHint").hide();
        });
    });

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

    function submitHandler() {
        if (!$("#badgeIconUrl").val()) {
            $.modal.msgWarning("请先上传徽章图标（若为历史 Emoji 数据，请上传一张图片作为图标）");
            return;
        }
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-badge-edit').serialize());
        }
    }
</script>
</body>
</html>
