<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            size="small"
            style="max-width: 600px"
        >
            <el-form-item prop="avatar" label="照片">
                <single-upload v-model="formData.avatar"></single-upload>
            </el-form-item>
            <el-form-item prop="name" label="姓名">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item prop="sex" label="性别">
                <el-select v-model="formData.sex" clearable filterable placeholder="请选择" @change="chageAvatar">
                    <el-option v-for="item in sexOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="birthday" label="出生年月">
                <el-date-picker
                    v-model="formData.birthday"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="work" label="单位">
                <el-input v-model="formData.work"></el-input>
            </el-form-item>
            <el-form-item prop="academicQualification" label="学历">
                <el-input v-model="formData.academicQualification"></el-input>
            </el-form-item>
            <el-form-item prop="jobTitle" label="职称">
                <el-input v-model="formData.jobTitle"></el-input>
            </el-form-item>
            <el-form-item prop="position" label="职位">
                <el-input v-model="formData.position"></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="email" label="邮箱">
                <el-input v-model="formData.email"></el-input>
            </el-form-item>
            <el-form-item prop="address" label="通讯地址">
                <el-input v-model="formData.address"></el-input>
            </el-form-item>
            <el-form-item prop="affiliation" label="研究方向">
                <el-radio-group v-model="formData.affiliation">
                    <el-radio v-for="item in affiliations" :key="item.value" :label="item.value">
                        {{ item.label }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="field" label="专业领域">
                <el-select
                    v-model="formData.field"
                    multiple
                    clearable
                    filterable
                    placeholder="请选择"
                    style="width: 100%"
                >
                    <el-option v-for="item in fieldOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="selfIntroduction" label="个人简介">
                <el-input type="textarea" v-model="formData.selfIntroduction" :rows="10"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id">删除 </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'ExpertEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('expert/get/' + this.$route.query.id)
                .then(res => {
                    if (!res.avatar) {
                        res.avatar =
                            'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-23yoEbtbgH.png';
                        if (res.sex == '女') {
                            res.avatar =
                                'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-33aDGeVuix.png';
                        }
                    }
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/setting/byFlag', { flag: 1 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.fieldOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/setting/byFlag', { flag: 15 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.affiliations.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {
                avatar: 'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-23yoEbtbgH.png'
            },
            rules: {
                phone: [
                    {
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ],
                email: [
                    {
                        type: 'email',
                        message: '请输入正确的邮箱地址',
                        trigger: 'blur'
                    }
                ]
            },
            sexOptions: [
                { label: '男', value: '男' },
                { label: '女', value: '女' }
            ],
            fieldOptions: [],
            affiliations: [],
            avatars: [
                'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-23yoEbtbgH.png',
                'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-33aDGeVuix.png'
            ]
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/expert/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/expert/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '删除失败');
                    }
                });
        },
        chageAvatar(res) {
            let avatar = this.formData.avatar;
            if (res == '男' && (avatar == this.avatars[1] || avatar == '')) {
                this.formData.avatar = this.avatars[0];
            } else if (res == '女' && (avatar == this.avatars[0] || avatar == '')) {
                this.formData.avatar = this.avatars[1];
            }
        }
    }
};
</script>
<style lang="less" scoped></style>
