<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="109px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px;"
                >
                    <el-form-item prop="name" label="机构名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="introduction" label="机构简介">
                        <el-input type="textarea" :rows="4" v-model="formData.introduction"></el-input>
                    </el-form-item>
                    <el-form-item prop="logo" label="服务机构logo">
                        <single-upload v-model="formData.logo"></single-upload>
                    </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="content" label="服务内容列表">
                        <el-input type="textarea" :rows="4" v-model="formData.content"></el-input>
                    </el-form-item>
                    <el-form-item prop="example" label="服务案例">
                        <rich-text v-model="formData.example"></rich-text>
                    </el-form-item>
                    <el-form-item prop="contact" label="联系人">
                        <el-input v-model="formData.contact"></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 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: 'ServiceProviderEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('serviceProvider/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: 6 })
            .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: {},
            rules: {
                name: [{ required: true, message: '请输入机构名称' }],
                field: [{ required: true, message: '请选择擅长领域', trigger: 'blur' }],
                logo: [{ required: true, message: '请上传服务机构logo' }],
                introduction: [{ required: true, message: '请输入机构简介' }],
                content: [{ required: true, message: '请输入服务内容列表' }],
                example: [{ required: true, message: '请输入服务案例' }],
                contact: [{ required: true, message: '请输入联系人' }],
                phone: { required: true, pattern: phonePattern, message: '请输入联系电话', trigger: 'blur' },
                email: [{ type: 'email', required: true, message: '请输入邮箱', trigger: 'blur' }],
                address: [{ required: true, message: '请输入所在地区' }]
            },
            fieldOptions: []
        };
    },
    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('/serviceProvider/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(`/serviceProvider/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>
