<template>
    <div class="head-container">
        <div class="head-container">
            <el-form :model="query" inline>
                <el-form-item>
                    <el-button type="primary" @click="$refs.addModal.show()">添加酒品</el-button>
                </el-form-item>
                <el-form-item label="搜索">
                    <el-input v-model="query.name" placeholder="酒名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getTableData">查询</el-button>
                    <el-button @click="resetHandle">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="head-container">
            <el-table :data="tableData.data" v-loading="tableData.loading">
                <el-table-column label="酒名" prop="name">
                    <template v-slot="scope">
                        <div style="display: flex;align-items: center;">
                            <el-image :src="scope.row.imgUrl"
                                style="width:40px;height: 40px;border-radius: 4px;background-color: #efefef;">
                                <div class="img_error" slot="error">
                                    <i class="icon el-icon-document-delete"></i>
                                </div>
                            </el-image>
                            <span style="margin-left: 10px;">{{ scope.row.name }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="单位" prop="unit">
                    <template v-slot="scope">
                        <el-tag type="primary">{{ scope.row.unit }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="有效期(天)" prop="period"></el-table-column>
                <el-table-column label="来源" prop="source">
                    <template v-slot="scope">
                        <span v-if="scope.row.source == 0">手动导入</span>
                        <span v-if="scope.row.source == 1">商品导入</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120">
                    <template v-slot="scope">
                        <div style="display: flex;gap: 10px;">
                            <el-button type="text"
                                @click="$refs.winAddModal.show({ name: scope.row.name, imgUrl: scope.row.imgUrl, expDay: scope.row.period, unit: scope.row.unit }, true)">存酒</el-button>
                            <el-button type="text" @click="$refs.addModal.show(scope.row)">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="delHandle(scope.row)">
                                <el-button type="text" slot="reference">删除</el-button>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="head-container">
            <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>
        <addModal ref="addModal" @success="getTableData" />
        <winAddModal ref="winAddModal" />
    </div>
</template>

<script>
import { tbShopStorageGoodlist, tbShopStorageGood } from '@/api/application'
import addModal from './addModal'
import winAddModal from '../winestorage/addModal'
export default {
    components: {
        addModal,
        winAddModal
    },
    data() {
        return {
            statusList: [
                {
                    value: 0,
                    label: '已取完'
                },
                {
                    value: 1,
                    label: '未取完'
                }
            ],
            query: {
                name: ""
            },
            tableData: {
                data: [],
                page: 0,
                size: 10,
                loading: false,
                total: 0
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 删除
        async delHandle(row) {
            try {
                let obj = { ...row }
                obj.isDel = 1
                await tbShopStorageGood(obj, 'put')
                this.$notify.success('删除成功')
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 重置查询
        resetHandle() {
            this.tableData.page = 0;
            this.query.name = ''
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1;
            this.getTableData()
        },
        // 获取商品列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbShopStorageGoodlist({
                    name: this.query.name,
                    page: this.tableData.page,
                    size: this.tableData.size
                })
                this.tableData.loading = false
                this.tableData.data = res.content
                this.tableData.total = res.totalElements
            } catch (error) {
                console.log(error)
            }
        }
    }
}
</script>