<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-button type="primary" @click="handleAdd" v-if="user.role === 'bidder'||user.role==='admin'">我要投标 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='确定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="您确定批量删除这些数据吗？"
                    @confirm="delBatch"
            >
                <el-button type="danger" slot="reference" v-if="user.role === 'admin'">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>

        </div>
        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column  label="投标的项目" width="80">
            <template v-slot="scope">
                <span v-if="bidders &&bidders.length">{{bidders.find(v => v.id === scope.row.pid).name}}</span>
            </template>
        </el-table-column>
            <el-table-column prop="tuser" label="招标人"></el-table-column>
            <el-table-column prop="user" label="投标人"></el-table-column>
            <el-table-column prop="time" label="投标时间" :formatter="formatData"></el-table-column>
            <el-table-column prop="states" label="投标状态"></el-table-column>
            <el-table-column prop="funds" label="投标资金(万元)"></el-table-column>
            <el-table-column label="审核" v-if="user.role === 'tenderee'" width="150">
                <template v-slot="scope">
                    <el-button type="success" @click="changeState(scope.row, '已中标')" :disabled="scope.row.states !== '待开标'">定标</el-button>
                    <el-button type="danger" @click="changeState(scope.row, '未中标')" :disabled="scope.row.states !== '待开标'">弃标</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="feedback" label="反馈信息" v-if="user.role === 'bidder'" width="130px" >
                <template slot-scope="scope">
                    <el-button @click="view1(scope.row.feedback)" type="primary" style="width: 100px">查看反馈信息</el-button>
                </template>
            </el-table-column>
            <el-table-column label="投标文件" width="110px">
                <template slot-scope="scope">
                    <el-button type="primary" @click="download(scope.row.fileurl)" v-if="user.role === 'tenderee'">下载查看</el-button>
                    <el-button type="primary" @click="download1(scope.row.fileurl)" v-if="user.role === 'bidder'">已上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作"  width="280" align="center">
                <template slot-scope="scope">
                    <el-button type="success" @click="handleEdit2(scope.row)" v-if="user.role === 'admin'||user.role==='tenderee' ">反馈信息<i class="el-icon-edit"></i></el-button>
                 <!--  <el-upload :action="'http://' + serverIp +':9090/file/upload'" :show-file-list="false" :headers="importHeaders"
                               :on-success="handleFileUploadSuccess" style="display: inline-block">
                        <el-button type="primary" class="ml-5" v-if="(user.role === 'bidder'&&scope.row.states==='待开标' )||user.role === 'admin'">上传投标标书<i class="el-icon-top"></i></el-button>
                    </el-upload>-->
                    <el-button type="success" @click="handleEdit(scope.row)" v-if="user.role === 'admin'">编辑 <i class="el-icon-edit"></i></el-button>
                    <el-popconfirm
                            class="ml-5"
                            confirm-button-text='确定'
                            cancel-button-text='我再想想'
                            icon="el-icon-info"
                            icon-color="red"
                            title="您确定删除吗？"
                            @confirm="del(scope.row.id)"
                    >

                        <el-button type="danger" slot="reference" v-if="user.role === 'admin'||scope.row.states==='未中标'">删除 <i class="el-icon-remove-outline"></i></el-button>

                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <el-dialog title="投标信息" :visible.sync="dialogFormVisible" width="30%" >
            <el-form label-width="80px" size="small">
              <!--  <el-form-item label="招标项目">
                    <el-select clearable v-model="form.pid" placeholder="请选择需要投标的项目">
                        <el-option v-for="item in bidders" :label="item.name" :key="item.id" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="招标人">
                    <el-select clearable v-model="form.tid" placeholder="请选择招标人">
                        <el-option v-for="item in tenderees" :label="item.nickname" :key="item.id" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>-->
                <el-form-item label="招标项目">
                    <el-select  @change="changeOption" clearable v-model="form.pid" placeholder="请选择" >
                        <el-option v-if="item.state==='审核通过'&&item.states==='待开标' &&
						 !(new Date() < Date.parse(item.time))&&!(new Date() >Date.parse(item.overtime))"   v-for="item in bidders" :label="item.name" :key="item.id" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="招标人">
                    <el-input v-model="form.tuser" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="招标人id">
                    <el-input v-model="form.tid" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="投标资金(单位万元)">
                    <el-input v-model="form.funds" autocomplete="off" style="width: 200px"></el-input>
                </el-form-item>
                <el-upload

                        :action="'http://' + serverIp +':9090/file/upload'"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :headers="importHeaders"
                        style="display: inline-block"
                >
                    <el-button type="primary" class="ml-5">上传投标标书<i class="el-icon-top"></i></el-button>
                 <!--   <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                </el-upload>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="反馈信息表" :visible.sync="dialogFormVisible2" width="38%">
            <el-form label-width="80px" size="small">
                <el-form-item label="原因:" size="100px">
                    <el-input type="textarea" v-model="form.feedback" autocomplete="off" style="width: 400px;height: 100px"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="save2">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="反馈信息" :visible.sync="viewDialogVis1" width="30%" >
            <el-card>
                <div>
                    <mavon-editor
                            class="md"
                            :value="feedback"
                            :subfield="false"
                            :defaultOpen="'preview'"
                            :toolbarsFlag="false"
                            :editable="false"
                            :scrollStyle="true"
                            :ishljs="true"
                    />
                </div>
            </el-card>
        </el-dialog>

        <el-dialog
                title="投标注意事项"
                :visible.sync="dialogVisible"
                width="30%"
        >
            <span><h4>查看招标信息后，选择需要投标的招标项目，可以投标多个项目。</h4><br>
                <h4 style="color: #FF0000">提交投标项目后请及时上传投标文件。</h4></span>
            <span slot="footer" class="dialog-footer">

    <el-button type="primary" @click="handleAdd1">我已阅读</el-button>
    <el-button @click="dialogVisible = false">取 消</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    import {serverIp} from "../../public/config";
    import moment from "moment"
    export default {

        name: "Tender",
        data() {
            return {
                today:moment(new Date).format('x'),
                tenderees:[],
                bidders:[],
                tableData: [],
                name: '',
                multipleSelection: [],
                pageNum: 1,
                pageSize: 10,
                total: 0,
                dialogVisible:false,
                dialogFormVisible: false,
                dialogFormVisible2:false,
                viewDialogVis1:false,
                form: {},
                teachers: [],
                article: localStorage.getItem("article") ? JSON.parse(localStorage.getItem("article")) : {},
                importHeaders: {token:  JSON.parse(localStorage.getItem('user')).token},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                serverIp: serverIp,
            }
        },
        created() {
            this.load()
            this.request.get("/bids/findAll").then(res =>{
                // console.log(res)
                this.bidders=res.data
            })
            this.request.get("/files/filerole/tenderee").then(res => {
                this.tenderees = res.data
            })

        },
        methods: {
            changeState(row, states) {
                this.form = JSON.parse(JSON.stringify(row))
                this.form.states = states;
                this.save1();
            },
            formatData(row, column, cellValue, index){
                //最终返回 s 就可以了
                var s =	new Date(cellValue).toLocaleDateString();
                return s;

            },
            load() {
                this.request.get("/tender/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.name,
                    }
                }).then(res => {

                    this.tableData = res.data.records
                    this.total = res.data.total

                })
                this.request.get("/user/role/teacher").then(res => {
                    this.teachers = res.data
                })
                this.request.get("/user/role/tenderee").then(res => {

                    this.tenderees = res.data
                    console.log(res.data)
                })

            },
            // selectCourse(courseId) {
            //     this.request.post('/course/studentCourse/' + courseId + "/" + this.user.id).then(res => {
            //         if (res.code === '200') {
            //             this.$message.success("选课成功")
            //         } else {
            //             this.$message.success(res.msg)
            //         }
            //     })
            // },

            del(id) {
                this.request.delete("/tender/" + id).then(res => {
                    if (res.code === '200') {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            handleEdit2(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible2 = true
            },
            view1(feedback) {
                this.feedback = feedback
                this.viewDialogVis1 = true
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            delBatch() {
                let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
                this.request.post("/tender/del/batch", ids).then(res => {
                    if (res.code === '200') {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
            handleAdd() {
                /*this.dialogFormVisible = true*/
                this.dialogVisible=true
                this.form = {}
            },
            handleAdd1() {
                this.dialogVisible=false
                this.dialogFormVisible = true
                this.form = {}

            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible = true
            },
            save() {
                this.request.post("/tender", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            },
            save1() {
                this.request.put("/tender", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            save2() {
                this.request.put("/tender/update", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible2 = false
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            reset() {
                this.name = ""
                this.load()
            },
            handleFileUploadSuccess(res) {
                console.log(res)
                this.$message.success("上传成功")
                this.load()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                console.log(res)
                this.load()
            },
            handleAvatarSuccess(res) {
                this.form.fileurl = res
                this.$message.success("上传成功")
            },
            download1(url) {
           //  window.open(url)
                this.$message.success("已经上传文件")
            },
            download(url) {
                window.open(url)
                this.$message.success("下载文件成功")
            },
            changeOption(val){
                let obj  = this.bidders.filter(item =>{
                    return    item.id == val
                })
                this.form.tuser = obj[0]['user']
                this.form.tid = obj[0]['uid']
            },


        }
    }
</script>

<style scoped>

</style>