<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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:label="#{dictType.groupName}" prop="parentName">
                    <el-input v-model="ruleForm.parentName" th:placeholder="#{pleaseInput} + #{dictType.groupName}" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item th:label="#{dictType.groupCode}" prop="parentCode">
                    <el-input v-model="ruleForm.parentCode" th:placeholder="#{pleaseInput} + #{dictType.groupCode}" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item th:label="#{dictType.name}" prop="name">
                    <el-input v-model="ruleForm.name" th:placeholder="#{pleaseInput} + #{dictType.name}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{dictType.code}" prop="code">
                    <el-input v-model="ruleForm.code" th:placeholder="#{pleaseInput} + #{dictType.code}"></el-input>
                </el-form-item>
                <el-form-item th:label="#{orderNum}" prop="orderNum">
                    <el-input v-model="ruleForm.orderNum" th:placeholder="#{pleaseInput} + #{orderNum}" type="number"></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 = {
        add: "/api/dictTypeGroups/add"
    };

    var app = new Vue({
        el: '#app',
        data: {
            ruleForm: {
                pid: [[${entity.id}]],
                parentName: [[${entity.name}]],
                parentCode: [[${entity.code}]],
                name: "",
                code: "",
                orderNum: "",
                type: 2
            },
            rules: {
                name: [
                    {required: true, message: '字典名称不能为空', trigger: 'blur'}
                ],
                code: [
                    {required: true, message: '字典编码不能为空', trigger: 'blur'}
                ],
                orderNum: [
                    {required: true, message: '排序不能为空', trigger: 'blur'}
                ]
            }
        },
        mounted: function () {
            this.loadUnauthorizedUsers();

            this.roleTypeList = [[${roleTypeList}]];
            console.log(this.roleTypeList);
        },
        methods: {
            loadUnauthorizedUsers() {
                this.$http.get(requestUrls.unauthorizedUsers)
                    .then((response) => {
                        this.unauthorizedUsers = response.data.data;
                    }).catch((response) => {

                });
            },
            sendMessage(event) {
                console.log("sendMessage");
                closeLayer();
            },
            submitForm(formName) {
                parent.openLoading();
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$http.post(requestUrls.add, this.ruleForm).then((response) => {
                            parent.closeLoading();
                            parent.showSuccess([[#{insertSuccess}]]);
                            parent.updateListData();
                            closeLayer();
                        }).catch((error) => {
                            parent.closeLoading();
                            let errorMessage = [[#{insertFailed}]];
                            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>