Mở hết phiếu theo ngày

Mở phơi ngày hôm nay

(function runAutoProcess() {
    // --- TỰ ĐỘNG LẤY NGÀY HIỆN TẠI ---
    const dateObj = new Date();
    const day = String(dateObj.getDate()).padStart(2, '0');
    const month = String(dateObj.getMonth() + 1).padStart(2, '0');
    const year = dateObj.getFullYear();
    
    const TARGET_DATE = `${day}/${month}/${year}`; 
    console.log("Ngày mục tiêu (Hôm nay):", TARGET_DATE);

    // --- CẤU HÌNH TỪ KHÓA CẦN BỎ QUA ---
    const EXCLUDED_KEYWORDS = ["PHIẾU CHỈ ĐỊNH"]; // Bỏ qua tất cả các loại phiếu chỉ định

    // --- BƯỚC 1: Bấm nút In nhiều phiếu ---
    function clickPrintMany(callback) {
        console.log("1. Đang tìm nút 'In nhiều phiếu'...");
        let buttons = document.querySelectorAll("button.ant-btn.item-tool.btn-print");
        let btn = [...buttons].find(b => b.innerText.trim().includes("In nhiều phiếu"));

        if (btn) {
            btn.click();
            console.log("→ Đã bấm nút In nhiều phiếu. Đợi popup...");
            setTimeout(callback, 2000); 
        } else {
            console.error("Không tìm thấy nút In nhiều phiếu!");
        }
    }

    // --- BƯỚC 2: Mở rộng toàn bộ cây ---
    function expandAll(callback) {
        let btns = document.querySelectorAll(".ant-tree-switcher.ant-tree-switcher_close");
        if (btns.length === 0) {
            console.log("2. Đã mở hết cây.");
            if (callback) callback();
            return;
        }
        console.log(`→ Đang mở ${btns.length} nhánh...`);
        btns.forEach(btn => btn.click());
        setTimeout(() => expandAll(callback), 500);
    }

    // --- BƯỚC 3: Tích chọn theo ngày (TRỪ PHIẾU CHỈ ĐỊNH) ---
    function checkByDate(dateStr, callback) {
        console.log(`3. Đang tích chọn các mục ngày: ${dateStr}...`);
        let titles = document.querySelectorAll(".ant-tree .custom-title");
        let count = 0;

        titles.forEach(titleEl => {
            const text = titleEl.innerText;
            const titleAttr = titleEl.getAttribute("title") || ""; // Lấy thuộc tính title ẩn

            // 1. Kiểm tra ngày
            if (text.includes(dateStr)) {
                
                // 2. KIỂM TRA LOẠI TRỪ (Bỏ qua Phiếu chỉ định)
                let isExcluded = false;
                for (let keyword of EXCLUDED_KEYWORDS) {
                    // Kiểm tra cả text hiển thị và title ẩn
                    if (titleAttr.toUpperCase().includes(keyword) || text.toUpperCase().includes(keyword)) {
                        isExcluded = true;
                        break;
                    }
                }

                if (isExcluded) {
                    console.log(`%cBỏ qua (Không tích): ${titleAttr} - ${text}`, "color: orange");
                    return; // Dừng, không chạy lệnh click phía dưới
                }

                // 3. Tích chọn nếu hợp lệ
                let itemContent = titleEl.closest(".item-content");
                if (itemContent) {
                    let checkbox = itemContent.querySelector(".ant-tree-checkbox");
                    if (checkbox && !checkbox.classList.contains("ant-tree-checkbox-checked")) {
                        checkbox.click();
                        count++;
                        console.log(`Đã tích: ${text}`);
                    }
                }
            }
        });
        console.log(`→ Tổng cộng đã tích thêm ${count} mục hợp lệ.`);
        if (callback) setTimeout(callback, 1000); 
    }

    // --- BƯỚC 4: Bấm Xem file in ---
    function clickViewFile() {
        console.log("4. Đang tìm nút 'Xem file in'...");
        let btn = [...document.querySelectorAll("button.ant-btn.ant-btn-dashed")]
            .find(b => b.textContent.includes("Xem file in"));

        if (btn) {
            btn.click();
            console.log("%cHOÀN THÀNH: Đã bấm xem file in!", "color: green; font-size: 14px; font-weight: bold;");
        } else {
            console.error("Không tìm thấy nút Xem file in.");
        }
    }

    // --- CHẠY LUỒNG ---
    clickPrintMany(() => {
        expandAll(() => {
            checkByDate(TARGET_DATE, () => {
                clickViewFile();
            });
        });
    });

})();
    
Mở phơi ngày hôm qua

