<?php
$edited = can_action('55', 'edited');
$sub_metting = 1;
$task_timer_id = $this->uri->segment(6);
if ($task_timer_id) {
    $sub_metting = 2;
    $mettings_info = get_row('tbl_mettings', array('mettings_id' => $task_timer_id));
}
$all_user_rows = $this->db
    ->select('tbl_users.user_id, tbl_users.username, tbl_account_details.fullname')
    ->from('tbl_users')
    ->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
    ->get()->result();
$all_user_map = array();
if (!empty($all_user_rows)) {
    foreach ($all_user_rows as $user_row) {
        $all_user_map[$user_row->user_id] = !empty($user_row->fullname) ? $user_row->fullname : $user_row->username;
    }
}
$staff_user_rows = $this->db
    ->select('tbl_users.user_id, tbl_users.username, tbl_account_details.fullname')
    ->from('tbl_users')
    ->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
    ->where('tbl_users.role_id !=', '2')
    ->get()->result();
$staff_user_map = array();
if (!empty($staff_user_rows)) {
    foreach ($staff_user_rows as $user_row) {
        $staff_user_map[$user_row->user_id] = !empty($user_row->fullname) ? $user_row->fullname : $user_row->username;
    }
}
$meeting_attendees_selected = array();
if (!empty($mettings_info->attendees)) {
    $decoded_attendees = @unserialize($mettings_info->attendees);
    if (is_array($decoded_attendees) && !empty($decoded_attendees['attendees'])) {
        foreach ($decoded_attendees['attendees'] as $attendee_id) {
            if (isset($all_user_map[$attendee_id])) {
                $meeting_attendees_selected[] = array(
                    'id' => $attendee_id,
                    'text' => $all_user_map[$attendee_id],
                );
            }
        }
    }
}
$selected_responsible_id = !empty($mettings_info->user_id) ? $mettings_info->user_id : '';
$selected_responsible_label = !empty($selected_responsible_id) && isset($staff_user_map[$selected_responsible_id]) ? $staff_user_map[$selected_responsible_id] : '';
?>
<div class="nav-tabs-custom ">
    <!-- Tabs within a box -->
    <ul class="nav nav-tabs">
        <li class="<?= $sub_metting == 1 ? 'active' : ''; ?>"><a href="#all_metting" data-toggle="tab"><?= lang('all_metting') ?></a>
        </li>

        <li class="<?= $sub_metting == 2 ? 'active' : ''; ?>"><a href="#new_metting" data-toggle="tab"><?= lang('new_metting') ?></a>
        </li>
    </ul>
    <div class="tab-content bg-white">
        <!-- ************** general *************-->
        <div class="tab-pane <?= $sub_metting == 1 ? 'active' : ''; ?>" id="all_metting">

            <div class="table-responsive">
                <table class="table table-striped " cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th><?= lang('subject') ?></th>
                            <th><?= lang('end_date') ?></th>
                            <th><?= lang('responsible') ?></th>
                            <th class="col-options no-sort text-right"><?= lang('action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $all_meetings_info = get_result('tbl_mettings', array('leads_id' => $leads_details->leads_id));

                        if (!empty($all_meetings_info)) :
                            foreach ($all_meetings_info as $v_mettings) :
                                $user = $this->items_model->check_by(array('user_id' => $v_mettings->user_id), 'tbl_users');
                        ?>
                                <tr id="leads_meetings_<?= $leads_details->leads_id ?>">
                                    <td>
                                        <a href="#"
                                           data-modal-url="<?= base_url('admin/opportunities/meeting_details/' . $v_mettings->mettings_id) ?>"
                                           data-modal-title="<?= lang('meeting_details') ?>"
                                           data-modal-size="md"><?= $v_mettings->meeting_subject ?></a>
                                    </td>
                                    <td><?= display_date($v_mettings->end_date) . '<span style="color:#3c8dbc"> at </span>' . display_time($v_mettings->end_date, true) ?>
                                    </td>
                                    <td><?= fullname($v_mettings->user_id) ?></td>
                                    <td>
                                        <a href="<?= base_url('admin/leads/leads_details/' . $v_mettings->mettings_id) ?>" class="btn btn-xs btn-info" data-placement="top" data-toggle="modal" data-target="#myModal">
                                            <i data-lucide="list"></i></a>
                                        <?= btn_edit('admin/leads/leads_details/' . $leads_details->leads_id . '/mettings/' . $v_mettings->mettings_id) ?>
                                        <?php echo ajax_anchor(base_url("admin/leads/delete_leads_mettings/" . $leads_details->leads_id . '/' . $v_mettings->mettings_id), "<i class='btn btn-danger btn-xs' data-lucide='trash-2'></i>", array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table-meeting-" . $v_mettings->mettings_id)); ?>
                                    </td>
                                </tr>
                        <?php
                            endforeach;
                        endif;
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="tab-pane <?= $sub_metting == 2 ? 'active' : ''; ?>" id="new_metting">
            <form role="form" enctype="multipart/form-data" id="form" action="<?php echo base_url(); ?>admin/leads/saved_metting/<?= $leads_details->leads_id ?>/<?php
                                                                                                                                                                    if (!empty($mettings_info)) {
                                                                                                                                                                        echo $mettings_info->mettings_id;
                                                                                                                                                                    }
                                                                                                                                                                    ?>" method="post" class="form-horizontal  ">
                <div class="form-group terms">
                    <label class="col-lg-3 control-label"><?= lang('metting_subject') ?>
                        <span class="text-danger"> *</span> </label>
                    <div class="col-lg-9">
                        <input type="text" required="" name="meeting_subject" class="form-control" value="<?php
                                                                                                            if (!empty($mettings_info->meeting_subject)) {
                                                                                                                echo $mettings_info->meeting_subject;
                                                                                                            }
                                                                                                            ?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-3 control-label"><?= lang('start_date') ?><span class="text-danger"> *</span></label>
                    <div class="col-lg-4">
                        <div class="input-group">
                            <input type="text" required="" name="start_date" class="form-control datepicker" value="<?php
                                                                                                                    if (!empty($mettings_info->start_date)) {
                                                                                                                        echo date('Y-m-d', display_date($mettings_info->start_date));
                                                                                                                    } else {
                                                                                                                        echo date('Y-m-d');
                                                                                                                    }
                                                                                                                    ?>" data-date-format="<?= config_item('date_picker_format'); ?>">
                            <div class="input-group-addon">
                                <a href="#"><i data-lucide="calendar"></i></a>
                            </div>
                        </div>
                    </div>
                    <label class="col-lg-2 control-label"><?= lang('start_time') ?><span class="text-danger"> *</span></label>
                    <div class="col-lg-3">
                        <div class="input-group">
                            <input type="text" required="" name="start_time" class="form-control timepicker" value="<?php
                                                                                                                    if (!empty($mettings_info->start_date)) {
                                                                                                                        echo display_time($mettings_info->start_date, true);
                                                                                                                    }
                                                                                                                    ?>">
                            <div class="input-group-addon">
                                <a href="#"><i data-lucide="clock-3"></i></a>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="form-group">
                    <label class="col-lg-3 control-label"><?= lang('end_date') ?><span class="text-danger"> *</span></label>
                    <div class="col-lg-4">
                        <div class="input-group">
                            <input type="text" required="" name="end_date" class="form-control datepicker" value="<?php
                                                                                                                    if (!empty($mettings_info->end_date)) {
                                                                                                                        echo display_date($mettings_info->end_date);
                                                                                                                    } else {
                                                                                                                        echo date('Y-m-d');
                                                                                                                    }
                                                                                                                    ?>" data-date-format="<?= config_item('date_picker_format'); ?>">
                            <div class="input-group-addon">
                                <a href="#"><i data-lucide="calendar"></i></a>
                            </div>
                        </div>
                    </div>
                    <label class="col-lg-2 control-label"><?= lang('end_time') ?><span class="text-danger"> *</span></label>
                    <div class="col-lg-3">
                        <div class="input-group">
                            <input type="text" required="" name="end_time" class="form-control timepicker" value="<?php
                                                                                                                    if (!empty($mettings_info->end_date)) {
                                                                                                                        echo display_time($mettings_info->end_date, true);
                                                                                                                    }
                                                                                                                    ?>">
                            <div class="input-group-addon">
                                <a href="#"><i data-lucide="clock-3"></i></a>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="form-group">
                    <div class="col-lg-8 col-lg-offset-3">
                        <?= form_multi_lookup_group('attendees', lang('attendess'), $meeting_attendees_selected, true, array('id' => 'lead_attendees'), '', array('type' => 'user', 'min_length' => 0, 'placeholder' => lang('search') . ' ' . lang('attendess'))); ?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-8 col-lg-offset-3">
                        <?= form_lookup_group('user_id', lang('responsible'), $selected_responsible_id, $selected_responsible_label, true, array('id' => 'lead_meeting_responsible'), '', array('type' => 'staff', 'min_length' => 0, 'placeholder' => lang('search') . ' ' . lang('responsible'))); ?>
                    </div>
                </div>
                <div class="form-group terms">
                    <label class="col-lg-3 control-label"><?= lang('location') ?><span class="text-danger"> *</span> </label>
                    <div class="col-lg-5">
                        <input type="text" required="" name="location" class="form-control" value="<?php
                                                                                                    if (!empty($mettings_info->location)) {
                                                                                                        echo $mettings_info->location;
                                                                                                    }
                                                                                                    ?>">
                    </div>
                </div>
                <div class="form-group terms">
                    <label class="col-lg-3 control-label"><?= lang('description') ?> </label>
                    <div class="col-lg-8">
                        <textarea name="description" class="form-control"><?php
                                                                            if (!empty($mettings_info)) {
                                                                                echo $mettings_info->description;
                                                                            }
                                                                            ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-3 control-label"></label>
                    <div class="col-lg-5">
                        <button type="submit" class="btn btn-sm btn-primary"><?= lang('updates') ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


