<?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
include("../function/func.php");
session_start();
checkSession();
$page="main";
$orderList=getOrderListAllInfo("","","");
if($_REQUEST['deleteId'] != ''){
    deleteOrder($_REQUEST['deleteId']);
}
$startDate=$_REQUEST['startdate'];
$endDate=$_REQUEST['enddate'];
if($_REQUEST['name']!=''){
    $orderList=getOrderListAllInfo($startDate,$endDate,$_REQUEST['name']);
}elseif($startDate!=''){
    $orderList=getOrderListAllInfo($startDate,$endDate,$_REQUEST['name']);
}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>桂田技術顧問報告管理系統</title>
    <link href="./css/common.css?id=<?php echo rand(0,10000);?>" rel="stylesheet">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
    <script>
        $( function() {
            $( "#startdate" ).datepicker({
                showButtonPanel: true,
                dateFormat: 'yy-mm-dd'
            });
            $( "#enddate" ).datepicker({
                showButtonPanel: true,
                dateFormat: 'yy-mm-dd'
            });
            
        } );
    </script>
  </head>
<body>
    <div class="bgBody">
        <?php include("./module/header.php");?>
        <div class="container">
            <?php include("./module/menu.php");?>
            <div class="mainContent">
                <div class="head">
                    <!--<div class="item title">報告列表</div>-->
                    <form action="main.php" method="get">
                        <div class="item search"><input type="text" name="name" placeholder="搜尋專案名稱" value="<?php echo $_REQUEST['name'];?>"></div>
                        <div class="item" style="padding-top:0.55em;margin-right:0.5em;" >開始日期</div>
                        <div class="item"><input type="text" id="startdate" name="startdate" class="date" value="<?php echo $_REQUEST['startdate'];?>"></div>
                        <div class="item" style="padding-top:0.55em;margin-right:0.5em;" >結束日期</div>
                        <div class="item"><input type="text" id="enddate" name="enddate" class="date" value="<?php echo $_REQUEST['enddate'];?>"></div>
                        <div class="item searchInput"><input type="submit" value="搜尋"></div>
                        <!--<div class="item clearButton" onclick="location.href = 'main.php';">清除</div>-->
                    </form>
                    <div class="clear"></div>
                </div>
                <div class="orderList">
                    <div class="orderItem mainOrderItem">
                        <div class="item button deleteButton" id="delete_all" style="font-size:13px;line-height:25px;margin-left:-0.2em;">批次刪除</div>
                        <div class="item projectId">報告編號</div>
                        <div class="item projectName">專案名稱</div>
                        <div class="item testDate">試驗日期</div>
                        <div class="item requester">委託單位</div>
                        <div class="item requester"></div>
                    </div>
                    <?php for($i=0;$i<count($orderList);$i++){?>
                    <div class="orderItem <?php if($i%2==0){echo "gray";}?>" >
                        <div class="item"><input type="checkbox" class="select_item" value="<?php echo $orderList[$i]['id'];?>" /></div>
                        <div class="item projectId"><?php echo $orderList[$i]['orderID'];?></div>
                        <div class="item projectName"><?php echo $orderList[$i]['projectName'];?></div>
                        <div class="item testDate"><?php echo $orderList[$i]['testDate'];?></div>
                        <div class="item requester"><?php echo $orderList[$i]['applicantName'];?></div>
                        <div class="item button blue" onclick="location.href = 'orderDetail.php?id=<?php echo $orderList[$i]['id'];?>';">修改</div>
                        <div class="item button blue" onclick="location.href = 'testItemList.php?id=<?php echo $orderList[$i]['id'];?>';">測試項目</div>
                        <div class="item button blue" onclick="location.href = 'reports.php?id=<?php echo $orderList[$i]['id'];?>';">報表</div>
                        <div class="item button deleteButton" onclick="deleteOrder(<?php echo $orderList[$i]['id'];?>);">刪除</div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script language="javascript">
    function deleteOrder(orderId){
        var yes = confirm('確認要刪除該報告資料？');
        if(yes){
            location.href = 'main.php?deleteId='+orderId
        }
    }
    <?php if($_REQUEST['deleteId'] != ''){ ?>
    alert("該報告已刪除");
    location.href = 'main.php'
    <?php } ?>
</script>
<script language="javascript">

  $("#select_all").click(function(){
    $(".select_item").prop('checked', $(this).prop('checked'));
  });

  $("#delete_all").click(function(){
    var selected = [];
    $('.select_item:checked').each(function() {
      selected.push($(this).val());
    });
    var yes = confirm('確認要刪除勾選的報告資料？');
    if(yes){
        batchDeleteOrders(selected);
    }
    
  });

  function batchDeleteOrders(orderIds){
    var count = 0;

    orderIds.forEach(function(orderId, index) {
      setTimeout(function() {
        $.ajax({
          url: 'deleteOrderBatch.php',
          type: 'POST',
          data: { 'orderId': orderId },
          success: function() {
            count++;
            if(count === orderIds.length) {
              alert(orderIds.length + " 報告已刪除");
              location.href = 'main.php'
            }
          }
        });
      }, 300 * index);
    });
  }
</script>