<template>
    <div style="margin-bottom: 10px">
        <el-config-provider :locale="zhCn">
            <el-row>
                <UploadExcelBoxConfig></UploadExcelBoxConfig>
            </el-row>
            <el-row style="margin-top: 10px;">
                <el-input v-model="boxNoQuery" style="width: 256px; margin-right: 10px;" placeholder="料盒号" />
                <el-input v-model="stationQuery" 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="addBoxConfigFunc()">新增配置</el-button>
            </el-row>
            <br />
            <el-table :data="boxConfigs" 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.boxNo" v-model="boxNo">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="boxNo" label="料盒号" fixed="left" sortable min-width="120px" />
                <el-table-column prop="station" label="站台号" fixed="left" min-width="120px" />
                <el-table-column fixed="right" label="操作" width="240px">
                    <template v-slot="scope">
                        <el-button plain type="primary" @click="editCurrentBoxConfig(scope.row)">编辑</el-button>
                        <el-button plain type="danger" @click="deleteCurrentBoxConfig(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="configFormRef" :model="configFormEntity" :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="boxNo">
                                <el-input v-model="configFormEntity.boxNo" disabled />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="站台号" prop="station">
                                <el-select-v2 v-model="configFormEntity.station" placeholder="请选择分配站台"
                                    :options="stationOptions"></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="submitUpdateBoxConfig(configFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
            <el-dialog v-model="dialogVisibleAdd" title="添加料盒配置" width="40%" draggable :show-close="false">
                <el-form ref="configAddFormRef" :model="configAddFormEntity" :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="boxNo">
                                <el-input v-model="configAddFormEntity.boxNo" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="站台号" prop="station">
                                <el-select-v2 v-model="configAddFormEntity.station" placeholder="请选择分配站台"
                                    :options="stationOptions"></el-select-v2>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisibleAdd = false">取消</el-button>
                        <el-button type="primary" @click="submitAddBoxConfig(configAddFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import { queryBoxConfigByPage, updateBoxConfig, addBoxConfig, deleteBoxConfig } from '@/api/config.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import UploadExcelBoxConfig from '@/excel/UploadExcelBoxConfig.vue'
</script>
<script>
export default {
    name: 'boxConfig',
    component: {
        UploadExcelBoxConfig
    },
    data() {
        return {
            boxConfigs: [],
            pageInfo: {},
            currentPage: 1,
            pageSize: 10,
            total: 0,
            boxNoQuery: '',
            stationQuery: '',
            loading: true,
            dialogVisible: false,
            dialogVisibleAdd: false,
            boxNo: '',
            labelPosition: 'top',
            configFormEntity: reactive({}),
            configFormRef: ref(),
            configAddFormEntity: reactive({}),
            configAddFormRef: ref(),
            rules: reactive({}),
            stationOptions: [
                {
                    label: '2001',
                    value: '2001'
                },
                {
                    label: '2002',
                    value: '2002'
                },
                {
                    label: '2003',
                    value: '2003'
                },
                {
                    label: '2004',
                    value: '2004'
                },
                {
                    label: '2005',
                    value: '2005'
                },
                {
                    label: '2006',
                    value: '2006'
                },
                {
                    label: '2007',
                    value: '2007'
                },
                {
                    label: '2008',
                    value: '2008'
                }
            ]
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            this.pageInfo.pageNum = this.currentPage
            this.pageInfo.pageSize = this.pageSize
            const tableRequest = {
                page: this.pageInfo,
                param: {
                    boxNo: this.boxNoQuery.trim(),
                    station: this.stationQuery.trim()
                },
            }
            queryBoxConfigByPage(tableRequest).then(res => {
                const tableResponse = res.data
                if (tableResponse.code != 0) {
                    ElMessage.error(tableResponse.message)
                }
                this.boxConfigs = tableResponse.rows
                this.total = tableResponse.total
            }).catch(err => {
                ElMessage.error('查询料盒配置错误' + err.message)
            })
            this.loading = false
        },
        reset() {
            this.queryKey = ''
            this.search
        },
        editCurrentBoxConfig(row) {
            this.configFormEntity = row
            this.dialogVisible = true
        },
        addBoxConfigFunc() {
            this.configAddFormEntity = reactive({})
            this.dialogVisibleAdd = true
        },
        deleteCurrentBoxConfig(row) {
            this.boxNo = row.boxNo
            const boxConfig = {
                boxNo: row.boxNo
            }
            deleteBoxConfig(boxConfig).then(res => {
                if (res.data.code == 0) {
                    ElMessage({
                        message: '删除料盒配置成功',
                        type: 'success',
                    })
                    this.search()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                ElMessage.error('删除料盒配置失败:' + err)
            })
        },
        submitUpdateBoxConfig(formData) {
            if (formData.boxNo == null | undefined || formData.station == null | undefined) {
                ElMessage({
                    message: '料盒号和站台号必须输入',
                    type: 'error',
                })
                return;
            }
            updateBoxConfig(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('更新料盒配置信息失败')
            })
        },
        submitAddBoxConfig(formData) {
            if (formData.boxNo == null | undefined || formData.station == null | undefined) {
                ElMessage({
                    message: '料盒号和站台号必须输入',
                    type: 'error',
                })
                return;
            }
            addBoxConfig(formData).then(res => {
                if (res.data.code == 0) {
                    this.dialogVisibleAdd = false
                    ElMessage({
                        message: '添加料盒配置成功',
                        type: 'success',
                    })
                    this.search()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                ElMessage.error('添加料盒配置失败')
            })
        },
        getCurrentRow(row) {
            this.boxNo = row.boxNo
        },
    },
    // watch: {
    //     currentPage() {
    //         this.getCurrentPageGoods()
    //     },
    //     pageSize() {
    //         this.getCurrentPageGoods()
    //     }
    // }
}
</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;
}
</style>