<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head th:replace="components/vue/vue-list :: head('新增', 'commit')"></head>
<body>
<div id="app" style="padding-top: 10px">
    <el-row>
        <el-col :xs="{span: 24, offset: 0}" :sm="{span: 12, offset: 6}" :md="{span: 8, offset: 8}"
                :lg="{span: 8, offset: 8}" :xl="{span: 8, offset: 8}">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item th:if="${entity.menuType} eq '2'" th:label="#{sideMenu.parentMenuName}" v-if="showSelectIcon">
                    <el-select v-model="ruleForm.pid" th:placeholder="#{pleaseChoose}">
                        <el-option
                                v-for="item in parentMenus"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.name}" prop="name">
                    <el-input v-model="ruleForm.name" th:placeholder="#{pleaseInput} + #{sideMenu.name}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.icon}" prop="icon" v-if="showSelectIcon">
                    <el-select v-model="ruleForm.icon" filterable th:placeholder="#{pleaseChoose}">
                        <el-option
                                v-for="item in icons"
                                :key="item.iconClass"
                                :label="item.label"
                                :value="item.iconClass">
                            <span style="float: left"><i v-bind:class="item.iconClass" style="font-size: 1.2em"></i></span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.label }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.url}" prop="url">
                    <el-input v-model="ruleForm.url" th:placeholder="#{pleaseInput} + #{sideMenu.url}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.requestMethod}" prop="requestMethod">
                    <el-select v-model="ruleForm.requestMethod" th:placeholder="#{pleaseChoose}">
                        <el-option label="*" value="*"></el-option>
                        <el-option label="GET" value="GET"></el-option>
                        <el-option label="POST" value="POST"></el-option>
                        <el-option label="PATCH" value="PATCH"></el-option>
                        <el-option label="DELETE" value="DELETE"></el-option>
                        <el-option label="GET,PATCH" value="GET,PATCH"></el-option>
                        <el-option label="GET,POST" value="GET,POST"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.remark}" prop="remark">
                    <!--<el-input v-model="ruleForm.remark" th:placeholder="#{pleaseInput} + #{sideMenu.remark}"></el-input>-->
                    <el-select v-model="ruleForm.remark" th:pleaseholder="#{pleaseChoose}">
                        <el-option th:label="#{sideMenu.remark.list}" value="list"></el-option>
                        <el-option th:label="#{sideMenu.remark.data}" value="data"></el-option>
                        <el-option th:label="#{add}" value="add"></el-option>
                        <el-option th:label="#{edit}" value="edit"></el-option>
                        <el-option th:label="#{delete}" value="delete"></el-option>
                        <el-option th:label="#{import}" value="import"></el-option>
                        <el-option th:label="#{export}" value="export"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item th:label="#{sideMenu.entityName}" prop="entityName">
                    <el-input v-model="ruleForm.entityName" th:placeholder="#{pleaseInput} + #{sideMenu.entityName}" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item th:label="#{orderNum}" prop="orderNum" v-if="showSelectIcon">
                    <el-input v-model="ruleForm.orderNum" th:placeholder="#{pleaseInput} + #{orderNum}"></el-input>
                </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/sideMenus/edit"
    };

    var app = new Vue({
        el: '#app',
        data: {
            showSelectIcon: true,
            icons: [[${icons}]],
            parentMenus: [[${parentMenus}]],
            ruleForm: {
                id: [[${entity.id}]],
                name: [[${entity.name}]],
                orderNum: [[${entity.orderNum}]],
                pid: [[${entity.pid}]],
                url: [[${entity.url}]],
                menuType: [[${entity.menuType}]],
                icon: [[${entity.icon}]],
                requestMethod: [[${entity.requestMethod}]],
                remark: [[${entity.remark}]],
                entityName: [[${entity.entityName}]]
            },
            rules: {
                name: [
                    {required: true, message: [[#{sideMenu.name}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                url: [
                    {required: true, message: [[#{sideMenu.url}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                orderNum: [
                    {required: true, message: [[#{orderNum}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                icon: [
                    {required: true, message: [[#{sideMenu.icon}]] + [[#{notEmpty}]], trigger: 'blur'}
                ]
            }
        },
        mounted: function () {
            this.showSelectIcon = (this.ruleForm.menuType !== 3);
        },
        methods: {
            sendMessage(event) {
                console.log("sendMessage");
                closeLayer();
            },
            submitForm(formName) {
                parent.openLoading();
                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) => {
                            console.error(response);
                            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.headPhoto = 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;
            }
        }
    });
    /*]]>*/
</script>
</html>