<template>
    <div style="margin-bottom: 10px">
        <el-config-provider :locale="zhCn">
            <el-row style="align-items: center;">
                <!-- <span>选择库区：</span>
                <el-select v-model="areaId" placeholder="请选择库区" style="width: 120px;" @change="getAllLocations">
                    <el-option label="3楼库区" :value='1' />
                    <el-option label="2楼库区" :value='2' />
                </el-select> -->
                <span style="margin-left: 5px;">状态说明：</span>
                <el-button color="green">空闲</el-button>
                <el-button color="red">占用</el-button>
                <el-button color="yellow">锁定</el-button>
                <el-button color="#00BFFF" style="margin: 0 0 0 auto;" @click="getAllLocations">刷新</el-button>
            </el-row>
            <hr style="border: 1px solid #ff0000" />
            <div>
                <el-scrollbar max-height="760px">
                    <div v-for="currentRowLocation in allLocations">
                        <span>第{{ currentRowLocation.row }}排</span>
                        <div class="row" v-for="currentLayerLocation in currentRowLocation.currentLayerLocations">
                            <div class="col" v-for="currentColLocation in currentLayerLocation.currentColLocations">
                                <el-button :color="getLocationStatus(currentColLocation)"
                                    @click="showDialog(currentColLocation)">
                                    <span style="font-size: 15px;">{{ currentColLocation.queue }}排{{ currentColLocation.line}}列{{ currentColLocation.layer}}层</span>
                                </el-button>
                            </div>
                        </div>
                        <hr style="border: 1px solid #000000" />
                    </div>
                </el-scrollbar>
                <el-dialog v-model="dialogVisible" :title="currentLocationTitle" width="30%" draggable :show-close="false">
                    <el-form :model="location">
                        <el-form-item label="占用/解除占用库位" :label-width="140">
                            <el-select v-model="location.locationStatus" placeholder="库位是否占用" style="width: 70%;">
                                <el-option label="空闲" :value='0' />
                                <el-option label="占用" :value='1' />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="锁定/解锁库位" :label-width="140">
                            <el-select v-model="location.isLock" placeholder="库位是否锁定" style="width: 70%;">
                                <el-option label="解锁" :value='0' />
                                <el-option label="锁定" :value='1' />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="载具" :label-width="140">
                            <el-input v-model="location.vehicleId" style="width: 70%;" />
                        </el-form-item>
                    </el-form>
                    <template #footer>
                        <span class="dialog-footer">
                            <el-button @click="dialogVisible = false">取消</el-button>
                            <el-button type="primary" @click="submitLocationStatus()">
                                确定
                            </el-button>
                        </span>
                    </template>
                </el-dialog>
            </div>
        </el-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { getLocations, updateLocation } from '@/api/location.js'
import { ElMessage, ElLoading } from 'element-plus'
import { errorBox } from '@/utils/myMessageBox.js'
import { reactive } from 'vue'
</script>
<script>
export default {
    name: 'location',
    data() {
        return {
            dialogVisible: false,
            currentSelectedLocation: {},
            allLocations: [],
            location: reactive({
                locationStatus: '',
                isLock: '',
                vehicleId: ''
            }),
            currentLocationTitle: '',
            areaId: 1
        }
    },
    mounted() {
        this.getAllLocations()
    },
    methods: {
        getLocationStatus: (currentColLocation) => {
            if (currentColLocation.isLock === 1) {
                return 'yellow'
            }
            if (currentColLocation.locationStatus === 1) {
                return 'red'
            }
            return 'green'
        },
        getAllLocations() {
            const loading = ElLoading.service({
                lock: true,
                text: 'Loading',
                background: 'rgba(0, 0, 0, 0.7)',
            })
            const data = { areaId: this.areaId }
            getLocations(data).then(res => {
                loading.close()
                if (res.data.code == 0) {
                    console.log(res.data.returnData)
                    this.allLocations = res.data.returnData
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                loading.close()
                ElMessage.error('查询库位失败！')
            })
        },
        showDialog(currentColLocation) {
            this.dialogVisible = true
            this.currentSelectedLocation = currentColLocation
            this.setCurrentLocationTitle()
            this.location.locationStatus = this.currentSelectedLocation.locationStatus
            this.location.isLock = this.currentSelectedLocation.isLock
            this.location.vehicleId = this.currentSelectedLocation.vehicleId
        },
        submitLocationStatus() {
            const loading = ElLoading.service({
                lock: true,
                text: 'Loading',
                background: 'rgba(0, 0, 0, 0.7)',
            })
            this.currentSelectedLocation.locationStatus = this.location.locationStatus
            this.currentSelectedLocation.isLock = this.location.isLock
            this.currentSelectedLocation.vehicleId = this.location.vehicleId
            updateLocation(this.currentSelectedLocation).then(res => {
                if (res.data.code == 0) {
                    loading.close()
                    this.dialogVisible = false
                    ElMessage({
                        message: '更新库位信息成功！',
                        type: 'success',
                    })
                    this.getAllLocations()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                loading.close()
                ElMessage.error('更新库位信息失败')
            })
        },
        setCurrentLocationTitle() {
            this.currentLocationTitle = '当前选择库位：' + this.currentSelectedLocation.queue + '排' + this.currentSelectedLocation.line + '列' + this.currentSelectedLocation.layer + '层'
        }
    }
}
</script>

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

.row {
    display: flex;
    justify-content: space-between;
    margin: 5px;
}

.col {
    /* width: 100px; */
    /* height: 40px; */
    padding: 1px;
    margin: 1px;
}

.col .el-button {
    width: 100px;
    height: 50px;
}
</style>