<!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="#{authority.username}" prop="username">
                    <el-input v-model="ruleForm.username" th:placeholder="#{pleaseInput} + #{authority.username}" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item th:label="#{authority.authorityName}" prop="authorities">
                    <el-select v-model="ruleForm.authorities" multiple th:placeholder="#{pleaseChoose}">
                        <el-option
                                v-for="item in roleTypeList"
                                :key="item.typeCode"
                                :label="item.typeName"
                                :value="item.typeCode">
                        </el-option>
                    </el-select>
                </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 = {
        edit: "/api/authorities/edit",
        unauthorizedUsers: "/api/authorities/unauthorized"
    };

    var app = new Vue({
        el: '#app',
        data: {
            unauthorizedUsers: [],
            roleTypeList: [],
            ruleForm: {
                userId: [[${entity.userId}]],
                username: [[${entity.username}]],
                authorities: [],
                authority: [[${entity.authority}]]
            },
            rules: {
                userId: [
                    {required: true, message: [[#{authority.userId}]] + [[#{notEmpty}]], trigger: 'blur'}
                ],
                authorities: [
                    {required: true, message: [[#{authority.authorityName}]] + [[#{notEmpty}]], trigger: 'blur'}
                ]
            }
        },
        mounted: function () {
            this.loadUnauthorizedUsers();

            this.roleTypeList = [[${roleTypeList}]];

            this.ruleForm.authorities = this.ruleForm.authority.split(",");
        },
        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.ruleForm.authority = this.ruleForm.authorities.join(",");
                        this.$http.patch(requestUrls.add, 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.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>