<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-date-picker v-model="workDateQuery" type="date" placeholder="选择工作日期" :shortcuts="shortcuts"
                    style="width: 158px; margin-right: 10px;" />
                    <el-select-v2 v-model="lackStatusQuery" style="width: 158px; margin-right: 10px;"
                        placeholder="请选择缺料状态" :options="lackStatusOptions" @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="workSummaryList" 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="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="lackNum" label="缺件数量" min-width="120px" />
                <el-table-column prop="eLocationId" label="拣货位置" min-width="120px" />
                <el-table-column prop="lackStatus" label="缺料状态" fixed="right" :formatter="lackStatusFormat"
                    min-width="120px" />
                <el-table-column prop="workStatus" label="工作状态" :formatter="workStatusFormat" min-width="120px" />
                <el-table-column prop="workDate" label="工作日期" :formatter="dateFormat" 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>
            <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-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import { getWorkSummary } from '@/api/kateWork.js'
import { errorBox } from '@/utils/myMessageBox.js'
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: 'workFlow',
    data() {
        return {
            workSummaryList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            workOrderQuery: '',
            workCenterQuery: '',
            goodsIdQuery: '',
            standIdQuery: '',
            workDateQuery: null,
            shortcuts: [
                {
                    text: '今天',
                    value: new Date(),
                },
                {
                    text: '昨天',
                    value: () => {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24)
                        return date
                    },
                },
            ],
            lackStatusQuery: -99,
            loading: true,
            workFlowId: '',
            lackStatusOptions: [
                {
                    value: -99,
                    label: '全部'
                },
                {
                    value: 0,
                    label: '不缺'
                },
                {
                    value: 1,
                    label: '缺料'
                }
            ],
        }
    },
    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(),
                lackStatus: this.lackStatusQuery == -99 ? null : this.lackStatusQuery,
                workDate: timeFormatter(this.workDateQuery),
                userName: store.getters.getUserName
            }
            getWorkSummary(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.workSummaryList = 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)
        },
        lackStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '不缺'
                case 1:
                    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.workDateQuery = null
            this.search()
        },
        getCurrentRow(row) {
            this.workFlowId = row.workFlowId
        },
    },
}
</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>