<template>
    <div>
        <div class="content-head">
            <dataBreadcrumb :breads="breads"></dataBreadcrumb>
            <h3>发布投标邀请书</h3>
            <h5>发布投标邀请书</h5>
        </div>
        <div class="content">
            <div class="describe">投标邀请书</div>
            <div class="form">
                <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
                    <el-form-item label="投标邀请书标题" prop="title">
                        <el-input v-model.trim="ruleForm.title" maxlength="30"></el-input>
                    </el-form-item>
                    <el-form-item label="上传附件">
                        <singleUpload tip="支持扩展名：.rar .zip .doc .docx .pdf .jpg..." @input="getfile"></singleUpload>
                    </el-form-item>
                    <el-form-item label="投标邀请书内容" prop="content">
                        <!-- <el-button style="margin-bottom: 30px;">插入邀请投标单位名称</el-button> -->
                        <quill-editor ref="text" v-model="ruleForm.content" :options="editorOption" class="myQuillEditor" />
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="bottomButton">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="submit1" v-if="type == 1">编辑</el-button>
            <el-button type="primary" @click="submit" v-else>提交审核</el-button>
        </div>
    </div>
</template>

<script>
import { biddingProjectId } from '@api/common/list';
import { inviteMessage } from '@api/purchaser/bid';
import { quillEditor } from 'vue-quill-editor';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

import dataBreadcrumb from '@components/dataBreadcrumb.vue';
import singleUpload from '@components/Upload/Upload.vue';
export default {
    components: {
        dataBreadcrumb,
        singleUpload,
        quillEditor
    },
    data() {
        return {
            type: '',
            editorOption: {},
            breads: ['招标管理', '招标项目管理', '工作台'],
            ruleForm: {
                title: '',
                content: '',
                accessoryPOList: []
            },
            rules: {
                title: [
                    {
                        required: true,
                        message: '请填写',
                        trigger: 'blur'
                    }
                ],
                content: [
                    {
                        required: true,
                        message: '请填写',
                        trigger: 'blur'
                    }
                ]
            }
        };
    },
    created() {
        let type = this.$route.query.type;
        this.type = type;
        if (type == 1) {
            this.getbiddingProjectId();
        }
    },
    methods: {
        cancel() {
            this.$router.go(-1);
        },
        getfile(val) {
            for (let item of val) {
                item.accessoryUrl = item.fileUrl;
            }
            this.ruleForm.accessoryPOList = val;
        },
        async getbiddingProjectId() {
            let projectId = localStorage.getItem('projectId');
            const { data, code, msg } = await biddingProjectId(projectId);
            if (code == 200) {
                for (let item of data.accessoryVOList) {
                    item.url = item.accessoryUrl;
                    item.name = item.fileName;
                }
                this.ruleForm = data;
            }
        },
        //提交表单
        async submit() {
            let ruleForm = this.ruleForm;
            ruleForm.projectId = localStorage.getItem('projectId');
            try {
                const valid = await this.$refs['ruleForm'].validate();
                if (valid) {
                    const { code, msg } = await inviteMessage(this.ruleForm);
                    if (code === 200) {
                        this.$message({
                            message: msg,
                            type: 'success'
                        });
                        this.$router.go(-1);
                    } else {
                        this.$message.error(msg);
                    }
                }
            } catch (err) {
                console.log(err);
            }
        },
        //提交表单
        async submit1() {
            let ruleForm = this.ruleForm;
            ruleForm.projectId = localStorage.getItem('projectId');
            ruleForm.accessoryPOList = this.accessoryPOList;
            try {
                const valid = await this.$refs['ruleForm'].validate();
                if (valid) {
                    const { code, msg } = await editBidding(this.ruleForm);
                    if (code === 200) {
                        this.$message({
                            message: msg,
                            type: 'success',
                            duration: 1500
                        });
                        setTimeout(() => {
                            this.$router.go(-1);
                        }, 1500);
                    } else if (code == 500) {
                        this.$message.error(msg);
                    }
                }
            } catch (err) {
                console.log(err);
            }
        }
    }
};
</script>


<style scoped>
.ql-editor {
    height: 400px;
}

.bottomButton {
    margin-top: 20px;
}

.form {
    padding-bottom: 80px;
}
</style>
