<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="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-radio-group v-model="formData.sex">
                            <el-radio
                                v-for="item in sexOptions"
                                :key="item.value"
                                :label="item.value"
                                @change="chageAvatar"
                            >
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </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="keyAchievement" label="主要成果">
                        <el-input type="textarea" v-model="formData.keyAchievement" :rows="4"></el-input>
                    </el-form-item> -->
                    <el-form-item prop="selfIntroduction" label="主要业绩">
                        <el-input type="textarea" v-model="formData.selfIntroduction" :rows="4"></el-input>
                    </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="qualification" label="资质">
                        <multi-upload v-model="formData.qualification"></multi-upload>
                    </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>
import { phonePattern } from '../../utils/variables';
export default {
    name: 'TechnicalManagerEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('technicalManager/get/' + this.$route.query.id)
                .then(res => {
                    if (!res.field) {
                        res.field = [];
                    }
                    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);
            });
    },
    data() {
        return {
            saving: false,
            formData: {
                avatar: 'https://zhirongip.oss-cn-hangzhou.aliyuncs.com/image/2021-08-13-09-59-23yoEbtbgH.png'
            },
            rules: {
                name: [{ required: true, message: '请输入姓名' }]
                // qualification: [{ required: true, message: '请添加资质图片' }],
                // content: [{ required: true, message: '服务内容列表' }],
                // field: [{ required: true, message: '请选择专业领域', trigger: 'blur' }],
                // contact: [{ required: true, message: '请输入联系人' }],
                // phone: { required: true, pattern: phonePattern, message: '请输入联系电话', trigger: 'blur' },
                // selfIntroduction: [{ required: true, message: '请填写自我介绍' }],
                // email: [{ type: 'email', required: true, message: '请输入邮箱', trigger: 'blur' }],
                // commission: [{ required: true, message: '请输入佣金', trigger: 'blur' }],
                // work: [{ required: true, message: '请输入所在单位' }]
                // phone: [
                //     {
                //         pattern: /^1[3-9]\d{9}$/,
                //         message: '请输入正确的手机号',
                //         trigger: 'blur'
                //     }
                // ]
                // email: [
                //     {
                //         type: 'email',
                //         message: '请输入正确的邮箱地址',
                //         trigger: 'blur'
                //     }
                // ]
            },
            fieldOptions: [],
            sexOptions: [
                { label: '男', value: '男' },
                { label: '女', value: '女' }
            ],
            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, status: 'PENDING' };
            data.isExpert = false;
            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 || '删除失败');
                    }
                });
        },
        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>
