<?php 
require("../connection.php");
include("../user.php");

$user_id = $_SESSION['user'];
$username = $_SESSION['user_name'];
$branch = $_SESSION['division_name'];

$row = mysqli_query($con, "SELECT * FROM `material_request` ORDER BY id DESC");

?>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo TITLE;?></title>
    <meta name="description" content="Ela Admin - HTML5 Admin Template">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="../images/bluebase.png">
    <link rel="shortcut icon" href="../images/bluebase.png">

    <link rel="stylesheet" href="../css/normalize.min.css">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/themify-icons.css">
    <link rel="stylesheet" href="../css/pe-icon-7-stroke.min.css">
    <link rel="stylesheet" href="../css/flag-icon.min.css">
    <link rel="stylesheet" href="../assets/css/cs-skin-elastic.css">
    <link rel="stylesheet" href="../assets/css/style.css">
    <link href="../css/chartist.min.css" rel="stylesheet">
    <link href="../css/jqvmap.min.css" rel="stylesheet">
    <link href="../css/weather-icons.css" rel="stylesheet" />
    <link href="../css/fullcalendar.min.css" rel="stylesheet" />

    <script src="../js/jquery-1.12.4.min.js"></script>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/jquery.dataTables.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/jquery.matchHeight.min.js"></script>
    <script src="../assets/js/main.js"></script>
    <script src="../js/jquery-3.3.1.min.js"></script>

    <!-- SweetAlert2 CSS and JS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <style>
        /* Your existing styles */
    </style>

    <link rel="stylesheet" href="/project_temp/datatable/css/jquery.dataTables.min.css">
    <script src="/project_temp/datatable/js/jquery.datatables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/project_temp/button/css/buttons.css" />

    <script>
    $(document).ready(function() {
        $('#example').DataTable();
    });


function handleReject_admin(id)
{
    Swal.fire({
            title: 'Reject Material Request',
            input: 'text',
            inputLabel: 'Enter Remarks',
            inputPlaceholder: 'Enter your remarks here...',
            showCancelButton: true,
            confirmButtonText: 'Reject',
            preConfirm: (remark) => {
                if (!remark) {
                    Swal.showValidationMessage('Remark is required');
                }
                return { remark: remark };
            }
        }).then((result) => {
            if (result.isConfirmed) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = 'admin_update_material_sts.php';
                
                const idField = document.createElement('input');
                idField.type = 'hidden';
                idField.name = 'id';
                idField.value = id;
                
                const actionField = document.createElement('input');
                actionField.type = 'hidden';
                actionField.name = 'action';
                actionField.value = 'reject';
                
                const remarkField = document.createElement('input');
                remarkField.type = 'hidden';
                remarkField.name = 'remark';
                remarkField.value = result.value.remark;
                
                form.appendChild(idField);
                form.appendChild(actionField);
                form.appendChild(remarkField);
                
                document.body.appendChild(form);
                form.submit();
            }
        });
}



    function handleReject(id) {
        Swal.fire({
            title: 'Reject Material Request',
            input: 'text',
            inputLabel: 'Enter Remarks',
            inputPlaceholder: 'Enter your remarks here...',
            showCancelButton: true,
            confirmButtonText: 'Reject',
            preConfirm: (remark) => {
                if (!remark) {
                    Swal.showValidationMessage('Remark is required');
                }
                return { remark: remark };
            }
        }).then((result) => {
            if (result.isConfirmed) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = 'update_material_status.php';
                
                const idField = document.createElement('input');
                idField.type = 'hidden';
                idField.name = 'id';
                idField.value = id;
                
                const actionField = document.createElement('input');
                actionField.type = 'hidden';
                actionField.name = 'action';
                actionField.value = 'reject';
                
                const remarkField = document.createElement('input');
                remarkField.type = 'hidden';
                remarkField.name = 'remark';
                remarkField.value = result.value.remark;
                
                form.appendChild(idField);
                form.appendChild(actionField);
                form.appendChild(remarkField);
                
                document.body.appendChild(form);
                form.submit();
            }
        });
    }
    </script>
