<?php

/**
 * Description of bugs
 *
 * @author Nayeem
 */
class Bugs extends Admin_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('bugs_model');
    }

    private function delete_response($type, $message)
    {
        if ($this->input->is_ajax_request()) {
            echo json_encode(array("status" => $type, 'message' => $message));
            exit();
        }

        set_message($type, $message);

        $redirect_url = 'admin/bugs';
        if (!empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], '/admin/bugs/delete_bug/') === false) {
            $redirect_url = $_SERVER['HTTP_REFERER'];
        }

        redirect($redirect_url);
    }

    private function render_bug_participants($permission)
    {
        if ($permission === 'all') {
            return '<div class="flex items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 p-3 dark:border-slate-700 dark:bg-slate-800/40">'
                . '<div class="flex h-10 w-10 items-center justify-center rounded-full bg-indigo-100 text-sm font-semibold text-indigo-700 dark:bg-indigo-500/20 dark:text-indigo-300">E</div>'
                . '<div class="min-w-0 flex-1">'
                . '<p class="text-sm font-semibold text-slate-800 dark:text-slate-100">' . lang('everyone') . '</p>'
                . '<p class="text-xs text-slate-500 dark:text-slate-400">' . lang('permission_for_all') . '</p>'
                . '</div>'
                . '</div>';
        }

        $participants_html = '';
        $get_permission = $this->decode_permission($permission);
        if (!empty($get_permission)) {
            foreach ($get_permission as $permission_id => $v_permission) {
                $user_info = $this->db->where(array('user_id' => $permission_id))->get('tbl_users')->row();
                $profile_info = $this->db->where(array('user_id' => $permission_id))->get('tbl_account_details')->row();
                if (!empty($user_info) && !empty($profile_info)) {
                    $avatar = !empty($profile_info->avatar) ? $profile_info->avatar : staffImage($permission_id);
                    if (!preg_match('/^https?:\/\//', $avatar)) {
                        $avatar = base_url(ltrim($avatar, '/'));
                    }
                    $participants_html .= '<div class="flex items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 p-3 dark:border-slate-700 dark:bg-slate-800/40">'
                        . '<img src="' . $avatar . '" alt="' . html_escape($profile_info->fullname) . '" class="h-10 w-10 rounded-full object-cover">'
                        . '<div class="min-w-0 flex-1">'
                        . '<p class="truncate text-sm font-semibold text-slate-800 dark:text-slate-100">' . html_escape($profile_info->fullname) . '</p>'
                        . '<p class="text-xs text-slate-500 dark:text-slate-400">' . (((int) $user_info->role_id === 1) ? 'Admin' : 'Staff') . '</p>'
                        . '</div>'
                        . '</div>';
                }
            }
        }

        if ($participants_html === '') {
            return '<p class="text-sm text-slate-500 dark:text-slate-400">' . lang('nothing_to_display') . '</p>';
        }

        return '<div class="space-y-2">' . $participants_html . '</div>';
    }

    private function decode_permission($permission): array
    {
        if (empty($permission) || $permission === 'all') {
            return array();
        }

        $decoded = json_decode((string) $permission);
        return is_object($decoded) || is_array($decoded) ? (array) $decoded : array();
    }

    private function bugs_member_response($type, $message, $id = null, $permission = null)
    {
        if ($this->input->is_ajax_request()) {
            $response = array('status' => $type, 'message' => $message);
            if ($type === 'success' && $id !== null && $permission !== null) {
                $response['patch'] = array(
                    array(
                        'selector' => '[data-bug-participants="' . (int) $id . '"]',
                        'html' => $this->render_bug_participants($permission),
                    ),
                );
            }
            echo json_encode($response);
            exit();
        }

        set_message($type, $message);
        if (empty($_SERVER['HTTP_REFERER'])) {
            redirect('admin/bugs');
        } else {
            redirect($_SERVER['HTTP_REFERER']);
        }
    }


    public function bugs_state_report()
    {
        if (!$this->input->is_ajax_request()) {
            redirect('admin/dashboard');
        }
        $data = array();
        $pathonor_jonno['bugs_state_report_div'] = $this->load->view("admin/bugs/bugs_state_report", $data, true);
        echo json_encode($pathonor_jonno);
        exit;
    }


    public function index($id = NULL, $opt_id = NULL)
    {
        $data['title'] = lang('all_bugs');
        $data['page_title'] = lang('bugs');
        $data['page_description'] = lang('bug_management');
        $data['active'] = 1;
        $data['dataTables'] = true;
        $data['datatable_preload'] = $this->capture_datatable_preload(function () {
            $this->bugsList();
        });
        $data['bug_statuses'] = $this->bugs_model->get_statuses();
        $data['header_actions'] = array();
        if (!empty(can_action('58', 'created')) || !empty(can_action('58', 'edited'))) {
            $data['header_actions'][] = array(
                'label' => lang('new_bugs'),
                'icon' => 'plus',
                'href' => base_url('admin/bugs/create'),
                'style' => 'primary',
            );
        }
        $header_filter_items = array(
            array('label' => lang('all'), 'active' => true, 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => '', 'data-bug-search' => '', 'data-bug-filter-label' => lang('all'))),
            array('label' => lang('assigned_to_me'), 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => 'assigned_to_me', 'data-bug-search' => '', 'data-bug-filter-label' => lang('assigned_to_me'))),
        );
        if (!empty($data['bug_statuses'])) {
            foreach ($data['bug_statuses'] as $status) {
                $header_filter_items[] = array(
                    'label' => $status['name'],
                    'attrs' => array(
                        'data-bug-filter-action' => '1',
                        'data-bug-filter' => $status['value'],
                        'data-bug-search' => '',
                        'data-bug-filter-label' => $status['name'],
                    ),
                );
            }
        }
        if (admin()) {
            $header_filter_items[] = array(
                'label' => lang('assigned_to') . ' ' . lang('everyone'),
                'attrs' => array(
                    'data-bug-filter-action' => '1',
                    'data-bug-filter' => 'everyone',
                    'data-bug-search' => 'by_staff',
                    'data-bug-filter-label' => lang('assigned_to') . ' ' . lang('everyone'),
                ),
				'style' => 'info',
            );
        }
        $data['header_filters'] = array(
            array(
                'label' => 'Filters',
                'icon' => 'sliders-horizontal',
                'selected_label' => lang('all'),
                'attrs' => array('id' => 'bugs-header-filter'),
                'items' => array_merge(
                    $header_filter_items,
                    array(
                        array(
                            'label' => lang('project'),
                            'icon' => 'briefcase',
                            'divider_before' => true,
                            'items' => array(
                                array('label' => lang('all') . ' ' . lang('project'), 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => '', 'data-bug-search' => 'by_project', 'data-bug-filter-label' => lang('all') . ' ' . lang('project'))),
                            ),
                            'lookup' => array(
                                'endpoint' => base_url('admin/common/project_lookup'),
                                'limit' => 12,
                                'min_length' => 0,
                                'action_attrs' => array('data-bug-filter-action' => '1', 'data-bug-search' => 'by_project'),
                                'filter_attr' => 'data-bug-filter',
                                'label_attr' => 'data-bug-filter-label',
                                'label_prefix' => lang('project') . ': ',
                                'placeholder' => lang('search') . ' ' . lang('project'),
                                'cache_key' => 'bugs_project_lookup',
                            ),
                        ),
                        array(
                            'label' => lang('opportunities'),
                            'icon' => 'target',
                            'items' => array(
                                array('label' => lang('all') . ' ' . lang('opportunities'), 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => '', 'data-bug-search' => 'by_opportunity', 'data-bug-filter-label' => lang('all') . ' ' . lang('opportunities'))),
                            ),
                            'lookup' => array(
                                'endpoint' => base_url('admin/common/opportunity_lookup'),
                                'limit' => 12,
                                'min_length' => 0,
                                'action_attrs' => array('data-bug-filter-action' => '1', 'data-bug-search' => 'by_opportunity'),
                                'filter_attr' => 'data-bug-filter',
                                'label_attr' => 'data-bug-filter-label',
                                'label_prefix' => lang('opportunities') . ': ',
                                'placeholder' => lang('search') . ' ' . lang('opportunities'),
                                'cache_key' => 'bugs_opportunity_lookup',
                            ),
                        ),
                        array(
                            'label' => lang('reporter'),
                            'icon' => 'user',
                            'items' => array(
                                array('label' => lang('all') . ' ' . lang('reporter'), 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => '', 'data-bug-search' => 'from_reporter', 'data-bug-filter-label' => lang('all') . ' ' . lang('reporter'))),
                            ),
                            'lookup' => array(
                                'endpoint' => base_url('admin/common/user_lookup'),
                                'limit' => 12,
                                'min_length' => 0,
                                'action_attrs' => array('data-bug-filter-action' => '1', 'data-bug-search' => 'from_reporter'),
                                'filter_attr' => 'data-bug-filter',
                                'label_attr' => 'data-bug-filter-label',
                                'label_prefix' => lang('reporter') . ': ',
                                'placeholder' => lang('search') . ' ' . lang('reporter'),
                                'cache_key' => 'bugs_reporter_lookup',
                            ),
                        ),
                        array(
                            'label' => lang('staff'),
                            'icon' => 'users',
                            'items' => array(
                                array('label' => lang('all') . ' ' . lang('staff'), 'attrs' => array('data-bug-filter-action' => '1', 'data-bug-filter' => '', 'data-bug-search' => 'by_staff', 'data-bug-filter-label' => lang('all') . ' ' . lang('staff'))),
                            ),
                            'lookup' => array(
                                'endpoint' => base_url('admin/common/staff_lookup'),
                                'limit' => 12,
                                'min_length' => 0,
                                'action_attrs' => array('data-bug-filter-action' => '1', 'data-bug-search' => 'by_staff'),
                                'filter_attr' => 'data-bug-filter',
                                'label_attr' => 'data-bug-filter-label',
                                'label_prefix' => lang('staff') . ': ',
                                'placeholder' => lang('search') . ' ' . lang('staff'),
                                'cache_key' => 'bugs_staff_lookup',
                            ),
                        ),
                    )
                ),
            ),
        );
        $data['header_filter_extras_html'] = '';
        $data['subview'] = $this->load->view('admin/bugs/bugs', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public function create($id = null, $opt_id = NULL)
    {
        $data['title'] = lang('all_bugs');
        // get permission user by menu id
        $data['assign_user'] = $this->bugs_model->allowed_user('58');
        $data['bug_statuses'] = $this->bugs_model->get_statuses();
        if ($id) { // retrive data from db by id
            $data['active'] = 2;
            $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
            $edited = can_action('58', 'edited');
            if ($id == 'project') {
                $data['project_id'] = $opt_id;
                $project_info = get_row('tbl_project', array('project_id' => $opt_id));
                $data['project_label'] = !empty($project_info->project_name) ? $project_info->project_name : null;
                if ($project_info->permission == 'all') {
                    $data['assign_user'] = $this->bugs_model->allowed_user('57');
                } else {
                    $data['assign_user'] = $this->bugs_model->permitted_allowed_user($project_info->permission);
                }
            } elseif ($id == 'opportunities') {
                $data['opportunities_id'] = $opt_id;
                $opportunity_info = get_row('tbl_opportunities', array('opportunities_id' => $opt_id));
                $data['opportunity_label'] = !empty($opportunity_info->opportunity_name) ? $opportunity_info->opportunity_name : null;
                $option_info = get_row('tbl_opportunities', array('opportunities_id' => $opt_id));
                if ($option_info->permission == 'all') {
                    $data['assign_user'] = $this->bugs_model->allowed_user('56');
                } else {
                    $data['assign_user'] = $this->bugs_model->permitted_allowed_user($option_info->permission);
                }
            } else {
                if (!empty($can_edit) && !empty($edited)) {
                    if (is_numeric($id)) {
                        // get all bug information
                        $data['bug_info'] = $this->db->where('bug_id', $id)->get('tbl_bug')->row();
                        if (!empty($data['bug_info']->project_id)) {
                            $project_info = get_row('tbl_project', array('project_id' => $data['bug_info']->project_id));
                            $data['project_label'] = !empty($project_info->project_name) ? $project_info->project_name : null;
                        }
                        if (!empty($data['bug_info']->opportunities_id)) {
                            $opportunity_info = get_row('tbl_opportunities', array('opportunities_id' => $data['bug_info']->opportunities_id));
                            $data['opportunity_label'] = !empty($opportunity_info->opportunity_name) ? $opportunity_info->opportunity_name : null;
                        }
                        if (!empty($data['bug_info']->reporter)) {
                            $reporter_profile = $this->db->where('user_id', $data['bug_info']->reporter)->get('tbl_account_details')->row();
                            $data['reporter_label'] = !empty($reporter_profile->fullname) ? $reporter_profile->fullname : null;
                        }
                    }
                }
            }

        }
        $data['active'] = 2;
        $data['subview'] = $this->load->view('admin/bugs/createbugs', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }

    public function bugsList($filterBy = null, $search_by = null)
    {
        if ($this->input->is_ajax_request()) {
            $this->load->model('datatables');
            $this->datatables->table = 'tbl_bug';
            $this->datatables->join_table = array('tbl_account_details');
            $this->datatables->join_where = array('tbl_account_details.user_id=tbl_bug.reporter');

            $custom_field = custom_form_table_search(6);
            $main_column = array('bug_title', 'tbl_account_details.fullname', 'bug_status', 'severity', 'reporter', 'permission');
            $action_array = array('bug_id');
            $result = array_merge($main_column, $custom_field, $action_array);
            $this->datatables->column_order = $result;
            $this->datatables->column_search = $result;
            $this->datatables->order = array('bug_id' => 'desc');
            $where = array();
            if (!empty($search_by)) {
                if ($search_by == 'by_project') {
                    $where = array('project_id' => $filterBy);
                }
                if ($search_by == 'by_opportunity') {
                    $where = array('opportunities_id' => $filterBy);
                }
                if ($search_by == 'from_reporter') {
                    $where = array('reporter' => $filterBy);
                }
                if ($search_by == 'by_staff') {
                    if ($filterBy == 'everyone') {
                        $where = array('permission' => 'all');
                    } else {
                        $where = $filterBy;
                    }
                }
            } else {
                if ($filterBy == 'assigned_to_me') {
                    $user_id = $this->session->userdata('user_id');
                    $where = $user_id;
                } else if (!empty($filterBy)) {
                    $where = array('bug_status' => $filterBy);
                }
            }

            $fetch_data = $this->datatables->get_bugs($filterBy, $search_by);
            $data = array();

            $edited = can_action('58', 'edited');
            $created = can_action('58', 'created');
            $deleted = can_action('58', 'deleted');
            $tasks_status = array_reverse($this->bugs_model->get_statuses());
            $assigned_user_ids = array();
            foreach ($fetch_data as $bug_row) {
                if (empty($bug_row) || empty($bug_row->permission) || $bug_row->permission == 'all') {
                    continue;
                }
                $permission_map = json_decode($bug_row->permission, true);
                if (!empty($permission_map) && is_array($permission_map)) {
                    foreach (array_keys($permission_map) as $permission_user_id) {
                        $assigned_user_ids[] = (int) $permission_user_id;
                    }
                }
            }
            $assigned_user_ids = array_values(array_unique(array_filter($assigned_user_ids)));
            $assigned_users = array();
            if (!empty($assigned_user_ids)) {
                $assigned_user_rows = $this->db
                    ->select('tbl_users.user_id, tbl_users.role_id, tbl_account_details.fullname, tbl_account_details.avatar')
                    ->from('tbl_users')
                    ->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
                    ->where_in('tbl_users.user_id', $assigned_user_ids)
                    ->get()
                    ->result();
                if (!empty($assigned_user_rows)) {
                    foreach ($assigned_user_rows as $assigned_user_row) {
                        $assigned_users[(int) $assigned_user_row->user_id] = $assigned_user_row;
                    }
                }
            }
            foreach ($fetch_data as $_key => $v_bugs) {
                if (!empty($v_bugs)) {
                    $action = null;
                    $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $v_bugs->bug_id));
                    $can_delete = $this->bugs_model->can_action('tbl_bug', 'delete', array('bug_id' => $v_bugs->bug_id));

                    $sub_array = array();

                    $name = null;
                    $name .= '<a class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300" href="' . base_url() . 'admin/bugs/details/' . $v_bugs->bug_id . '">' . $v_bugs->bug_title . '</a>';

                    $sub_array[] = $name;
                    if ($v_bugs->bug_status == 'unconfirmed') {
                        $label = 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
                    } elseif ($v_bugs->bug_status == 'confirmed') {
                        $label = 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
                    } elseif ($v_bugs->bug_status == 'in_progress') {
                        $label = 'bg-indigo-100 text-indigo-700 dark:bg-indigo-900/40 dark:text-indigo-300';
                    } else {
                        $label = 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
                    }
                    $change_status = null;
                    $ch_url = base_url() . 'admin/bugs/change_status/';
                    if (!empty($edited) && !empty($created)) {
                        $items = '';
                        foreach ($tasks_status as $v_status) {
                            $is_active = ($v_bugs->bug_status == $v_status['value']);
                            $active_class = $is_active
                                ? 'bg-slate-100 font-medium text-slate-900 dark:bg-gray-700 dark:text-white'
                                : 'text-slate-700 dark:text-gray-300';
                            $items .= '<li><a class="block px-3 py-2 ' . $active_class . ' hover:bg-slate-50 dark:hover:bg-gray-700" data-no-instant-nav="1" href="' . $ch_url . $v_bugs->bug_id . '/' . ($v_status['value']) . '">' . lang($v_status['value']) . '</a></li>';
                        }
                        $change_status = '<details class="relative inline-flex ml-1">
                            <summary class="inline-flex cursor-pointer list-none items-center justify-center gap-1 rounded-md border border-slate-200 bg-white px-2 py-1 text-slate-500 shadow-sm transition-colors hover:bg-slate-50 hover:text-slate-900 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200">
                                <i data-lucide="chevron-down" class="h-3 w-3"></i>
                            </summary>
                            <ul class="absolute right-0 top-full z-50 mt-1 min-w-[160px] overflow-hidden rounded-lg border border-slate-200 bg-white py-1 text-xs shadow-lg dark:border-gray-700 dark:bg-gray-800">'
                            . $items .
                            '</ul>
                        </details>';
                    }
                    $sub_array[] = (!empty($v_bugs->created_time) ? display_datetime($v_bugs->created_time) : '-');
                    $sub_array[] = '<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold ' . $label . '">' . lang($v_bugs->bug_status) . '</span> ' . $change_status;
                    if ($v_bugs->priority == 'High') {
                        $plabel = 'danger';
                    } elseif ($v_bugs->priority == 'Medium') {
                        $plabel = 'info';
                    } else {
                        $plabel = 'primary';
                    }

                    $sub_array[] = '<span class="tags label label-' . $plabel . '">' . lang($v_bugs->severity) . '</span>';

                    if ($this->session->userdata('user_type') == '1') {
                        $reporter_name = !empty($v_bugs->fullname) ? $v_bugs->fullname : fullname($v_bugs->reporter);
                        $sub_array[] = '<span class="tags">' . $reporter_name . '</span>';
                    }
                    $assigned = '<div class="flex items-center">';
                    if ($v_bugs->permission != 'all') {
                        $get_permission = $this->decode_permission($v_bugs->permission);
                        if (!empty($get_permission)) :
                            foreach ($get_permission as $permission => $v_permission) :
                                $user_info = isset($assigned_users[(int) $permission]) ? $assigned_users[(int) $permission] : null;
                                if (!empty($user_info)) {
                                    $ring = ($user_info->role_id == 1) ? 'ring-rose-500' : 'ring-emerald-500';
                                    $tooltip_name = !empty($user_info->fullname) ? $user_info->fullname : lang('undefined_user');
                                    $avatar = (!empty($user_info->avatar) && file_exists($user_info->avatar)) ? $user_info->avatar : 'assets/img/user/default_avatar.jpg';
                                    $assigned .= '<span class="relative inline-flex items-center" data-tooltip="' . html_escape($tooltip_name) . '">
                                        <img src="' . base_url() . $avatar . '" class="h-7 w-7 rounded-full ring-2 ring-white dark:ring-slate-900 ' . $ring . '" alt="' . html_escape($tooltip_name) . '">
                                    </span>';
                                }
                            endforeach;
                        endif;
                    } else {
                        $assigned .= '<span class="inline-flex items-center gap-1 ml-0 rounded-full bg-slate-100 px-2 py-1 text-xs font-semibold text-slate-700"><i data-lucide="users" class="h-4 w-4"></i>' . lang("everyone") . '</span>';
                    };
                    if (!empty($can_edit) && !empty($edited)) {
                        $assigned .= btn_modal(
                            'admin/bugs/update_users/' . $v_bugs->bug_id,
                            null,
                            lang('add_more'),
                            'md',
                            'user-plus'
                        );
                    };
                    $assigned .= '</div>';

                    $sub_array[] = $assigned;

                    $custom_form_table = custom_form_table(6, $v_bugs->bug_id);

                    if (!empty($custom_form_table)) {
                        foreach ($custom_form_table as $c_label => $v_fields) {
                            $sub_array[] = $v_fields;
                        }
                    }
                    if (!empty($can_edit) && !empty($edited)) {
                        $action .= btn_edit('admin/bugs/create/' . $v_bugs->bug_id) . ' ';
                    }
                    if (!empty($can_delete) && !empty($deleted)) {
                        $action .= ajax_anchor(base_url("admin/bugs/delete_bug/$v_bugs->bug_id"), '', array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_" . $_key)) . ' ';
                    }
                    $sub_array[] = '<div class="erp-table-actions">' . $action . '</div>';
                    $data[] = $sub_array;
                }
            }
            render_table($data, $where);
        } else {
            redirect('admin/dashboard');
        }
    }

    public function save_bug($id = NULL)
    {
        $created = can_action('58', 'created');
        $edited = can_action('58', 'edited');
        if (!empty($created) || !empty($edited) && !empty($id)) {
            $data = $this->bugs_model->array_from_post(array(
                'issue_no',
                'bug_title',
                'bug_description',
                'reproducibility',
                'priority',
                'severity',
                'reporter',
                'client_visible',
                'bug_status'
            ));

            if (empty($id)) {
                $data['created_time'] = date("Y-m-d H:i:s");
            }
            $result = 0;
            $project_id = $this->input->post('project_id', TRUE);

            if (!empty($project_id)) {
                $data['project_id'] = $project_id;
            } else {
                $data['project_id'] = NULL;
                $result += count((array)1);
            }
            $opportunities_id = $this->input->post('opportunities_id', TRUE);
            if (!empty($opportunities_id)) {
                $data['opportunities_id'] = $opportunities_id;
            } else {
                $data['opportunities_id'] = NULL;
                $result += count((array)1);
            }
            if ($result == 2) {
                if (!empty($id)) {
                    $bugs_info = $this->db->where('bug_id', $id)->get('tbl_bug')->row();
                    $data['project_id'] = $bugs_info->project_id;
                    $data['opportunities_id'] = $bugs_info->opportunities_id;
                } else {
                    $data['project_id'] = $this->input->post('un_project_id', TRUE);
                    $data['opportunities_id'] = $this->input->post('un_opportunities_id', TRUE);
                }
            }
            save_custom_field(6, $id);

            $permission = $this->input->post('permission', true);
            if (!empty($permission)) {

                if ($permission == 'everyone') {
                    $assigned = 'all';
                    $assigned_to['assigned_to'] = $this->bugs_model->allowed_user_id('58');
                } else {
                    $assigned_to = $this->bugs_model->array_from_post(array('assigned_to'));
                    if (!empty($assigned_to['assigned_to'])) {
                        foreach ($assigned_to['assigned_to'] as $assign_user) {
                            $assigned[$assign_user] = $this->input->post('action_' . $assign_user, true);
                        }
                    }
                }
                if (!empty($assigned)) {
                    if ($assigned != 'all') {
                        $assigned = json_encode($assigned);
                    }
                } else {
                    $assigned = 'all';
                }
                $data['permission'] = $assigned;
            } else {
                set_message('error', lang('assigned_to') . ' Field is required');
                if (empty($_SERVER['HTTP_REFERER'])) {
                    redirect('admin/bugs');
                } else {
                    redirect($_SERVER['HTTP_REFERER']);
                }
            }


            //save data into table.
            $this->bugs_model->_table_name = "tbl_bug"; // table name
            $this->bugs_model->_primary_key = "bug_id"; // $id
            if (!empty($id)) {
                $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
                if (!empty($can_edit)) {
                    $return_id = $this->bugs_model->save($data, $id);
                } else {
                    set_message('error', lang('there_in_no_value'));
                    redirect('admin/projects');
                }
            } else {
                $return_id = $this->bugs_model->save($data);
            }

            save_custom_field(6, $id);
            if ($assigned == 'all') {
                $assigned_to['assigned_to'] = $this->bugs_model->allowed_user_id('58');
            }
            if (!empty($id)) {
                $msg = lang('update_bug');
                $activity = 'activity_update_bug';
                $id = $id;
                if (!empty($assigned_to['assigned_to'])) {
                    // send update
                    $this->notify_update_bugs($assigned_to['assigned_to'], $id, TRUE);
                }
            } else {
                $id = $return_id;
                $msg = lang('save_bug');
                $activity = 'activity_new_bug';
                if (!empty($assigned_to['assigned_to'])) {
                    $this->notify_bugs($assigned_to['assigned_to'], $id);
                    $this->notify_bugs_reported($id);
                }
            }

            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $id,
                'activity' => $activity,
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $id,
                'value1' => $data['bug_title'],
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $type = "success";
            $message = $msg;
            set_message($type, $message);
        }
        if (!empty($data['project_id']) && is_numeric($data['project_id'])) {
            redirect('admin/projects/project_details/' . $data['project_id']);
        } elseif (!empty($opportunities_id) && is_numeric($opportunities_id)) {
            redirect('admin/opportunities/opportunity_details/' . $opportunities_id);
        } else if (!empty($id)) {
            redirect('admin/bugs/view_bug_details/' . $id);
        } else {
            redirect('admin/bugs');
        }
    }

    function notify_update_bugs($users, $bug_id)
    {
        $email_template = email_templates(array('email_group' => 'bug_updated'));

        $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bug_id), 'tbl_bug');
        $message = $email_template->template_body;

        $subject = $email_template->subject;

        $bug_title = str_replace("{BUG_TITLE}", $bugs_info->bug_title, $message);
        $bug_status = str_replace("{STATUS}", lang($bugs_info->bug_status), $bug_title);

        $assigned_by = str_replace("{MARKED_BY}", ucfirst($this->session->userdata('name')), $bug_status);
        $Link = str_replace("{BUG_URL}", base_url() . 'admin/bugs/view_bug_details/' . $bugs_info->bug_id, $assigned_by);
        $message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);

        $data['message'] = $message;
        $message = $this->load->view('email_template', $data, TRUE);

        $params['subject'] = $subject;
        $params['message'] = $message;
        $params['resourceed_file'] = '';

        foreach ($users as $v_user) {
            $login_info = $this->bugs_model->check_by(array('user_id' => $v_user), 'tbl_users');
            $params['recipient'] = $login_info->email;
            $this->bugs_model->send_email($params);

            if ($v_user != $this->session->userdata('user_id')) {
                add_notification(array(
                    'to_user_id' => $v_user,
                    'from_user_id' => true,
                    'description' => 'assign_to_you_the_bugs',
                    'link' => 'admin/bugs/view_bug_details/' . $bug_id,
                    'value' => $bugs_info->bug_title,
                ));
            }
        }
        show_notification($users);
    }

    private function notify_update_bugs_in_app($users, $bug_id, $bugs_info = null)
    {
        if (empty($users)) {
            return;
        }

        if (empty($bugs_info)) {
            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bug_id), 'tbl_bug');
        }

        foreach ($users as $v_user) {
            if ($v_user != $this->session->userdata('user_id')) {
                add_notification(array(
                    'to_user_id' => $v_user,
                    'from_user_id' => true,
                    'description' => 'assign_to_you_the_bugs',
                    'link' => 'admin/bugs/view_bug_details/' . $bug_id,
                    'value' => $bugs_info->bug_title,
                ));
            }
        }
        show_notification($users);
    }

    function notify_bugs_reported($bug_id)
    {

        $email_template = email_templates(array('email_group' => 'bug_reported'));
        $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bug_id), 'tbl_bug');

        $message = $email_template->template_body;

        $subject = $email_template->subject;

        $bug_title = str_replace("{BUG_TITLE}", $bugs_info->bug_title, $message);

        $assigned_by = str_replace("{ADDED_BY}", ucfirst($this->session->userdata('name')), $bug_title);
        $Link = str_replace("{BUG_URL}", base_url() . 'admin/bugs/view_bug_details/' . $bugs_info->bug_id, $assigned_by);
        $message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);

        $data['message'] = $message;
        $message = $this->load->view('email_template', $data, TRUE);

        $params['subject'] = $subject;
        $params['message'] = $message;
        $params['resourceed_file'] = '';

        $login_info = $this->bugs_model->check_by(array('user_id' => $bugs_info->reporter), 'tbl_users');
        $params['recipient'] = $login_info->email;
        $this->bugs_model->send_email($params);
    }

    function notify_bugs($users, $bug_id, $update = NULL)
    {
        if (!empty($update)) {
            $email_template = email_templates(array('email_group' => 'bugs_updated'));
            $description = 'not_bug_update';
        } else {
            $email_template = email_templates(array('email_group' => 'bug_assigned'));
            $description = 'assign_to_you_the_bugs';
        }
        $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bug_id), 'tbl_bug');
        $message = $email_template->template_body;

        $subject = $email_template->subject;

        $bug_title = str_replace("{BUG_TITLE}", $bugs_info->bug_title, $message);

        $assigned_by = str_replace("{ASSIGNED_BY}", ucfirst($this->session->userdata('name')), $bug_title);
        $Link = str_replace("{BUG_URL}", base_url() . 'admin/bugs/view_bug_details/' . $bugs_info->bug_id, $assigned_by);
        $message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);

        $data['message'] = $message;
        $message = $this->load->view('email_template', $data, TRUE);

        $params['subject'] = $subject;
        $params['message'] = $message;
        $params['resourceed_file'] = '';

        foreach ($users as $v_user) {
            $login_info = $this->bugs_model->check_by(array('user_id' => $v_user), 'tbl_users');
            $params['recipient'] = $login_info->email;
            $this->bugs_model->send_email($params);

            if ($v_user != $this->session->userdata('user_id')) {
                add_notification(array(
                    'to_user_id' => $v_user,
                    'from_user_id' => true,
                    'description' => $description,
                    'link' => 'admin/bugs/view_bug_details/' . $bug_id,
                    'value' => $bugs_info->bug_title,
                ));
            }
        }
        show_notification($users);
    }

    public function update_users($id)
    {
        // get all assign_user
        $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
        $edited = can_action('58', 'edited');
        if (!empty($can_edit) && !empty($edited)) {
            $data['assign_user'] = $this->bugs_model->allowed_user('58');

            $data['bugs_info'] = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');
            $data['modal_subview'] = $this->load->view('admin/bugs/_modal_users', $data, FALSE);
        } else {
            set_message('error', lang('there_in_no_value'));
            if (empty($_SERVER['HTTP_REFERER'])) {
                redirect('admin/bugs');
            } else {
                redirect($_SERVER['HTTP_REFERER']);
            }
        }
    }

    public function update_member($id)
    {
        $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
        $edited = can_action('58', 'edited');
        if (!empty($can_edit) && !empty($edited)) {
            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');

            $permission = $this->input->post('permission', true);
            if (!empty($permission)) {
                if ($permission == 'everyone') {
                    $assigned = 'all';
                    $assigned_to['assigned_to'] = $this->bugs_model->allowed_user_id('58');
                } else {
                    $assigned_to = $this->bugs_model->array_from_post(array('assigned_to'));
                    if (!empty($assigned_to['assigned_to'])) {
                        foreach ($assigned_to['assigned_to'] as $assign_user) {
                            $assigned[$assign_user] = $this->input->post('action_' . $assign_user, true);
                        }
                    }
                }
                if (!empty($assigned)) {
                    if ($assigned != 'all') {
                        $assigned = json_encode($assigned);
                    }
                } else {
                    $assigned = 'all';
                }
                $data['permission'] = $assigned;
            } else {
                $this->bugs_member_response('error', lang('assigned_to') . ' Field is required');
            }

            //save data into table.
            $this->bugs_model->_table_name = "tbl_bug"; // table name
            $this->bugs_model->_primary_key = "bug_id"; // $id
            $this->bugs_model->save($data, $id);
            if ($assigned == 'all') {
                $assigned_to['assigned_to'] = $this->bugs_model->allowed_user_id('58');
            }

            $msg = lang('update_bug');
            $activity = 'activity_update_bug';
            if (!empty($assigned_to['assigned_to'])) {
                $this->notify_update_bugs_in_app($assigned_to['assigned_to'], $id, $bugs_info);
            }

            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $id,
                'activity' => $activity,
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $id,
                'value1' => $bugs_info->bug_title,
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $type = "success";
            $message = $msg;
            $this->bugs_member_response($type, $message, $id, $data['permission']);
        } else {
            $this->bugs_member_response('error', lang('there_in_no_value'));
        }
    }

    public function change_status($id, $status)
    {
        $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
        $edited = can_action('58', 'edited');
        if (!empty($can_edit) && !empty($edited)) {

            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');
            if (!empty($bugs_info->permission) && $bugs_info->permission != 'all') {
                $user = $this->decode_permission($bugs_info->permission);
                foreach ($user as $key => $v_user) {
                    $allowed_user[] = $key;
                }
            } else {
                $allowed_user = $this->bugs_model->allowed_user_id('58');
            }

            if (!empty($notifiedUsers)) {
                foreach ($notifiedUsers as $users) {
                    if ($users != $this->session->userdata('user_id')) {
                        add_notification(array(
                            'to_user_id' => $users,
                            'from_user_id' => true,
                            'description' => 'not_changed_status',
                            'link' => 'admin/bugs/view_bug_details/' . $id,
                            'value' => lang('status') . ' : ' . $bugs_info->bug_status . ' to ' . $status,
                        ));
                    }
                }
                show_notification($notifiedUsers);
            }

            if (!empty($allowed_user)) {
                $this->notify_update_bugs($allowed_user, $id, TRUE);
            }

            $data['bug_status'] = $status;

            //save data into table.
            $this->bugs_model->_table_name = "tbl_bug"; // table name
            $this->bugs_model->_primary_key = "bug_id"; // $id
            $id = $this->bugs_model->save($data, $id);
            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $id,
                'activity' => 'activity_update_bug',
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $id,
                'value1' => lang($data['bug_status']),
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $type = "success";
            $message = lang('update_bug');
            set_message($type, $message);
        } else {
            set_message('error', lang('there_in_no_value'));
        }
        if (empty($_SERVER['HTTP_REFERER'])) {
            redirect('admin/bugs');
        } else {
            redirect($_SERVER['HTTP_REFERER']);
        }
    }

    public function save_bugs_notes($id = NULL)
    {

        $data = $this->bugs_model->array_from_post(array('notes'));

        //save data into table.
        $this->bugs_model->_table_name = "tbl_bug"; // table name
        $this->bugs_model->_primary_key = "bug_id"; // $id
        $id = $this->bugs_model->save($data, $id);
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'bugs',
            'module_field_id' => $id,
            'activity' => 'activity_update_bug',
            'icon' => 'fa-bug',
            'link' => 'admin/bugs/view_bug_details/' . $id . '/notes',
            'value1' => $data['notes'],
        );
        // Update into tbl_project
        $this->bugs_model->_table_name = "tbl_activities"; //table name
        $this->bugs_model->_primary_key = "activities_id";
        $this->bugs_model->save($activities);

        $type = "success";
        $message = lang('update_bug');
        set_message($type, $message);
        redirect('admin/bugs/details/' . $id . '/notes');
    }

	

    public function save_comments()
    {

        $data['bug_id'] = $this->input->post('bug_id', TRUE);
        $data['comment'] = $this->input->post('comment', TRUE);

        $files = $this->input->post("files", true);
        $target_path = getcwd() . "/uploads/";
        //process the fiiles which has been uploaded by dropzone
        if (!empty($files) && is_array($files)) {
            foreach ($files as $key => $file) {
                if (!empty($file)) {
                    $file_name = $this->input->post('file_name_' . $file, true);
                    $new_file_name = move_temp_file($file_name, $target_path);
                    $file_ext = explode(".", $new_file_name);
                    $is_image = check_image_extension($new_file_name);
                    $size = $this->input->post('file_size_' . $file, true) / 1000;
                    if ($new_file_name) {
                        $up_data[] = array(
                            "fileName" => $new_file_name,
                            "path" => "uploads/" . $new_file_name,
                            "fullPath" => getcwd() . "/uploads/" . $new_file_name,
                            "ext" => '.' . end($file_ext),
                            "size" => round($size, 2),
                            "is_image" => $is_image,
                        );
                        $success = true;
                    } else {
                        $success = false;
                    }
                }
            }
        }
        //process the files which has been submitted manually
        if ($_FILES) {
            $files = $_FILES['manualFiles'];
            if ($files && count(array($files)) > 0) {
                foreach ($files["tmp_name"] as $key => $file) {
                    $temp_file = $file;
                    $file_name = $files["name"][$key];
                    $file_size = $files["size"][$key];
                    $new_file_name = move_temp_file($file_name, $target_path, "", $temp_file);
                    if ($new_file_name) {
                        $file_ext = explode(".", $new_file_name);
                        $is_image = check_image_extension($new_file_name);
                        $up_data[] = array(
                            "fileName" => $new_file_name,
                            "path" => "uploads/" . $new_file_name,
                            "fullPath" => getcwd() . "/uploads/" . $new_file_name,
                            "ext" => '.' . end($file_ext),
                            "size" => round($file_size, 2),
                            "is_image" => $is_image,
                        );
                    }
                }
            }
        }
        if (!empty($up_data)) {
            $data['comments_attachment'] = json_encode($up_data);
        }
        $data['user_id'] = $this->session->userdata('user_id');

        //save data into table.
        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
        $this->bugs_model->_primary_key = "task_comment_id"; // $id
        $comment_id = $this->bugs_model->save($data);
        if (!empty($comment_id)) {
            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $data['bug_id'],
                'activity' => 'activity_new_bug_comment',
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/2',
                'value1' => $data['comment'],
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $data['bug_id']), 'tbl_bug');

            if (!empty($bugs_info->permission) && $bugs_info->permission != 'all') {
                $user = $this->decode_permission($bugs_info->permission);
                foreach ($user as $key => $v_user) {
                    $notifiedUsers[] = $key;
                }
            } else {
                $notifiedUsers = $this->bugs_model->allowed_user_id('58');
            }

            if (!empty($notifiedUsers)) {
                foreach ($notifiedUsers as $users) {
                    if ($users != $this->session->userdata('user_id')) {
                        add_notification(array(
                            'to_user_id' => $users,
                            'from_user_id' => true,
                            'description' => 'not_new_comment',
                            'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/2',
                            'value' => lang('bug') . ' : ' . $bugs_info->bug_title,
                        ));
                    }
                }
                show_notification($notifiedUsers);
            }
            // send notification
            $this->notify_comments_bugs($comment_id);
            $response_data = "";
            $view_data['comment_details'] = $this->db->where(array('task_comment_id' => $comment_id))->order_by('comment_datetime', 'DESC')->get('tbl_task_comment')->result();
            $response_data = $this->load->view("admin/bugs/comments_list", $view_data, true);
            echo json_encode(array("status" => 'success', "data" => $response_data, 'message' => lang('bug_comment_save')));
            exit();
        } else {
            echo json_encode(array("status" => 'error', 'message' => lang('error_occurred')));
            exit();
        }
    }

    public function save_comments_reply($task_comment_id)
    {
        $data['bug_id'] = $this->input->post('bug_id', TRUE);
        $data['comment'] = $this->input->post('reply_comments', TRUE);
        $data['user_id'] = $this->session->userdata('user_id');
        $data['comments_reply_id'] = $task_comment_id;
        //save data into table.
        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
        $this->bugs_model->_primary_key = "task_comment_id"; // $id
        $comment_id = $this->bugs_model->save($data);
        if (!empty($comment_id)) {
            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $data['bug_id'],
                'activity' => 'activity_new_comment_reply',
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/2',
                'value1' => $this->db->where('task_comment_id', $task_comment_id)->get('tbl_task_comment')->row()->comment,
                'value2' => $data['comment'],
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $data['bug_id']), 'tbl_bug');
            $comments_info = $this->bugs_model->check_by(array('task_comment_id' => $task_comment_id), 'tbl_task_comment');
            $user = $this->bugs_model->check_by(array('user_id' => $comments_info->user_id), 'tbl_users');
            if ($user->role_id == 2) {
                $url = 'client/';
            } else {
                $url = 'admin/';
            }
            $notifiedUsers = array($comments_info->user_id);

            if (!empty($notifiedUsers)) {
                foreach ($notifiedUsers as $users) {
                    if ($users != $this->session->userdata('user_id')) {
                        add_notification(array(
                            'to_user_id' => $users,
                            'from_user_id' => true,
                            'description' => 'not_comment_reply',
                            'link' => $url . 'bugs/view_bug_details/' . $data['bug_id'] . '/2',
                            'value' => lang('bug') . ' : ' . $bugs_info->bug_title,
                        ));
                    }
                }
                show_notification($notifiedUsers);
            }

            // send notification
            $this->notify_comments_bugs($comment_id);
            $response_data = "";
            $view_data['comment_reply_details'] = $this->db->where(array('task_comment_id' => $comment_id))->order_by('comment_datetime', 'ASC')->get('tbl_task_comment')->result();
            $response_data = $this->load->view("admin/bugs/comments_reply", $view_data, true);
            echo json_encode(array("status" => 'success', "data" => $response_data, 'message' => lang('bug_comment_save')));
            exit();
        } else {
            echo json_encode(array("status" => 'error', 'message' => lang('error_occurred')));
            exit();
        }
    }

    function notify_comments_bugs($comment_id)
    {
        $email_template = email_templates(array('email_group' => 'bug_comments'));
        $bugs_comment_info = $this->bugs_model->check_by(array('task_comment_id' => $comment_id), 'tbl_task_comment');

        $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bugs_comment_info->bug_id), 'tbl_bug');
        $message = $email_template->template_body;

        $subject = $email_template->subject;

        $bug_name = str_replace("{BUG_TITLE}", $bugs_info->bug_title, $message);
        $assigned_by = str_replace("{POSTED_BY}", ucfirst($this->session->userdata('name')), $bug_name);
        $Link = str_replace("{COMMENT_URL}", base_url() . 'admin/bugs/view_bug_details/' . $bugs_info->bug_id . '/' . $data['active'] = 2, $assigned_by);
        $comments = str_replace("{COMMENT_MESSAGE}", $bugs_comment_info->comment, $Link);
        $message = str_replace("{SITE_NAME}", config_item('company_name'), $comments);

        $data['message'] = $message;
        $message = $this->load->view('email_template', $data, TRUE);

        $params['subject'] = $subject;
        $params['message'] = $message;
        $params['resourceed_file'] = '';

        if (!empty($bugs_info->permission) && $bugs_info->permission != 'all') {
            $user = $this->decode_permission($bugs_info->permission);
            foreach ($user as $key => $v_user) {
                $allowed_user[] = $key;
            }
        } else {
            $allowed_user = $this->bugs_model->allowed_user_id('58');
        }
        if (!empty($allowed_user)) {
            foreach ($allowed_user as $v_user) {
                $login_info = $this->bugs_model->check_by(array('user_id' => $v_user), 'tbl_users');
                $params['recipient'] = $login_info->email;
                $this->bugs_model->send_email($params);
            }
        }
    }

    public function delete_bug_comments($task_comment_id)
    {
        $comments_info = $this->bugs_model->check_by(array('task_comment_id' => $task_comment_id), 'tbl_task_comment');
        if (empty($comments_info)) {
            echo json_encode(array("status" => 'error', 'message' => "No Record Found"));
            exit();
        }

        if (!empty($comments_info->comments_attachment)) {
            $attachment = json_decode($comments_info->comments_attachment);
            foreach ($attachment as $v_file) {
                remove_files($v_file->fileName);
            }
        }
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'bugs',
            'module_field_id' => $comments_info->bug_id,
            'activity' => 'activity_comment_deleted',
            'icon' => 'fa-bug',
            'link' => 'admin/bugs/view_bug_details/' . $comments_info->bug_id . '/2',
            'value1' => $comments_info->comment,
        );
        // Update into tbl_project
        $this->bugs_model->_table_name = "tbl_activities"; //table name
        $this->bugs_model->_primary_key = "activities_id";
        $this->bugs_model->save($activities);

        //save data into table.
        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
        $this->bugs_model->_primary_key = "task_comment_id"; // $id
        $this->bugs_model->delete($task_comment_id);

        //save data into table.
        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
        $this->bugs_model->delete_multiple(array('comments_reply_id' => $task_comment_id));

        echo json_encode(array("status" => 'success', 'message' => lang('bug_comment_deleted')));
        exit();
    }

    public function delete_bug_files($attachments_id)
    {
        $file_info = $this->bugs_model->check_by(array('attachments_id' => $attachments_id), 'tbl_attachments');
        if (empty($file_info)) {
            echo json_encode(array("status" => 'error', 'message' => "No Record Found"));
            exit();
        }
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'bugs',
            'module_field_id' => $file_info->bug_id,
            'activity' => 'activity_bug_attachfile_deleted',
            'icon' => 'fa-bug',
            'link' => 'admin/bugs/view_bug_details/' . $file_info->bug_id . '/3',
            'value1' => $file_info->title,
        );
        // Update into tbl_project
        $this->bugs_model->_table_name = "tbl_activities"; //table name
        $this->bugs_model->_primary_key = "activities_id";
        $this->bugs_model->save($activities);

        //save data into table.
        $this->bugs_model->_table_name = "tbl_attachments"; // table name        
        $this->bugs_model->delete_multiple(array('attachments_id' => $attachments_id));

        $uploadFileinfo = $this->db->where('attachments_id', $attachments_id)->get('tbl_attachments_files')->result();
        if (!empty($uploadFileinfo)) {
            foreach ($uploadFileinfo as $Fileinfo) {
                remove_files($Fileinfo->file_name);
            }
        }
        //save data into table.
        $this->bugs_model->_table_name = "tbl_attachments_files"; // table name
        $this->bugs_model->delete_multiple(array('attachments_id' => $attachments_id));

        echo json_encode(array("status" => 'success', 'message' => lang('bug_attachfile_deleted')));
        exit();
    }

    public function new_attachment($id)
    {
        $data['dropzone'] = true;
        $data['bugs_details'] = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');
        $data['modal_subview'] = $this->load->view('admin/bugs/new_attachment', $data, FALSE);
    }

    public function attachment_details($type, $id)
    {
        $data['type'] = $type;
        $data['attachment_info'] = $this->bugs_model->check_by(array('attachments_id' => $id), 'tbl_attachments');
        $data['modal_subview'] = $this->load->view('admin/bugs/attachment_details', $data, FALSE);
    }

    public function save_attachment($attachments_id = NULL)
    {

        $data = $this->bugs_model->array_from_post(array('title', 'description', 'bug_id'));

        $data['user_id'] = $this->session->userdata('user_id');

        // save and update into tbl_files
        $this->bugs_model->_table_name = "tbl_attachments"; //table name
        $this->bugs_model->_primary_key = "attachments_id";
        if (!empty($attachments_id)) {
            $id = $attachments_id;
            $this->bugs_model->save($data, $id);
            $msg = lang('project_file_updated');
        } else {
            $id = $this->bugs_model->save($data);
            $msg = lang('project_file_added');
        }
        $files = $this->input->post("files", true);

        $target_path = getcwd() . "/uploads/";
        //process the fiiles which has been uploaded by dropzone
        if (!empty($files) && is_array($files)) {
            foreach ($files as $key => $file) {
                if (!empty($file)) {
                    $file_name = $this->input->post('file_name_' . $file, true);
                    $new_file_name = move_temp_file($file_name, $target_path);
                    $file_ext = explode(".", $new_file_name);
                    $is_image = check_image_extension($new_file_name);

                    if ($new_file_name) {
                        $up_data = array(
                            "files" => "uploads/" . $new_file_name,
                            "uploaded_path" => getcwd() . "/uploads/" . $new_file_name,
                            "file_name" => $new_file_name,
                            "size" => $this->input->post('file_size_' . $file, true),
                            "ext" => end($file_ext),
                            "is_image" => $is_image,
                            "image_width" => 0,
                            "image_height" => 0,
                            "attachments_id" => $id
                        );
                        $this->bugs_model->_table_name = "tbl_attachments_files"; // table name
                        $this->bugs_model->_primary_key = "uploaded_files_id"; // $id
                        $uploaded_files_id = $this->bugs_model->save($up_data);

                        // saved into comments
                        $comment = $this->input->post('comment_' . $file, true);
                        $u_cdata = array(
                            "comment" => $comment,
                            "bug_id" => $data['bug_id'],
                            "user_id" => $this->session->userdata('user_id'),
                            "uploaded_files_id" => $uploaded_files_id,
                        );
                        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
                        $this->bugs_model->_primary_key = "task_comment_id"; // $id
                        $this->bugs_model->save($u_cdata);

                        $success = true;
                    } else {
                        $success = false;
                    }
                }
            }
        }
        //process the files which has been submitted manually
        if ($_FILES) {
            $files = $_FILES['manualFiles'];
            if ($files && count(array($files)) > 0) {
                $comment = $this->input->post('comment', true);
                foreach ($files["tmp_name"] as $key => $file) {
                    $temp_file = $file;
                    $file_name = $files["name"][$key];
                    $file_size = $files["size"][$key];
                    $new_file_name = move_temp_file($file_name, $target_path, "", $temp_file);
                    if ($new_file_name) {
                        $file_ext = explode(".", $new_file_name);
                        $is_image = check_image_extension($new_file_name);
                        $up_data = array(
                            "files" => "uploads/" . $new_file_name,
                            "uploaded_path" => getcwd() . "/uploads/" . $new_file_name,
                            "file_name" => $new_file_name,
                            "size" => $file_size,
                            "ext" => end($file_ext),
                            "is_image" => $is_image,
                            "image_width" => 0,
                            "image_height" => 0,
                            "attachments_id" => $id
                        );
                        $this->bugs_model->_table_name = "tbl_attachments_files"; // table name
                        $this->bugs_model->_primary_key = "uploaded_files_id"; // $id
                        $uploaded_files_id = $this->bugs_model->save($up_data);

                        // saved into comments
                        if (!empty($comment[$key])) {
                            $u_cdata = array(
                                "comment" => $comment[$key],
                                "user_id" => $this->session->userdata('user_id'),
                                "uploaded_files_id" => $uploaded_files_id,
                            );
                            $this->bugs_model->_table_name = "tbl_task_comment"; // table name
                            $this->bugs_model->_primary_key = "task_comment_id"; // $id
                            $this->bugs_model->save($u_cdata);
                        }
                    }
                }
            }
        }

        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'bugs',
            'module_field_id' => $data['bug_id'],
            'activity' => 'activity_new_project_attachment',
            'icon' => 'fa-folder-open-o',
            'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/3',
            'value1' => $data['title'],
        );
        // Update into tbl_project
        $this->bugs_model->_table_name = "tbl_activities"; //table name
        $this->bugs_model->_primary_key = "activities_id";
        $this->bugs_model->save($activities);

        // send notification message
        $this->notify_attchemnt_bugs($id);

        // messages for user
        $type = "success";
        $message = $msg;
        set_message($type, $message);
        redirect('admin/bugs/view_bug_details/' . $data['bug_id'] . '/' . '3');
    }

    public function save_attachment_comments()
    {
        $attachments_id = $this->input->post('attachments_id', true);
        if (!empty($attachments_id)) {
            $data['attachments_id'] = $attachments_id;
        } else {
            $data['uploaded_files_id'] = $this->input->post('uploaded_files_id', true);
        }
        $data['bug_id'] = $this->input->post('bug_id', true);
        $data['comment'] = $this->input->post('description', true);

        $files = $this->input->post("files", true);
        $target_path = getcwd() . "/uploads/";
        //process the fiiles which has been uploaded by dropzone
        if (!empty($files) && is_array($files)) {
            foreach ($files as $key => $file) {
                if (!empty($file)) {
                    $file_name = $this->input->post('file_name_' . $file, true);
                    $new_file_name = move_temp_file($file_name, $target_path);
                    $file_ext = explode(".", $new_file_name);
                    $is_image = check_image_extension($new_file_name);
                    $size = $this->input->post('file_size_' . $file, true) / 1000;
                    if ($new_file_name) {
                        $up_data[] = array(
                            "fileName" => $new_file_name,
                            "path" => "uploads/" . $new_file_name,
                            "fullPath" => getcwd() . "/uploads/" . $new_file_name,
                            "ext" => '.' . end($file_ext),
                            "size" => round($size, 2),
                            "is_image" => $is_image,
                        );
                        $success = true;
                    } else {
                        $success = false;
                    }
                }
            }
        }
        //process the files which has been submitted manually
        if ($_FILES) {
            $files = $_FILES['manualFiles'];
            if ($files && count(array($files)) > 0) {
                $comment = $this->input->post('comment', true);
                foreach ($files["tmp_name"] as $key => $file) {
                    $temp_file = $file;
                    $file_name = $files["name"][$key];
                    $file_size = $files["size"][$key];
                    $new_file_name = move_temp_file($file_name, $target_path, "", $temp_file);
                    if ($new_file_name) {
                        $file_ext = explode(".", $new_file_name);
                        $is_image = check_image_extension($new_file_name);
                        $up_data[] = array(
                            "fileName" => $new_file_name,
                            "path" => "uploads/" . $new_file_name,
                            "fullPath" => getcwd() . "/uploads/" . $new_file_name,
                            "ext" => '.' . end($file_ext),
                            "size" => round($file_size, 2),
                            "is_image" => $is_image,
                        );
                    }
                }
            }
        }
        if (!empty($up_data)) {
            $data['comments_attachment'] = json_encode($up_data);
        }
        $data['user_id'] = $this->session->userdata('user_id');

        //save data into table.
        $this->bugs_model->_table_name = "tbl_task_comment"; // table name
        $this->bugs_model->_primary_key = "task_comment_id"; // $id
        $comment_id = $this->bugs_model->save($data);
        if (!empty($comment_id)) {
            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $data['bug_id'],
                'activity' => 'activity_new_bug_comment',
                'icon' => 'fa-bug',
                'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/2',
                'value1' => $data['comment'],
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);

            $notifiedUsers = array();
            $bugs_info = $this->bugs_model->check_by(array('bug_id' => $data['bug_id']), 'tbl_bug');

            if (!empty($bugs_info->permission) && $bugs_info->permission != 'all') {
                $user = $this->decode_permission($bugs_info->permission);
                foreach ($user as $key => $v_user) {
                    $notifiedUsers[] = $key;
                }
            } else {
                $notifiedUsers = $this->bugs_model->allowed_user_id('58');
            }
            if (!empty($notifiedUsers)) {
                foreach ($notifiedUsers as $users) {
                    if ($users != $this->session->userdata('user_id')) {
                        add_notification(array(
                            'to_user_id' => $users,
                            'from_user_id' => true,
                            'description' => 'not_new_comment',
                            'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/2',
                            'value' => lang('bug') . ' : ' . $bugs_info->bug_title,
                        ));
                    }
                }
                show_notification($notifiedUsers);
            }
            $response_data = "";
            $view_data['comment_details'] = $this->db->where(array('task_comment_id' => $comment_id))->order_by('comment_datetime', 'DESC')->get('tbl_task_comment')->result();
            $response_data = $this->load->view("admin/bugs/comments_list", $view_data, true);
            echo json_encode(array("status" => 'success', "data" => $response_data, 'message' => lang('bug_comment_save')));
            exit();
        } else {
            echo json_encode(array("status" => 'error', 'message' => lang('error_occurred')));
            exit();
        }
    }

    public function download_files($uploaded_files_id, $comments = null)
    {
        $this->load->helper('download');
        if (!empty($comments)) {
            if ($uploaded_files_id) {
                $down_data = file_get_contents('uploads/' . $uploaded_files_id); // Read the file's contents
                force_download($uploaded_files_id, $down_data);
            } else {
                echo json_encode(array("status" => 'error', 'message' => lang('error_occurred')));
                exit();
            }
        } else {
            $uploaded_files_info = $this->bugs_model->check_by(array('uploaded_files_id' => $uploaded_files_id), 'tbl_attachments_files');
            if ($uploaded_files_info->uploaded_path) {
                $data = file_get_contents($uploaded_files_info->uploaded_path); // Read the file's contents
                force_download($uploaded_files_info->file_name, $data);
            } else {
                echo json_encode(array("status" => 'error', 'message' => lang('error_occurred')));
                exit();
            }
        }
    }

    public function download_all_files($attachment_id)
    {
        $uploaded_files_info = $this->db->where('attachments_id', $attachment_id)->get('tbl_attachments_files')->result();
        $attachment_info = $this->db->where('attachments_id', $attachment_id)->get('tbl_attachments')->row();
        $this->load->library('zip');
        if (!empty($uploaded_files_info)) {
            $filename = slug_it($attachment_info->title);
            foreach ($uploaded_files_info as $v_files) {
                $down_data = ($v_files->files); // Read the file's contents
                $this->zip->read_file($down_data);
            }
            $this->zip->download($filename . '.zip');
        } else {
            $type = "error";
            $message = lang('operation_failed');
            set_message($type, $message);
            redirect('admin/bugs/view_bug_details/' . $attachment_info->bug_id . '/3');
        }
    }


    public function save_bug_attachment($attachments_id = NULL)
    {
        $data = $this->bugs_model->array_from_post(array('title', 'description', 'bug_id'));
        $data['user_id'] = $this->session->userdata('user_id');

        // save and update into tbl_files
        $this->bugs_model->_table_name = "tbl_attachments"; //table name
        $this->bugs_model->_primary_key = "attachments_id";
        if (!empty($attachments_id)) {
            $id = $attachments_id;
            $this->bugs_model->save($data, $id);
            $msg = lang('bug_file_updated');
        } else {
            $id = $this->bugs_model->save($data);
            $msg = lang('bug_file_added');
        }

        if (!empty($_FILES['bug_files']['name']['0'])) {
            $old_path_info = $this->input->post('uploaded_path', true);
            if (!empty($old_path_info)) {
                foreach ($old_path_info as $old_path) {
                    unlink($old_path);
                }
            }
            $mul_val = $this->bugs_model->multi_uploadAllType('bug_files');

            foreach ($mul_val as $val) {
                $val == TRUE || redirect('admin/bugs/view_bug_details/3/' . $data['bug_id']);
                $fdata['files'] = $val['path'];
                $fdata['file_name'] = $val['fileName'];
                $fdata['uploaded_path'] = $val['fullPath'];
                $fdata['size'] = $val['size'];
                $fdata['ext'] = $val['ext'];
                $fdata['is_image'] = $val['is_image'];
                $fdata['image_width'] = $val['image_width'];
                $fdata['image_height'] = $val['image_height'];
                $fdata['attachments_id'] = $id;
                $this->bugs_model->_table_name = "tbl_attachments_files"; // table name
                $this->bugs_model->_primary_key = "uploaded_files_id"; // $id
                $this->bugs_model->save($fdata);
            }
        }
        // save into activities
        $activities = array(
            'user' => $this->session->userdata('user_id'),
            'module' => 'bugs',
            'module_field_id' => $data['bug_id'],
            'activity' => 'activity_new_bug_attachment',
            'icon' => 'fa-bug',
            'link' => 'admin/bugs/view_bug_details/' . $data['bug_id'] . '/3',
            'value1' => $data['title'],
        );
        // Update into tbl_project
        $this->bugs_model->_table_name = "tbl_activities"; //table name
        $this->bugs_model->_primary_key = "activities_id";
        $this->bugs_model->save($activities);
        // send notification message
        $this->notify_attchemnt_bugs($id);
        // messages for user
        $type = "success";
        $message = $msg;
        set_message($type, $message);
        redirect('admin/bugs/view_bug_details/' . $data['bug_id'] . '/3');
    }

    function notify_attchemnt_bugs($attachments_id)
    {
        $email_template = email_templates(array('email_group' => 'bug_attachment'));
        $bugs_comment_info = $this->bugs_model->check_by(array('attachments_id' => $attachments_id), 'tbl_attachments');

        $bugs_info = $this->bugs_model->check_by(array('bug_id' => $bugs_comment_info->bug_id), 'tbl_bug');

        $message = $email_template->template_body;

        $subject = $email_template->subject;

        $bug_name = str_replace("{BUG_TITLE}", $bugs_info->bug_title, $message);
        $assigned_by = str_replace("{UPLOADED_BY}", ucfirst($this->session->userdata('name')), $bug_name);
        $Link = str_replace("{BUG_URL}", base_url() . 'admin/bugs/view_bug_details/' . $bugs_info->bug_id . '/' . $data['active'] = 3, $assigned_by);
        $message = str_replace("{SITE_NAME}", config_item('company_name'), $Link);

        $data['message'] = $message;
        $message = $this->load->view('email_template', $data, TRUE);

        $params['subject'] = $subject;
        $params['message'] = $message;
        $params['resourceed_file'] = '';
        if (!empty($bugs_info->permission) && $bugs_info->permission != 'all') {
            $user = $this->decode_permission($bugs_info->permission);
            foreach ($user as $key => $v_user) {
                $allowed_user[] = $key;
            }
        } else {
            $allowed_user = $this->bugs_model->allowed_user_id('58');
        }
        if (!empty($allowed_user)) {
            foreach ($allowed_user as $v_user) {
                $login_info = $this->bugs_model->check_by(array('user_id' => $v_user), 'tbl_users');
                $params['recipient'] = $login_info->email;
                $this->bugs_model->send_email($params);

                if ($v_user != $this->session->userdata('user_id')) {
                    add_notification(array(
                        'to_user_id' => $v_user,
                        'from_user_id' => true,
                        'description' => 'not_uploaded_attachment',
                        'link' => 'admin/bugs/view_bug_details/' . $bugs_info->bug_id . '/2',
                        'value' => lang('bug') . ' : ' . $bugs_info->bug_title,
                    ));
                }
            }
            show_notification($allowed_user);
        }
    }

    public function details($id, $active = NULL, $edit = NULL)
    {
        $data['title'] = lang('bug_details');
        $data['page_header'] = lang('bug_management');
        //get all bug information
        $data['bug_details'] = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');
        if (empty($data['bug_details'])) {
            set_message('error', lang('there_in_no_value'));
            redirect('admin/bugs');
        }
        $data['bug_statuses'] = $this->bugs_model->get_statuses();
        $data['custom_fields'] = custom_form_label(6, $id);
        $data['project_info'] = !empty($data['bug_details']->project_id)
            ? get_row('tbl_project', array('project_id' => $data['bug_details']->project_id))
            : null;
        $data['opportunity_info'] = !empty($data['bug_details']->opportunities_id)
            ? get_row('tbl_opportunities', array('opportunities_id' => $data['bug_details']->opportunities_id))
            : null;
        $data['reporter_user'] = !empty($data['bug_details']->reporter)
            ? $this->db->where('user_id', $data['bug_details']->reporter)->get('tbl_users')->row()
            : null;
        $data['reporter_profile'] = !empty($data['bug_details']->reporter)
            ? $this->db->where('user_id', $data['bug_details']->reporter)->get('tbl_account_details')->row()
            : null;
        $participants = array();
        $participants_all = false;
        if (!empty($data['bug_details']->permission) && $data['bug_details']->permission != 'all') {
            $get_permission = $this->decode_permission($data['bug_details']->permission);
            if (!empty($get_permission)) {
                foreach ($get_permission as $permission => $v_permission) {
                    $user_info = $this->db->where(array('user_id' => $permission))->get('tbl_users')->row();
                    $profile_info = $this->db->where(array('user_id' => $permission))->get('tbl_account_details')->row();
                    if (!empty($user_info) && !empty($profile_info)) {
                        $participants[] = array(
                            'user_id' => $permission,
                            'fullname' => $profile_info->fullname,
                            'avatar' => $profile_info->avatar,
                            'role_id' => $user_info->role_id,
                        );
                    }
                }
            }
        } else {
            $participants_all = true;
        }
        $data['participants'] = $participants;
        $data['participants_all'] = $participants_all;
        $data['dropzone'] = true;
        if (empty($active)) {
            $data['active'] = 'details';
        } else {
            $data['active'] = $active;
        }
        $data['all_tabs'] = bugs_details_tabs($id);
        $data['module'] = 'bugs';
        $data['id'] = $id;
		$data['perm'] = '58';
		$data['redirect'] = 'bugs/details';
		$data['table'] = 'tbl_bug';
        $data['page_title'] = !empty($data['bug_details']->bug_title) ? $data['bug_details']->bug_title : lang('bug_details');
        $bug_code = !empty($data['bug_details']->bug_code) ? $data['bug_details']->bug_code : ('#' . $id);
        $related_name = !empty($data['project_info']->project_name) ? $data['project_info']->project_name : (!empty($data['opportunity_info']->opportunity_name) ? $data['opportunity_info']->opportunity_name : lang('standalone_bug'));
        $severity_name = !empty($data['bug_details']->severity) ? $data['bug_details']->severity : '-';
        $data['page_description'] = $related_name . ' . ' . lang('severity') . ': ' . $severity_name . ' . ' . lang('status') . ': ' . lang($data['bug_details']->bug_status);
        $bug_tone = $data['bug_details']->bug_status === 'resolved' ? 'success' : ($data['bug_details']->bug_status === 'in_progress' ? 'primary' : ($data['bug_details']->bug_status === 'confirmed' ? 'warning' : 'neutral'));
        $data['header_badge'] = array(
            'label' => $bug_code,
            'tone' => $bug_tone,
        );
        $edited = can_action('58', 'edited');
        $deleted = can_action('58', 'deleted');
        $can_edit = $this->bugs_model->can_action('tbl_bug', 'edit', array('bug_id' => $id));
        $can_delete = $this->bugs_model->can_action('tbl_bug', 'delete', array('bug_id' => $id));
        $data['header_actions'] = array();
        if (!empty($can_edit) && !empty($edited)) {
            $data['header_actions'][] = array(
                'label' => lang('edit'),
                'icon' => 'pencil',
                'href' => base_url('admin/bugs/create/' . $data['bug_details']->bug_id),
				'style' => 'primary',
            );
        }
        if (!empty($can_delete) && !empty($deleted)) {
            $data['header_actions'][] = array(
                'label' => lang('delete'),
                'icon' => 'trash-2',
                'href' => base_url('admin/bugs/delete_bug/' . $data['bug_details']->bug_id),
                'style' => 'danger',
                'attrs' => array(
                    'onclick' => "return confirm('" . lang('delete_alert') . "');",
                ),
            );
        }
        $bug_tab_labels = array(
            'details' => lang('details'),
            'comments' => lang('comments'),
            'attachments' => lang('attachments'),
            'tasks' => lang('tasks'),
            'notes' => lang('notes'),
            'activities' => lang('activities'),
        );
        $bug_filter_items = array();
        foreach ($data['all_tabs'] as $tab) {
            if (empty($tab['name']) || empty($tab['url'])) {
                continue;
            }
            $bug_filter_items[] = array(
                'label' => isset($bug_tab_labels[$tab['name']]) ? $bug_tab_labels[$tab['name']] : ucfirst(str_replace('_', ' ', $tab['name'])),
                'icon' => !empty($tab['icon']) ? $tab['icon'] : 'chevron-right',
                'href' => base_url($tab['url']),
                'active' => $data['active'] === $tab['name'],
                'meta' => isset($tab['count']) && $tab['count'] !== '' ? (string) $tab['count'] : null,
            );
        }
		// 'label' => 'Filters',
        //         'icon' => 'sliders-horizontal',
        //         'selected_label' => lang('all'),
        //         'attrs' => array('id' => 'bugs-header-filter'),
        $data['header_filters'] = array(
            array(
                'label' => lang('more_actions'),
				'selected_label' => lang($data['active']),
                'icon' => 'ellipsis',
				'attrs' => array('id' => 'bugs-header-actions'),
                'items' => $bug_filter_items,
            ),
        );
        $data['subview'] = $this->load->view('admin/common/tab_view', $data, TRUE);
        $this->load->view('admin/_layout_main', $data);
    }


    public function delete_bug($id)
    {
        $can_delete = $this->bugs_model->can_action('tbl_bug', 'delete', array('bug_id' => $id));
        $deleted = can_action('58', 'deleted');
        if (!empty($can_delete) && !empty($deleted)) {
            $bug_info = $this->bugs_model->check_by(array('bug_id' => $id), 'tbl_bug');
            if (empty($bug_info)) {
                $type = "error";
                $message = "No Record Found";
                $this->delete_response($type, $message);
            }

            // save into activities
            $activities = array(
                'user' => $this->session->userdata('user_id'),
                'module' => 'bugs',
                'module_field_id' => $bug_info->bug_id,
                'activity' => 'activity_bug_deleted',
                'icon' => 'fa-bug',
                'value1' => $bug_info->bug_title,
            );
            // Update into tbl_project
            $this->bugs_model->_table_name = "tbl_activities"; //table name
            $this->bugs_model->_primary_key = "activities_id";
            $this->bugs_model->save($activities);
            $mwhere = array('module' => 'bugs', 'module_field_id' => $id);
            $this->bugs_model->_table_name = "tbl_attachments"; //table name
            $this->bugs_model->_order_by = "module_field_id";
            $files_info = $this->bugs_model->get_by($mwhere, FALSE);

            foreach ($files_info as $v_files) {
                $uploadFileinfo = $this->db->where('attachments_id', $v_files->attachments_id)->get('tbl_attachments_files')->result();
                if (!empty($uploadFileinfo)) {
                    foreach ($uploadFileinfo as $Fileinfo) {
                        remove_files($Fileinfo->file_name);
                    }
                }
                $this->bugs_model->_table_name = "tbl_attachments_files"; //table name
                $this->bugs_model->delete_multiple(array('attachments_id' => $v_files->attachments_id));
            }
            //delete into table.
            $this->bugs_model->_table_name = "tbl_attachments"; // table name
            $this->bugs_model->delete_multiple($mwhere);

            // deleted comments with file
            $all_comments_info = $this->db->where($mwhere)->get('tbl_task_comment')->result();
            if (!empty($all_comments_info)) {
                foreach ($all_comments_info as $comments_info) {
                    if (!empty($comments_info->comments_attachment)) {
                        $attachment = json_decode($comments_info->comments_attachment);
                        foreach ($attachment as $v_file) {
                            remove_files($v_file->fileName);
                        }
                    }
                }
            }
            //delete data into table.
            $this->bugs_model->_table_name = "tbl_task_comment"; // table name
            $this->bugs_model->delete_multiple($mwhere);

            $pin_info = $this->bugs_model->check_by(array('module_name' => 'bugs', 'module_id' => $id), 'tbl_pinaction');
            if (!empty($pin_info)) {
                $this->bugs_model->_table_name = 'tbl_pinaction';
                $this->bugs_model->delete_multiple(array('module_name' => 'bugs', 'module_id' => $id));
            }
            $this->bugs_model->_table_name = "tbl_bug"; // table name
            $this->bugs_model->_primary_key = "bug_id"; // $id
            $this->bugs_model->delete($id);

            $type = "success";
            $message = lang('bug_deleted');
        } else {
            $type = "error";
            $message = lang('there_in_no_value');
        }
        $this->delete_response($type, $message);
    }

    public function view_bug_details($id, $active = NULL, $edit = NULL)
    {
        $this->details($id, $active = NULL, $edit = NULL);
    }
}
