<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="130px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px;"
                >
                    <el-form-item prop="type" label="专利区分">
                        <el-radio-group v-model="formData.typeId">
                            <el-radio v-for="item in typeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="lawStatus" label="法律状态">
                        <el-radio-group v-model="formData.lawStatusId">
                            <el-radio v-for="item in lawStatusOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="name" label="专利名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="agency" label="专利代理机构">
                        <el-input v-model="formData.agency"></el-input>
                    </el-form-item>
                    <el-form-item prop="agency" label="代理人">
                        <el-input v-model="formData.agent"></el-input>
                    </el-form-item>
                    <el-form-item prop="applyMain" label="申请主体">
                        <el-input v-model="formData.applyMain"></el-input>
                    </el-form-item>
                    <el-form-item prop="applyName" label="申请人">
                        <el-input v-model="formData.applyName"></el-input>
                    </el-form-item>
                    <el-form-item prop="code" label="申请号">
                        <el-input v-model="formData.code"></el-input>
                    </el-form-item>
                    <el-form-item prop="applyTime" label="申请日">
                        <el-date-picker
                            v-model="formData.applyTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="authTime" label="公开日">
                        <el-date-picker
                            v-model="formData.authTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="openCode" label="公开号">
                        <el-input v-model="formData.openCode"></el-input>
                    </el-form-item>
                    <el-form-item prop="industryClass" label="行业分类">
                        <el-select
                            v-model="formData.industryClass"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in industryClassOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="owner" label="专利权人">
                        <el-input v-model="formData.owner"></el-input>
                    </el-form-item>
                    <el-form-item prop="ownerOneAddr" label="专利权人地址">
                        <el-input v-model="formData.ownerOneAddr"></el-input>
                    </el-form-item>
                    <el-form-item prop="inventor" label="发明人">
                        <!-- <el-input v-model="formData.inventor"></el-input> -->
                        <el-tag
                            :key="tag"
                            size="medium"
                            v-for="tag in formData.inventor"
                            closable
                            :disable-transitions="false"
                            @close="handleClose(tag)"
                        >
                            {{ tag }}
                        </el-tag>
                        <el-input
                            class="input-new-tag"
                            v-model="inputValue"
                            ref="saveTagInput"
                            @keyup.enter.native="handleInputConfirm"
                            @blur="handleInputConfirm"
                        >
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="ipc" label="IPC分类号">
                        <!-- <el-tag
                            :key="tag"
                            size="medium"
                            v-for="tag in formData.ipc"
                            closable
                            :disable-transitions="false"
                            @close="handleClose(tag)"
                        >
                            {{ tag }}
                        </el-tag>
                        <el-input
                            class="input-new-tag"
                            v-model="inputValue"
                            ref="saveTagInput"
                            @keyup.enter.native="handleInputConfirm"
                            @blur="handleInputConfirm"
                        >
                        </el-input> -->
                        <el-select
                            v-model="formData.ipc"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入IPC分类号，按回车分割"
                            clearable
                            popper-class="hideSelect"
                            style="width: 100%"
                        >
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="techOne" label="技术分类">
                        <el-input v-model="formData.techOne"></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="digest" label="摘要">
                        <el-input type="textarea" :rows="10" v-model="formData.digest"></el-input>
                    </el-form-item>
                    <el-form-item prop="manual" label="权利要求书">
                        <el-input type="textarea" :rows="10" v-model="formData.manual"></el-input>
                    </el-form-item>
                    <el-form-item prop="wordUrl" label="文档">
                        <file-upload v-model="formData.wordUrl" :limit="1"></file-upload>
                    </el-form-item>
                    <el-form-item prop="pdfUrl" label="PDF">
                        <file-upload v-model="formData.pdfUrl" :limit="1"></file-upload>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'IntellectualPropertyEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('intellectualProperty/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/setting/byFlag', { flag: 13 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.industryClassOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/setting/byFlag', { flag: 27 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.lawStatusOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/setting/byFlag', { flag: 28 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.typeOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {
                inventor: []
            },
            rules: {
                name: [{ required: true, message: '请输入专利名称' }],
                typeId: [{ required: true, message: '请选择专利区分' }],
                lawStatusId: [{ required: true, message: '请选择法律状态' }],
                agency: [{ required: true, message: '请输入代理人', trigger: 'blur' }],
                applyMain: [{ required: true, message: '请输入申请主体' }],
                applyName: [{ required: true, message: '请输入申请人' }],
                code: [{ required: true, message: '请输入申请号', trigger: 'blur' }],
                applyTime: [{ required: true, message: '请选择申请日', trigger: 'blur' }],
                openCode: [{ required: true, message: '请输入公开号', trigger: 'blur' }],
                industryClass: [{ required: true, message: '请选择行业分类', trigger: 'blur' }],
                owner: [{ required: true, message: '请输入专利权人', trigger: 'blur' }],
                ownerOneAddr: [{ required: true, message: '请输入专利权人地址', trigger: 'blur' }],
                inventor: [{ required: true, message: '请输入发明人', trigger: 'blur' }],
                ipc: [{ required: true, message: '请输入IPC分类号', trigger: 'blur' }],
                techOne: [{ required: true, message: '请输入技术分类', trigger: 'blur' }],
                img: [{ required: true, message: '请上传摘要附图', trigger: 'blur' }],
                digest: [{ required: true, message: '请填写摘要', trigger: 'blur' }],
                manual: [{ required: true, message: '请填写权利要求书', trigger: 'blur' }],
                wordUrl: [{ required: true, message: '请上传文档', trigger: 'blur' }],
                pdfUrl: [{ required: true, message: '请上传PDF', trigger: 'blur' }],
                authTime: [{ required: true, message: '请选择公开日' }]
            },
            typeOptions: [],
            lawStatusOptions: [],
            industryClassOptions: [],
            inputValue: '',
            inputValue1: ''
        };
    },
    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('/intellectualProperty/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(`/intellectualProperty/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 || '删除失败');
                    }
                });
        },
        handleClose(tag) {
            this.formData.inventor.splice(this.formData.inventor.indexOf(tag), 1);
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.formData.inventor.push(inputValue);
            }
            this.inputValue = '';
        },
        handleClose1(tag) {
            this.formData.ipc.splice(this.formData.ipc.indexOf(tag), 1);
        },
        handleInputConfirm1() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.formData.ipc.push(inputValue);
            }
            this.inputValue1 = '';
        }
    }
};
</script>
<style lang="less" scoped>
.el-tag {
    margin-bottom: 11px;
}
.el-tag + .el-tag {
    margin-left: 10px;
}
.input-new-tag {
    width: 100%;
    vertical-align: bottom;
}
</style>
