<?php
$isEdit = !empty($model->id);
$modelId = !empty($model->id) ? $model->id : '';
$binName = !empty($model->bin_name) ? $model->bin_name : '';
$rackId  = !empty($model->rack_id) ? $model->rack_id : '';
$isActive   = !empty($model->isActive) ? $model->isActive : '';
$isBaseBin  = !empty($model->is_base_bin) ? $model->is_base_bin : 'N';
?>

<form id="bin-form">

<div class="tab-content">
    <div class="tab-pane active">
        <ul class="clearfix form-ul material">

            <li class="col-sm-6">
                <label for="MasInvBin_bin_name">Bin Name</label>
                <input type="text" id="MasInvBin_bin_name" name="MasInvBin[bin_name]"
                    class="form-control text-bold"
                    placeholder="Enter bin name"
                    value="<?php echo CHtml::encode($binName); ?>" />
            </li>

            <li class="col-sm-6">
                <label for="MasInvBin_rack_id">Rack Name</label>
                <select id="MasInvBin_rack_id" name="MasInvBin[rack_id]" class="form-control text-bold">
                    <option value="">Select rack</option>
                    <?php foreach($rackList as $rid => $rname): ?>
                    <option value="<?php echo $rid; ?>" <?php echo ($rackId == $rid) ? 'selected' : ''; ?>>
                        <?php echo CHtml::encode($rname); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </li>

            <li class="col-sm-6">
                <label for="MasInvBin_is_base_bin">Set As Default</label>
                <select id="MasInvBin_is_base_bin" name="MasInvBin[is_base_bin]" class="form-control text-bold">
                    <option value="N" <?php echo ($isBaseBin == 'N') ? 'selected' : ''; ?>>No</option>
                    <option value="Y" <?php echo ($isBaseBin == 'Y') ? 'selected' : ''; ?>>Yes</option>
                </select>
            </li>

            <li class="col-sm-6">
                <label for="MasInvBin_isActive">Status</label>
                <select id="MasInvBin_isActive" name="MasInvBin[isActive]" class="form-control text-bold">
                    <option value="">Select status</option>
                    <option value="Y" <?php echo ($isActive == 'Y') ? 'selected' : ''; ?>>Active</option>
                    <option value="N" <?php echo ($isActive == 'N') ? 'selected' : ''; ?>>Inactive</option>
                </select>
            </li>

            <input type="hidden" id="MasInvBin_id" name="MasInvBin[id]" value="<?php echo $modelId; ?>" />
        </ul>
    </div>
</div>

<script>
$(document).ready(function () {
    toggleStatusLock();
    $('#MasInvBin_is_base_bin').on('change', function () {
        toggleStatusLock();
    });
    function toggleStatusLock() {
        if ($('#MasInvBin_is_base_bin').val() === 'Y') {
            $('#MasInvBin_isActive').val('Y').prop('disabled', true);
        } else {
            $('#MasInvBin_isActive').prop('disabled', false);
        }
    }
});
</script>

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="bin-save-btn btn btn-theme" onclick="saveBin()">Save</button>
</div>

</form>

<script>
function saveBin() {
    if (window.binSubmitting) return;

    if ($.trim($('#MasInvBin_bin_name').val()) === '') {
        t8.error('Bin Name should not be empty.');
        return;
    }
    if (!$('#MasInvBin_rack_id').val()) {
        t8.error('Rack should not be empty.');
        return;
    }
    if (!$('#MasInvBin_isActive').val()) {
        t8.error('Status should not be empty.');
        return;
    }

    // Re-enable disabled fields before submit so they get posted
    $('#MasInvBin_isActive').prop('disabled', false);

    window.binSubmitting = true;
    $('#loader').show();
    $('.bin-save-btn').prop('disabled', true).addClass('btn_loding').removeClass('btn-theme');

    $.ajax({
        url: '<?php echo Yii::app()->createUrl("masinvbin/SaveBin"); ?>',
        type: 'POST',
        data: $('#bin-form').serialize(),
        dataType: 'json',
        success: function(data) {
            $('#loader').hide();
            window.binSubmitting = false;
            $('.bin-save-btn').prop('disabled', false).removeClass('btn_loding').addClass('btn-theme');
            if (data.status === 'success') {
                t8.success(data.message || 'Bin Saved Successfully!');
                $('.modal').modal('hide');
                window.location.reload();
            } else {
                t8.error(data.message || 'Error while saving!');
            }
        },
        error: function() {
            $('#loader').hide();
            window.binSubmitting = false;
            $('.bin-save-btn').prop('disabled', false).removeClass('btn_loding').addClass('btn-theme');
            t8.error('Something went wrong!');
        }
    });
}
</script>