<template>
    <div>
        <div class="content-head">
            <dataBreadcrumb :breads="breads"></dataBreadcrumb>
            <h3>{{ type == 1 ? '编辑' : '发布' }}招标公告</h3>
            <h5>{{ type == 1 ? '编辑' : '发布' }}招标公告</h5>
            <div v-if="type == 1" class="abs" style="width:400px">
                <div>拒绝原因：{{ ruleForm.reason }}</div>
            </div>
        </div>
        <div class="content">
            <div class="describe">招标公告</div>
            <div class="form">
                <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" 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"
                            :fileList="ruleForm.accessoryPOList"
                        ></singleUpload>
                    </el-form-item>
                    <el-form-item label="公告内容" prop="content">
                        <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="back">取消</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, projectDetail } from '@api/common/list';
import { bidMessage,editBidding} 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() {
        // 检测文件是否上传
        let checkupload = (rule, value, callback) => {
            if (this.justimg.length === 0) {
                callback(new Error('请上传正面照片'));
            } else {
                callback();
            }
        };
        return {
            accessoryPOList: [],
            editorOption: {},
            breads: ['招标管理', '招标项目管理', '工作台'],
            ruleForm: {
                title: '',
                content: ''
            },
            rules: {
                title: [
                    {
                        required: true,
                        message: '请填写',
                        trigger: 'blue'
                    }
                ],
                content: [
                    {
                        required: true,
                        message: '请填写',
                        trigger: 'blue'
                    }
                ]
            },
            projectCode: '',
            projectName: '',
            type: ''
        };
    },
    created() {
        let type = this.$route.query.type;
        this.type = type;
        if (type == 1) {
            this.getbiddingProjectId();
        }
        this.projectCode = localStorage.getItem('projectCode');
        this.projectName = localStorage.getItem('projectName');
    },
    methods: {
        back() {
            this.$router.go(-1);
        },
        cancel() {
            this.$router.go(-1);
        },
        getfile(val) {
            for (let item of val) {
                item.accessoryUrl = item.fileUrl;
            }
            this.accessoryPOList = val;
        },
        // 获取详情
        async getbiddingProjectId() {
            let projectId = localStorage.getItem('projectId');
            const { data, code, msg } = await biddingProjectId(projectId);
            if (code == 200) {
                data.accessoryPOList = data.accessoryVOList;
                for (let item of data.accessoryPOList) {
                    item.name = item.fileName;
                    item.url = item.accessoryUrl;
                }
                this.ruleForm = data;
            }
        },
        //提交表单
        async submit() {
            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 bidMessage(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);
            }
        },
        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>
