<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" @click="getTableData">查询</el-button>
                    <el-button @click="resetHandle">重置</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-column label="设备号" prop="address"></el-table-column>
                <el-table-column label="品牌" prop="contentType">
                    <template v-slot="scope">
                        {{ scope.row.contentType | devicesName }}
                    </template>
                </el-table-column>
                <el-table-column label="出品模式" prop="config.model">
                    <template v-slot="scope">
                        {{ scope.row.config.model | modelsName }}
                    </template>
                </el-table-column>
                <el-table-column label="打印类型" prop="subType">
                    <template v-slot="scope">
                        {{ scope.row.subType | subTypesName }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" sortable prop="createdAt">
                    <template v-slot="scope">
                        {{ scope.row.createdAt | timeFilter }}
                    </template>
                </el-table-column>
                <el-table-column label="排序" sortable prop="sort"></el-table-column>
                <el-table-column label="状态" prop="status">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                            @change="statusChange($event, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template v-slot="scope">
                        <el-button type="text" icon="el-icon-edit"
                            @click="$refs.addDevice.show(scope.row)">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="delTableHandle([scope.row.id])">
                            <el-button type="text" icon="el-icon-delete" style="margin-left: 20px !important;"
                                slot="reference">删除</el-button>
                        </el-popconfirm>
                    </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"></el-pagination>
        </div>
        <addDevice ref="addDevice" @success="getTableData" />
    </div>
</template>

<script>
import { devices, models, subTypes } from './devices'
import addDevice from './components/addDevice'
import { tbPrintMachineGet, tbPrintMachine } from '@/api/devices'
import dayjs from 'dayjs'
export default {
    components: {
        addDevice
    },
    data() {
        return {
            query: {
                name: '',
                type: ''
            },
            devices,
            tableData: {
                data: [],
                page: 0,
                size: 10,
                loading: false,
                total: 0
            }
        }
    },
    filters: {
        devicesName(value) {
            const item=devices.find(item => item.value == value)
            return item?item.name:''
        },
        modelsName(value) {
            const item=models.find(item => item.value == value)
            return item?item.name:''
        },
        subTypesName(value) {
            const item=subTypes.find(item => item.value == value)
            return item?item.name:''
        },
        timeFilter(s) {
            return dayjs(s).format('YYYY-MM-DD HH:mm:ss')
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 切换状态
        async statusChange(e, row) {
            try {
                this.tableData.loading = true
                const data = { ...row }
                data.status = e
                await tbPrintMachine(data, 'put')
                this.getTableData()
            } catch (error) {
                console.log(error)
                this.tableData.loading = false
            }
        },
        // 重置查询
        resetHandle() {
            this.query.name = ''
            this.query.type = ''
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 获取商品列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbPrintMachineGet({
                    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>