<?php
/*=======================================================\
|                        FrontHrm                        |
|--------------------------------------------------------|
|   Creator: Phương <trananhphuong83@gmail.com>          |
|   Date :   09-Jul-2017                                 |
|   Description: Frontaccounting Payroll & Hrm Module    |
|   Free software under GNU GPL                          |
|                                                        |
\=======================================================*/

function employee_list($name, $id = null, $spec_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
	global $path_to_root,$Ajax;
    if($id == null){
        $id = get_post($name);
    }
    $sql = "SELECT emp_id, concat(emp_first_name,' ',emp_last_name) as name FROM " . TB_PREF . "employee where emp_id=".  db_escape($id);
    $res = db_query($sql);
    $row = db_fetch($res);
    
    $placeholder = "Select Employee";
    $allocs = array();
    if (!empty($spec_option)) {
        $allocs[''] = $spec_option;
        $placeholder = $spec_option;
    }
    if (!empty($row['emp_id'])) {
        $allocs[$row['emp_id']] = $row['name'];
    }
    $_selector = array_selector(
            $name, $id, $allocs, array('select_submit' => $submit_on_change,'style'=>'width:100%')
    );
    $select_js = "$('select[name=\"$name\"]').select2({
        minimumInputLength: 1,
        allowClear: true,
        placeholder: '".$placeholder."',
        ajax: {
          url: '$path_to_root/modules/api/hrm?start=0&length=1000',
          dataType: 'json',
          processResults: function (data) {
            return {
              results: data.items
            };
          }
        }
      });";
    if (in_ajax()) {
        $Ajax->addScript(true, $select_js);
    } else
        add_js_source($select_js);
    $Ajax->activate('_page_body');

    return $_selector;
}

//--------------------------------------------------------------------------

function employee_list_cells($label, $name, $id = null, $all_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
	if ($label != null)
		echo "<td>$label</td>\n";
	echo "<td style='min-width:200px'>";
	echo employee_list($name, $id, $all_option, $submit_on_change, $show_inactive, $dept);
	echo "</td>\n";
}

//--------------------------------------------------------------------------

function employee_list_row($label, $name, $id = null, $all_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
	echo "<tr><td class='label'>$label</td>";
	employee_list_cells(null, $name, $id, $all_option, 
	$submit_on_change, $show_inactive, $dept);
	echo "</tr>";
}

//--------------------------------------------------------------------------

function gender_radio_row($label, $name, $gender=null) {
    echo "<tr><td class='label'>$label</td><td>";
	
    if($gender==0){
        echo "<input type='radio' name=$name value='1'>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='0' checked>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='2'>Other";
    }
    elseif($gender==1) {
        echo "<input type='radio' name=$name value='1' checked>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='0'>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='2'>Other";
    }
    else {
        echo "<input type='radio' name=$name value='1'>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='0'>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='2' checked>Other";
    }
    echo "</td></tr>";
}