<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 label="手机号">
                    <el-input v-model="query.telphone" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="query.status">
                        <el-option v-for="item in statusList" :key="item.value" :value="item.value"
                            :label="item.label"></el-option>
                    </el-select>
                </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" row-key="id">
                <el-table-column label="用户名" prop="nickname">
                    <template v-slot="scope">
                        <div style="display: flex;align-items: center;">
                            <el-image src=""
                                style="width:40px;height: 40px;background-color: #efefef;display: flex;align-items: center;justify-content: center;">
                                <i class="el-icon-folder-delete" slot="error" style="font-size: 20px;color: #999;"></i>
                            </el-image>
                            <span style="margin-left: 10px;">{{ scope.row.telphone }}</span>
                        </div>
                    </template>
                </el-table-column>
                <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;">
                                <i class="el-icon-folder-delete" slot="error"></i>
                            </el-image>
                            <span style="margin-left: 10px;">{{ scope.row.name }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="数量" prop="name">
                    <template v-slot="scope">
                        <span>{{ scope.row.num }}({{ scope.row.unit }})</span>
                    </template>
                </el-table-column>
                <el-table-column label="存酒时间" prop="savTime"></el-table-column>
                <el-table-column label="到期时间" prop="expTime"></el-table-column>
                <el-table-column label="状态" prop="status">
                    <template v-slot="scope">
                        <el-tag :type="statusList.find(item => item.value === scope.row.status).type">{{
                            statusList.find(item => item.value === scope.row.status).label }}</el-tag>
                    </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.HistoryModal.show(scope.row.id)">查看记录</el-button>
                            <el-button type="text" v-if="scope.row.status == 0" disabled>已取完</el-button>
                            <el-button type="text" @click="$refs.addModal.show(scope.row)"
                                v-if="scope.row.status == 1">取酒</el-button>
                            <el-button type="text" v-if="scope.row.status == 2" disabled>已过期</el-button>
                        </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" />
        <HistoryModal ref="HistoryModal" />
    </div>
</template>

<script>
import addModal from './addModal'
import HistoryModal from './historyModal'
import { storageList } from '@/api/application'
export default {
    components: {
        addModal,
        HistoryModal
    },
    data() {
        return {
            statusList: [
                {
                    value: '',
                    label: '全部',
                    type: 'warning'
                },
                {
                    value: 0,
                    label: '已取完',
                    type: 'warning'
                },
                {
                    value: 1,
                    label: '存储中',
                    type: 'primary'
                },
                {
                    value: 2,
                    label: '已过期',
                    type: 'info'
                }
            ],
            query: {
                name: "",
                telphone: "",
                status: ''
            },
            tableData: {
                data: [],
                page: 0,
                size: 10,
                loading: false,
                total: 0
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 重置查询
        resetHandle() {
            this.tableData.page = 0
            this.query.name = ''
            this.query.telphone = ''
            this.query.status = ''
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1;
            this.getTableData()
        },
        // 获取商品列表
        async getTableData() {
            try {
                this.tableData.loading = true
                const res = await storageList({
                    ...this.query,
                    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>