<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增问题反馈')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-feedback-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">系统名称：</label>
                <div class="col-sm-8">
                    <select name="danganId" class="form-control" required >
                        <option value="">请选择</option>
                        <option th:each="dangan:${danganList}" th:text="${dangan.danganName}" th:value="${dangan.danganId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">主题：</label>
                <div class="col-sm-8">
                    <textarea name="feedbackTheme" class="form-control" type="text" required></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">反馈内容：</label>
                <div class="col-sm-8">
                    <textarea type="text" class="form-control" name="content" rows="4" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <input type="hidden" id="fileName" name="fileName">
                <input type="hidden" id="filePath" name="filePath">
                <label class="col-sm-3 control-label ">附件：</label>
                <div class="col-sm-8">
                    <input id="file" name="file" class="form-control" type="file"  onchange="uploadFile();" multiple >
                    <div id="fileinfo">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/feedback"
        $("#form-feedback-add").validate({
            focusCleanup: true
        });

        function uploadFile(){
            var formData = new FormData();
            if ($('#file')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            var this_1=  document.getElementById('file');
            var files = this_1.files;

            for (var i = 0, len = files.length; i < len; i++) {
                formData.append('files', files[i]);
            }
            $.ajax({
                url: ctx + "common/uploadMultipart",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    console.log(result);
                    if(result.code == '0'){
                        $("#fileName").val(result.fileName);
                        $("#filePath").val(result.url);
                        if(result.fileName != null ){
                            let names = result.fileName.split(",");
                            let html='';
                            for(let i = 0 ;i<names.length;i++) {
                                html+= "<div>"+names[i]+"</div>";
                            }
                            $("#fileinfo").html(html);
                        }

                    }
                }
            });
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-feedback-add').serialize());
            }
        }

        $("input[name='feedbackTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                    	var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                    		type: "post",
                    		url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    			if (result.code == web_status.SUCCESS) {
                    				$('#' + obj.id).summernote('insertImage', result.url);
                    			} else {
                    				$.modal.alertError(result.msg);
                    			}
                    		},
                    		error: function(error) {
                    			$.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });
    </script>
</body>
</html>