(function runAutoProcess() {
    // --- TỰ ĐỘNG TÍNH NGÀY HÔM QUA ---
    const dateObj = new Date();
    dateObj.setDate(dateObj.getDate() - 1); // Trừ đi 1 ngày

    const day = String(dateObj.getDate()).padStart(2, '0');
    const month = String(dateObj.getMonth() + 1).padStart(2, '0');
    const year = dateObj.getFullYear();
    
    const TARGET_DATE = `${day}/${month}/${year}`;
    console.log("Ngày mục tiêu (Hôm qua):", TARGET_DATE);

    // --- CẤU HÌNH TỪ KHÓA CẦN BỎ QUA ---
    const EXCLUDED_KEYWORDS = ["PHIẾU CHỈ ĐỊNH"]; 

    // --- BƯỚC 1: Bấm nút In nhiều phiếu ---
    function clickPrintMany(callback) {
        console.log("1. Đang tìm nút 'In nhiều phiếu'...");
        let buttons = document.querySelectorAll("button.ant-btn.item-tool.btn-print");
        let btn = [...buttons].find(b => b.innerText.trim().includes("In nhiều phiếu"));

        if (btn) {
            btn.click();
            console.log("→ Đã bấm nút In nhiều phiếu. Đợi popup...");
            setTimeout(callback, 2000); 
        } else {
            console.error("Không tìm thấy nút In nhiều phiếu!");
        }
    }

    // --- BƯỚC 2: Mở rộng toàn bộ cây ---
    function expandAll(callback) {
        let btns = document.querySelectorAll(".ant-tree-switcher.ant-tree-switcher_close");
        if (btns.length === 0) {
            console.log("2. Đã mở hết cây.");
            if (callback) callback();
            return;
        }
        console.log(`→ Đang mở ${btns.length} nhánh...`);
        btns.forEach(btn => btn.click());
        setTimeout(() => expandAll(callback), 500);
    }

    // --- BƯỚC 3: Tích chọn (TRỪ PHIẾU CHỈ ĐỊNH) ---
    function checkByDate(dateStr, callback) {
        console.log(`3. Đang tích chọn các mục ngày: ${dateStr}...`);
        let titles = document.querySelectorAll(".ant-tree .custom-title");
        let count = 0;

        titles.forEach(titleEl => {
            const text = titleEl.innerText;
            const titleAttr = titleEl.getAttribute("title") || ""; 

            if (text.includes(dateStr)) {
                
                // KIỂM TRA LOẠI TRỪ
                let isExcluded = false;
                for (let keyword of EXCLUDED_KEYWORDS) {
                    if (titleAttr.toUpperCase().includes(keyword) || text.toUpperCase().includes(keyword)) {
                        isExcluded = true;
                        break;
                    }
                }

                if (isExcluded) {
                    console.log(`%cBỏ qua: ${titleAttr}`, "color: orange");
                    return; 
                }

                let itemContent = titleEl.closest(".item-content");
                if (itemContent) {
                    let checkbox = itemContent.querySelector(".ant-tree-checkbox");
                    if (checkbox && !checkbox.classList.contains("ant-tree-checkbox-checked")) {
                        checkbox.click();
                        count++;
                    }
                }
            }
        });
        console.log(`→ Đã tích thêm ${count} mục.`);
        if (callback) setTimeout(callback, 1000); 
    }

    // --- BƯỚC 4: Bấm Xem file in ---
    function clickViewFile() {
        console.log("4. Đang tìm nút 'Xem file in'...");
        let btn = [...document.querySelectorAll("button.ant-btn.ant-btn-dashed")]
            .find(b => b.textContent.includes("Xem file in"));

        if (btn) {
            btn.click();
            console.log("%cHOÀN THÀNH: Đã bấm xem file in!", "color: green; font-size: 14px; font-weight: bold;");
        } else {
            console.error("Không tìm thấy nút Xem file in.");
        }
    }

    clickPrintMany(() => {
        expandAll(() => {
            checkByDate(TARGET_DATE, () => {
                clickViewFile();
            });
        });
    });

})();
    
Mở phơi ngày bất kỳ (tự sửa)

