<template>
    <div style="margin-bottom: 10px">
        <el-config-provider :locale="zhCn">
            <el-row>
                <el-input v-model="queryKey" style="width: 256px; margin-right: 10px;" placeholder="箱号" />
                <el-button type="primary" @click="search()">搜索</el-button>
                <el-button type="warning" @click="reset()">重置</el-button>
                <!-- <el-button type="success" @click="refresh()">刷新</el-button> -->
            </el-row>
            <br />
            <el-table :data="vehicles" stripe border v-loading="loading" class="table-class" max-height="650px"
                highlight-current-row @row-click="getCurrentRow" :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }">
                <el-table-column width="65px" fixed="left">
                    <template v-slot="scope">
                        <el-radio :label="scope.row.vehicleId" v-model="vehicleId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="vehicleId" label="箱号" fixed="left" min-width="120px" />
                <el-table-column prop="currentLocation" label="当前位置" fixed="left" min-width="120px" />
                <el-table-column prop="vehicleStatus" label="料箱状态" :formatter="vehicleStatusFormat" min-width="120px" />
                <el-table-column prop="isEmpty" label="是否空箱" :formatter="isEmptyFormat" min-width="120px" />
                <el-table-column fixed="right" label="操作" width="200px" v-if="selVehicle == null">
                    <template v-slot="scope">
                        <el-button plain type="primary" @click="editCurrentRowVehicle(scope.row)">编辑</el-button>
                        <el-button plain type="danger" @click="deleteCurrentRowVehicle(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br />
            <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 25, 50]"
                :small="false" :disabled="false" :background="false" :default-page-size="10"
                layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="search"
                @current-change="search" />
            <el-dialog v-model="dialogVisible" title="托盘信息" width="40%" draggable :show-close="false">
                <el-form ref="vehicleFormRef" :model="vehicleFormEntity" :label-position="labelPosition" label-width="100px"
                    style="max-width: 100%" :rules="rules" status-icon>
                    <el-row :gutter="16">
                        <el-col :span="12" :offset="0">
                            <el-form-item label="料箱号" prop="vehicleId">
                                <el-input v-model="vehicleFormEntity.vehicleId" disabled />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="当前位置" prop="currentLocation">
                                <el-input v-model="vehicleFormEntity.currentLocation" placeholder="请输入当前位置" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="16">
                        <el-col :span="12" :offset="0">
                            <el-form-item label="是否空箱" prop="isEmpty">
                                <el-select-v2 v-model="vehicleFormEntity.isEmpty" placeholder="请选择托盘状态"
                                        :options="isEmptyOptions"></el-select-v2>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="托盘状态" prop="vehicleStatus">
                                <el-select-v2 v-model="vehicleFormEntity.vehicleStatus" placeholder="请选择托盘状态"
                                        :options="vehicleStatusOptions"></el-select-v2>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="submitVehicleInfo(vehicleFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import { getAllVehicles, updateVehicleInfo, deleteCurrentVehicle } from '@/api/vehicle'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import { vehicleStatusFormatter, locationFormatter } from '@/utils/formatter.js'
</script>
<script>
export default {
    name: 'stand',
    props: ['selVehicle'],
    emits: ['update:selVehicle'],
    data() {
        return {
            pageInfo: {},
            vehicles: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            queryKey: '',
            loading: true,
            dialogVisible: false,
            vehicleId: '',
            vehicleFormEntity: reactive({}),
            labelPosition: 'top',
            vehicleFormRef: ref(),
            rules: reactive({
                locationId: [
                    { required: true, message: '请输入库位' }
                ]
            }),
            // 物料高度
            isEmptyOptions: [
                {
                    value: 0,
                    label: '带料'
                },
                {
                    value: 1,
                    label: '空箱'
                }
            ],
            // 托盘状态
            vehicleStatusOptions: [
            {
                    value: 0,
                    label: '入库中'
                },
                {
                    value: 1,
                    label: '在库中'
                },
                {
                    value: 2,
                    label: '出库中'
                }
            ]
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            this.pageInfo.pageNum = this.currentPage
            this.pageInfo.pageSize = this.pageSize
            const tableRequest = {
                page: this.pageInfo,
                param: {
                    vehicleId: this.queryKey.trim()
                },
            }
            getAllVehicles(tableRequest).then(res => {
                const tableResponse = res.data
                if (tableResponse.code != 0) {
                    console.log(tableResponse.code + ':' + tableResponse.message)
                    ElMessage.error(tableResponse.message)
                }
                this.vehicles = tableResponse.rows
                this.total = tableResponse.total
            }).catch(err => {
                ElMessage.error('查询托盘信息错误' + err.message)
            })
            this.loading = false
        },
        vehicleStatusFormat: (row, column, cellValue, index) => {
            return vehicleStatusFormatter(cellValue)
        },
        locationFormat: (row, column, cellValue, index) => {
            return locationFormatter(cellValue)
        },
        isEmptyFormat: (row, column, cellValue, index) => {
            if (cellValue == 0) {
                return '带料'
            }
            if (cellValue == 1) {
                return '空箱'
            }
        },
        reset() {
            this.queryKey = ''
            this.search()
        },
        editCurrentRowVehicle(row) {
            this.vehicleFormEntity = row
            this.dialogVisible = true
        },
        deleteCurrentRowVehicle(row) {
            this.vehicleId = row.vehicleId
            const vehicle = {
                vehicleId: row.vehicleId
            }
            deleteCurrentVehicle(vehicle).then(res => {
                if (res.data.code == 0) {
                    ElMessage({
                        message: '删除料箱成功',
                        type: 'success',
                    })
                    this.search()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                ElMessage.error('删除料箱信息失败:' + err)
            })
        },
        submitVehicleInfo(formData) {
            updateVehicleInfo(formData).then(res => {
                if (res.data.code == 0) {
                    this.dialogVisible = false
                    ElMessage({
                        message: '更新料箱信息成功',
                        type: 'success',
                    })
                    this.search()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                ElMessage.error('更新料箱信息失败:' + err)
            })
        },
        getCurrentRow(row) {
            this.vehicleId = row.vehicleId
            this.$emit('update:selVehicle', row)
        }
    }
}
</script>

<style scoped>
.el-pagination {
    padding-left: 5px;
}

.el-row .el-button {
    width: 72px;
    margin-left: 0px;
    margin-right: 5px;
}

.table-class {
    width: 100%;
    /* font-size: 5px; */
}

.el-row .el-form-item {
    width: 10% inherit;
    justify-content: center;
}

.el-row .el-form-item .el-select-v2 {
    width: 100% !important;
}

.el-row .el-form-item .el-input-number {
    width: 100% !important;
}

.el-row .el-form-item .el-button {
    margin: auto;
}
</style>