<?php 
$isEdit   = !empty($model->id);
$isActive = ($model && $model->isActive == 'Y');

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'rack-form',
    'enableAjaxValidation'   => false,
    'enableClientValidation' => false,
));
?>

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

            <li class="col-sm-6">
                <?php echo $form->label($model, 'rack_name', array('label' => 'Rack Name')); ?>
                <?php echo $form->textField($model, 'rack_name', array(
                    'class'       => 'form-control text-bold',
                    'data-label'  => 'Rack Name',
                    'placeholder' => 'Enter rack name'
                )); ?>
            </li>

            <li class="col-sm-6">
                <?php echo $form->label($model, 'is_base_rack', array('label' => 'Set As Default')); ?>
                <?php echo $form->dropDownList($model, 'is_base_rack', array(
                    'N' => 'No',
                    'Y' => 'Yes',
                ), array(
                    'class'      => 'form-control text-bold',
                    'data-label' => 'Set As Default',
                    'id'         => 'MasInvRack_is_base_rack',
                )); ?>
            </li>

            <li class="col-sm-6">
                <?php echo $form->label($model, 'isActive', array('label' => 'Status')); ?>
                <?php echo $form->dropDownList($model, 'isActive', array(
                    'Y' => 'Active',
                    'N' => 'Inactive'
                ), array(
                    'class'      => 'form-control text-bold',
                    'data-label' => 'Status',
                    'prompt'     => 'Select status',
                    'id'         => 'MasInvRack_isActive',
                )); ?>
            </li>

            <?php echo $form->hiddenField($model, 'id'); ?>
        </ul>
    </div>
</div>

<script>
$(document).ready(function () {
    toggleRackStatusLock();

    $('#MasInvRack_is_base_rack').on('change', function () {
        toggleRackStatusLock();
    });

    function toggleRackStatusLock() {
        var isDefault = $('#MasInvRack_is_base_rack').val();
        if (isDefault === 'Y') {
            $('#MasInvRack_isActive').val('Y').prop('disabled', true);
        } else {
            $('#MasInvRack_isActive').prop('disabled', false);
        }
    }
});
</script>

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

    <?php
        $url = Yii::app()->createUrl('/masinvrack/SaveRack');
        echo CHtml::ajaxSubmitButton('Save', $url, array(
            'dataType' => 'json',
            'type'     => 'post',
            'beforeSend' => 'function(){
                if(window.rackSubmitting){ return false; }
                window.rackSubmitting = true;
                $("#loader").show();
                $(".rack-save-btn").prop("disabled", true)
                    .addClass("btn_loding")
                    .removeClass("btn-theme");
            }',
            'success' => 'function(data){
                $("#loader").hide();
                window.rackSubmitting = false;
                $(".rack-save-btn").prop("disabled", false)
                    .removeClass("btn_loding")
                    .addClass("btn-theme");

                if(data.status == "success"){
                    t8.success(data.message || "Rack Saved Successfully!");
                    $(".modal").modal("hide");
                    window.location.reload();
                } else {
                    t8.error(data.message || "Error while saving!");
                }
            }',
            'error' => 'function(){
                window.rackSubmitting = false;
                $("#loader").hide();
                $(".rack-save-btn").prop("disabled", false)
                    .removeClass("btn_loding")
                    .addClass("btn-theme");
                t8.error("Something went wrong!");
            }'
        ), array('class' => 'rack-save-btn btn btn-theme'));
    ?>
</div>

<?php $this->endWidget(); ?>