<?php

defined('BASEPATH') or exit('No direct script access allowed');

$baseCurrency = get_base_currency();

$aColumns = [
    db_prefix() . 'checkinout.id',
    'firstname',
    'lastname',
    'GROUP_CONCAT(checktime SEPARATOR", ") as checkinout',
];

$sIndexColumn = 'id';
$sTable       = db_prefix() . 'checkinout';

$where  = [];
$filter = [];

// if ($this->ci->input->post('leads_related')) {
//     array_push($filter, 'OR rel_type="lead"');
// }
// if ($this->ci->input->post('customers_related')) {
//     array_push($filter, 'OR rel_type="customer"');
// }
// if ($this->ci->input->post('expired')) {
//     array_push($filter, 'OR open_till IS NOT NULL AND open_till <"' . date('Y-m-d') . '" AND status NOT IN(2,3)');
// }
$check = $this->ci->proposals_model->checkTeacher();
if ($check) {
    array_push($where, 'AND user_id = ' .$this->ci->session->userdata('staff_user_id')  . ' ');
}
//echo $check; exit;
// $statuses  = $this->ci->proposals_model->get_statuses();
// $statusIds = [];

// foreach ($statuses as $status) {
//     if ($this->ci->input->post('proposals_' . $status)) {
//         array_push($statusIds, $status);
//     }
// }
// if (count($statusIds) > 0) {
//     array_push($filter, 'AND status IN (' . implode(', ', $statusIds) . ')');
// }

// $agents    = $this->ci->proposals_model->get_sale_agents();
// $agentsIds = [];
// foreach ($agents as $agent) {
//     if ($this->ci->input->post('sale_agent_' . $agent['sale_agent'])) {
//         array_push($agentsIds, $agent['sale_agent']);
//     }
// }
// if (count($agentsIds) > 0) {
//     array_push($filter, 'AND assigned IN (' . implode(', ', $agentsIds) . ')');
// }

// $years      = $this->ci->proposals_model->get_proposals_years();
// $yearsArray = [];
// foreach ($years as $year) {
//     if ($this->ci->input->post('year_' . $year['year'])) {
//         array_push($yearsArray, $year['year']);
//     }
// }
// if (count($yearsArray) > 0) {
//     array_push($filter, 'AND YEAR(date) IN (' . implode(', ', $yearsArray) . ')');
// }

// if (count($filter) > 0) {
//     array_push($where, 'AND (' . prepare_dt_filter($filter) . ')');
// }

// if (!has_permission('proposals', '', 'view')) {
//     array_push($where, 'AND ' . get_proposals_sql_where_staff(get_staff_user_id()));
// }

$join          = [];
// $custom_fields = get_table_custom_fields('proposal');

// foreach ($custom_fields as $key => $field) {
//     $selectAs = (is_cf_date($field) ? 'date_picker_cvalue_' . $key : 'cvalue_' . $key);

//     array_push($customFieldsColumns, $selectAs);
//     array_push($aColumns, 'ctable_' . $key . '.value as ' . $selectAs);
//     array_push($join, 'LEFT JOIN ' . db_prefix() . 'customfieldsvalues as ctable_' . $key . ' ON ' . db_prefix() . 'proposals.id = ctable_' . $key . '.relid AND ctable_' . $key . '.fieldto="' . $field['fieldto'] . '" AND ctable_' . $key . '.fieldid=' . $field['id']);
// }
array_push($join, 'LEFT JOIN ' . db_prefix() . 'staff ON ' . db_prefix() . 'staff.staffid = '.db_prefix().'checkinout.user_id');

$aColumns = hooks()->apply_filters('proposals_table_sql_columns', $aColumns);

// Fix for big queries. Some hosting have max_join_limit
// if (count($custom_fields) > 4) {
//     @$this->ci->db->query('SET SQL_BIG_SELECTS=1');
// }

$result = data_tables_init($aColumns, $sIndexColumn, $sTable, $join, $where, [],'GROUP BY user_id,DATE(checktime)');

$output  = $result['output'];
$rResult = $result['rResult'];
// echo "<pre>";
// print_r($result); exit;
foreach ($rResult as $aRow) {
    $row = [];

    $numberOutput = '<a href="' . admin_url('proposals/list_proposals/' . $aRow[db_prefix() . 'proposals.id']) . '" onclick="init_proposal(' . $aRow[db_prefix() . 'proposals.id'] . '); return false;">' . format_proposal_number($aRow[db_prefix() . 'proposals.id']) . '</a>';

    $numberOutput .= '<div class="row-options">';

    $numberOutput .= '<a href="' . site_url('proposal/' . $aRow[db_prefix() . 'proposals.id'] . '/' . $aRow['hash']) . '" target="_blank">' . _l('view') . '</a>';
    if (has_permission('proposals', '', 'edit')) {
        $numberOutput .= ' | <a href="' . admin_url('proposals/proposal/' . $aRow[db_prefix() . 'proposals.id']) . '">' . _l('edit') . '</a>';
    }
    $numberOutput .= '</div>';

    $row[] = 1;

    $row[] = $aRow['firstname'] .' '.$aRow['lastname'];
    $times = explode(',', $aRow['checkinout']);
    if($times[0]){
        $row[] = date('d-m-Y h:i:s a',strtotime($times[0]));
    }else{
        $row[] = ' ';
    }
    
    if($times[1] != ''){
        $row[] = date('d-m-Y h:i:s a',strtotime($times[1]));
    }else{
        $row[] = ' ';
    }

    if(count($times) == 2){
        $datetime1 = new DateTime($times[0]);
        $datetime2 = new DateTime($times[1]);
        $interval = $datetime1->diff($datetime2);
        $row[] = $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
    }else{
        $row[] = ' ';
    }

    // Custom fields add values
    foreach ($customFieldsColumns as $customFieldColumn) {
        $row[] = (strpos($customFieldColumn, 'date_picker_') !== false ? _d($aRow[$customFieldColumn]) : $aRow[$customFieldColumn]);
    }

    $row['DT_RowClass'] = 'has-row-options';

    $row = hooks()->apply_filters('proposals_table_row_data', $row, $aRow);

    $output['aaData'][] = $row;
}
