<template>
    <div class="app-container">
        <div class="head-container">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-input v-model="query.name" size="small" clearable placeholder="商品名称" style="width: 100%;"
                        class="filter-item" @keyup.enter.native="getTableData" />
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="getTableData">查询</el-button>
                    <el-button @click="resetHandle">重置</el-button>
                </el-col>
            </el-row>
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading">
                <el-table-column label="商品信息">
                    <template v-slot="scope">
                        <div class="shop_info">
                            <el-image :src="scope.row.coverImg" class="cover">
                                <div class="img_error" slot="error">
                                    <i class="icon el-icon-document-delete"></i>
                                </div>
                            </el-image>
                            <div class="info">
                                <span>{{ scope.row.name }}</span>
                                <div>
                                    <el-tag type="primary">{{ scope.row.typeEnum | typeEnum }}</el-tag>
                                </div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="库存" prop="stockNumber">
                    <template v-slot="scope">
                        {{ `${scope.row.stockNumber} ${scope.row.unitName}` }}
                    </template>
                </el-table-column>
                <el-table-column label="库存开关">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.isStock" :active-value="1" :inactive-value="0"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="100">
                    <template v-slot="scope">
                        <el-button type="text" size="mini"
                            @click="$refs.invoicingDetail.show(scope.row)">库存记录</el-button>
                    </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>
        <invoicingDetail ref="invoicingDetail" />
    </div>
</template>

<script>
import { tbProductGet } from '@/api/invoicing'
import settings from '@/settings'
import invoicingDetail from './components/invoicingDetail'
export default {
    components: {
        invoicingDetail
    },
    data() {
        return {
            query: {
                name: ''
            },
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                sort: 'id',
                loading: false,
                list: []
            }
        }
    },
    filters: {
        typeEnum(m) {
            return settings.typeEnum.find(item => item.typeEnum == m).label
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbProductGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    sort: this.tableData.sort,
                    name: this.query.name,
                    shopId: localStorage.getItem('shopId')
                })
                this.tableData.loading = false
                this.tableData.list = res.content.map(item => {
                    let stockNumber = 0
                    for (let i of item.skuList) {
                        stockNumber += i.stockNumber
                    }
                    item.stockNumber = stockNumber
                    return item
                })
                this.tableData.total = res.totalElements
            } catch (error) {
                console.log(error);
            }
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 重置查询
        resetHandle() {
            this.query.blurry = ''
            this.tableData.page = 0;
            this.getTableData()
        }
    }
}
</script>

<style scoped lang="scss">
.shop_info {
    display: flex;
    align-items: center;

    .cover {
        $size: 50px;
        width: $size;
        height: $size;
        border-radius: 2px;
        background-color: #efefef;
    }

    .info {
        display: flex;
        flex-direction: column;
        padding-left: 10px;
        justify-content: space-between;
    }
}
</style>