<!-- 物业模块-移动球机管控-入库审核 -->
<template>
    <div class="MainBox">
        <el-form :model="queryForm" inline class="card-box mb-4">
            <el-form-item label="时间范围">
                <el-date-picker v-model="queryForm.dateRange" type="daterange" value-format="YYYY-MM-DD"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="所属项目">
                <el-input v-model="queryForm.projectName" placeholder="请输入项目名称"></el-input>
            </el-form-item>
            <el-form-item label="球机状态">
                <el-select v-model="queryForm.returnStatus" placeholder="请选择球机状态">
                    <el-option label="全部" value=""></el-option>
                    <el-option v-for="item in StatusList" :key="item.value" :label="item.label"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button size="default" @click="handleReset">重置</el-button>
                <el-button type="primary" size="default" @click="handleQuery">查询</el-button>
            </el-form-item>
        </el-form>

        <div class="card-box">
            <el-table :data="tableData" v-loading="loading" class="mt-2">
                <el-table-column prop="cameraNo" label="球机编号"></el-table-column>
                <el-table-column prop="projectName" label="所属项目"></el-table-column>
                <el-table-column prop="remark" label="回收核验结果"></el-table-column>
                <el-table-column prop="returnBy" label="回收人"></el-table-column>
                <el-table-column prop="returnTime" label="提交入库时间" format="YYYY-MM-DD HH:mm:ss">
                    <template #default="scope">
                        {{ formatDate(scope.row.returnTime) }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="returnStatus" label="回收状态">
                    <template #default="scope">
                        <el-tag :type="scope.row.returnStatus === '待回收' ? 'warning' : 'success'">
                            {{ scope.row.returnStatus }}
                        </el-tag>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" width="180">
                    <template #default="scope">
                        <el-button size="small" type="primary" link @click="handleAudit(scope.row)">入库审核</el-button>
                        <el-button size="small" type="Info" link @click="handleReject(scope.row)">驳回</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <span class="total-count">显示第 {{ (pageNum - 1) * pageSize + 1 }} 到 {{ pageNum * pageSize }} 条记录，共 {{
                    total }}
                    条</span>
                <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5, 10, 20]"
                    :total="total" layout="prev, pager, next" @size-change="getList" @current-change="getList"></el-pagination>
            </div>
        </div>
        <!-- 入库审核弹窗 -->
        <DialogBox v-show="dialogShow" ref="dialogRef" :show="dialogShow" :CloseDialog="handleCancel" :rowData="currentRowData" @refresh="handleRefresh" />
    </div>
</template>
<script setup name="Index">
import { ref, onMounted } from "vue";
import DialogBox from "./components/DialogC.vue";
import { listInventoryAudit, rejectInventoryAudit } from "@/api/tenement/inventoryAudit";
import { ElMessage, ElMessageBox } from "element-plus";
import { formatDate } from "@/utils";

// 查询表单数据
const queryForm = ref({
    dateRange: [],
    projectName: "",
    returnStatus: ""
});

// 状态列表
const StatusList = ref([
    {
        label: "待审核",
        value: "0"
    },
    {
        label: "已通过",
        value: "1"
    },
    {
        label: "已驳回",
        value: "2"
    },
]);

// 表格数据
const tableData = ref([]);
const dialogShow = ref(false); // 入库审核弹窗-显示状态
const currentRowData = ref(null); // 当前选中的行数据
const pageNum = ref(1); // 当前页码
const pageSize = ref(5); // 每页显示条数
const total = ref(0); // 总记录数
const loading = ref(false); // 加载状态


// 获取列表数据
const getList = async () => {
    loading.value = true;
    try {
        const params = {
            pageNum: pageNum.value,
            pageSize: pageSize.value
        };
        
        // 添加查询条件
        if (queryForm.value.dateRange && queryForm.value.dateRange.length === 2) {
            params.startTime = queryForm.value.dateRange[0];
            params.endTime = queryForm.value.dateRange[1];
        }
        if (queryForm.value.projectName) {
            params.projectName = queryForm.value.projectName;
        }
        if (queryForm.value.returnStatus !== "") {
            params.returnStatus = queryForm.value.returnStatus;
        }
        
        const response = await listInventoryAudit(params);
        if (response.code === 200) {
            tableData.value = response.rows || response.data || [];
            total.value = response.total || 0;
        } else {
            ElMessage.error(response.msg || "获取数据失败");
        }
    } catch (error) {
        console.error("获取列表数据失败:", error);
        ElMessage.error("获取数据失败，请稍后重试");
    } finally {
        loading.value = false;
    }
};

// 查询
const handleQuery = () => {
    pageNum.value = 1;
    getList();
};

// 重置
const handleReset = () => {
    queryForm.value = {
        dateRange: [],
        projectName: "",
        returnStatus: ""
    };
    pageNum.value = 1;
    getList();
};

// 组件挂载时获取数据
onMounted(() => {
    getList();
});

// 入库审核弹窗-打开
const handleAudit = (row) => {
    currentRowData.value = row; // 保存当前行数据
    dialogShow.value = true
}
// 关闭弹窗
const handleCancel = () => {
    console.log('关闭弹窗')
    dialogShow.value = false
    currentRowData.value = null; // 清空行数据
}

// 刷新列表（入库成功后调用）
const handleRefresh = () => {
    getList()
}

// 驳回入库审核
const handleReject = async (row) => {
    try {
        await ElMessageBox.confirm(
            `确定要驳回球机编号为"${row.cameraNo}"的入库审核吗？`,
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        
        const params = {
            id: row.id || row.returnId,
            cameraId: row.cameraId || row.id,
            cameraNo: row.cameraNo,
            returnIds: row.returnId ? [row.returnId] : []
        }
        
        const response = await rejectInventoryAudit(params)
        if (response.code === 200) {
            ElMessage.success('驳回成功')
            getList() // 刷新列表
        } else {
            ElMessage.error(response.msg || '驳回失败')
        }
    } catch (error) {
        if (error !== 'cancel') {
            console.error('驳回失败:', error)
            ElMessage.error('驳回失败，请稍后重试')
        }
    }
}

</script>
<style scoped lang="scss">
.MainBox {
    padding: 20px;
    height: 100%;
    background: #F9FAFB;
}

.FlexBox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 10px;
}

.TitleBox {
    font-size: 16px;
    font-weight: bold;
    color: #303133;
}


.mt-2 {
    margin-top: 20px;
}


.card-box {
    background: #fff;
    padding: 16px;
    border-radius: 4px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.pagination-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 16px;
}

.total-count {
    color: #606266;
}
</style>
