<?php

$this->placeholder('breadcrumb')->set('<a href="/accounting.php">Accounting</a> > Release Expense');
$applyToStatus = isset($this->rma->apply_to_period->period_status) ?
    $this->rma->apply_to_period->period_status : '';
$fieldDisable = '';
if ($applyToStatus == 'closed' || $applyToStatus == 'processing' || $this->hasReleaseExpensesViewAccess) {
    $fieldDisable = 'disabled';
}
$open_periods = isset($this->openPeriods) ? $this->openPeriods : array();
?>
<div class="panelHeader">
    <h3>Edit Release Expense</h3>
</div>
<div class="panelContent">
    <form action="/releaseexpenses/updatereleaseexpense" method="post" onsubmit="return validateForm()">
        <input type="hidden" name="releaseManualAdjustmentId"
               value="<?php echo $this->rma->release_manual_adjustment_id ;?>">
        <input type="hidden" name="applyToStatus" value="<?php echo $applyToStatus; ?>">
        <table class="form full">
            <tr>
                <th class="required"><span class="required">*</span>UPC:</th>
                <td>
                    <input type="text" class="small" name="upc" id="upc"
                        value="<?php echo $this->rma->release->upc ;?>" onblur="displayVendorId()"
                        <?php echo $fieldDisable; ?>>
                </td>
            </tr>
            <tr>
                <th class="required"><span class="required">*</span>Vendor ID:</th>
                <td>
                    <input type="text" class="small" name="vendorId" id="vendorId"
                        value="<?php echo $this->rma->vendor_id ;?>" <?php echo $fieldDisable; ?>>
                </td>
            </tr>
            <tr>
                <th class="required"><span class="required">*</span>Amount:</th>
                <td>
                    <input type="text" class="small" name="amount" id="amount"
                        value="<?php echo $this->rma->amount;?>" <?php echo $fieldDisable; ?>>
                </td>
            </tr>
            <tr>
                <th><span class="required">*</span>Currency:</th>
                <td>
                    <select class="small" name="currencyId" id="currencyId" <?php echo $fieldDisable; ?>>
                        <option value="">Select One</option>
                        <?php
                        foreach ($this->currencyList as $currency) { ?>
                            <option value="<?= $currency["id"];?>"
                                <?php echo (($currency['id'] == $this->rma->currency->id)
                                    ? 'selected="selected"'
                                    : '')
                                ?>>
                                <?=$currency['currency_name'] . ' (' . $currency["ISO_4217_code"] . ')'?>
                            </option>
                        <?php
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th><span class="required">*</span>Adjust For Period:</th>
                <td>
                    <?php
                        if ($applyToStatus == 'open') {
                            ?>
                            <select name="adjustForPeriodId" class="small" required="required" <?php if ($this->hasReleaseExpensesViewAccess) { echo $fieldDisable; } ?>>
                            <?php
                            if (is_array($open_periods) && !empty($open_periods)) {
                                foreach ($open_periods as $key => $value) {
                                    ?>
                                    <option value="<?php echo $value['period_id']; ?>"
                                        <?php echo (($value['period_id'] == $this->rma->adjust_for_period->id)
                                            ? 'selected="selected"'
                                            : '')
                                        ?>>
                                        <?php echo $value['year'] . ' M ' . $value['month']; ?>
                                    </option>
                                <?php
                                }
                            }
                            ?>
                            </select>
                            <?php
                        } else {
                            ?>
                            <select name="year" class="small" <?php echo $fieldDisable; ?>>
                            <?php for ($year = 1999; $year <= intval(substr(date('Y-m-d'), 0, 4)); $year++) {?>
                                <option value="<?php echo $year;?>"
                                    <?php echo (($year ==  $this->rma->adjust_for_period->year)
                                        ? 'selected="selected"'
                                        : '')
                                    ?>>
                                    <?php echo $year;?>
                                </option>
                            <?php }?>
                            </select>
                            &nbsp;M&nbsp;
                            <select name="month" class="small" <?php echo $fieldDisable; ?>>
                            <?php for ($month = 1; $month <= 12; $month++) {?>
                                <option value="<?php echo $month;?>"
                                    <?php echo (($month == $this->rma->adjust_for_period->month)
                                        ? 'selected="selected"'
                                        : '')
                                    ?>>
                                    <?php echo $month;?>
                                </option>
                            <?php }?>
                            </select>
                            <?php
                        }
                    ?>
                </td>
            </tr>
            <tr>
                <th class="required"><span class="required">*</span>Apply To:</th>
                <td>
                    <?php
                        if ($applyToStatus == 'open') {
                            ?>
                            <select name="applyToPeriodId" class="small" required="required" <?php if ($this->hasReleaseExpensesViewAccess) { echo $fieldDisable; } ?>>
                            <?php
                            if (is_array($open_periods) && !empty($open_periods)) {
                                foreach ($open_periods as $key => $val) {
                                    ?>
                                    <option value="<?php echo $val['period_id']; ?>"
                                        <?php echo (($val['period_id'] == $this->rma->apply_to_period->id)
                                            ? 'selected="selected"'
                                            : '')
                                        ?>>
                                        <?php echo $val['year'] . ' M ' . $val['month']; ?>
                                    </option>
                                <?php
                                }
                            }
                            ?>
                            </select>
                        <?php
                        } else {
                            ?>
                            <input type="text" class="small" name="applyTo"
                                value="<?php echo $this->rma->apply_to_period->year . ' M ' .
                                $this->rma->apply_to_period->month;?>" <?php echo $fieldDisable; ?>>
                        <?php
                        }
                    ?>
                </td>
            </tr>
            <tr>
                <th class="required"><span class="required">*</span>Category:</th>
                <td>
                    <select class="small" name="categoryId" id="categoryId" <?php if ($this->hasReleaseExpensesViewAccess) { echo $fieldDisable; } ?>>
                        <option value="">Select One</option>
                        <?php
                        foreach ($this->categoryList->manual_adjustment_categories as $category) {
                            ?>
                            <option value="<?php echo $category->category_id;?>"
                                <?php echo(($category->category_id == $this->rma->category->id)
                                    ? 'selected="selected"'
                                    : '')
                                ?>>
                                <?php echo $category->category;?>
                            </option>
                        <?php
                        } ?>
                    </select>
                </td>
            </tr>
        </table>
        <table class="half form">
            <tr>
                <th class="required"><span class="required">*</span>Comment:</th>
                <td><textarea name="comment" id="comment" <?php if ($this->hasReleaseExpensesViewAccess) { echo $fieldDisable; } ?>><?=htmlspecialchars($this->rma->description)?></textarea></td>
            </tr>
        </table>
        <?php if (!$this->hasReleaseExpensesViewAccess) { ?>
        <div class="panelButtons">
            <input type="submit" value="Update"/>
        </div>
        <?php } ?>
    </form>
</div>