(function runAutoProcess() {
    // ===========================================
    // CẤU HÌNH NGÀY CẦN IN TẠI ĐÂY
    const TARGET_DATE = "09/01/2026";         
    // ===========================================

    // Danh sách từ khóa cần bỏ qua (Không tích)
    const EXCLUDED_KEYWORDS = ["PHIẾU CHỈ ĐỊNH"]; 

    console.log(`--- BẮT ĐẦU QUY TRÌNH CHO NGÀY: ${TARGET_DATE} ---`);

    // --- BƯỚC 1: Bấm nút In nhiều phiếu ---
    function clickPrintMany(callback) {
        console.log("1. Đang tìm nút 'In nhiều phiếu'...");
        let buttons = document.querySelectorAll("button.ant-btn.item-tool.btn-print");
        let btn = [...buttons].find(b => b.innerText.trim().includes("In nhiều phiếu"));

        if (btn) {
            btn.click();
            console.log("→ Đã bấm nút In nhiều phiếu. Đợi popup...");
            setTimeout(callback, 2000); // Đợi 2s để popup hiện ra
        } else {
            console.error("Không tìm thấy nút In nhiều phiếu!");
        }
    }

    // --- BƯỚC 2: Mở rộng toàn bộ cây (Đệ quy) ---
    function expandAll(callback) {
        let btns = document.querySelectorAll(".ant-tree-switcher.ant-tree-switcher_close");
        
        if (btns.length === 0) {
            console.log("2. Đã mở hết cây.");
            if (callback) callback();
            return;
        }

        console.log(`→ Đang mở ${btns.length} nhánh...`);
        btns.forEach(btn => btn.click());

        // Đợi 0.5s rồi kiểm tra lại xem còn nhánh nào đóng không
        setTimeout(() => expandAll(callback), 500);
    }

    // --- BƯỚC 3: Tích chọn theo ngày (TRỪ PHIẾU CHỈ ĐỊNH) ---
    function checkByDate(dateStr, callback) {
        console.log(`3. Đang tích chọn các mục ngày: ${dateStr}...`);
        let titles = document.querySelectorAll(".ant-tree .custom-title");
        let count = 0;

        titles.forEach(titleEl => {
            const text = titleEl.innerText;
            const titleAttr = titleEl.getAttribute("title") || ""; // Lấy title ẩn nếu có

            // 1. Kiểm tra ngày
            if (text.includes(dateStr)) {
                
                // 2. KIỂM TRA LOẠI TRỪ (Bỏ qua Phiếu chỉ định)
                let isExcluded = false;
                for (let keyword of EXCLUDED_KEYWORDS) {
                    // Kiểm tra cả text hiển thị và title ẩn, chuyển về chữ hoa để so sánh chuẩn
                    if (titleAttr.toUpperCase().includes(keyword) || text.toUpperCase().includes(keyword)) {
                        isExcluded = true;
                        break;
                    }
                }

                if (isExcluded) {
                    console.log(`%cBỏ qua (Không tích): ${titleAttr || text}`, "color: orange");
                    return; // Dừng vòng lặp hiện tại, không click
                }

                // 3. Tích chọn nếu hợp lệ
                let itemContent = titleEl.closest(".item-content");
                if (itemContent) {
                    let checkbox = itemContent.querySelector(".ant-tree-checkbox");
                    // Chỉ tích nếu chưa tích
                    if (checkbox && !checkbox.classList.contains("ant-tree-checkbox-checked")) {
                        checkbox.click();
                        count++;
                        console.log(`Đã tích: ${text}`);
                    }
                }
            }
        });
        console.log(`→ Tổng cộng đã tích thêm ${count} mục hợp lệ.`);
        if (callback) setTimeout(callback, 1000); // Đợi 1s sau khi tích xong
    }

    // --- BƯỚC 4: Bấm Xem file in ---
    function clickViewFile() {
        console.log("4. Đang tìm nút 'Xem file in'...");
        let btn = [...document.querySelectorAll("button.ant-btn.ant-btn-dashed")]
            .find(b => b.textContent.includes("Xem file in"));

        if (btn) {
            btn.click();
            console.log("%cHOÀN THÀNH: Đã bấm xem file in!", "color: green; font-size: 14px; font-weight: bold;");
        } else {
            console.error("Không tìm thấy nút Xem file in.");
        }
    }

    // --- CHẠY LUỒNG ---
    clickPrintMany(() => {
        expandAll(() => {
            checkByDate(TARGET_DATE, () => {
                clickViewFile();
            });
        });
    });

})();
    
Mở phơi ngày bất kỳ (tự sửa) + MỞ PHƠI CHƯA TÍCH NGÀY

