<template>
    <div>
        <div class="content-head">
            <dataBreadcrumb :breads="breads"></dataBreadcrumb>
            <h3>{{ title }}</h3>
            <h5>{{ title }}</h5>
            <div class="abs" style="width:400px">
              <div v-if="state">拒绝原因：{{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="120px" class="demo-ruleForm">
                    <el-form-item label="公告标题" prop="title">
                        <el-input v-model.trim="ruleForm.title" placeholder="请输入" maxlength="30"></el-input>
                    </el-form-item>
                    <el-form-item label="上传附件" prop="file">
                        <singleUpload
                            :fileList="ruleForm.accessoryVOList"
                            tip="支持扩展名：.rar .zip .doc .docx .pdf .jpg..."
                            @input="getfile"
                        ></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 class="centerButton">
                    <el-button @click="cancle">取消</el-button>
                    <el-button type="primary" @click="submit" v-if="!state">提交审核</el-button>
                    <el-button type="primary" @click="change" v-else>提交修改</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { createChange, noticeChange,editchange } from '@api/purchaser/bid';
import dataBreadcrumb from '@components/dataBreadcrumb.vue';
import singleUpload from '@components/Upload/Upload.vue';
import { quillEditor } from 'vue-quill-editor';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
export default {
    components: {
        dataBreadcrumb,
        singleUpload,
        quillEditor
    },
    data() {
        return {
            state:false,
            id: '',
            editorOption: {},
            title: '发布变更公告',
            breads: ['招标管理', '招标项目管理', '工作台'],
            ruleForm: {
                title: '',
                content: '',
                accessoryPOList: []
            },
            rules: {
                title: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blue'
                    }
                ],
                release: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blue'
                    }
                ],
                content: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blue'
                    }
                ]
            }
        };
    },
    created() {
        let id = this.$route.query.id;
        this.id = id;
        if (id) {
            this.title = '修改变更公告';
            this.state=true
            this.changeDetail(id);
        }
    },
    methods: {
        getfile(val) {
            this.ruleForm.accessoryPOList = val;
        },
        cancle() {
            this.$router.go(-1);
        },
        //提交表单
        async submit() {
            try {
                const valid = await this.$refs['ruleForm'].validate();
                if (valid) {
                   let ruleForm = this.ruleForm;
                  ruleForm.projectId = localStorage.getItem('projectId');
                    const { code, msg } = await createChange(this.ruleForm);
                    if (code === 200) {
                        this.$message({
                            message: msg,
                            type: 'success',
                            duration: 1500
                        });
                        setTimeout(() => {
                            this.$router.go(-1);
                        }, 1500);
                    }
                }
            } catch (err) {
                console.log(err);
            }
        },
        // 修改
       async  change(){
             try {
                const valid = await this.$refs['ruleForm'].validate();
                if (valid) {
                   let ruleForm = this.ruleForm;
                  ruleForm.projectId = localStorage.getItem('projectId');
                    if (this.id) {
                        ruleForm.id = this.id;
                    }
                    const { code, msg } = await editchange(this.ruleForm);
                    if (code === 200) {
                        this.$message({
                            message: msg,
                            type: 'success',
                            duration: 1500
                        });
                        setTimeout(() => {
                            this.$router.go(-1);
                        }, 1500);
                    }
                }
            } catch (err) {
                console.log(err);
            }
        },
        // 详情
        async changeDetail(id) {
            try {
                const { data, code } = await noticeChange(id);
                if (code === 200) {
                    for (let item of data.accessoryVOList) {
                        console.log(item);
                        item.name = item.fileName;
                        item.fileUrl = item.accessoryUrl;
                        item.url = item.accessoryUrl
                    }
                    data.accessoryPOList=data.accessoryVOList
                    this.ruleForm = data;
                }
            } catch (e) {
                console.log(e);
            }
        }
    }
};
</script>


<style scoped>
.content {
    padding: 0;
    padding-bottom: 100px;
}
.content-head{
  position: relative;
}
.content-head>.abs{
  position: absolute;
  right:10px;
  bottom: 10px;
}
</style>
