<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>固件管理</title>
    <!-- 外部引入库文件 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <!-- 外部引入自定义样式 -->
    <link rel="stylesheet" href="/assets/css/table.css">
</head>
<body id="app">
<div class="main-container">
    <div class="table-container">
        <h4 class="mb-3">固件管理</h4>

        <div class="search-bar">
            <div class="search-item">
                <div class="input-group">
                    <span class="input-group-text">
                        固件名
                    </span>
                    <input
                            v-model="searchParams.firmwareName"
                            type="text"
                            class="form-control"
                            placeholder="请输入固件名称"
                            @keyup.enter="fetchData()"
                    >
                </div>
            </div>

            <div class="search-item">
                <div class="input-group">
                    <span class="input-group-text">
                       类型
                    </span>
                    <select
                            v-model="searchParams.firmwareType"
                            class="form-select"
                            @change="fetchData()"
                    >
                        <option value="">全部状态</option>
                        <option value="1">调参固件</option>
                        <option value="2">AP固件</option>
                        <option value="3">INAV固件</option>
                    </select>
                </div>
            </div>

            <div class="d-flex gap-2">
                <button class="btn btn-primary" @click="fetchData()">
                    <i class="bi bi-search me-1"></i> 搜索
                </button>
                <button class="btn btn-success" @click="resetSearch()">
                    <i class="bi bi-arrow-counterclockwise me-1"></i> 重置
                </button>
            </div>
        </div>

        <div class="mb-2">
            <button class="btn btn-info" @click="openAddModal()">
                <i class="bi bi-plus-circle me-1"></i> 新增
            </button>
        </div>

        <div class="table-responsive">
            <table class="table table-sm table-striped table-hover table-bordered ">
                <thead class="table-light">
                <tr>
                    <!-- 全选复选框 -->
                    <th style="width: 50px;">
                        <input
                                type="checkbox"
                                class="form-check-input"
                                v-model="selectAll"
                                @change="toggleSelectAll()"
                        >
                    </th>
                    <th>固件名</th>
                    <th>大小(字节)</th>
                    <th>描述</th>
                    <th>类型</th>
                    <th>创建时间</th>
                    <th style="width: 120px;">操作</th>
                </tr>
                </thead>
                <tbody>
                <!-- 加载中状态 -->
                <tr v-if="loading">
                    <td colspan="8" class="p-0">
                        <div class="loading-container">
                            <div class="spinner-border text-primary" role="status" style="width: 2rem; height: 2rem;">
                                <span class="visually-hidden">Loading...</span>
                            </div>
                            <p class="mt-2 text-muted">加载中，请稍候...</p>
                        </div>
                    </td>
                </tr>

                <!-- 无数据状态 -->
                <tr v-else-if="tableData.length === 0">
                    <td colspan="8" class="p-0">
                        <div class="no-data">
                            <i class="bi bi-folder-x"></i>
                            <h5>暂无匹配数据</h5>
                            <p class="text-muted">请尝试调整搜索条件或重置查询</p>
                        </div>
                    </td>
                </tr>

                <!-- 数据列表 -->
                <tr v-else v-for="(item, index) in tableData" :key="item.id"
                    :class="{ selected: selectedIds.includes(item.id) }">
                    <td>
                        <input
                                type="checkbox"
                                class="form-check-input"
                                v-model="selectedIds"
                                :value="item.id"
                                @change="toggleSelectItem(item.id)"
                        >
                    </td>
                    <td>{{ item.firmwareName }}</td>
                    <td>{{ item.firmwareSize }}</td>
                    <td>{{ item.firmwareDescription }}</td>
                    <td>{{ item.firmwareTypeName }}</td>
                    <td>{{ formatTime(item.createTime) }}</td>
                    <td>
                        <div class="d-flex gap-1">
                            <button class="btn btn-sm btn-primary" @click="openUploadModal(item)" title="上传固件">
                                <i class="bi bi-cloud-upload-fill"></i>
                            </button>
                            <button class="btn btn-sm btn-primary" @click="openEditModal(item)">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-danger" @click="handleDelete(item.id)">
                                <i class="bi bi-trash"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <div class="batch-actions mt-3">
            <div class="d-flex align-items-center gap-2">
                <span class="text-muted">已选中 {{ selectedIds.length }} 条数据</span>
                <select
                        class="form-select"
                        v-model="batchAction"
                        :disabled="selectedIds.length === 0"
                        @change="handleBatchOperation"
                        style="width: auto; min-width: 160px;"
                >
                    <option value="">-- 批量操作 --</option>
                    <option value="delete">批量删除</option>
                </select>

                <button
                        class="btn btn-secondary"
                        @click="clearSelected()"
                        :disabled="selectedIds.length === 0"
                >
                    <i class="bi bi-x-circle me-1"></i> 取消选择
                </button>
            </div>
        </div>

        <div class="d-flex justify-content-between align-items-center mt-3">
            <div class="page-info">
                共 {{ total }} 条数据，当前第 {{ pageNum }}/{{ totalPages }} 页
            </div>
            <nav>
                <ul class="pagination pagination-sm mb-0">
                    <li class="page-item" :class="{ disabled: pageNum === 1 }">
                        <a class="page-link" href="#" @click.prevent="changePage(1)">首页</a>
                    </li>
                    <li class="page-item" :class="{ disabled: pageNum === 1 }">
                        <a class="page-link" href="#" @click.prevent="changePage(pageNum - 1)">上一页</a>
                    </li>
                    <li class="page-item" :class="page === pageNum ? 'active' : ''" v-for="page in pageList"
                        :key="page">
                        <a class="page-link" href="#" @click.prevent="changePage(page)">{{ page }}</a>
                    </li>
                    <li class="page-item" :class="{ disabled: pageNum === totalPages }">
                        <a class="page-link" href="#" @click.prevent="changePage(pageNum + 1)">下一页</a>
                    </li>
                    <li class="page-item" :class="{ disabled: pageNum === totalPages }">
                        <a class="page-link" href="#" @click.prevent="changePage(totalPages)">末页</a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>