(async function autoSelectAndPrint() {
    // ================= CẤU HÌNH =================
    // Ngày cần chọn (theo định dạng hiển thị trên web)
    const TARGET_DATE_UI = "16/01/2026"; 

    // Danh sách các mục cần tích
    const TARGET_CATEGORIES = [
        "Kết quả Xét nghiệm thường quy(LIS)",
        "Kết quả Huyết học - truyền máu",
        "Kết quả Vi sinh thường quy",
        "Kết quả Sinh Học Phân Tử",
        "Kết quả Vi sinh nuôi cấy",
        "Kết Quả XN Giải Phẫu Bệnh",
		"Kết quả Xét nghiệm Kỹ thuật cao",
        "Kết quả chụp X-Quang",
        "Kết quả Nội soi",
        "Kết quả Siêu âm",
        "Kết quả Thăm dò chức năng",
        "Kết quả chụp Cắt lớp vi tính",
        "Kết quả chụp Cộng hưởng từ",
        "Phiếu kết quả xét nghiệm Miễn dịch lâm sàng"
    ];

    // ================= HÀM HỖ TRỢ =================
    const sleep = (ms) => new Promise(r => setTimeout(r, ms));
    const log = (msg) => console.log(`%c[AUTO] ${msg}`, "color: #00ff00; font-weight: bold;");

    // ================= BƯỚC 1: BẤM NÚT IN NHIỀU PHIẾU =================
    log("1. Tìm nút 'In nhiều phiếu'...");
    const buttons = document.querySelectorAll("button.ant-btn.item-tool.btn-print");
    const printBtn = [...buttons].find(b => b.innerText.trim().includes("In nhiều phiếu"));

    if (!printBtn) {
        console.error("❌ Không tìm thấy nút 'In nhiều phiếu'. Hãy kiểm tra lại giao diện.");
        return;
    }
    printBtn.click();
    log("Đã bấm nút. Đang đợi popup hiện ra...");
    await sleep(2000); // Chờ popup load

    // ================= BƯỚC 2: MỞ RỘNG CÂY (EXPAND ALL) =================
    log("2. Đang mở rộng toàn bộ cây thư mục...");
    // Lặp click nút mở rộng cho đến khi hết nút đóng
    for (let i = 0; i < 10; i++) { // Thử tối đa 10 lần quét
        let closeBtns = document.querySelectorAll(".ant-tree-switcher.ant-tree-switcher_close");
        if (closeBtns.length === 0) break;
        
        closeBtns.forEach(btn => btn.click());
        await sleep(800); // Đợi hiệu ứng mở cây
    }
    log("Cây thư mục đã mở xong.");

    // ================= BƯỚC 3: XỬ LÝ LOGIC CHỌN (1 KẾT QUẢ VS NHIỀU KẾT QUẢ) =================
    log(`3. Bắt đầu tích chọn theo ngày: ${TARGET_DATE_UI}`);
    
    // Lấy tất cả các tiêu đề đang hiển thị
    const allTitles = document.querySelectorAll(".custom-title");

    for (let catName of TARGET_CATEGORIES) {
        // Tìm element tiêu đề khớp với tên danh mục
        let titleEl = [...allTitles].find(t => t.innerText.trim() === catName);
        
        if (!titleEl) continue; // Không có mục này trong hồ sơ bệnh nhân thì bỏ qua

        // Tìm lên thẻ li cha (tree-item)
        let liNode = titleEl.closest("li.tree-item");
        if (!liNode) continue;

        // Kiểm tra xem mục này có con hay không (Dựa vào class hoặc cấu trúc DOM)
        // Cách check: tìm xem bên trong có thẻ ul.ant-tree-child-tree không
        let childList = liNode.querySelector("ul.ant-tree-child-tree");
        
        // Cần đảm bảo là cây con này đang chứa các item con thực sự (đôi khi html rỗng)
        let hasChildren = childList && childList.querySelectorAll("li.tree-item").length > 0;

        if (!hasChildren) {
            // === TRƯỜNG HỢP 1: CHỈ CÓ 1 KẾT QUẢ (AUTO TÍCH) ===
            let checkbox = liNode.querySelector(".ant-tree-checkbox");
            if (checkbox && !checkbox.classList.contains("ant-tree-checkbox-checked")) {
                checkbox.click();
                console.log(`   ☑️ Tích đơn: ${catName}`);
            }
        } else {
            // === TRƯỜNG HỢP 2: NHIỀU KẾT QUẢ (CHỌN THEO NGÀY) ===
            // Duyệt qua các con của nó để tìm ngày
            let items = childList.querySelectorAll("li.tree-item");
            items.forEach(item => {
                let itemText = item.querySelector(".custom-title")?.innerText || "";
                
                // Kiểm tra ngày
                if (itemText.includes(TARGET_DATE_UI)) {
                    let cb = item.querySelector(".ant-tree-checkbox");
                    if (cb && !cb.classList.contains("ant-tree-checkbox-checked")) {
                        cb.click();
                        console.log(`   ☑️ Tích ngày ${TARGET_DATE_UI} tại mục: ${catName}`);
                    }
                }
            });
        }
    }

    await sleep(1000);

    // ================= BƯỚC 4: BẤM NÚT XEM FILE IN =================
    log("4. Tìm nút 'Xem file in'...");
    let actionBtns = [...document.querySelectorAll("button.ant-btn.ant-btn-dashed")];
    let viewBtn = actionBtns.find(b => b.textContent.includes("Xem file in"));

    if (viewBtn) {
        viewBtn.click();
        log("✅ HOÀN TẤT! Tab mới sẽ được mở.");
    } else {
        console.error("❌ Không tìm thấy nút 'Xem file in'.");
    }

})();