<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
$name = "";
$location = "";
$contact = "";
$email = "";
$gst = "";
$ntn = "";
$status = "";
$wht = "";
$exemption_date = "";
$category_id = "";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
      <script>
        $(function () {
            $('#datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
            
        });
    </script>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Supplier Setup</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (isset($_GET['view'])) { ?>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add <i
                            class="mdi-content-add-box" style="font-size:12px;"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['code'] = __SUPPLIER__;
                $data['name'] = $name;
                $data['location'] = $location;
                $data['contact'] = $contact;
                $data['email'] = $email;
                $data['gst'] = $gst;
                $data['ntn'] = $ntn;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
              	$data['wht'] = $wht;
				$data['exemption_date'] = date('Y-m-d', strtotime($exemption_date));
              	$data['category_id'] = $category_id;

                $usercheck = SupplierSetup::CheckSupplier($name, 0);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter customer name");
                if ((strlen($location) == 0))
                    array_push($errors, "Please enter location");
                if ((strlen($contact) == 0))
                    array_push($errors, "Please enter contact");
                //if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) || ((strlen($email) > 50)))
                //array_push($errors, "Invalid email format");
                if ($usercheck)
                    array_push($errors, "customer name already exist");
            if (count($errors) == 0) {
                $q = SupplierSetup::AddSupplierSetup($data);

            if ($q) {
                $output = '';
                //header("Location: ?view&sucess"); ?>
                <script>window.location.href = '?view&sucess'</script>
            <?php
            echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            } else {
                $output = '';
                echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            }
            } else {
                //Prepare errors for output
                foreach ($errors as $val) {
                    $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                }
            }
            }
            ?>
                <div class="row">
                    <form action="" method="post" id="supplier_setup_form" novalidate="novalidate">
                        <div class="col s12">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="name" maxlength="250"
                                               name="name"
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Supplier Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                                <textarea class="materialize-textarea" id="location"
                                                          name="location"><?php echo $location; ?></textarea>
                                        <label for="location" class="control-label">Location *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="contact" name="contact"
                                               value='<?php echo $contact; ?>'/>
                                        <label for="contact" class="control-label">Contact *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="email" name="email"
                                               maxlength="50"
                                               value='<?php echo $email; ?>'/>
                                        <label for="email" class="control-label">Email</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="gst" name="gst"
                                               maxlength="20"
                                               value='<?php echo $gst; ?>'/>
                                        <label for="gst" class="control-label">GST</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="ntn" name="ntn"
                                               maxlength="15"
                                               value='<?php echo $ntn; ?>'/>
                                        <label for="ntn" class="control-label">NTN</label>
                                    </div>
                                </div>
                              <div class="row margin">
                                        <div class="input-field col s12">
                                            <?php
                                            $filterCategory = " ORDER BY name";
                                            $qryCategory = TaxCategorySetup::GetTaxCategorySetupList($filterCategory);
                                            if ($qryCategory) {
                                                echo '<select id="category_id" name="category_id" class="form-control" required>';
                                                foreach ($qryCategory as $qery) {
                                                    if ($qery->id == $category_id) {
                                                        $select = "selected";
                                                    } else {
                                                        $select = "";
                                                    }
                                                    echo '<option ' . $select . ' value=' . $qery->id . '>' . $qery->name . '</option>';
                                                }
                                                echo '</select>';
                                            }
                                            ?>
                                            <label for="category_id" class="control-label">Tax Category *</label>
                                        </div>
                                    </div>
                              
                              <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="number" id="wht" name="wht"
                                                   maxlength="15"
                                                   value='<?php echo $wht; ?>'/>
                                            <label for="wht" class="control-label">WHT%</label>
                                        </div>
                                    </div>
                              
                               
                              
                              
                                  
                                  <div class="row margin" id="exemption_date_row">
                                        <div class="input-field col s12">
                                            <label class="control-label">Exemption Date *</label>
                                              <input class="form-control" required type="text" id="datepicker"
                                                     name="exemption_date"
                                                     placeholder="<?= date('01-01-Y') ?>"
                                                     value='<?php echo $exemption_date; ?>'/>
                                        </div>
                                    </div>
                              
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="inactive" <?php if ($status == 'inactive') {
                                                echo "selected='selected'";
                                            } ?>>Inactive
                                            </option>
                                        </select>
                                        <label for="status" class="control-label">Status</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit
                                </button>
                                <a href="supplier_setup.php?view">
                                    <button type="button" class="btn waves-effect waves-light">Cancel</button>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <?php
            } // add close

            if (isset($_GET['view'])) {

                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div id='card-alert' class='card green lighten-5'>
                        <div class='card-content green-text'>
                            <p>Successfully updated!</p>
                        </div>
                        <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <?php if (isset($_GET['error'])) { ?>
                    <div id='card-alert' class='card orange lighten-5'>
                        <div class='card-content orange-text'>
                            <p>WARNING : <strong>Attention! </strong>Error occurred!</p>
                        </div>
                        <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <div class="table-datatables">
                    <table id="data-table-simple" class="responsive-table display">
                        <thead>
                        <tr>
                            <th>Action</th>
                            <th>Supplier Name</th>
                            <th>Location</th>
                            <th>Contact</th>
                            <th>Email</th>
                          	<th>WHT</th>
                          	<th>Tax Category</th>
                          	<th>Exemption Date</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Supplier Name</th>
                            <th>Location</th>
                            <th>Contact</th>
                            <th>Email</th>
                          	<th>WHT</th>
                          	<th>Tax Category</th>
                          	<th>Exemption Date</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = SupplierSetup::GetSupplierSetupList();
              			$tax_category = TaxCategorySetup::GetTaxCategorySetupById($qry->category_id);
                        if ($qry) {
                            foreach ($qry as $qery) {
                              $tax_category = TaxCategorySetup::GetTaxCategorySetupById($qery->category_id);
                                ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <a href='?edit&id=<?php echo $qery->id; ?>&v' title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                            <a href="?edit&id=<?php echo $qery->id; ?>" title="Edit">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon orange"><i
                                                            class="mdi-image-edit"></i></span>
                                            </a>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->id; ?>&v" title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo ucwords($qery->name); ?></td>
                                    <td><?php echo ucwords($qery->location); ?></td>
                                    <td><?php echo $qery->contact; ?></td>
                                    <td><?php echo $qery->email; ?></td>
                                  	<td><?php echo $qery->wht; ?></td>
                                  	<td><?php echo $tax_category->name; ?></td>
                                  	<td>
                                        <?php 
                                        echo (!empty($qery->exemption_date) && 
                                               $qery->exemption_date != '0000-00-00' && 
                                               $qery->exemption_date != '1970-01-01') 
                                            ? date('d-m-Y', strtotime($qery->exemption_date)) 
                                            : '';  
                                        ?>
                                    </td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                    <td><?php echo $qery->datetime; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];

                $data['name'] = $name;
                $data['location'] = $location;
                $data['contact'] = $contact;
                $data['email'] = $email;
                $data['gst'] = $gst;
                $data['ntn'] = $ntn;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
              	$data['wht'] = $wht;
              	$data['exemption_date'] = date('Y-m-d', strtotime($exemption_date));
              	$data['category_id'] = $category_id;
                $usercheck = SupplierSetup::CheckSupplier($name, $id);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter customer name");
                if ((strlen($location) == 0))
                    array_push($errors, "Please enter location");
                if ((strlen($contact) == 0))
                    array_push($errors, "Please enter contact");

                if ($usercheck)
                    array_push($errors, "Supplier name already exist");

                if (count($errors) == 0) {
                    $q = SupplierSetup::UpdateSupplierSetup($id, $data);

                    if ($q) {
                        $output = '';
                        //header("Location: ?view&sucess"); ?>
                        <script>window.location.href = '?view&sucess'</script>
                        <?php
                        echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    } else {
                        $output = '';
                        echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                }
            }
            if (isset($_GET['edit'])) {
                $id = $_GET['id'];
                if (empty($_POST)) {
                    $qry = SupplierSetup::GetSupplierSetupById($id);
                    $name = $qry->name;
                    $location = $qry->location;
                    $contact = $qry->contact;
                    $email = $qry->email;
                    $gst = $qry->gst;
                    $ntn = $qry->ntn;
                  	$wht = $qry->wht;
                  	$category_id = $qry->category_id;
               //   echo $wht;
                  	
                    $exemption_date = (!empty($qry->exemption_date) && $qry->exemption_date != '0000-00-00') 
                    ? date('d-m-Y', strtotime($qry->exemption_date)) 
                    : '';
                    $status = $qry->status;
                  
                   $taxCat = CustomerCategorySetup::GetCustomerCategorySetupById($category_id);
                    $taxCategory = $taxCat->name;
                }
                if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                    ?>
                    <style>
                        h4 {
                            float: left;
                            margin: 0;
                            width: 30%;
                        }

                        p {
                            float: left;
                        }

                        .activity-icon {
                            margin: 0;
                        }
                    </style>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <div class="row">
                                    <div class="col s6">Supplier Name:</div>
                                    <div class="col s6"><?php echo $name; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Supplier Code:</div>
                                    <div class="col s6"><?php echo $code; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Location:</div>
                                    <div class="col s6"><?php echo $location; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Contact:</div>
                                    <div class="col s6"><?php echo $contact; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Email:</div>
                                    <div class="col s6"><?php echo $email; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">GST:</div>
                                    <div class="col s6"><?php echo $gst; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">NTN:</div>
                                    <div class="col s6"><?php echo $ntn; ?></div>
                                </div>
                              	<div class="row margin">
    <div class="input-field col s12">
        <input class="form-control" 
               type="number" 
               id="wht" 
               name="wht"
               step="0.01" 
               value="<?php echo number_format((float)$wht, 2, '.', ''); ?>" />
        <label for="wht" class="control-label active">WHT%</label>

    </div>
</div>


                              	<div class="row">
                                    <div class="col s6">Category:</div>
                                    <div class="col s6"><?php echo $customerCategory; ?></div>
                                </div>
                              	<div class="row">
                                    <div class="col s6">Exemption Expiry:</div>
                                    <div class="col s6"><?php echo $exemption_date; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Status</div>
                                    <div class="col s6">
                                        <?php
                                        if ($status == 'active') {
                                            echo "Active";
                                        } else if ($status == 'inactive') {
                                            echo "Inactive";
                                        } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } else {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <form action="?edit&id=<?php echo $id ?>" method="post" id="supplier_setup_form"
                              novalidate="novalidate">
                            <div class="col s12">
                                <div class="col s8">
                                    <div class="row">
                                        <?php echo $output; ?>
                                    </div>
                                    <div class="row margin">
                                        <span>(*) Mark Fields are mandatory</span><br/><br/>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="name" maxlength="250"
                                                   name="name"
                                                   value="<?php echo $name; ?>"/>
                                            <label for="name" class="control-label">Supplier Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                                <textarea class="materialize-textarea" id="location"
                                                          name="location"><?php echo $location; ?></textarea>
                                            <label for="location" class="control-label">Location *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="contact" name="contact"
                                                   value='<?php echo $contact; ?>'/>
                                            <label for="contact" class="control-label">Contact *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="email" name="email"
                                                   maxlength="50"
                                                   value='<?php echo $email; ?>'/>
                                            <label for="email" class="control-label">Email </label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="gst" name="gst"
                                                   maxlength="15"
                                                   value='<?php echo $gst; ?>'/>
                                            <label for="gst" class="control-label">GST</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="ntn" name="ntn"
                                                   maxlength="15"
                                                   value='<?php echo $ntn; ?>'/>
                                            <label for="ntn" class="control-label">NTN</label>
                                        </div>
                                    </div>
                                  
                                            
                                  <div class="row margin">
                                        <div class="input-field col s12">
                                            <?php
                                            $filterCategory = " ORDER BY name";
                                            $qryCategory = TaxCategorySetup::GetTaxCategorySetupList($filterCategory);
                                            if ($qryCategory) {
                                                echo '<select id="category_id" name="category_id" class="form-control" required>';
                                                foreach ($qryCategory as $qery) {
                                                    if ($qery->id == $category_id) {
                                                        $select = "selected";
                                                    } else {
                                                        $select = "";
                                                    }
                                                    echo '<option ' . $select . ' value=' . $qery->id . '>' . $qery->name . '</option>';
                                                }
                                                echo '</select>';
                                            }
                                            ?>
                                            <label for="category_id" class="control-label">Tax Category *</label>
                                        </div>
                                    </div>
                                  
                                <div class="row margin">
    <div class="input-field col s12">
        <input class="form-control" 
               type="number" 
               id="wht" 
               name="wht"
               step="0.01" 
               value="<?php echo number_format((float)$wht, 2, '.', ''); ?>" />
        <label for="wht" class="control-label active">WHT%</label>

    </div>
</div>


                                  
                        
                                  
                                  
                                  
                                  
                                  <div class="row margin" id="exemption_date_row">
                                        <div class="input-field col s12">
                                            <label class="control-label">Exemption Date *</label>
                                              <input class="form-control" required type="text" id="datepicker"
                                                     name="exemption_date"
                                                     placeholder="<?= date('01-01-Y') ?>"
                                                     value='<?php echo $exemption_date; ?>'/>
                                        </div>
                                    </div>
                                  
                                    <div class="row margin">
                                        <div id="status" class="input-field col s12">
                                            <label class="control-label">Status</label><br>
                                            <select class="form-control" name="status">
                                                <option value="active" <?php if ($status == 'active') {
                                                    echo "selected='selected'";
                                                } ?>>Active
                                                </option>
                                                <option value="inactive" <?php if ($status == 'inactive') {
                                                    echo "selected='selected'";
                                                } ?>>Inactive
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="row margin">
                                <div class="input-field col s12">
                                    <?php if ($_SESSION['sess_role'] == 'VE' || $_SESSION['sess_role'] == 'VEC') { ?>
                                        <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">
                                            Submit
                                        </button>
                                    <?php } ?>
                                    <a href="supplier_setup.php?view">
                                        <button type="button" class="btn waves-effect waves-light">Cancel</button>
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                <?php } ?>
                <?php
            }
            ?>

        </div>
    </div>
</div>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>

<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
<script>
    $(document).ready(function () {
        function toggleExemptionDateField() {
            const selectedCategory = $('#category_id').find('option:selected').text().toLowerCase();
            if (selectedCategory === 'taxable') {
                $('#exemption_date_row').hide(); // Hide the exemption date field
            } else {
                $('#exemption_date_row').show(); // Show the exemption date field
            }
        }

        // Initial check
        toggleExemptionDateField();

        // Trigger check on change
        $('#category_id').on('change', function () {
            toggleExemptionDateField();
        });
    });
  
$(document).ready(function () {
  function toggleWHTField(triggeredByChange) {
    const selectedCategory = ($('#category_id').find('option:selected').text() || '').trim().toLowerCase();
    const isExempt = (selectedCategory === 'exempted' || selectedCategory === 'undertaking');
    const currentVal = $('#wht').val();

    $('#wht').prop('readonly', false);

    // Only auto-set to 0.00 if (a) user is adding OR they just changed the category,
    // AND (b) the field is currently empty. Never overwrite the DB value on initial edit load.
    if (isExempt) {
      if ((!isEdit || triggeredByChange) && (currentVal === '' || currentVal === null)) {
        $('#wht').val('0.00');
        if (window.M && M.updateTextFields) M.updateTextFields();
      }
    }
  }

  // Initial run: don't treat as a user change
  toggleWHTField(false);

  // On category change: allow conditional override
  $('#category_id').on('change', function () {
    toggleWHTField(true);
  });
});
  

  document.addEventListener('DOMContentLoaded', function() {
    M.updateTextFields(); // refresh Materialize labels
});
  var isEdit = <?php echo isset($_GET['edit']) && empty($_GET['v']) ? 'true' : 'false'; ?>;

</script>
</body>
</html>