<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>
<div id="wrapper">
    <div class="content">
        <div class="row">
            <div class="col-md-5">
                <div class="panel_s">
                    <div class="panel-body">
                        <h4 class="no-margin">
                            <?php echo isset($edit_type) ? 'Edit Engagement Type' : 'Add Engagement Type'; ?>
                        </h4>
                        <hr class="hr-panel-heading" />

                        <?php echo form_open(admin_url('project_costing/project_costing_admin/engagement_types'), ['id' => 'engagement-type-form']); ?>
                        <?php if (isset($edit_type)) { ?>
                            <input type="hidden" name="id" value="<?php echo $edit_type['id']; ?>">
                        <?php } ?>

                        <?php
                        $categoryOptions = [];
                        foreach ($categories as $cat) {
                            $categoryOptions[] = [
                                'id'   => $cat['id'],
                                'name' => $cat['name'],
                            ];
                        }
                        echo render_select('client_category_id', $categoryOptions, ['id', 'name'], 'Client Category', isset($edit_type) ? $edit_type['client_category_id'] : '');
                        ?>

                        <?php echo render_input('name', 'Type of Engagement', isset($edit_type) ? $edit_type['name'] : ''); ?>
                        <?php echo render_input('sst_percent', 'SST Percentage (%)', isset($edit_type) ? $edit_type['sst_percent'] : '0.00', 'number', ['step' => '0.01', 'min' => '0']); ?>

                        <div class="pc-box">
                            <h5>Bidding</h5>
                            <div class="row">
                                <div class="col-md-4">
                                    <?php echo render_input('bidding_sme', 'SME %', isset($edit_type) ? $edit_type['bidding_sme'] : '50', 'number', ['step' => '0.01']); ?>
                                </div>
                                <div class="col-md-4">
                                    <?php echo render_input('bidding_admin', 'Admin %', isset($edit_type) ? $edit_type['bidding_admin'] : '25', 'number', ['step' => '0.01']); ?>
                                </div>
                                <div class="col-md-4">
                                    <?php echo render_input('bidding_profit', 'Profit %', isset($edit_type) ? $edit_type['bidding_profit'] : '25', 'number', ['step' => '0.01']); ?>
                                </div>
                            </div>
                            <div class="alert alert-info mtop10">
                                Total Bidding: <strong id="bidding-total">0.00</strong>%
                            </div>
                        </div>

                        <div class="pc-box">
                            <h5>Non-Bidding</h5>
                            <div class="row">
                                <div class="col-md-4">
                                    <?php echo render_input('non_bidding_sme', 'SME %', isset($edit_type) ? $edit_type['non_bidding_sme'] : '50', 'number', ['step' => '0.01']); ?>
                                </div>
                                <div class="col-md-4">
                                    <?php echo render_input('non_bidding_admin', 'Admin %', isset($edit_type) ? $edit_type['non_bidding_admin'] : '25', 'number', ['step' => '0.01']); ?>
                                </div>
                                <div class="col-md-4">
                                    <?php echo render_input('non_bidding_profit', 'Profit %', isset($edit_type) ? $edit_type['non_bidding_profit'] : '25', 'number', ['step' => '0.01']); ?>
                                </div>
                            </div>
                            <div class="alert alert-info mtop10">
                                Total Non-Bidding: <strong id="non-bidding-total">0.00</strong>%
                            </div>
                        </div>

                        <div class="checkbox checkbox-primary">
                            <input type="checkbox" name="status" id="status" <?php echo !isset($edit_type) || (isset($edit_type) && $edit_type['status'] == 1) ? 'checked' : ''; ?>>
                            <label for="status">Active</label>
                        </div>

                        <div class="mtop15">
                            <button type="submit" class="btn btn-primary">Save</button>
                            <?php if (isset($edit_type)) { ?>
                                <a href="<?php echo admin_url('project_costing/project_costing_admin/engagement_types'); ?>" class="btn btn-default">Cancel</a>
                            <?php } ?>
                        </div>

                        <?php echo form_close(); ?>
                    </div>
                </div>
            </div>

            <div class="col-md-7">
                <div class="panel_s">
                    <div class="panel-body">
                        <h4 class="no-margin">Engagement Types</h4>
                        <hr class="hr-panel-heading" />

                        <div class="table-responsive">
                            <table class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Client Category</th>
                                        <th>Type of Engagement</th>
                                        <th>SST %</th>
                                        <th>Bidding</th>
                                        <th>Non-Bidding</th>
                                        <th>Status</th>
                                        <th width="130">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if (!empty($types)) { ?>
                                        <?php foreach ($types as $type) { ?>
                                            <tr>
                                                <td><?php echo $type['id']; ?></td>
                                                <td><?php echo html_escape($type['client_category_name']); ?></td>
                                                <td><?php echo html_escape($type['name']); ?></td>
                                                <td><?php echo number_format((float)$type['sst_percent'], 2); ?>%</td>
                                                <td>
                                                    SME: <?php echo number_format((float)$type['bidding_sme'], 2); ?>%<br>
                                                    Admin: <?php echo number_format((float)$type['bidding_admin'], 2); ?>%<br>
                                                    Profit: <?php echo number_format((float)$type['bidding_profit'], 2); ?>%
                                                </td>
                                                <td>
                                                    SME: <?php echo number_format((float)$type['non_bidding_sme'], 2); ?>%<br>
                                                    Admin: <?php echo number_format((float)$type['non_bidding_admin'], 2); ?>%<br>
                                                    Profit: <?php echo number_format((float)$type['non_bidding_profit'], 2); ?>%
                                                </td>
                                                <td>
                                                    <span class="label label-<?php echo $type['status'] == 1 ? 'success' : 'default'; ?>">
                                                        <?php echo $type['status'] == 1 ? 'Active' : 'Inactive'; ?>
                                                    </span>
                                                </td>
                                                <td>
                                                    <a href="<?php echo admin_url('project_costing/project_costing_admin/engagement_types/' . $type['id']); ?>" class="btn btn-default btn-icon">
                                                        <i class="fa fa-pencil"></i>
                                                    </a>
                                                    <a href="<?php echo admin_url('project_costing/project_costing_admin/engagement_types?delete=' . $type['id']); ?>" class="btn btn-danger btn-icon _delete">
                                                        <i class="fa fa-remove"></i>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    <?php } else { ?>
                                        <tr>
                                            <td colspan="8" class="text-center">No engagement types found.</td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php init_tail(); ?>

<script>
function pcRecalcTotals() {
    var bidding =
        (parseFloat($('[name="bidding_sme"]').val()) || 0) +
        (parseFloat($('[name="bidding_admin"]').val()) || 0) +
        (parseFloat($('[name="bidding_profit"]').val()) || 0);

    var nonBidding =
        (parseFloat($('[name="non_bidding_sme"]').val()) || 0) +
        (parseFloat($('[name="non_bidding_admin"]').val()) || 0) +
        (parseFloat($('[name="non_bidding_profit"]').val()) || 0);

    $('#bidding-total').text(bidding.toFixed(2));
    $('#non-bidding-total').text(nonBidding.toFixed(2));
}

$(function () {
    pcRecalcTotals();

    $('#engagement-type-form').on('input', 'input', function () {
        pcRecalcTotals();
    });

    $('#engagement-type-form').on('submit', function (e) {
        var bidding = parseFloat($('#bidding-total').text()) || 0;
        var nonBidding = parseFloat($('#non-bidding-total').text()) || 0;

        if (bidding.toFixed(2) != '100.00') {
            alert('Bidding percentages must total 100.00%. Current total: ' + bidding.toFixed(2) + '%');
            e.preventDefault();
            return false;
        }

        if (nonBidding.toFixed(2) != '100.00') {
            alert('Non-Bidding percentages must total 100.00%. Current total: ' + nonBidding.toFixed(2) + '%');
            e.preventDefault();
            return false;
        }
    });
});
</script>