<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="name" label="名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="patentType" label="专利类型" v-if="formData.category == 'GENERAL'">
                        <el-radio-group v-model="formData.patentType">
                            <el-radio v-for="item in patentTypeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="logoType" label="商标类型" v-if="formData.category == 'LOGO'">
                        <el-radio-group v-model="formData.logoType">
                            <el-radio v-for="item in logoTypeOptions" :key="item.value" :label="item.value"
                                >{{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="img" label="成交案例附图" v-if="formData.category != 'SERVICE'">
                        <single-upload v-model="formData.img"></single-upload>
                    </el-form-item>
                    <el-form-item
                        prop="settingId"
                        v-if="formData.category != 'COPYRIGHT' && formData.category != 'SOFT_COPYRIGHT'"
                        label="所属领域"
                    >
                        <el-select
                            v-model="formData.settingId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in settingIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item
                        prop="settingId"
                        v-else
                        :label="`${formData.category == 'COPYRIGHT' ? '作品' : '软著'}类型`"
                    >
                        <el-select
                            v-model="formData.settingId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in settingIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="soldAt" label="成交时间">
                        <el-date-picker
                            v-model="formData.soldAt"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                            style="width: 100%"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="soldPrice" label="成交价格">
                        <el-input-number
                            type="number"
                            v-model="formData.soldPrice"
                            style="width: 50%"
                        ></el-input-number>
                    </el-form-item>
                    <el-form-item prop="mode" label="交易方式" v-if="formData.category != 'SERVICE'">
                        <el-radio-group v-model="formData.mode">
                            <el-radio v-for="item in modeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="soldAddress" label="成交地点">
                        <el-input v-model="formData.soldAddress"></el-input>
                    </el-form-item>
                    <el-form-item prop="manual" label="描述">
                        <rich-text v-model="formData.manual"></rich-text>
                    </el-form-item>
                    <!-- <el-form-item prop="view" label="浏览量">
                        <el-input-number type="number" v-model="formData.view" style="width: 50%"></el-input-number>
                    </el-form-item> -->
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'SuccessCaseEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('successCase/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    if (res.category == 'DECLASSIFICATION') {
                        this.getSetting(13);
                    } else if (res.category == 'LOGO') {
                        this.getSetting(2);
                    } else if (res.category == 'COPYRIGHT') {
                        this.getSetting(3);
                    } else if (res.category == 'SOFT_COPYRIGHT') {
                        this.getSetting(4);
                    } else if (res.category == 'SERVICE') {
                        this.getSetting(6);
                    } else {
                        this.getSetting(1);
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.category) {
            let category = this.$route.query.category;
            this.formData.category = this.$route.query.category;
            if (category == 'DECLASSIFICATION') {
                this.getSetting(13);
            } else if (category == 'LOGO') {
                this.getSetting(2);
            } else if (category == 'COPYRIGHT') {
                this.getSetting(3);
            } else if (category == 'SOFT_COPYRIGHT') {
                this.getSetting(4);
            } else if (category == 'SERVICE') {
                this.getSetting(6);
            } else {
                this.getSetting(1);
            }
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                name: [{ required: true, message: '请输入名称' }],
                patentType: [{ required: true, message: '请选择专利类型' }],
                logoType: [{ required: true, message: '请选择商标类型' }],
                settingId: [{ required: true, message: '请选择', trigger: 'blur' }],
                soldAt: [{ required: true, message: '请选择成交时间' }],
                soldPrice: [{ required: true, message: '请输入成交价格' }],
                img: [{ required: true, message: '请添加成交案例附图', trigger: 'blur' }],
                mode: [{ required: true, message: '请选择交易方式', trigger: 'blur' }],
                manual: [{ required: true, message: '请填写描述', trigger: 'blur' }],
                soldAddress: [{ required: true, message: '请输入成交地点' }]
            },
            patentTypeOptions: [
                { label: '发明', value: 'INVENTION' },
                { label: '实用新型', value: 'UTILITY_MODEL' },
                { label: '外观设计', value: 'APPEARANCE_DESIGN' }
            ],
            logoTypeOptions: [
                { label: '普通商标', value: 'GENERAL' },
                { label: '集体商标', value: 'COLLECTIVE' },
                { label: '证明商标', value: 'PROOF' }
            ],
            settingIdOptions: [],
            modeOptions: [
                { label: '转让', value: 'TRANSFER' },
                { label: '许可', value: 'PERMIT' },
                { label: '入股', value: 'INVEST' },
                { label: '其他', value: 'OTHER' }
            ],
            categoryOptions: [
                { label: 'DECLASSIFICATION', value: 'DECLASSIFICATION' },
                { label: 'GENERAL', value: 'GENERAL' },
                { label: 'LOGO', value: 'LOGO' },
                { label: 'COPYRIGHT', value: 'COPYRIGHT' },
                { label: 'SOFT_COPYRIGHT', value: 'SOFT_COPYRIGHT' },
                { label: 'TECHNOLOGY', value: 'TECHNOLOGY' },
                { label: 'PRODUCT', value: 'PRODUCT' }
            ]
        };
    },
    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('/successCase/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(`/successCase/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 || '删除失败');
                    }
                });
        },
        getSetting(flag) {
            this.$http
                .post('/setting/byFlag', { flag: flag })
                .then(res => {
                    if (res.length > 0) {
                        res.forEach(item => {
                            this.settingIdOptions.push({
                                label: item.name,
                                value: item.id
                            });
                        });
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