</div>


<!-- 上传文件 -->
<div class="modal fade" id="uploadFileModal" tabindex="-1" aria-labelledby="uploadFileModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="uploadFileModalLabel">上传固件</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form>
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label">选择固件</label>
                        <div class="col-sm-9">
                            <input
                                    type="file"
                                    class="form-control"
                                    id="firmwareFile"
                                    name="file"
                                    accept=".bin"
                            >
                            <div class="form-text text-muted mt-1">支持 .bin 格式，单个文件不超过 100MB</div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" @click.prevent="uploadFile">上传</button>
            </div>
        </div>
    </div>
</div>


<!-- 新增 or 编辑 -->
<div class="modal fade" id="addOrEditModel" tabindex="-1" aria-labelledby="addOrEditModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="addOrEditModalLabel">{{ addOrEditTitle }}</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form>
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label">固件名称</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" v-model="addOrEditDto.firmwareName"
                                   placeholder="请输入固件名称">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label">固件描述</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" v-model="addOrEditDto.firmwareDescription"
                                   placeholder="请输入固件描述">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label">固件类型</label>
                        <div class="col-sm-9">
                            <select class="form-control" v-model="addOrEditDto.firmwareType">
                                <option value=1>调参固件</option>
                                <option value=2>AP固件</option>
                                <option value=3>INAV固件</option>
                            </select>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label">固件地址</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" v-model="addOrEditDto.downloadUrl"
                                   placeholder="请输入固件地址">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" @click.prevent="saveEntity">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 外部引入库文件 -->
<script src="https://cdn.jsdelivr.net/npm/vue@3.3.4/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios@1.4.0/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- 外部引入模拟请求文件 -->
<script src="/assets/js/axiosRequest.js"></script>
<script src="/assets/js/message.js"></script>
<script src="/assets/js/confirmModal.js"></script>

