<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="94px"
                    label-position="right"
                    size="small"
                    style="max-width: 600px;"
                >
                    <el-form-item
                        prop="settingId"
                        :label="
                            formData.type == 'COMPANY'
                                ? '企业性质'
                                : formData.type == 'PRODUCT'
                                ? '产品区分'
                                : '技术区分'
                        "
                    >
                        <el-radio-group v-model="formData.settingId">
                            <el-radio v-for="item in settingIdOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="field" label="行业领域">
                        <el-select
                            v-model="formData.field"
                            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>
                    </el-form-item>
                    <el-form-item
                        prop="name"
                        :label="
                            formData.type == 'TECHNOLOGY'
                                ? '技术名称'
                                : formData.type == 'PRODUCT'
                                ? '产品名称'
                                : '企业名称'
                        "
                    >
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item
                        prop="owner"
                        :label="
                            formData.type == 'COMPANY'
                                ? '所属地区'
                                : formData.type == 'PRODUCT'
                                ? '产品所属人'
                                : '技术所属人'
                        "
                    >
                        <el-cascader
                            v-if="formData.type == 'COMPANY'"
                            v-model="formData.owner"
                            :options="areaList"
                            :show-all-levels="false"
                            clearable
                            :props="{ expandTrigger: 'hover' }"
                        ></el-cascader>
                        <el-input v-else v-model="formData.owner"></el-input>
                    </el-form-item>
                    <el-form-item prop="img" label="附图">
                        <single-upload v-model="formData.img"></single-upload>
                    </el-form-item>
                    <el-form-item
                        prop="manual"
                        :label="
                            formData.type == 'COMPANY'
                                ? '企业简介'
                                : formData.type == 'PRODUCT'
                                ? '产品详情'
                                : '技术详情'
                        "
                    >
                        <rich-text v-model="formData.manual"></rich-text>
                    </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 areaList from '../../area';
export default {
    name: 'DigitalEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('digital/get/' + this.$route.query.id)
                .then(res => {
                    if (res.type == 'PRODUCT') {
                        this.getSetting(12);
                    } else if (res.type == 'COMPANY') {
                        this.getSetting(11);
                        res.owner = res.owner.split('/');
                    } else {
                        this.getSetting(10);
                    }
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.type) {
            let type = this.$route.query.type;
            this.formData.type = this.$route.query.type;
            if (type == 'PRODUCT') {
                this.getSetting(12);
            } else if (type == 'COMPANY') {
                this.getSetting(11);
            } else {
                this.getSetting(10);
            }
        }
        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: {},
            rules: {
                settingId: [{ required: true, message: '请选择区分' }],
                field: [{ required: true, message: '请选择行业领域' }],
                name: [{ required: true, message: '请输入名称' }],
                owner: [{ required: true, message: '请输入', trigger: 'blur' }],
                manual: [{ required: true, message: '请填写技术详情' }],
                img: [{ required: true, message: '请添加附图', trigger: 'blur' }]
            },
            settingIdOptions: [],
            fieldOptions: [],
            areaList
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };
            if (data.type === 'COMPANY') {
                data.owner = data.owner.join('/');
            }

            this.saving = true;
            this.$http
                .post('/digital/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(`/digital/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>
