<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                删除
            </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="108px"
                    label-position="right"
                    size="small"
                    style="max-width: 600px;"
                >
                    <el-form-item prop="qualification" label="照片">
                        <multi-upload v-model="formData.qualification"></multi-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="work" label="所在单位">
                        <el-input v-model="formData.work"></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="sex" label="性别">
                        <el-radio-group v-model="formData.sex">
                            <el-radio v-for="item in sexOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="affiliation" label="研究方向">
                        <el-radio-group v-model="formData.affiliation">
                            <el-radio v-for="item in affiliationOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="field" label="专业领域">
                        <template>
                            <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>
                        </template>
                    </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 class="form-submit">
                        <el-button @click="onSave" :loading="saving" type="primary">
                            保存
                        </el-button>
                        <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                            删除
                        </el-button>
                        <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
                    </el-form-item> -->
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'ExpertEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('technicalManager/get/' + this.$route.query.id)
                .then(res => {
                    if (!res.field) {
                        res.field = [];
                    }
                    if (!res.qualification) {
                        res.qualification = [
                            'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-33aDGeVuix.png'
                        ];
                    }
                    this.formData = res;
                    console.log('1111');
                    console.log(this.formData.qualification);
                })
                .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);
            });
    },
    data() {
        return {
            saving: false,
            formData: {
                qualification: ['https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-33aDGeVuix.png']
            },
            rules: {
                name: [{ required: true, message: '请输入姓名' }],
                field: [{ required: true, message: '请选择专业领域', trigger: 'blur' }],
                qualification: [{ required: true, message: '请上传图片' }],
                affiliation: [{ required: true, message: '请选择研究方向' }],
                selfIntroduction: [{ required: true, message: '请填写个人简介' }]
            },
            fieldOptions: [],
            sexOptions: [
                { label: '男', value: '男' },
                { label: '女', value: '女' }
            ],
            affiliationOptions: [
                { label: '知识产权', value: 'INTELLECTUAL_PROPERTY' },
                { label: '国防科工', value: 'NATIONAL_DEFENCE' }
            ]
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };
            data.expert = true;
            this.saving = true;
            this.$http
                .post('/technicalManager/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(`/technicalManager/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 || '删除失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
