<template>
    <div class="app-container">
        <div class="head-container">
            <div class="filter_wrap">
                <el-input v-model="query.name" size="small" clearable placeholder="供应商" @keyup.enter.native="getTableData"
                    style="width: 200px;" />
                <el-button type="primary" @click="getTableData">查询</el-button>
                <el-button @click="resetHandle">重置</el-button>
            </div>
        </div>
        <div class="head-container">
            <el-button type="primary" icon="el-icon-plus" @click="$refs.addSupplier.show()">添加供应商</el-button>
        </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="purveyorTelephone"></el-table-column>
                <el-table-column label="地址" prop="address"></el-table-column>
                <el-table-column label="标签" prop="tip"></el-table-column>
                <el-table-column label="备注" prop="remark"></el-table-column>
                <el-table-column label="操作" width="200">
                    <template v-slot="scope">
                        <el-button type="text" size="mini" round icon="el-icon-edit"
                            @click="$refs.addSupplier.show(scope.row)">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="delHandle([scope.row.id])">
                            <el-button type="text" size="mini" round icon="el-icon-delete" slot="reference">
                                删除
                            </el-button>
                        </el-popconfirm>
                    </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>
        <addSupplier ref="addSupplier" @success="getTableData" />
    </div>
</template>

<script>
import { tbShopPurveyorGet, tbShopPurveyor } from '@/api/invoicing'
import addSupplier from '../components/addSupplier'
export default {
    components: {
        addSupplier
    },
    data() {
        return {
            query: {
                name: ''
            },
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                sort: 'id',
                loading: false,
                list: []
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 删除
        async delHandle(ids) {
            try {
                await tbShopPurveyor(ids, 'delete')
                this.$notify({
                    title: '成功',
                    message: `删除成功`,
                    type: 'success'
                });
                this.getTableData()
            } catch (error) {
                console.log(error)
            }
        },
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbShopPurveyorGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    sort: this.tableData.sort,
                    purveyorName: this.query.name,
                    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.tableData.page = 0;
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        }
    }
}
</script>