<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="icon" label="头像">
                <single-upload v-model="formData.icon"></single-upload>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
                <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="手机">
                <el-input v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item prop="roleId" label="角色">
                <el-select v-model="formData.roleId" placeholder="请选择" style="width: 100%;">
                    <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item prop="departId" label="部门">
                <el-cascader :options="departs" v-model="formData.departId">
                </el-cascader>
            </el-form-item> -->
            <el-form-item prop="birthday" label="生日">
                <el-date-picker v-model="formData.birthday" format="yyyy-MM-dd" value-format="timestamp">
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="createTime" label="创建时间">
                <el-date-picker v-model="formData.createTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="timestamp">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'
export default {
    created() {
        if (this.$route.query.id) {
            this.$http.get({
                url: '/userInfo/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {
                    res.data.roleId = res.data.roleId ? String(res.data.roleId) : '5';
                    //res.data.departId = res.data.departId ? res.data.departId.split(',') : [];
                    this.formData = res.data;
                }
            })
        }
        // this.$http.get({
        //     url: '/sysRole/all'
        // }).then(res => {
        //     if (res.success) {
        //         this.roles = res.data;
        //     }
        // });
        // this.$http.get({
        //     url: '/departInfo/departTree'
        // }).then(res => {
        //     if (res.success) {
        //         const parse = (trees) => {
        //             trees.sort((a, b) => {
        //                 return a.extra.sort - b.extra.sort;
        //             });
        //             return trees.map(i => {
        //                 let t = {
        //                     value: i.id,
        //                     label: i.name,
        //                     parentId: i.parentId,
        //                     extra: i.extra
        //                 };
        //                 if (i.children instanceof Array) {
        //                     t.children = parse(i.children);
        //                 }
        //                 return t;
        //             });
        //         };
        //         this.departs = parse(res.data);
        //     }
        // });
    },
    data() {
        return {
            saving: false,
            formData: {
                departId: []
            },
            rules: {
                icon: [
                    { required: true, message: '请上传头像', trigger: 'blur' },
                ],
                username: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (!value) {
                                callback(new Error('请输入手机号'));
                            } else if (/^1[3-9]\d{9}$/.test(value)) {
                                callback();
                            } else {
                                callback(new Error('请输入正确的手机号'));
                            }
                        }, trigger: 'blur'
                    }
                ],
                roleId: [
                    { required: true, message: '请选择角色', trigger: 'blur' },
                ]
            },
            roles: [
                { id: '5', name: '管理员' },
                { id: '7', name: '用户' },
                { id: '8', name: '总经理' },
                { id: '9', name: '片区经理' },
                { id: '10', name: '业务员' },
            ],
            departs: [],
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            delete data.roleInfoList
            this.$http.post({
                url: this.formData.id ? '/userInfo/update' : '/userInfo/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/userInfo/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
