<template>
    <div class="main">
        <div class="title">发布供给信息-技术经理人</div>
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="128px"
            label-position="right"
            size="small"
            style="max-width: 600px"
        >
            <el-form-item prop="name" label="姓名">
                <el-input style="width: 200px" placeholder="请输入名称" 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">
                        {{ 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: 400px"
                    >
                        <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="work" label="所在单位">
                <el-input style="width: 400px" placeholder="请输入所在单位" v-model="formData.work"></el-input>
            </el-form-item>
            <el-form-item prop="qualification" label="资质">
                <multi-upload v-model="formData.qualification" :limit="6">
                    请上传资质主图片，格式JPG或PNG，大小不超过1M，最多可上传6张
                </multi-upload>
            </el-form-item>
            <el-form-item prop="selfIntroduction" label="主要业绩" class="block">
                <el-input
                    style="width: 400px"
                    type="textarea"
                    v-model="formData.selfIntroduction"
                    maxlength="500"
                    show-word-limit
                    :autosize="{ minRows: 6, maxRow: 6 }"
                    placeholder="请填写主要业绩，500字以内"
                    resize="none"
                ></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="电话">
                <el-input style="width: 200px" v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item prop="email" label="邮箱">
                <el-input style="width: 200px" v-model="formData.email"></el-input>
            </el-form-item>
        </el-form>
        <div class="btn">
            <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 type="info" @click="$router.go(-1)" :disabled="saving">返回</el-button>
        </div>
    </div>
</template>
<script>
import { mapState } from 'vuex';
import { phonePattern } from '../../../utils/variables';
export default {
    name: 'TechnicalManagerEdit',
    computed: {
        ...mapState(['userInfo'])
    },
    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);
            });
        this.formData = {
            contact: this.userInfo.nickname,
            phone: this.userInfo.phone,
            email: this.userInfo.email
        };
    },
    data() {
        return {
            needChange: true,
            saving: false,
            formData: {},
            rules: {
                name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
                qualification: [{ required: true, message: '请添加资质图片', trigger: 'blur' }],
                content: [{ required: true, message: '服务内容列表', trigger: 'blur' }],
                field: [{ required: true, message: '请选择专业领域', trigger: 'blur' }],
                contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
                phone: { required: true, pattern: phonePattern, message: '请输入联系电话', trigger: 'blur' },
                selfIntroduction: [{ required: true, message: '请填写主要业绩', trigger: 'blur' }],
                email: [{ type: 'email', required: true, message: '请输入邮箱', trigger: 'blur' }],
                commission: [{ required: true, message: '请输入佣金', trigger: 'blur' }],
                work: [{ required: true, message: '请输入所在单位', trigger: 'blur' }]
            },
            fieldOptions: [],
            sexOptions: [
                { label: '男', value: '男' },
                { label: '女', value: '女' }
            ]
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                    setTimeout(() => {
                        this.$confirm('发布成功，正在等待平台审核', '提示', {
                            confirmButtonText: '继续发布',
                            cancelButtonText: '退出',
                            type: 'warning'
                        }).then(() => {
                            setTimeout(() => {
                                this.$router.push('/technicalEdit');
                                this.formData = '';
                                this.formData = {
                                    contact: this.userInfo.nickname,
                                    negotiateDirectly: false,
                                    phone: this.userInfo.phone,
                                    email: this.userInfo.email
                                };
                            }, 1000);
                        });
                        // .catch(() => {
                        //     this.show = false;
                        // });
                    }, 1000);
                } 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(() => {
                    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>
/deep/ .el-button {
    width: 100px;
    border-radius: 4px;
    margin-left: 25px;
}
/deep/ .el-form {
    margin-top: 30px;
}
/deep/ .el-upload__tip {
    width: 550px;
}
.main {
    background-color: #fff;
    margin: 17px 20px 0;
    padding-bottom: 60px;
    .btn {
        text-align: right;
    }
    .title {
        font-size: 16px;
        font-weight: bold;
        color: #000000;
        padding-bottom: 17px;
        line-height: 26px;
        border-bottom: 1px solid @bg;
    }
}
</style>
