<!--FIXME ! -->
<?php
if (!$model->isNewRecord()) {
    ?>
    <script>
        $('#phones-grid_Modal').on('show', function () {
            $('h4').text('<?php echo __("PROVISIONING_GENERAL_HEADER_UPDATE") . ': ' . CHtml::encode($model->type); ?> ');
        });
    </script>
    <?php
} else {
    ?>
    <script>
        $('#phones-grid_Modal').on('show', function () {
            $('h4').text('<?php echo __("PROVISIONING_GENERAL_HEADER_CREATE"); ?> ');
        });
    </script>
    <?php
}
$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'phones-form',
    'gridId' => 'phones-grid',
    'action' => $model->isNewRecord ? array('create') : array()
        ));

$users = User::model()->findAll(array(), array('limit' => 1000));

//SORT USER ; 
$sortableHelper = array();
foreach ($users as $user) {
    $sortableHelper[$user->id] = $user->lastname;
}
asort($sortableHelper);
foreach ($sortableHelper as $id => $lastname) {
    foreach ($users as $user) {
        if ($user->id == $id) {
            $sortableHelper[$id] = $user;
        }
    }
}
$users = $sortableHelper;
$listdata = array();
foreach ($users as $user) {
    $UserSipaccounts = SipAccounts::model()->findAllByAttributes(array("user_id" => $user->id));
    $listdata[$user->getDisplayName()] = CHtml::listData($UserSipaccounts, "id", "authname");
}

$nobody = SipAccounts::model()->findByPk($model->nobody_index);

if ($nobody != NULL) {
    $listdata[__('PROVISIONING_MODEL_PHONE_SIGNED_OUT')] = array($model->nobody_index => __('PROVISIONING_MODEL_DUMMY_USER') . ': ' . $nobody->authname);
}
echo $form->dropDownListRow($model, 'sip_account_id', $listdata, array('class' => 'span3', 'data-translation-id' => 'PROVISIONING_MODEL_SIP_ACCOUNT_ID'));
echo $form->textFieldRow($model, 'descr', array('class' => 'span3', 'data-translation-id' => 'PROVISIONING_MODEL_DESCR'));


echo $form->dropdownListRow($model, 'firmware_update_key', $availebleFirmwares, array('class' => 'span3',
    'data-translation-id' => 'PHONE_MODEL_FIRMWARE_UPDATE_KEY',
    'data-translation-target' => 'FIRMWARE_UPDATE_KEY_DROPDOWN',
));

if ($this->parameterProfilesAllowed($model)) {
    echo $form->dropDownListRow(
            $model, 'provisioning_profile', $this->getProvisioningProfileData(), array(
        'class' => 'span3',
        'data-translation-target' => 'USERGROUP_MODEL_PROVISIONING_PROFILE_TRANSLATION'
    ));
}

if ($this->extraFieldsAllowed($model)) {
    echo $form->dropDownListRow(
            $model, 'group_phonebook', $this->getPhonebookData(), array(
        'class' => 'span3',
        'data-translation-id' => 'PHONE_MODEL_GROUP_PHONEBOOK'
    ));

    echo $form->dropDownListRow(
            $model, 'privat_phonebook', $this->getPhonebookData(), array(
        'class' => 'span3',
        'data-translation-id' => 'PHONE_MODEL_PRIVATE_PHONEBOOK'
    ));
}

echo CHtml::submitButton('save', array('id' => 'phoneSaveAction', 'style' => 'display:none'));

$this->endWidget();
if (Yii::app()->user->translateMode) {

    echo "<i data-translation-target = 'FIRMWARE_UPDATE_KEY_DROPDOWN' data-translation-id='PROVISIONING_GENERAL_CONTROLLER_ACTION_UPDATE_STD_FIRMWARE'></i>";
    echo '<i data-translation-target="USERGROUP_MODEL_PROVISIONING_PROFILE_TRANSLATION" data-translation-id="USERGROUP_MODEL_PROVISIONING_PROFILE"></i>';
    echo '<i data-translation-target="USERGROUP_MODEL_PROVISIONING_PROFILE_TRANSLATION" data-translation-id="GROUPS_ADVANCED_TAB_CHOOSE_PROVISIONING_PROFILE"></i>';
}
