<template>
    <el-dialog title="新增附件" :visible.sync="show" center width="600px">
        <el-form
            hide-required-asterisk
            :model="form"
            :rules="rules"
            ref="form"
            label-width="140px"
            style="padding-right: 130px;"
        >
            <el-form-item label="附件名称" prop="attachmentName">
                <!-- <el-input v-model="form.attachmentName" placeholder="请输入附件名"></el-input> -->
                <el-select readonly v-model="form.attachmentName" clearable filterable placeholder="请输入或者选择附件">
                    <el-option
                        v-for="item in attachmentOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上传文件" prop="url">
                <attachment-upload v-model="form" :fileSize.sync="fileSize"></attachment-upload>
            </el-form-item>
            <el-form-item>
                <el-button style="width: 150px;" size="normal" type="primary" @click="onSubmit">提交</el-button>
                <el-button style="width: 120px;" size="normal" @click="show = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import logoPatent from '@/mixins/logoPatent';
export default {
    mixins: [logoPatent],
    props: {
        info: {}
    },
    data() {
        return {
            form: {
                attachmentName: '',
                fileName: '',
                url: '',
                remark: ''
            },
            show: false,
            rules: {
                attachmentName: { required: true, message: '请输入附件名称', trigger: 'blur' },
                url: { required: true, message: '请输入附件名称', trigger: 'change' }
            },
            fileSize: 0
        };
    },
    methods: {
        onSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        cancel() {
            this.$emit('cancel');
        },
        submit() {
            let data = { ...this.form };
            data.userId = this.$store.state.userInfo.id;
            data.patentId = this.info.id;
            data.version = 1;
            if (this.fileSize) {
                data.size = this.fileSize + 'KB';
            }
            this.saving = true;
            this.$http
                .post('/attachment/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.show = false;
                    this.$emit('refreash');
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        }
    }
};
</script>

<style></style>
