<template>
    <div style="margin-bottom: 10px; height: 100%; padding-left: 1%; padding-right: 1%;">
        <el-config-provider :locale="zhCn">
            <div style="display: flex;justify-content: space-between;">
                <el-row>
                    <el-input v-model="standIdQuery" style="width: 158px; margin-right: 10px;" placeholder="站台号"
                        :suffix-icon="Search" />
                    <el-select-v2 v-model="standTypeQuery" style="width: 158px; margin-right: 10px;"
                        placeholder="请选择站台类型" :options="standTypeOptions" @change="search()"></el-select-v2>
                    <el-button type="primary" @click="search()">搜索</el-button>
                    <el-button type="warning" @click="reset()">重置</el-button>
                </el-row>
            </div>
            <br />
            <el-table :data="standList" stripe border v-loading="loading" class="table-class" highlight-current-row
                max-height="650px" @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.standId" v-model="standId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="standId" label="id" fixed="left" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="allowIn" label="允许入库" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="allowOut" label="允许出库" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="isLock" label="锁定" :formatter="isLockFormat" min-width="120px"
                    show-overflow-tooltip />
                <el-table-column prop="standStatus" label="状态" :formatter="standStatusFormat" show-overflow-tooltip
                    min-width="120px" />
                <el-table-column prop="equipmentId" label="设备号" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="areaId" label="区域" min-width="120px" />
                <el-table-column prop="standType" label="类型" :formatter="standTypeFormat" min-width="120px" />
                <el-table-column prop="standIp" label="电脑ip地址" min-width="120px" />
                <el-table-column prop="outerId" label="外部编号" min-width="120px" />
                <el-table-column prop="lastUseTime" label="上次使用时间" :formatter="timeFormat" min-width="120px"
                    show-overflow-tooltip />
                <el-table-column prop="pickGoods" label="拣选料号" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="pickVehicle" label="拣选箱号" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="pickTip" label="提示" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="pickVehicleCount" label="站台料箱数量" min-width="120px" />
                <el-table-column fixed="right" label="操作" width="120px">
                    <template v-slot="scope">
                        <el-button plain type="primary" @click="editCurrentRow(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">
                <div
                    style="max-width: 100%; max-height: 500px; overflow: auto; display: flex; justify-content: center;">
                    <el-form ref="standFormRef" :model="standFormEntity" :label-position="labelPosition"
                        label-width="100px" style="width: 95%;" :rules="rules" status-icon>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="站台号" prop="standId">
                                    <el-input v-model="standFormEntity.standId" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="类型" prop="isLock">
                                    <el-select-v2 v-model="standFormEntity.standType" placeholder="请选择类型"
                                        :options="standTypeOptions" disabled></el-select-v2>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="锁定" prop="isLock">
                                    <el-select-v2 v-model="standFormEntity.isLock" placeholder="请选择是否锁定"
                                        :options="isLockOptions"></el-select-v2>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态" prop="standStatus">
                                    <el-select-v2 v-model="standFormEntity.standStatus" placeholder="请选择站台状态"
                                        :options="standStatusOptions"></el-select-v2>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="上次使用时间" prop="lastUseTime">
                                    <el-input v-model="standFormEntity.lastUseTime" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="站台料箱数量" prop="pickVehicleCount">
                                    <el-input-number v-model.number="standFormEntity.pickVehicleCount"
                                        controls-position="right" clearable />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="submitInfo(standFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import { getStandsByPage, updateStandInfo } from '@/api/stand.js'
import { errorBox } from '@/utils/myMessageBox.js'
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import { dateFormatter, timeFormatter } from '@/utils/formatter.js'
import { Search } from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
</script>
<script>
export default {
    name: 'standSettings',
    data() {
        return {
            standList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            standIdQuery: '',
            standTypeQuery: -99,
            loading: true,
            dialogVisible: false,
            standId: '',
            standFormEntity: reactive({}),
            standFormRef: ref(),
            labelPosition: 'top',
            rules: reactive({}),
            standTypeOptions: [
                {
                    value: -99,
                    label: '全部'
                },
                {
                    value: 1,
                    label: '入库站台'
                },
                {
                    value: 2,
                    label: '备料站台'
                },
                {
                    value: 3,
                    label: '堆垛机'
                }
            ],
            isLockOptions: [
                {
                    value: 0,
                    label: '未锁定'
                },
                {
                    value: 1,
                    label: '已锁定'
                },
            ],
            standStatusOptions: [
                {
                    value: 0,
                    label: '可用'
                },
                {
                    value: 1,
                    label: '不可用'
                },
            ],
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            const request = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                standId: this.standIdQuery.trim(),
                standType: this.standTypeQuery == -99 ? null : this.standTypeQuery,
                userName: store.getters.getUserName
            }
            getStandsByPage(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.standList = tableResponse.returnData.lists
                    this.total = tableResponse.returnData.total
                } else {
                    errorBox(tableResponse.message)
                }
            }).catch(err => {
                console.log(err)
                errorBox('查询站台信息错误')
            })
            this.loading = false
        },
        dateFormat: (row, column, cellValue, index) => {
            return dateFormatter(cellValue)
        },
        timeFormat: (row, column, cellValue, index) => {
            return timeFormatter(cellValue)
        },
        standTypeFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 1:
                    return '入库站台'
                case 2:
                    return '备料站台'
                case 3:
                    return '堆垛机'
                default:
                    return '未知'
            }
        },
        isLockFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '未锁定'
                case 1:
                    return '已锁定'
                default:
                    return '未知'
            }
        },
        standStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '可用'
                case 1:
                    return '不可用'
                default:
                    return '未知'
            }
        },
        reset() {
            this.standIdQuery = ''
            this.standTypeQuery = -99
            this.search()
        },
        editCurrentRow(row) {
            this.standId = row.standId
            this.standFormEntity = {
                standId: row.standId,
                standType: row.standType,
                isLock: row.isLock,
                standStatus: row.standStatus,
                lastUseTime: row.lastUseTime,
                pickVehicleCount: row.pickVehicleCount,
            }
            this.dialogVisible = true
        },
        submitInfo(formData) {
            const params = {
                standId: formData.standId,
                standType: formData.standType,
                isLock: formData.isLock,
                standStatus: formData.standStatus,
                lastUseTime: formData.lastUseTime,
                pickVehicleCount: formData.pickVehicleCount,
                userName: store.getters.getUserName
            }
            updateStandInfo(params).then(res => {
                if (res.data.code == 0) {
                    this.dialogVisible = false
                    ElMessage.success('更新站台信息成功。')
                    this.search()
                } else {
                    errorBox(res.data.message)
                }
            }).catch(err => {
                console.log(err)
                errorBox('更新站台信息失败。')
            })
        },
        getCurrentRow(row) {
            this.standId = row.standId
        },
    },
}
</script>

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

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

.table-class {
    width: 100%;
}

.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;
}

.title-area {
    display: flex;
    /* min-height: 10%; */
    max-height: max-content;
    margin-bottom: 10px;
    min-width: inherit;
    border: solid 1px;
    border-radius: 10px;
    box-shadow: 0px 15px 10px -15px #000;
    overflow: auto;
    flex-direction: column;
    padding: 10px;
}
</style>