<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: 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>
                <el-row>
                    <el-button style="background-color: #00CED1; color: #000;"
                        @click="openUploadDialog()">导入</el-button>
                </el-row>
            </div>
            <br />
            <el-table :data="dbsList" 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.dbsId" v-model="dbsId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="dbsId" label="id" fixed="left" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="workSequence" label="顺序号" fixed="left" min-width="120px" />
                <el-table-column prop="machineNo" label="机器序列号" min-width="120px" />
                <el-table-column prop="workOrder" label="工单" min-width="120px" />
                <el-table-column prop="planStartDate" label="开工日期" :formatter="dateFormat" min-width="120px" />
                <el-table-column prop="dbsStatus" label="状态" min-width="120px" />
                <el-table-column prop="lastUpdateTime" label="最近更新时间" :formatter="timeFormat" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="lastUpdateUser" label="最近更新用户" min-width="120px" />
                <el-table-column fixed="right" label="操作" width="240px">
                    <template v-slot="scope">
                        <el-button plain type="primary" @click="editCurrentRowDbs(scope.row)">编辑</el-button>
                        <el-button plain type="danger" @click="deleteCurrentRowDbs(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="DBS信息" width="40%" draggable :show-close="false">
                <div
                    style="max-width: 100%; max-height: 500px; overflow: auto; display: flex; justify-content: center;">
                    <el-form ref="dbsFormRef" :model="dbsFormEntity" :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="dbsId">
                                    <el-input v-model="dbsFormEntity.dbsId" disabled />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="工单" prop="workOrder">
                                    <el-input v-model="dbsFormEntity.workOrder" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="机器序列号" prop="machineNo">
                                    <el-input v-model="dbsFormEntity.machineNo" clearable />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="顺序号" prop="workSequence">
                                    <el-input-number v-model.number="dbsFormEntity.workSequence"
                                        controls-position="right" :min="0" clearable />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="16">
                            <el-col :span="12" :offset="0">
                                <el-form-item label="开工日期" prop="planStartDate">
                                    <el-input v-model="dbsFormEntity.planStartDate" clearable />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态" prop="dbsStatus">
                                    <el-input-number v-model.number="dbsFormEntity.dbsStatus"
                                        controls-position="right" :min="0" :max="2" 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="submitDbsInfo(dbsFormEntity)">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
            <el-dialog v-model="uploadDialogVisible" title="DBS" 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>
                    <UploadExcelDbs></UploadExcelDbs>
                </fieldset>
            </el-dialog>
        </el-config-provider>
    </div>
</template>

<script setup>
import store from '@/store'
import { getDbs, deleteDbs, updateDbs } from '@/api/kateWork.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'
import { dateFormatter, timeFormatter } from '@/utils/formatter.js'
import UploadExcelDbs from '@/excel/UploadExcelDbs.vue'
import { queryUploadRecord } from '@/api/excel.js'
import { Search } from '@element-plus/icons-vue'
</script>
<script>
export default {
    name: 'dbs',
    data() {
        return {
            dbsList: [],
            currentPage: 1,
            uploadRecord: reactive({
                fileName: '',
                uploadTime: '',
                uploadUser: ''
            }),
            pageSize: 10,
            total: 0,
            workOrderQuery: '',
            loading: true,
            dialogVisible: false,
            uploadDialogVisible: false,
            dbsId: '',
            dbsFormEntity: reactive({}),
            labelPosition: 'top',
            dbsFormRef: ref(),
            rules: reactive({})
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loading = true
            const request = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                workOrder: this.workOrderQuery.trim(),
                userName: store.getters.getUserName
            }
            getDbs(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.dbsList = tableResponse.returnData.lists
                    this.total = tableResponse.returnData.total
                } else {
                    ElMessage.error(tableResponse.message)
                }
            }).catch(err => {
                ElMessage.error('查询DBS错误' + err.message)
            })
            this.loading = false
        },
        dateFormat: (row, column, cellValue, index) => {
            return dateFormatter(cellValue)
        },
        timeFormat: (row, column, cellValue, index) => {
            return timeFormatter(cellValue)
        },
        reset() {
            this.workOrderQuery = ''
            this.search()
        },
        editCurrentRowDbs(row) {
            this.dbsId = row.dbsId
            this.dbsFormEntity = {
                dbsId: row.dbsId,
                workSequence: row.workSequence,
                machineNo: row.machineNo,
                workOrder: row.workOrder,
                planStartDate: timeFormatter(row.planStartDate),
                dbsStatus: row.dbsStatus,
                lastUpdateTime: row.lastUpdateTime,
                lastUpdateUser: row.lastUpdateUser
            }
            this.dialogVisible = true
        },
        deleteCurrentRowDbs(row) {
            this.dbsId = row.dbsId
            ElMessageBox.confirm(
                '该操作会删除选择的DBS数据。\n是否继续',
                '警告',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                const params = {
                    dbsId: this.dbsId,
                    userName: store.getters.getUserName
                }
                deleteDbs(params).then(res => {
                    if (res.data.code == 0) {
                        ElMessage({
                            message: '删除DBS信息成功',
                            type: 'success',
                        })
                        this.search()
                    } else {
                        ElMessage.error(res.data.message)
                    }
                }).catch(err => {
                    console.log(err)
                    ElMessage.error('删除DBS信息成功')
                })
            }).catch(() => {
                ElMessage({
                    type: 'info',
                    message: '操作取消',
                })
            })
        },
        submitDbsInfo(formData) {
            const params = {
                dbsId: this.dbsId,
                dbsStatus: formData.dbsStatus,
                workSequence: formData.workSequence,
                machineNo: formData.machineNo,
                planStartDate: formData.planStartDate,
                userName: store.getters.getUserName
            }
            updateDbs(params).then(res => {
                if (res.data.code == 0) {
                    this.dialogVisible = false
                    ElMessage({
                        message: '更新DBS成功。',
                        type: 'success',
                    })
                    this.search()
                } else {
                    ElMessage.error(res.data.message)
                }
            }).catch(err => {
                ElMessage.error('更新DBS失败。')
            })
        },
        getCurrentRow(row) {
            this.dbsId = row.dbsId
        },
        openUploadDialog() {
            // 请求上传记录
            const param = {
                userName: store.getters.getUserName,
                fileDescription: 'DBS',
                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)
                ElMessage.error('发生异常')
            })
            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>