<?php

$name = 'phoneKey[' . $data->name . '][' . $column->name . ']';

$htmlId = str_replace(".", "_", $name);
$htmlId = str_replace("[", "_", $htmlId);
$htmlId = str_replace("]", "", $htmlId);
$htmlId = str_replace(" ", "_", $htmlId);

$htmlOptions = array('name' => $name, "id" => $htmlId);



if ($data->locked) {
    $htmlOptions['disabled'] = 'disabled';
}

if ($column->name == 'function') {
    $function = $data->function;
    if (empty($function) && !$data->isNewRecord) {
        $function = '0';
    }
    $htmlOptions['data-translation-target'] = 'TEST';
    $htmlOptions['onChange'] = 'toggleKeySelection(this)';
    $availableFunctions = $this->getSortedAvialableFunctions($data); //$data->availableFunctions;
    echo CHtml::dropDownList($name, $function, $availableFunctions, $htmlOptions);
    unset($htmlOptions['onChange']);
}

if ($column->name == 'number_data') {
    $attrWithoutDataField = array('no', '', '0', 'X05', 'XO8', 'X10', 'X12');
    $attrWithDropdown = array('X01', 'X03', 'X13');

    if (in_array($data->function, $attrWithDropdown)) {
        $dataList = array();
        if ($data->function == 'X01') {
            $dataList = $this->getForwardList();
        } else if ($data->function == 'X03') {
            $dataList = $this->getUserQueues();
        } else if ($data->function == 'X13') {
            $dataList = $this->getUserCallGroup();
        }

        if (isset($dataList[$data->number_data])) {
            echo CHtml::dropDownList($name, $data->number_data, $dataList, $htmlOptions);
        } else {
            echo CHtml::textField($name, $data->number_data, $htmlOptions);
        }
    } else {
        $textFieldHtmlOptions = $htmlOptions;
        if (in_array($data->function, $attrWithoutDataField)) {
            $textFieldHtmlOptions['disabled'] = 'disabled';
        }
        echo CHtml::textField($name, $data->number_data, $textFieldHtmlOptions);
    }

    echo CHtml::hiddenField('phoneKey[' . $data->name . '][phone]', $data->phone, $htmlOptions);
    echo CHtml::hiddenField('phoneKey[' . $data->name . '][name]', $data->name, $htmlOptions);
    echo CHtml::hiddenField('phoneKey[' . $data->name . '][key_idx]', $data->key_idx, $htmlOptions);
}

if ($column->name == 'label') {
    if ($data->function == 'no' || empty($data->function)) {
        $htmlOptions['disabled'] = 'disabled';
    }
    echo CHtml::activeTextField($data, $column->name, $htmlOptions);
}

if ($column->name == 'locked') {
    if ($data->locked) {
        echo "<div class='icon-lock'></div>";
    } else {
        echo "<div class='icon-unlock'></div>";
    }
}
