<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-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="user" label="中标人"></el-table-column>
            <el-table-column prop="states" label="投标状态"></el-table-column>
            <el-table-column  label="招标人" width="80">
                <template v-slot="scope">
                    <span v-if="tenderees &&tenderees.length">{{tenderees.find(v => v.id === scope.row.tid).nickname}}</span>
                </template>
            </el-table-column>
         <!-- <el-table-column label="审核" v-if="user.role === 'admin'" width="203">
                <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 label="操作"  width="420" align="center">
                <template slot-scope="scope">



                    <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'">删除 <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>

    </div>
</template>

<script>
    import {serverIp} from "../../public/config";
    export default {
        name: "Course",
        data() {
            return {
                tableData: [],
                article:[],
                name: '',
                multipleSelection: [],
                pageNum: 1,
                pageSize: 10,
                total: 0,
                dialogFormVisible: false,
                form: {},
                articles: [],
                bidders:[],
                tenderees:[],
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                article: localStorage.getItem("article") ? JSON.parse(localStorage.getItem("article")) : {},
                importHeaders: {token:  JSON.parse(localStorage.getItem('user')).token},
                serverIp: serverIp,
            }
        },
        created() {
            this.load()

            this.request.get("/bids").then(res =>{
                // console.log(res)
                this.bidders=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).toLocaleString();
                return s;

            },
            load() {
                this.request.get("/tender/pages", {
                    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("/manage/name").then(res => {
                    this.articles = res.data
                }),

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


            },

            changeEnable(row) {
                this.request.post("/manage/update", row).then(res => {
                    if (res.code === '200') {
                        this.$message.success("操作成功")
                    }
                })
            },
            del(id) {
                this.request.delete("/manage/" + id).then(res => {
                    if (res.code === '200') {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            delBatch() {
                let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
                this.request.post("/manage/del/batch", ids).then(res => {
                    if (res.code === '200') {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
            handleAdd() {
                this.dialogFormVisible = true
                this.form = {}
            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible = true
            },
            save() {
                this.request.post("/manage", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            save1() {
                this.request.put("/manage", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            reset() {
                this.name = ""
                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.$message.success("上传成功")
                this.load()
            },
            selectArticle(budgetId){
                this.request.post('/manage/articleBudget/' + budgetId + "/" + this.user.id).then(res => {
                    if (res.code === '200') {
                        this.$message.success("恭喜该标已选中")
                    } else {
                        this.$message.success(res.msg)
                    }
                })
            }

        }
    }
</script>

<style scoped>

</style>