<?php
include_once("model/db.php");
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// لو مش عامل login → رجّعه لصفحة login
if (!isset($_SESSION["id_user"])) {
    echo "<script>
            window.location.href = 'login.php'
            </script>";
    exit;
} else {
    $idPublicUser  = $_SESSION['id_account'];

    ?>
    <script>

    const powersEmp =  <?php echo json_encode($_SESSION['powersEmp']) ;?>;
    var nameEmp = '<?php echo $_SESSION['name_emp'] ;?>';
</script>
<?php
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<?php
include_once("header.php");
?>

<body id="page-top">
  <div id="pageLoader">
  <div class="loader-box">
    <div class="loader-spinner"></div>
    <div class="loader-text">جارٍ تحميل النظام...</div>
  </div>
</div>

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include_once("nav.php");
        ?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <?php
                include_once("nav_head.php");
                ?>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                <div class="container-fluid khedma-page mainBody khedma-page-container" dir="rtl">
                    <h1 class="h3 mb-4 text-gray-800"></h1>
                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <!-- Footer -->
      <footer class="khedma-footer">
          <div class="footer-content">
              <img src="img/pe.png" alt="PROG EGYPT" class="footer-logo">
              <span class="footer-text">Powered by PROG EGYPT</span>
          </div>
      </footer>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <?php
    include_once("footer.php");
    ?>

    <div class="modal fade" id="invoiceModal" tabindex="-1" role="dialog" aria-labelledby="invoiceModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
    
      <div class="modal-header">
        <h5 class="modal-title" id="invoiceModalLabel">فاتورة العميل</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="إغلاق">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      
      <div class="modal-body row">
        <!-- Header -->


                     <center>

                        <img style="width:25%;border:1px; border-raduis 50%;" id="inv_logo" data-field="logo"
                          class="inv-logo col-12 isImg"
                          src=""
                          alt="logo">
                          <br/>

                      <!-- بيانات التواصل -->
                  </center>
                  <hr>
                  <div class="col-12 text-center" style="text-align: center; !important">

                    <div class="text-center" style="text-align: center; !important">

                      <h6 class="isName" style="text-align: center; !important"><span id="name">sdfsdf</span></h6>
                      <h6 class="isAddress" style="text-align: center; !important"><span id="address">sdfsdf</span></h6>
                      <h6 class="isPhone" style="text-align: center; !important"><span id="phone">sdfsdf</span></h6>
                      <h6 class="isMail" style="text-align: center; !important"><span id="mail">sdfsdf</span></h6>

                    </div>

                  </div>

            <div class="col-6">
              <p><strong>اسم العميل: </strong><span id="nameCusInv">sdfsdf</span></p>
              <p><strong>رقم الفاتورة:</strong> <span id="idInv">sdfsdf</span></p>
            </div>
            <div class="col-6 text-right">
              <p><strong>اسم الموظف:</strong> <span id="nameEmp">sdfsdf</span> </p>
              <p ><strong>تاريخ:</strong><span id="dateInv">sdfsdf</span></p>
            </div>

        <!-- Items Table -->
        <table class="table table-bordered invoice-items">
          <thead class="thead-light">
            <tr>
              <th>#</th>
              <th>الخدمة</th>
              <th>الكمية</th>
              <th>السعر</th>
              <th>المجموع</th>
            </tr>
          </thead>
          <tbody id="tbodyInv">


          </tbody>
        </table>

        <!-- Notes -->
               <div class="col-12 text-center isTextFooter">
        <h6 id="textHeader">sdf sdf sdf sdf sdf sd d </h6>
      </div>

      </div>

      
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">إغلاق</button>
        <button type="button" class="btn btn-primary" id="printInvoice">طباعة الفاتورة</button>
      </div>
      
    </div>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

    <script src="./js/scripts/main.js"></script>
    <script src="./js/scripts/pages.js"></script>
    <script src="./js/scripts/input.js"></script>
    <script src="./js/scripts/student.js"></script>
    <script src="./js/scripts/rev.js"></script>
    <script src="./js/scripts/emp.js"></script>
    <script src="./js/scripts/rebort.js"></script>
    <script src="./js/scripts/powers.js"></script>
    <script src="./js/scripts/custumRebort.js"></script>
    <script src="./js/scripts/sales.js"></script>
    <script src="./js/scripts/setting.js"></script>

    
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>



<script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>

<!-- أزرار التصدير -->
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.print.min.js"></script>

<!-- مكتبات مساعدة للتصدير -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script> <!-- Excel -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script> <!-- PDF -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>

    <script>
        main.idEmp = <?php echo $_SESSION["id_user"] ?>
    </script>
    <script>

        $(window).on("load", function () {
          $('#pageLoader').fadeOut(300);
            main.getDateToday()
            powers.getPowersEmp()
            pages.home()
            setting.loadInvoiceOptions(1)
            main.getDataCompany()
            sales.getDataService()
            $('.nameEmpBtn').text(nameEmp)
        });
        
        // $(document).on("click", "img", function () {
        //     var src = $(this).attr("src");
        //     $('.bodyModalPage').html(`<img src='${src}' style='width:100%'/>`);
        //     $('#modalPage').modal('show');
        //     $('.doneButtonModalPage').text('');
        // });


        $(document).on("click", ".groupBtn", function () {
            let clicked = $(this);

            // رجّع كل الأزرار لحالتها العادية (outline)
            $(".groupBtn").each(function () {
                let btn = $(this);
                if (btn.hasClass("btn-primary")) {
                    btn.removeClass("btn-primary").addClass("btn-outline-primary");
                }
                if (btn.hasClass("btn-success")) {
                    btn.removeClass("btn-success").addClass("btn-outline-success");
                }
            });

            // حوّل الزرار المضغوط إلى عادي
            if (clicked.hasClass("btn-outline-success")) {
                clicked.removeClass("btn-outline-success").addClass("btn-success");
            }
            if (clicked.hasClass("btn-outline-primary")) {
                clicked.removeClass("btn-outline-primary").addClass("btn-primary");
            }
        });

          document.getElementById('printInvoice').addEventListener('click', function() {
      // حدد محتوى الفاتورة فقط
      var printContents = document.querySelector('#invoiceModal .modal-body').innerHTML;
      var originalContents = document.body.innerHTML;

      document.body.innerHTML = printContents; // استبدال محتوى الصفحة بالفاتورة فقط
      window.print(); // فتح نافذة الطباعة
      document.body.innerHTML = originalContents; // استرجاع الصفحة الأصلية بعد الطباعة

      location.reload(); // إعادة تحميل الصفحة لإرجاع جميع الأحداث والتفاعلات
  });
  function setActiveNav(id) {
    $(".khedma-nav-links .nav-link").removeClass("active");
    $("#" + id).addClass("active");
}

    </script>
</body>

</html>