<template>
    <div class="app-container">
        <div class="head-container">
            <div class="filter_wrap">
                <el-input v-model="query.name" clearable placeholder="供应商" @keyup.enter.native="getTableData"
                    style="width: 200px;" />
                <el-select v-model="query.type" placeholder="付款状态">
                    <el-option :label="item.label" :value="item.value" v-for="item in types" :key="item.id"></el-option>
                </el-select>
                <el-button type="primary" @click="getTableData">查询</el-button>
                <el-button @click="resetHandle">重置</el-button>
            </div>
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading">
                <el-table-column label="供应商" prop="purveyorName"></el-table-column>
                <el-table-column label="剩余付款金额" prop="waitAmount">
                    <template v-slot="scope">
                        <span class="num" v-if="scope.row.waitAmount > 0">￥{{ scope.row.waitAmount }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="待付款笔数" prop="waitCount">
                    <template v-slot="scope">
                        <template v-if="scope.row.waitCount > 0">
                            有<span class="count">{{ scope.row.waitCount }}笔</span>未付
                        </template>
                        <template v-else>-</template>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="type">
                    <template v-slot="scope">
                        <el-tag :type="scope.row.type == 0 ? 'warning' : 'success'">
                            {{ scope.row.type == 0 ? '待支付' : '已完结' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="上笔进货日期" prop="lastTransactAt">
                    <template v-slot="scope">
                        {{ dayjs(scope.row.lastTransactAt).format('YYYY-MM-DD HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="80">
                    <template v-slot="scope">
                        <router-link :to="{ name: 'purchase_detail', query: { purveyorId: scope.row.purveyorId } }">
                            <el-button type="text" size="mini">查看详情</el-button>
                        </router-link>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination :total="tableData.total" :current-page="tableData.page + 1" :page-size="tableData.size"
            @current-change="paginationChange" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>
</template>

<script>
import dayjs from 'dayjs'
import { tbShopPurveyorTransactGet, dictDetail } from '@/api/invoicing'
export default {
    data() {
        return {
            dayjs,
            types: [],
            query: {
                name: '',
                type: ''
            },
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                sort: 'id',
                loading: false,
                list: []
            }
        }
    },
    mounted() {
        this.dictDetail()
        this.getTableData()
    },
    methods: {
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbShopPurveyorTransactGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    sort: this.tableData.sort,
                    purveyorName: this.query.name,
                    status: this.query.type,
                    type: 'reject',
                    shopId: localStorage.getItem('shopId')
                })
                this.tableData.loading = false
                this.tableData.list = res.content
                this.tableData.total = res.totalElements
            } catch (error) {
                console.log(error);
            }
        },
        // 重置查询
        resetHandle() {
            this.query.name = ''
            this.query.type = ''
            this.tableData.page = 0;
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        async dictDetail() {
            const { content } = await dictDetail({
                dictName: 'purveyor_transact_status',
                size: 100,
                page: 0
            })
            this.types = content
        }
    }
}
</script>

<style scoped lang="scss">
.num {
    color: #F56C6C;
    font-weight: bold;
}

.count {
    color: #409EFF;
}
</style>