<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="workOrderQuery" style="width: 158px; margin-right: 10px;" placeholder="工单号"
                        :suffix-icon="Search" />
                    <el-input v-model="workCenterQuery" style="width: 158px; margin-right: 10px;" placeholder="工位/小盒子"
                        :suffix-icon="Search" />
                    <el-input v-model="goodsIdQuery" style="width: 158px; margin-right: 10px;" placeholder="料号"
                        :suffix-icon="Search" />
                    <el-input v-model="standIdQuery" style="width: 158px; margin-right: 10px;" placeholder="站台号"
                        :suffix-icon="Search" />
                    <el-select-v2 v-model="lightStatusQuery" style="width: 158px; margin-right: 10px;"
                        placeholder="请选择灯光状态" :options="lightStatusOptions" @change="search()"></el-select-v2>
                    <el-select-v2 v-model="workStatusQuery" style="width: 158px; margin-right: 10px;"
                        placeholder="请选择工作状态" :options="workStatusOptions" @change="search()"></el-select-v2>
                    <el-button type="primary" @click="search()">搜索</el-button>
                    <el-button type="warning" @click="reset()">重置</el-button>
                </el-row>
                <el-row>
                    <el-button style="background-color: #00CED1; color: #000;"
                        @click="openUploadDialog()">导入</el-button>
                </el-row>
            </div>
            <br />
            <el-table :data="workFlowList" 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.workFlowId" v-model="workFlowId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="workFlowId" label="id" fixed="left" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="orderId" label="订单号" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="workStation" label="站台号" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="workOrder" label="工单号" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="workCenter" label="工位/小盒子" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="goodsId" label="料号" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="needNum" label="需求数量" min-width="120px" />
                <el-table-column prop="pickedNum" label="已拣数量" min-width="120px" />
                <el-table-column prop="lightStatus" label="亮灯状态" :formatter="lightStatusFormat" min-width="120px" />
                <el-table-column prop="workStatus" label="工作状态" :formatter="workStatusFormat" min-width="120px" />
                <el-table-column prop="createTime" label="创建时间" :formatter="timeFormat" min-width="120px"
                    show-overflow-tooltip />
                <el-table-column prop="finishTime" label="完成时间" :formatter="timeFormat" min-width="120px"
                    show-overflow-tooltip />
                <el-table-column prop="opUser" 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="workFlowFormRef" :model="workFlowFormEntity" :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="workFlowId">
                                    <el-input v-model="workFlowFormEntity.workFlowId" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="订单id" prop="orderId">
                                    <el-input v-model="workFlowFormEntity.orderId" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="站台号" prop="workStation">
                                    <el-input v-model="workFlowFormEntity.workStation" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="工单" prop="workOrder">
                                    <el-input v-model="workFlowFormEntity.workOrder" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="工位/小盒子" prop="workCenter">
                                    <el-input v-model="workFlowFormEntity.workCenter" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="料号" prop="goodsId">
                                    <el-input v-model="workFlowFormEntity.goodsId" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="需求数量" prop="needNum">
                                    <el-input-number v-model.number="workFlowFormEntity.needNum"
                                        controls-position="right" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="已拣数量" prop="pickedNum">
                                    <el-input-number v-model.number="workFlowFormEntity.pickedNum"
                                        controls-position="right" :min="0" :max="workFlowFormEntity.needNum"
                                        clearable />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="任务状态" prop="lightStatus">
                                    <el-select-v2 v-model="workFlowFormEntity.lightStatus" placeholder="请选择亮灯状态"
                                        :options="lightStatusOptions"></el-select-v2>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="任务状态" prop="workStatus">
                                    <el-select-v2 v-model="workFlowFormEntity.workStatus" placeholder="请选择任务状态"
                                        :options="workStatusOptions"></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="createTime">
                                    <el-input v-model="workFlowFormEntity.createTime" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="完成时间" prop="finishTime">
                                    <el-input v-model="workFlowFormEntity.finishTime" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="操作用户" prop="opUser">
                                    <el-input v-model="workFlowFormEntity.opUser" disabled />
                                </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(workFlowFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
            <el-dialog v-model="uploadDialogVisible" title="工作流" width="40%" draggable :show-close="true">
                <fieldset class="title-area">
                    <legend>上次更新</legend>
                    <div style="padding: 5px;">
                        <div style="display: flex; margin-bottom: 10px;">
                            <div style="display: flex; width: 100%;">
                                <div style="width: 30%; align-content: center;">时间：</div>
                                <el-input v-model="uploadRecord.uploadTime" readonly />
                            </div>
                            <div style="display: flex; width: 100%; margin-left: 5px;">
                                <div style="width: 30%; align-content: center;">人员：</div>
                                <el-input v-model="uploadRecord.uploadUser" readonly />
                            </div>
                        </div>
                        <div style="display: flex; width: 100%;">
                            <div style="width: 13%; align-content: center;">文件名：</div>
                            <el-input type="textarea" :rows="1" v-model="uploadRecord.fileName" :maxlength="-1"
                                :show-word-limit="false" :autosize="{ minRows: 1, maxRows: 4 }" readonly>
                            </el-input>
                        </div>
                    </div>
                </fieldset>
                <fieldset class="title-area">
                    <legend>再次更新</legend>
                    <UploadExcelWorkFlow></UploadExcelWorkFlow>
                </fieldset>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import { getWorkFlows, updateWorkFlows } from '@/api/kateWork.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 { queryUploadRecord } from '@/api/excel.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
</script>
<script>
export default {
    name: 'pickTaskMonitor',
    data() {
        return {
            workFlowList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            workOrderQuery: '',
            workCenterQuery: '',
            goodsIdQuery: '',
            standIdQuery: '',
            lightStatusQuery: -99,
            workStatusQuery: -99,
            loading: true,
            dialogVisible: false,
            workFlowId: '',
            workFlowFormEntity: reactive({}),
            workFlowFormRef: ref(),
            labelPosition: 'top',
            rules: reactive({}),
            lightStatusOptions: [
                {
                    value: -99,
                    label: '全部'
                },
                {
                    value: 0,
                    label: '未亮灯'
                },
                {
                    value: 1,
                    label: '已亮灯'
                },
                {
                    value: 2,
                    label: '已拍灯'
                }
            ],
            workStatusOptions: [
                {
                    value: -99,
                    label: '全部'
                },
                {
                    value: 0,
                    label: '未开始'
                },
                {
                    value: 1,
                    label: '正在做'
                },
                {
                    value: 2,
                    label: '已完成'
                }
            ],
            uploadDialogVisible: false,
            uploadRecord: reactive({
                fileName: '',
                uploadTime: '',
                uploadUser: ''
            }),
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            const request = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                workStation: this.standIdQuery.trim(),
                workOrder: this.workOrderQuery.trim(),
                workCenter: this.workCenterQuery.trim(),
                goodsId: this.goodsIdQuery.trim(),
                lightStatus: this.lightStatusQuery == -99 ? null : this.lightStatusQuery,
                workStatus: this.workStatusQuery == -99 ? null : this.workStatusQuery,
                userName: store.getters.getUserName
            }
            getWorkFlows(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.workFlowList = 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)
        },
        lightStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '未亮灯'
                case 1:
                    return '已亮灯'
                case 2:
                    return '已拍灯'
                default:
                    return '未知'
            }
        },
        workStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '未开始'
                case 1:
                    return '正在做'
                case 2:
                    return '已完成'
                default:
                    return '未知'
            }
        },
        reset() {
            this.standIdQuery = ''
            this.workOrderQuery = ''
            this.workCenterQuery = ''
            this.goodsIdQuery = ''
            this.search()
        },
        editCurrentRow(row) {
            this.workFlowId = row.workFlowId
            this.workFlowFormEntity = {
                workFlowId: row.workFlowId,
                orderId: row.orderId,
                workStation: row.workStation,
                workOrder: row.workOrder,
                workCenter: row.workCenter,
                goodsId: row.goodsId,
                pickedNum: row.pickedNum,
                needNum: row.needNum,
                lightStatus: row.lightStatus,
                workStatus: row.workStatus,
                createTime: row.createTime,
                finishTime: row.finishTime,
                opUser: row.opUser
            }
            this.dialogVisible = true
        },
        submitInfo(formData) {
            const params = {
                workFlowId: formData.workFlowId,
                pickedNum: formData.pickedNum,
                lightStatus: formData.lightStatus,
                workStatus: formData.workStatus,
                userName: store.getters.getUserName
            }
            updateWorkFlows(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.workFlowId = row.workFlowId
        },
        openUploadDialog() {
            // 请求上传记录
            const param = {
                userName: store.getters.getUserName,
                fileDescription: 'WORKFLOW',
                isAsc: false,
                sortBy: 'upload_time'
            }
            queryUploadRecord(param).then(res => {
                if (res.data.code == 0) {
                    if (res.data.returnData.total > 0) {
                        this.uploadRecord = res.data.returnData.lists[0]
                    } else {
                        this.uploadRecord = reactive({
                            fileName: '',
                            uploadTime: '',
                            uploadUser: ''
                        })
                    }
                } else {
                    this.uploadRecord = reactive({
                        fileName: '',
                        uploadTime: '',
                        uploadUser: ''
                    })
                }
            }).catch(err => {
                console.log(err)
                errorBox('发生异常')
            })
            this.uploadDialogVisible = true
        },
    },
}
</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>