<?php
/***
Auth: Aarti ojha
Description: This page is used for adding or editing department details. It allows administrators to select a category and enter the department name, with the option to either create a new department or update an existing one based on the provided ID.
*/

$msg = '';
$id = isset($_GET['id']) ? base64_decode($_GET['id']) : '';

// Call the function to get project data
$projectData = getProjectData($link, $id, $db);
$vProjectName = $projectData['vProjectName'];
$category = $projectData['category'];

?>

<div id="success"><?php echo $msg; ?></div>
<form id="mastersrcat" method="post" action="">
<input type="hidden" name="pId" id="id" value="<?php echo $id; ?>" />
    <span class="breadcrumb_head" style="height:37px;padding:9px 16px">
        <?php echo (!empty($id) ? "Edit" : "New"); ?> Department
    </span>
    <div class="style2-table">
        <div class="table">
            <table class="tableview tableview-2 main-form">
                <tbody>
                <?php if($msg!=''){?>
                        <tr>
                            <td colspan="2" style="color:#ffffff; width:100%; background:#00CC66; padding:5px; border:1px solid #02944b; text-align:center;">
                                <?=$msg?>
                            </td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td class="left boder0-right">
                            <label>Category <em>*</em></label>
                            <div class="log-case">
                                <?php
                                $selcountry = "SELECT * FROM $db.web_category WHERE status=1  ORDER BY category";
                                $query1 = mysqli_query($link, $selcountry);
                                ?>

                                <select name="category" id="category" class="select-styl1">
                                    <option value=''>Select Category</option>
                                    <?php
                                    while ($c_row = mysqli_fetch_array($query1)) {
                                        $ICategoryID = $c_row['id'];
                                        $VCategoryName = $c_row['category'];
                                        $catval = explode(",", $category);

                                        $sel = (in_array($ICategoryID, $catval)) ? ' selected' : '';
                                        ?>
                                        <option value='<?= $ICategoryID ?>' <?= $sel ?>>
                                            <?= $VCategoryName ?> - <?= $c_row['type'] ?> </option>
                                    <?php
                                    }
                                    ?>
                                </select>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="left">
                            <label>Department Name<em>*</em></label>
                            <div class="log-case">
                                <input name="vProjectName" id="vProjectName" type="text" value="<?= $vProjectName ?>"
                                    class="input-style1" />
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="left">
                            <center>
                                <label>&nbsp;</label>
                                <?php if ($id != '') { ?>
                                    <input name="Update" type="submit" value="Update"  id="submitclkproject" class="button-orange1"
                                        style="float: inherit;" />
                                <?php } else { ?>
                                    <input name="Submit" type="submit" value="Create"  id="submitclkproject" class="button-orange1"
                                        style="float: inherit;" />
                                <?php } ?>
                            </center>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</form>