<!-- 页面核心逻辑 -->
<script>
    const {createApp} = Vue;

    createApp({
        data() {
            return {
                tableData: [], // 表格数据源
                loading: false, // 加载状态
                searchParams: {
                    firmwareName: '',
                    firmwareType: '',
                },
                // 分页参数
                pageNum: 1, // 当前页码
                pageSize: 10, // 每页条数
                total: 0, // 总数据量
                totalPages: 0, // 总页数
                // 批量操作
                selectedIds: [], // 选中的ID集合
                selectAll: false, // 全选状态
                batchAction: '', // 批量操作选择
                pageRange: 5,
                modalInstances: {},
                resetPasswordDto: {
                    userId: null,
                    nickName: null,
                    username: null,
                    password: null,
                },
                addOrEditTitle: '',
                addOrEditDto: {
                    id: null,
                    firmwareName: null,
                    firmwareDescription: null,
                    firmwareType: 1,
                    downloadUrl: ''
                }
            }
        },
        computed: {
            // 计算当前显示的页码列表
            pageList() {
                const list = [];
                if (this.totalPages === 0) return list;

                // 总页数小于等于显示范围，直接显示所有页码
                if (this.totalPages <= this.pageRange) {
                    for (let i = 1; i <= this.totalPages; i++) {
                        list.push(i);
                    }
                } else {
                    // 总页数大于显示范围，显示当前页前后2个页码
                    let start = Math.max(1, this.pageNum - 2);
                    let end = Math.min(this.totalPages, this.pageNum + 2);

                    // 确保显示5个页码
                    if (end - start < this.pageRange - 1) {
                        if (start === 1) {
                            end = this.pageRange;
                        } else if (end === this.totalPages) {
                            start = this.totalPages - this.pageRange + 1;
                        }
                    }

                    for (let i = start; i <= end; i++) {
                        list.push(i);
                    }
                }
                return list;
            }
        },
        methods: {
            formatTime(time) {
                if (!time) return '-';
                const date = new Date(time);
                return date.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit'
                });
            },

            async fetchData() {
                this.loading = true;
                try {
                    const response = await request.get('/biz/firmware/page', {
                        ...this.searchParams,
                        current: this.pageNum,
                        size: this.pageSize
                    });
                    console.log(response)

                    if (response.code === 200) {
                        this.tableData = response.data.records; // 列表数据
                        this.total = response.data.total; // 总条数
                        this.totalPages = response.data.pages; // 总页数
                    }
                } catch (error) {
                    console.error('获取数据失败:', error);
                    this.tableData = [];
                    this.total = 0;
                    this.totalPages = 0;
                } finally {
                    this.loading = false;
                    this.clearSelected();
                }
            },

            // 切换页码
            changePage(page) {
                if (page < 1 || page > this.totalPages || page === this.pageNum) return;
                this.pageNum = page;
                this.fetchData();
            },

            // 重置搜索
            resetSearch() {

                this.searchParams = {
                    firmwareName: '',
                    firmwareType: '',
                };
                this.pageNum = 1;
                this.fetchData();
            },

            // 全选/取消全选
            toggleSelectAll() {
                if (this.selectAll) {
                    this.selectedIds = this.tableData.map(item => item.id);
                } else {
                    this.selectedIds = [];
                }
            },

            // 单个选中/取消选中
            toggleSelectItem(id) {
                this.selectAll = this.selectedIds.length === this.tableData.length && this.tableData.length > 0;
            },

            // 清空选中状态
            clearSelected() {
                this.selectedIds = [];
                this.selectAll = false;
                this.batchAction = '';
            },

            // 处理批量操作
            handleBatchOperation() {
                if (!this.batchAction || this.selectedIds.length === 0) return;

                switch (this.batchAction) {
                    case 'delete':
                        this.handleBatchDelete();
                        break;
                }

                this.batchAction = '';
            },

            // 批量删除
            async handleBatchDelete() {
                showConfirmModal({
                    title: '删除确认',
                    content: `确定要删除选中的 ${this.selectedIds.length} 条数据吗？`,
                    onConfirm: async () => {
                        try {
                            const response = await request.post('/biz/firmware/batchDelete', {ids: this.selectedIds});
                            if (response.code === 200) {
                                $message.success("删除成功");
                                this.fetchData();
                            }
                        } catch (error) {
                            console.error('删除失败:', error);
                            $message.error('删除失败，请重试！');
                        }
                    }
                });
            },

            // 单个删除
            async handleDelete(id) {
                showConfirmModal({
                    title: '删除确认',
                    content: '确定要删除这条数据吗？',
                    onConfirm: async () => {
                        try {
                            const response = await request.delete('/biz/firmware/delete', {id});
                            if (response.code === 200) {
                                $message.success("删除成功");
                                this.fetchData(); // 重新加载数据
                            }
                        } catch (error) {
                            console.error('删除失败:', error);
                            $message.error('删除失败，请重试！');
                        }
                    }
                });
            },
            // 打开文件上传模态框
            openUploadModal(item) {
                this.modalInstances['uploadFileModal'].show();
            },
            uploadFile() {
                request.put("/biz/user/resetPasswordRequest", this.resetPasswordDto)
                    .then((res) => {
                        if (res.code === 200) {
                            $message.success('修改成功！', 500);
                            this.modalInstances['resetPwdModal'].hide();
                        } else {
                            alert('修改失败！');
                        }
                        this.resetPasswordDto = {};
                    });

            },
            // 打开新增模态框
            openAddModal() {
                this.addOrEditTitle = '新增';
                this.modalInstances['addOrEditModel'].show();
            },

            // 打开编辑模态框
            openEditModal(item) {
                this.addOrEditTitle = '编辑';
                this.addOrEditDto = item;
                this.modalInstances['addOrEditModel'].show();
            },
            saveEntity() {
                let url = (this.addOrEditDto === null || this.addOrEditDto.id === null) ? '/biz/firmware/save' : '/biz/firmware/update';
                request.post(url, this.addOrEditDto)
                    .then((res) => {
                        if (res.code === 200) {
                            $message.success('保存成功！', 500);
                            this.fetchData();
                            this.modalInstances['addOrEditModel'].hide();
                        } else {
                            $message.error('保存失败！', 500);
                        }
                        this.clearForm();

                    }).catch(() => {

                })
            },
            // 清空表单数据
            clearForm() {
                this.addOrEditDto = {
                    id: null,
                    firmwareName: null,
                    firmwareDescription: null,
                    firmwareType: 1,
                    downloadUrl: ''
                };
            }
        },
        mounted() {
            this.fetchData();
            const modalIds = ['uploadFileModal', 'addOrEditModel'];
            modalIds.forEach(id => {
                const modalElement = document.getElementById(id);
                if (modalElement) {
                    this.modalInstances[id] = new bootstrap.Modal(modalElement, {
                        backdrop: 'static',
                        keyboard: true
                    });
                }
            });
        }
    }).mount('#app');
</script>
</body>
</html>
