<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="goodsIdQuery" style="width: 158px; margin-right: 10px;" placeholder="料号"
                        :suffix-icon="Search" />
                    <el-date-picker v-model="pullDateQuery" type="date" placeholder="选择收货日期" :shortcuts="shortcuts"
                    style="width: 158px; margin-right: 10px;" clearable />
                    <el-select-v2 v-model="kanbanStatusQuery" style="width: 158px; margin-right: 10px;"
                        placeholder="请选择看板状态" :options="kanbanStatusOptions" @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: #20B2AA; color: #000;" @click="genKanban()">生成看板</el-button>
                    <el-button style="background-color: #32CD32; color: #000;" @click="exportExcel()">导出看板</el-button>
                </el-row>
            </div>
            <br />
            <el-table :data="kanbanList" stripe border v-loading="loadingFlag" 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.recordId" v-model="recordId">&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column prop="recordId" label="id" fixed="left" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="goodsId" label="料号" show-overflow-tooltip min-width="120px" />
                <el-table-column prop="kanbanId" label="看板id" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="kanbanStatus" label="看板状态" :formatter="kanbanStatusFormat" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="lastPullTime" label="上次收货时间" :formatter="timeFormat" min-width="120px" show-overflow-tooltip />
                <el-table-column prop="lastPullUser" label="上次收货用户" min-width="120px" />
                <el-table-column prop="lastRequestTime" label="上次请求时间" :formatter="timeFormat" min-width="120px" />
                <el-table-column prop="lastRequestUser" 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 { getClcKanbanByPage, genClcKanbanRequirement } from '@/api/kateWork.js'
import { downloadClcKanbanRequirementExcel } from '@/api/excel.js'
import { successBox, errorBox } from '@/utils/myMessageBox.js'
import { dateFormatter, timeFormatter } from '@/utils/formatter.js'
import { Search } from '@element-plus/icons-vue'
import loading from '@/utils/loading.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
</script>
<script>
export default {
    name: 'clcKanban',
    data() {
        return {
            kanbanList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            goodsIdQuery: '',
            pullDateQuery: null,
            shortcuts: [
                {
                    text: '今天',
                    value: new Date(),
                },
                {
                    text: '昨天',
                    value: () => {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24)
                        return date
                    },
                },
            ],
            kanbanStatusQuery: -99,
            loadingFlag: true,
            recordId: '',
            kanbanStatusOptions: [
                {
                    value: -99,
                    label: '全部'
                },
                {
                    value: 0,
                    label: '空'
                },
                {
                    value: 1,
                    label: '满'
                }
            ],
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        search() {
            this.loadingFlag = true
            const request = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                goodsId: this.goodsIdQuery.trim(),
                kanbanStatus: this.kanbanStatusQuery == -99 ? null : this.kanbanStatusQuery,
                lastPullTime: timeFormatter(this.pullDateQuery),
                userName: store.getters.getUserName
            }
            getClcKanbanByPage(request).then(res => {
                const tableResponse = res.data
                if (tableResponse.code == 0) {
                    this.kanbanList = tableResponse.returnData.lists
                    this.total = tableResponse.returnData.total
                } else {
                    errorBox(tableResponse.message)
                }
            }).catch(err => {
                console.log(err)
                errorBox('查询看板错误')
            })
            this.loadingFlag = false
        },
        dateFormat: (row, column, cellValue, index) => {
            return dateFormatter(cellValue)
        },
        timeFormat: (row, column, cellValue, index) => {
            return timeFormatter(cellValue)
        },
        kanbanStatusFormat: (row, column, cellValue, index) => {
            switch (cellValue) {
                case 0:
                    return '空'
                case 1:
                    return '满'
                default:
                    return '未知'
            }
        },
        reset() {
            this.goodsIdQuery = ''
            this.pullDateQuery = null
            this.kanbanStatusQuery = -99
            this.search()
        },
        getCurrentRow(row) {
            this.recordId = row.recordId
        },
        genKanban() {
            const request = {
                userName: store.getters.getUserName
            }
            loading.open('生成中...')
            genClcKanbanRequirement(request).then(res => {
                loading.close()
                const response = res.data
                if (response.code == 0) {
                    successBox('生成看板需求成功。')
                } else {
                    errorBox(response.message)
                }
            }).catch(err => {
                loading.close()
                console.log(err)
                errorBox('生成看板需求异常。')
            })
        },
        exportExcel() {
            const request = {
                goodsId: this.goodsIdQuery.trim(),
                kanbanStatus: this.kanbanStatusQuery == -99 ? null : this.kanbanStatusQuery,
                lastPullTime: timeFormatter(this.pullDateQuery),
                userName: store.getters.getUserName
            }
            downloadClcKanbanRequirementExcel(request).then(res => {
                const link = document.createElement('a');//创建a标签
                try {
                    // let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});    //如果后台返回的不是blob对象类型，先定义成blob对象格式，该type导出为xls格式，
                    let blob = res.data    //如果后台返回的直接是blob对象类型，直接获取数据
                    // let _fileName = res.headers['content-disposition'].split(';')[1].split('=')[1]; //拆解获取文件名，如果后端有给返回文件名的话
                    let _fileName = "需求看板" + dateFormatter(new Date) + ".xlsx"
                    link.style.display = 'none'//隐藏

                    // 兼容不同浏览器的URL对象
                    const url = window.URL || window.webkitURL || window.moxURL
                    link.href = url.createObjectURL(blob)
                    link.setAttribute('download', _fileName.substring(_fileName.lastIndexOf('_') + 1))
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    url.revokeObjectURL(link.href)//移除url对象
                } catch (e) {
                    console.log(e)
                    errorBox('下载文件失败')
                }
            }).catch(err => {
                console.log(err)
                errorBox('导出失败')
            })
        },
    },
}
</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>