<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="vehicleIdQuery" style="width: 256px; margin-right: 10px;" placeholder="箱号"
                        :suffix-icon="Search" />
                    <el-input v-model="standQuery" style="width: 256px; margin-right: 10px;" placeholder="工站"
                        :suffix-icon="Search" />
                    <el-button type="primary" @click="search()">搜索</el-button>
                    <el-button type="warning" @click="reset()">重置</el-button>
                </el-row>
            </div>
            <br />
            <el-table :data="pickTaskList" 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.pickTaskId" v-model="pickTaskId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="pickTaskId" label="id" fixed="left" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="vehicleId" label="箱号" fixed="left" min-width="120px" />
                <el-table-column prop="standId" label="站台号" fixed="left" min-width="120px" />
                <el-table-column prop="pickStatus" label="拣选状态" :formatter="pickStatusFormat" min-width="120px" />
                <el-table-column prop="lastUpdateTime" label="最近更新时间" :formatter="timeFormat" show-overflow-tooltip
                    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="pickTaskFormRef" :model="pickTaskFormEntity" :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="id" prop="pickTaskId">
                                    <el-input v-model="pickTaskFormEntity.pickTaskId" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="工站" prop="vehicleId">
                                    <el-input v-model="pickTaskFormEntity.vehicleId" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="小盒子" prop="standId">
                                    <el-input v-model="pickTaskFormEntity.standId" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="任务状态" prop="pickStatus">
                                    <el-select-v2 v-model="pickTaskFormEntity.pickStatus" placeholder="请选择任务状态"
                                        :options="taskStatusOptions"></el-select-v2>
                                </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(pickTaskFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import { getPickTasksByPage, updatePickTaskInfo } from '@/api/taskMonitor.js'
import { ElMessage } from 'element-plus'
import { errorBox } from '@/utils/myMessageBox.js'
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: 'pickTaskMonitor',
    data() {
        return {
            pickTaskList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            vehicleIdQuery: '',
            standQuery: '',
            loading: true,
            dialogVisible: false,
            pickTaskId: '',
            pickTaskFormEntity: reactive({}),
            pickTaskFormRef: ref(),
            labelPosition: 'top',
            rules: reactive({}),
            taskStatusOptions: [
                {
                    value: -1,
                    label: '暂存'
                },
                {
                    value: 0,
                    label: '待下发'
                },
                {
                    value: 1,
                    label: '已下发'
                },
                {
                    value: 2,
                    label: '已到达'
                }
            ],
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            const request = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                vehicleId: this.vehicleIdQuery.trim(),
                standId: this.standQuery.trim(),
                userName: store.getters.getUserName
            }
            getPickTasksByPage(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.pickTaskList = tableResponse.returnData.lists
                    this.total = tableResponse.returnData.total
                } else {
                    ElMessage.error(tableResponse.message)
                }
            }).catch(err => {
                ElMessage.error('查询拣选任务错误' + err.message)
            })
            this.loading = false
        },
        dateFormat: (row, column, cellValue, index) => {
            return dateFormatter(cellValue)
        },
        timeFormat: (row, column, cellValue, index) => {
            return timeFormatter(cellValue)
        },
        pickStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case -1:
                    return '暂存'
                case 0:
                    return '待下发'
                case 1:
                    return '已下发'
                case 2:
                    return '已到达'
                default:
                    return '未知'
            }
        },
        reset() {
            this.vehicleIdQuery = ''
            this.standQuery = ''
            this.search()
        },
        editCurrentRow(row) {
            this.pickTaskId = row.pickTaskId
            this.pickTaskFormEntity = {
                pickTaskId: row.pickTaskId,
                vehicleId: row.vehicleId,
                standId: row.standId,
                pickStatus: row.pickStatus,
                lastUpdateTime: row.lastUpdateTime
            }
            this.dialogVisible = true
        },
        submitInfo(formData) {
            const params = {
                pickTaskId: formData.pickTaskId,
                pickStatus: formData.pickStatus,
                userName: store.getters.getUserName
            }
            updatePickTaskInfo(params).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('更新拣选任务失败。')
            })
        },
        getCurrentRow(row) {
            this.pickTaskId = row.pickTaskId
        },
    },
}
</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>