<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="components/vue/vue-list :: head('新闻', 'commit,ueditor')"></head>
<body>
<div id="app" style="padding-top: 10px">
    <el-row>
        <el-col :xs="{span: 24, offset: 0}" :sm="{span: 20, offset: 2}" :md="{span: 20, offset: 2}"
                :lg="{span: 20, offset: 2}" :xl="{span: 20, offset: 2}">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item th:label="#{newsInfo.title}" prop="title">
                    <el-input v-model="ruleForm.title" th:placeholder="#{pleaseInput} + #{newsInfo.title}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.newsStatus}" prop="newsStatus">
                    <el-select v-model="ruleForm.newsStatus" th:placeholder="#{pleaseChoose} + #{newsInfo.newsStatus}">
                        <el-option
                                v-for="item in newsStatusList"
                                :key="item.typeCode"
                                :label="item.typeName"
                                :value="item.typeCode">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.imageUrl}" prop="imageUrl">
                    <el-upload
                            class="upload-demo"
                            action="/fileUploads/upload"
                            :on-success="handleAvatarSuccess"
                            :on-remove="handleRemove"
                            :before-upload="beforeAvatarUpload"
                            :file-list="imageUrlList"
                            list-type="picture"
                            limit="1">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
                    </el-upload>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.publishDate}" prop="publishDate">
                    <el-date-picker v-model="ruleForm.publishDate" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" th:placeholder="#{pleaseChoose}"></el-date-picker>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.tags}" prop="tags">
                    <el-input v-model="ruleForm.tags" th:placeholder="#{pleaseInput} + #{newsInfo.tags}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.newsType}" prop="newsType">
                    <el-select size="small" v-model="ruleForm.newsType" th:placeholder="#{pleaseChoose}">
                        <el-option
                                v-for="item in newsTypeList"
                                :key="item.typeCode"
                                :label="item.typeName"
                                :value="item.typeCode">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{newsInfo.content}">
                    <div id="content_ueditor"></div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')"><span th:text="#{submit}"></span></el-button>
                    <el-button @click="closeWindow"><span th:text="#{close}"></span></el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</div>
</body>
<script th:src="@{/js/vue/common.js}"></script>
<script th:src="@{/js/vue/validateRule.js}"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    //Vue.http.options.emulateJSON = true;
    //Vue.http.options.emulateHTTP = true;
    var headerName = [[${_csrf.headerName}]];
    axios.defaults.headers.common[headerName] = [[${_csrf.token}]];

    //国际化
    international([[${session.locale}]]);

    var requestUrls = {
        url: "/api/newsInfos/edit"
    };

    var content_ueditor = UE.getEditor('content_ueditor', {
        initialFrameWidth: '100%',
        initialFrameHeight: '350',
        autoHeight: false
    });

    var app = new Vue({
        el: '#app',
        data: {
            newsTypeList: [[${newsTypeList}]],
            newsStatusList: [[${newsStatusList}]],
            imageUrlList: [{
                name: [[${entity.imageUrl}]],
                url: [[${entity.imageUrl}]]
            }],
            content_ueditor: '',
            ruleForm: {
                id: [[${entity.id}]],
                title: [[${entity.title}]],
                newsStatus: String([[${entity.newsStatus}]]),
                content: [[${entity.content}]],
                imageUrl: [[${entity.imageUrl}]],
                publishDate: [[${entity.publishDate}]],
                tags: [[${entity.tags}]],
                newsType: String([[${entity.newsType}]]),
            },
            rules: {
                title: [
                    {required: true, message: [[#{newsInfo.title}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                imageUrl: [
                    {required: true, message: [[#{newsInfo.imageUrl}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                content: [
                    {required: true, message: [[#{newsInfo.content}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                newsStatus: [
                    {required: true, message: [[#{newsInfo.newsStatus}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                newsType: [
                    {required: true, message: [[#{newsInfo.newsType}]] + [[#{notEmpty}]], trigger: 'blur'}
                ]
            }
        },
        mounted: function () {
            content_ueditor.ready(function () {
                content_ueditor.setContent([[${entity.content}]]);
            });
        },
        methods: {
            sendMessage(event) {
                console.log("sendMessage");
                closeLayer();
            },
            submitForm(formName) {
                parent.openLoading();

                //富文本内容
                this.ruleForm.content = content_ueditor.getContent();

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$http.patch(requestUrls.url, this.ruleForm).then((response) => {
                            parent.closeLoading();
                            parent.showSuccess([[#{updateSuccess}]]);
                            parent.updateListData();
                            closeLayer();
                        }).catch((error) => {
                            parent.closeLoading();
                            let errorMessage = [[#{updateFailed}]];
                            if (error.response) {
                                errorMessage = error.response.data.message;
                            }
                            this.$message.error(errorMessage);
                        });
                    } else {
                        parent.closeLoading();
                        setTimeout(()=>{
                            let isError= document.getElementsByClassName("is-error");
                            isError[0].querySelector('input').focus();
                        },100);
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            closeWindow(event) {
                closeLayer();
            },
            handleAvatarSuccess(res, file) {
                //this.ruleForm.headPhoto = URL.createObjectURL(file.raw);
                console.log(eval(res));
                res = eval(res);
                this.ruleForm.imageUrl = res.data[0];
            },
            beforeAvatarUpload(file) {
                console.log(file.type);
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG && !isPNG) {
                    this.$message.error('上传图片只能是 JPG/PNG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            handleRemove(file, fileList) {
                this.ruleForm.imageUrl = '';
            }
        }
    });

    /*]]>*/
</script>
</html>