<template>
    <div class="app-container">
        <div class="head-container">
            <el-form :model="query" inline>
                <el-form-item>
                    <el-input v-model="query.name" placeholder="请输入设备名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="query.type" placeholder="请选择设备类型">
                        <el-option :label="item.name" :value="item.value" v-for="item in devices" :key="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary">查询</el-button>
                    <el-button>重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="head-container">
            <el-button type="primary" icon="el-icon-plus" @click="$refs.addDevice.show()">添加云打印机</el-button>
        </div>
        <div class="head-container">
            <el-table :data="tableData.data" v-loading="tableData.loading">
                <el-table-column label="设备名称" prop="name"></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"></el-pagination>
        </div>
        <addDevice ref="addDevice" />
    </div>
</template>

<script>
import devices from './devices'
import addDevice from './components/addDevice'
import { tbPrintMachineGet } from '@/api/devices'
export default {
    components: {
        addDevice
    },
    data() {
        return {
            query: {
                name: '',
                type: ''
            },
            devices,
            tableData: {
                data: [],
                page: 0,
                size: 10,
                loading: false,
                total: 0
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 重置查询
        resetHandle() {
            this.query.name = ''
            this.query.categoryId = ''
            this.query.typeEnum = ''
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 获取商品列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbPrintMachineGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    name: this.query.name,
                    contentType: this.query.type
                })
                this.tableData.loading = false
                this.tableData.data = res
                this.tableData.total = res.length
            } catch (error) {
                console.log(error)
            }
        },
    }
}
</script>