<template>
    <PageHeader :title="title">
    </PageHeader>
    <Form ref="userValidate" :model="userinfo" :rules="userValidate" :label-width="80" style="margin-top: 20px;">
        <FormItem label="用户名" prop="name">
            <Input v-model="userinfo.name" placeholder="请输入用户名" style="max-width: 600px;"></Input>
        </FormItem>
        <!-- v-if="$route.query.id == $store.state.id && $route.query.id" -->
        <FormItem v-if="elements.indexOf('4-3') == -1 && $route.query.id == $store.state.id && $route.query.id"
            label="原密码" prop="oldpassword">
            <Input v-model="userinfo.oldpassword" placeholder="请确认原密码" type="password" password
                style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem label="密码" prop="password">
            <Input v-model="userinfo.password" placeholder="请输入密码" type="password" password
                style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem label="确认密码" prop="secondpassword">
            <Input v-model="userinfo.secondpassword" placeholder="请再次输入密码" type="password" password
                style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem v-if="elements.indexOf('4-3') != -1" name="组织更改" label="组织">
            <Select filterable v-model="userinfo.organization_id" style="max-width: 300px;">
                <Option v-for="item in organization" :value="item.id">{{ item.path }}</Option>
            </Select>
        </FormItem>
        <FormItem v-if="elements.indexOf('4-3') != -1" name="角色更改" label="角色" prop="role_id">
            <Select filterable v-model="userinfo.role_id" style="max-width: 300px;">
                <Option v-for="item in role" :value="item.id">{{ item.name }}</Option>
            </Select>
        </FormItem>
        <FormItem label="jira账户" prop="jiar_account">
            <Input v-model="userinfo.jira_account" placeholder="请输入jira账户" style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem label="jira密码" prop="jira_password">
            <Input v-model="userinfo.jira_password" placeholder="请输入jira账户密码" style="max-width: 600px;" type="password"
                password></Input>
        </FormItem>
        <FormItem label="email" prop="email">
            <Input v-model="userinfo.email_address" placeholder="请输入邮箱地址" style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem label="邮箱密码" prop="email_password">
            <Input v-model="userinfo.email_password" placeholder="请输入邮箱密码" type="password" password
                style="max-width: 600px;"></Input>
        </FormItem>
        <FormItem style="text-align: left;">
            <Button type="primary" @click="submit">提交</Button>
            <Button style="margin-left: 8px" @click="cancle">取消</Button>
        </FormItem>
    </Form>
</template>
<script>
export default {
    data() {
        const validatePass = (rule, value, callback) => {
            if (value === '' && !this.$route.query.id) {
                callback(new Error('请输入密码'));
            } else {
                // && this.$route.query.id == this.$store.state.id && this.$route.query.id
                if (this.userinfo.oldpassword == '' && this.$route.query.id == this.$store.state.id && this.$route.query.id) {
                    this.$refs.userValidate.validateField('oldpassword')
                }
                if (this.userinfo.secondpassword !== '') {
                    this.$refs.userValidate.validateField('secondpassword');
                }
                callback();
            }
        };
        const validatePassCheck = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else if (value !== this.userinfo.password) {
                callback(new Error('两次密码不一致!'));
            } else {
                callback();
            }
        };
        // && this.$route.query.id && this.$route.query.id == this.$store.state.id
        const validateOldPassCheck = (rule, value, callback) => {
            if (value === '' && this.userinfo.password && this.$route.query.id == this.$store.state.id && this.$route.query.id) {
                callback(new Error('请输入旧密码'));
            } else callback()
        };
        const validateJiraPassCheck = (rule, value, callback) => {
            if (value === '' && this.userinfo.jiar_account) {
                callback(new Error('请输入jira密码'));
            } else callback()
        };
        return {
            page_id: 17,
            elements: [],
            userinfo: {
                name: '',
                password: '',
                secondpassword: '',
                oldpassword: '',
                organization_id: null,
                role_id: 0,
                jiar_account: '',
                jiar_password: '',
                email_address: ''
            },
            userValidate: {
                name: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' }
                ],
                oldpassword: [
                    { validator: validateOldPassCheck, trigger: 'blur' }
                ],
                password: [
                    { validator: validatePass, trigger: 'blur', required: this.$route.query.id ? false : true }
                ],
                secondpassword: [
                    { validator: validatePassCheck, trigger: 'blur', required: this.$route.query.id ? false : true }
                ],
                role_id: [
                    { required: true, message: '请选择角色', trigger: 'change', type: 'number' }
                ],
                email: [
                    { type: 'email', message: '邮箱格式错误', trigger: 'blur' }
                ],
                jiar_password: [
                    { validator: validateJiraPassCheck, trigger: 'blur' }
                ]
            },
            role: [],
            organization: [],
        }

    },
    computed: {
        title() {
            return this.$route.query.id ? "编辑信息" : "新增用户"
        }
    },
    methods: {
        async submit() {
            var next_ = await this.$refs["userValidate"].validate((valid) => {
                if (!valid) {
                    this.$Message.error('请完善信息')
                };
            })
            if (!next_) return
            'id' in this.userinfo ? this.updateUser() : this.addUser()
        },
        cancle() {
            setTimeout(() => {
                this.$router.back()
            }, 500);
        },
        getOrg() {
            this.$request("get", "/identity/organization_list", null, this, (data) => { console.info(data), this.organization = data.data })
        },
        getRole() {
            this.$request("get", "/identity/role_list", null, this, (data) => { this.role = data.data })
        },
        getUser(id) {
            this.$request("get", "/user/getuser/" + id, null, this, (data) => { this.userinfo = data.data })
        },
        addUser() {
            console.info(this.userinfo)
            this.$request("post", "/user/adduser", this.userinfo, this, () => { this.cancle() })
        },
        updateUser() {
            this.$request("post", "/user/updateuser", this.userinfo, this, () => { this.cancle() })
        }
    },
    created() {
        this.getOrg();
        this.getRole();
        this.$route.query.id && this.getUser(this.$route.query.id);
        this.$getElements(this.page_id, this, (data) => this.elements = data.data)
    }
}
</script>