</head>
<body>
    <?php require('../sidemenu.php');?>

    <div id="right-panel" class="right-panel">
        <?php require('../header.php');?>

        <div class="content">
            <div class="animated fadeIn">
                <div style="background-color:white;margin-bottom:10px;">
                    <strong style="color:blue"><center>LIST OF MATERIAL REQUEST</center></strong>
                </div>
              
                <table id="example" class="table table-striped table-bordered" style="text-transform:uppercase;">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Material Name</th>
                            <th>Material Description</th>
                            <th>Measurement Type</th>
                            <th>Measurement</th>
                            <th>Action</th>
                            <th>Remark</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php 
                    $s = 1;
                    while($type = mysqli_fetch_array($row)) {
                        ?>
                        <tr>
                            <td><?php echo $s++ ;?></td>
                            <td><?php echo $type['material_name'];?></td>
                            <td><?php echo $type['material_description'];?></td>
                            <td><?php echo $type['measurement_type'];?></td>
                            <td><?php echo $type['measurement'];?></td>
                            <?php
                            if($user_id==1)
                            {

                                $stat = $type['sts'];				
                                if($stat == "1") {
                                    ?>
                                    <td class="text-left">
                                        <form method="POST" action="admin_update_material_sts.php">
                                            <input type="hidden" name="id" value="<?php echo $type['id']; ?>">
                                            <button type="submit" name="action" value="approve" class="btn btn-success">Approve</button>
                                            <button type="button" onclick="handleReject_admin('<?php echo $type['id']; ?>')" class="btn btn-danger">Reject</button>
                                        </form>
                                    </td>
                                    <td>-</td>
                                    <?php
                                } else if($stat == "1") {
                                    ?>
                                    <td>
                                        <label for="" style="color:red;font-weight:600">Approved by Purchase Dept/Waiting for Admin Approval</label>
                                    </td>
                                    <td>-</td>
                                    <?php
                                } else if($stat == "2") {
                                    ?>
                                    <td>
                                        <label for="" style="color:red;font-weight:600">Rejected by Purchase Dept</label>
                                    </td>
                                    <td><?php echo $type['remark']; ?></td>
                                    <?php
                                } else if($stat == "3") {
                                    ?>
                                    <td>
                                        <label for="" style="color:green;font-weight:600">Approved by Admin</label>
                                    </td>
                                    <td>-</td>
                                    <?php
                                } else if($stat == "4") {
                                    ?>
                                    <td>
                                        <label for="" style="color:red;font-weight:600">Rejected by Admin</label>
                                    </td>
                                    <td><?php echo $type['remark']; ?></td>
                                    <?php
                                }
                                else if($stat == "5") {
                                    ?>
                                    <td>
                                        <label for="" style="color:green;font-weight:600">Approved by Associative</label>
                                    </td>
                                    <td>-</td>
                                    <?php
                                }
                                else if($stat == "6") {
                                    ?>
                                    <td>
                                        <label for="" style="color:red;font-weight:600">Rejected by Associative</label>
                                    </td>
                                    <td><?php echo $type['remark']; ?></td>
                                    <?php
                                }

                            }
                            else if($user_id==3)
                            {
                            $stat = $type['sts'];				
                            if($stat == "0") {
                                ?>
                                <td class="text-left">
                                    <form method="POST" action="update_material_status.php">
                                        <input type="hidden" name="id" value="<?php echo $type['id']; ?>">
                                        <button type="submit" name="action" value="approve" class="btn btn-success">Approve</button>
                                        <button type="button" onclick="handleReject('<?php echo $type['id']; ?>')" class="btn btn-danger">Reject</button>
                                    </form>
                                </td>
                                <td>-</td>
                                <?php
                            } else if($stat == "1") {
                                ?>
                                <td>
                                    <label for="" style="color:red;font-weight:600">Approved by Purchase Dept/Waiting for Admin Approval</label>
                                </td>
                                <td>-</td>
                                <?php
                            } else if($stat == "2") {
                                ?>
                                <td>
                                    <label for="" style="color:red;font-weight:600">Rejected by Purchase Dept</label>
                                </td>
                                <td><?php echo $type['remark']; ?></td>
                                <?php
                            } else if($stat == "3") {
                                ?>
                                <td>
                                    <label for="" style="color:green;font-weight:600">Approved by Admin</label>
                                </td>
                                <td>-</td>
                                <?php
                            } else if($stat == "4") {
                                ?>
                                <td>
                                    <label for="" style="color:red;font-weight:600">Rejected by Admin</label>
                                </td>
                                <td><?php echo $type['remark']; ?></td>
                                <?php
                            }
                            else if($stat == "5") {
                                ?>
                                <td>
                                    <label for="" style="color:green;font-weight:600">Approved by Associative</label>
                                </td>
                                <td>-</td>
                                <?php
                            }
                            else if($stat == "6") {
                                ?>
                                <td>
                                    <label for="" style="color:red;font-weight:600">Rejected by Associative</label>
                                </td>
                                <td><?php echo $type['remark']; ?></td>
                                <?php
                            }
                        }
                            ?>
                        </tr>
                    <?php
                    }
                    ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>

<?php require("../mainjavascript.js"); ?>
