<?php echo message_box('success'); ?>
<?php echo message_box('error'); ?>
<style>
    .note-editor .note-editable {
        height: 150px;
    }

    a:hover {
        text-decoration: none;
    }

    .client-task-tab-shell {
        display: flex;
        flex-wrap: wrap;
        gap: .5rem;
        align-items: center;
        margin: 0;
        padding: .75rem;
        list-style: none;
    }

    .client-task-tab-shell > li > a {
        display: inline-flex;
        align-items: center;
        gap: .5rem;
        min-height: 2.75rem;
        border-radius: .75rem;
        padding: .65rem 1rem;
        color: hsl(var(--muted-foreground));
        font-size: .875rem;
        font-weight: 700;
        line-height: 1;
        transition: background-color .15s ease, color .15s ease, box-shadow .15s ease;
    }

    .client-task-tab-shell > li > a:hover {
        background: hsl(var(--muted));
        color: hsl(var(--foreground));
    }

    .client-task-tab-shell > li.active > a,
    .client-task-tab-shell > li.active > a:hover,
    .client-task-tab-shell > li.active > a:focus {
        background: #0f172a;
        color: #fff;
        box-shadow: 0 8px 18px rgba(15, 23, 42, .16);
    }

    .client-task-tab-count {
        display: inline-flex;
        min-width: 1.25rem;
        height: 1.25rem;
        align-items: center;
        justify-content: center;
        border-radius: 999px;
        background: hsl(var(--muted));
        color: hsl(var(--muted-foreground));
        padding: 0 .4rem;
        font-size: .75rem;
        font-weight: 800;
    }

    .client-task-tab-shell > li.active .client-task-tab-count {
        background: rgba(255, 255, 255, .9);
        color: #0f172a;
    }

    .client-task-anchor-section {
        scroll-margin-top: 7rem;
    }

    .client-task-anchor-section + .client-task-anchor-section {
        margin-top: 1.5rem;
    }
</style>
<?php
$project_info = null;
$project_settings = array();
if (!empty($task_details->project_id)) {
    $project_info = $this->db->where('project_id', $task_details->project_id)->get('tbl_project')->row();
    if (!empty($project_info->project_settings)) {
        $project_settings = (array) json_decode($project_info->project_settings);
    }
}
// get all comments by tasks id
$comment_details = $this->db->where('module_field_id', $task_details->module_field_id)->get('tbl_task_comment')->result();
// get all $total_timer by tasks id
$activities_info = $this->db->where(array('module' => 'tasks', 'module_field_id' => $task_details->task_id))->order_by('activity_date', 'desc')->get('tbl_activities')->result();

$status_classes = array(
    'completed' => 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/40 dark:bg-emerald-950/30 dark:text-emerald-200',
    'not_started' => 'border-sky-200 bg-sky-50 text-sky-700 dark:border-sky-900/40 dark:bg-sky-950/30 dark:text-sky-200',
    'deferred' => 'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-900/40 dark:bg-rose-950/30 dark:text-rose-200',
    'in_progress' => 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/40 dark:bg-amber-950/30 dark:text-amber-200',
);
$task_status_class = isset($status_classes[$task_details->task_status]) ? $status_classes[$task_details->task_status] : 'border-border bg-muted text-muted-foreground';
$timer_is_on = !empty($task_details->timer_status) && $task_details->timer_status == 'on';
$task_due_time = !empty($task_details->due_date) ? safe_strtotime($task_details->due_date) : null;
$task_is_overdue = !empty($task_due_time) && safe_strtotime(date('Y-m-d')) > $task_due_time && $task_details->task_status != 'completed';
$created_by_info = !empty($task_details->created_by) ? $this->db->where('user_id', $task_details->created_by)->get('tbl_account_details')->row() : null;
$milestones_info = !empty($task_details->milestones_id) ? $this->db->where('milestones_id', $task_details->milestones_id)->get('tbl_milestones')->row() : null;
$task_participants = array();
if (!empty($task_details->permission) && $task_details->permission != 'all') {
    $get_permission = json_decode($task_details->permission);
    if (!empty($get_permission)) {
        foreach ($get_permission as $permission => $v_permission) {
            $profile_info = $this->db->where(array('user_id' => $permission))->get('tbl_account_details')->row();
            if (!empty($profile_info)) {
                $task_participants[] = $profile_info;
            }
        }
    }
}
?>
<div class="mt-6 max-w-full space-y-6">
    <nav class="min-w-0 rounded-2xl border border-border bg-card shadow-sm" aria-label="<?= lang('task') ?>">
        <ul class="nav nav-pills client-task-tab-shell">
            <li class="<?= $active == 1 ? 'active' : '' ?>" data-task-anchor-nav="task_details"><a href="<?= current_url() ?>#task_details"><i data-lucide="check-square" class="h-4 w-4"></i><?= lang('tasks') . ' ' . lang('details') ?></a>
            </li>
            <?php if (!empty($project_settings[11]) && $project_settings[11] == 'comment_on_project_tasks') { ?>
                <li class="<?= $active == 2 ? 'active' : '' ?>"><a href="#task_comments"
                                                                   data-toggle="tab"><i data-lucide="message-square" class="h-4 w-4"></i><?= lang('comments') ?> <?php if (!empty($comment_details)) { ?><span class="client-task-tab-count"><?= count($comment_details) ?></span><?php } ?></a>
                </li>
            <?php } ?>
            <?php if (!empty($project_settings[12]) && $project_settings[12] == 'show_project_tasks_attachments') { ?>
                <li class="<?= $active == 3 ? 'active' : '' ?>"><a href="#task_attachments"
                                                                   data-toggle="tab"><i data-lucide="paperclip" class="h-4 w-4"></i><?= lang('attachment') ?>
                        <?php if (!empty($project_files_info)) { ?><span class="client-task-tab-count"><?= count($project_files_info) ?></span><?php } ?></a>
                </li>
            <?php } ?>
            <li class="<?= $active == 6 ? 'active' : '' ?>" data-task-anchor-nav="activities"><a href="<?= current_url() ?>#activities"><i data-lucide="activity" class="h-4 w-4"></i><?= lang('activities') ?><?php if (!empty($activities_info)) { ?><span class="client-task-tab-count"><?= count($activities_info) ?></span><?php } ?></a>
            </li>
        </ul>
    </nav>
    <div class="min-w-0">
        <div class="tab-content min-w-0 rounded-2xl border border-border bg-card p-4 shadow-sm">
            <!-- Task Details tab Starts -->
            <section class="client-task-anchor-section" id="task_details" style="position: relative;">
                <?php $this->load->view('client/tasks/partials/details', get_defined_vars()); ?>
            </section>
            <!-- Task Details tab Ends -->

            <!-- Task Comments Panel Starts --->
            <?php if (!empty($project_settings[11]) && $project_settings[11] == 'comment_on_project_tasks') { ?>
                <div class="tab-pane <?= $active == 2 ? 'active' : '' ?>" id="task_comments"
                     style="position: relative;">
                    <div class="panel panel-custom">
                        <div class="panel-heading">
                            <h3 class="panel-title"><?= lang('comments') ?></h3>
                        </div>
                        <div class="panel-body chat" id="chat-box">
                            <form id="form_validation"
                                  action="<?php echo base_url() ?>client/tasks/save_comments" method="post"
                                  class="form-horizontal">
                                <input type="hidden" name="task_id" value="<?php
                                if (!empty($task_details->task_id)) {
                                    echo $task_details->task_id;
                                }
                                ?>" class="form-control">
                                <div class="form-group">
                                    <div class="col-sm-12">
                                                <textarea class="form-control textarea"
                                                          placeholder="<?= $task_details->task_name . ' ' . lang('comments') ?>"
                                                          name="comment" style="height: 70px;"></textarea>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <div class="pull-right">
                                            <button type="submit" id="sbtn"
                                                    class="btn btn-primary"><?= lang('post_comment') ?></button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <hr/>

                            <?php

                            if (!empty($comment_details)):foreach ($comment_details as $key => $v_comment):
                                $user_info = $this->db->where(array('user_id' => $v_comment->user_id))->get('tbl_users')->row();
                                $profile_info = $this->db->where(array('user_id' => $v_comment->user_id))->get('tbl_account_details')->row();
                                if ($user_info->role_id == 1) {
                                    $label = '<small style="font-size:10px;padding:2px;" class="label label-danger ">' . lang('admin') . '</small>';
                                } elseif ($user_info->role_id == 3) {
                                    $label = '<small style="font-size:10px;padding:2px;" class="label label-primary">' . lang('staff') . '</small>';
                                } else {
                                    $label = '<small style="font-size:10px;padding:2px;" class="label label-success">' . lang('client') . '</small>';
                                }
                                ?>

                                <div class="col-sm-12 item ">

                                    <img src="<?php echo base_url() . $profile_info->avatar ?>" alt="user image"
                                         class="img-xs img-circle"/>


                                    <p class="message">
                                        <small class="text-muted pull-right"><i
                                                data-lucide="clock-3"></i> <?= time_ago($v_comment->comment_datetime) ?>
                                            <?php if ($v_comment->user_id == $this->session->userdata('user_id')) { ?>
                                                <?= btn_delete('client/tasks/delete_task_comments/' . $v_comment->task_id . '/' . $v_comment->task_comment_id) ?>
                                            <?php } ?></small>
                                        <span class="name">
                                            <?= ($profile_info->fullname) . ' ' . $label ?>
                                        </span>

                                        <?php if (!empty($v_comment->comment)) echo $v_comment->comment; ?>
                                    </p>

                                </div><!-- /.item -->
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php } ?>
            <!-- Task Comments Panel Ends--->
            <!-- Task Attachment Panel Starts --->
            <?php if (!empty($project_settings[12]) && $project_settings[12] == 'show_project_tasks_attachments') { ?>
                <div class="tab-pane <?= $active == 3 ? 'active' : '' ?>" id="task_attachments"
                     style="position: relative;">
                    <div class="panel panel-custom">
                        <div class="panel-heading">
                            <h3 class="panel-title"><?= lang('attachment') ?></h3>
                        </div>
                        <div class="panel-body">

                            <form action="<?= base_url() ?>client/tasks/save_task_attachment/<?php
                            if (!empty($add_files_info)) {
                                echo $add_files_info->attachments_id;
                            }
                            ?>" enctype="multipart/form-data" method="post" id="form" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-lg-3 control-label"><?= lang('file_title') ?> <span
                                            class="text-danger">*</span></label>
                                    <div class="col-lg-6">
                                        <input name="title" class="form-control" value="<?php
                                        if (!empty($add_files_info)) {
                                            echo $add_files_info->title;
                                        }
                                        ?>" required placeholder="<?= lang('file_title') ?>"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label"><?= lang('description') ?></label>
                                    <div class="col-lg-6">
                                        <textarea name="description" class="form-control"
                                                  placeholder="<?= lang('description') ?>"><?php
                                            if (!empty($add_files_info)) {
                                                echo $add_files_info->description;
                                            }
                                            ?></textarea>
                                    </div>
                                </div>
                                <?php if (empty($add_files_info)) { ?>
                                    <div id="add_new">
                                        <div class="form-group" style="margin-bottom: 0px">
                                            <label for="field-1"
                                                   class="col-sm-3 control-label"><?= lang('upload_file') ?></label>
                                            <div class="col-sm-6">
                                                <div class="fileinput fileinput-new" data-provides="fileinput">
                                                    <?php if (!empty($project_files)):foreach ($project_files as $v_files_image): ?>
                                                        <span class=" btn btn-default btn-file"><span
                                                                class="fileinput-new"
                                                                style="display: none">Select file</span>
                                                                <span class="fileinput-exists"
                                                                      style="display: block"><?= lang('change') ?></span>
                                                                <input type="hidden" name="task_files[]"
                                                                       value="<?php echo $v_files_image->files ?>">
                                                                <input type="file" name="task_files[]">
                                                            </span>
                                                        <span
                                                            class="fileinput-filename"> <?php echo $v_files_image->file_name ?></span>
                                                    <?php endforeach; ?>
                                                    <?php else: ?>
                                                        <span class="btn btn-default btn-file"><span
                                                                class="fileinput-new"><?= lang('select_file') ?></span>
                                                            <span class="fileinput-exists"><?= lang('change') ?></span>
                                                            <input type="file" name="task_files[]">
                                                        </span>
                                                        <span class="fileinput-filename"></span>
                                                        <a href="#" class="close fileinput-exists"
                                                           data-dismiss="fileinput"
                                                           style="float: none;">&times;</a>
                                                    <?php endif; ?>
                                                </div>
                                                <div id="msg_pdf" style="color: #e11221"></div>
                                            </div>
                                            <div class="col-sm-2">
                                                <strong><a href="javascript:void(0);" id="add_more"
                                                           class="addCF "><i
                                                            data-lucide="plus"></i>&nbsp;<?= lang('add_more') ?>
                                                    </a></strong>
                                            </div>
                                        </div>
                                    </div>
                                <?php } ?>
                                <br/>
                                <input type="hidden" name="task_id" value="<?php
                                if (!empty($task_details->task_id)) {
                                    echo $task_details->task_id;
                                }
                                ?>" class="form-control">
                                <div class="form-group">
                                    <div class="col-sm-3">
                                    </div>
                                    <div class="col-sm-3">
                                        <button type="submit"
                                                class="btn btn-primary"><?= lang('upload_file') ?></button>
                                    </div>
                                </div>
                            </form>

                        </div>
                    </div>
                    <?php
                    if (!empty($project_files_info)) {
                        ?>
                        <div class="panel">
                            <div class="panel-heading" style="border-bottom: 2px solid #00BCD4">
                                <strong><?= lang('attach_file_list') ?></strong></div>
                            <div class="panel-body">
                                <?php
                                $this->load->helper('file');
                                foreach ($project_files_info as $key => $v_files_info) {
                                    ?>
                                    <div class="panel-group" id="accordion" style="margin:8px 0px"
                                         role="tablist" aria-multiselectable="true">
                                        <div class="box box-info" style="border-radius: 0px ">
                                            <div class="panel-heading pl-sm" role="tab" id="headingOne" style="">
                                                <h4 class="panel-title">
                                                    <a data-toggle="collapse" data-parent="#accordion"
                                                       href="#<?php echo $key ?>" aria-expanded="true"
                                                       aria-controls="collapseOne">
                                                        <strong
                                                            style="text-decoration: underline"><?php echo $files_info[$key]->title; ?> </strong>
                                                        <small style="color:#ffffff " class="pull-right">
                                                            <?php if ($files_info[$key]->user_id == $this->session->userdata('user_id')) { ?>
                                                                <?= btn_delete('client/tasks/delete_task_files/' . $files_info[$key]->task_id . '/' . $files_info[$key]->attachments_id) ?>
                                                            <?php } ?></small>
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="<?php echo $key ?>" class="panel-collapse collapse <?php
                                            if (!empty($in) && $files_info[$key]->files_id == $in) {
                                                echo 'in';
                                            }
                                            ?>" role="tabpanel" aria-labelledby="headingOne">
                                                <div class="content">
                                                    <div class="table-responsive">
                                                        <table id="table-files" class="table table-striped ">
                                                            <thead>
                                                            <tr>
                                                                <th width="45%"><?= lang('files') ?></th>
                                                                <th class=""><?= lang('size') ?></th>
                                                                <th><?= lang('date') ?></th>
                                                                <th><?= lang('uploaded_by') ?></th>
                                                                <th><?= lang('action') ?></th>
                                                            </tr>
                                                            </thead>
                                                            <tbody>
                                                            <?php
                                                            $this->load->helper('file');

                                                            if (!empty($v_files_info)) {
                                                                foreach ($v_files_info as $v_files) {
                                                                    $user_info = $this->db->where(array('user_id' => $files_info[$key]->user_id))->get('tbl_users')->row();
                                                                    ?>
                                                                    <tr class="file-item">
                                                                        <td>
                                                                            <?php if ($v_files->is_image == 1) : ?>
                                                                                <div class="file-icon"><a
                                                                                        href="<?= base_url() ?>client/tasks/download_files/<?= $files_info[$key]->task_id ?>/<?= $v_files->uploaded_files_id ?>">
                                                                                        <img
                                                                                            style="width: 50px;border-radius: 5px;"
                                                                                            src="<?= base_url() . $v_files->files ?>"/></a>
                                                                                </div>
                                                                            <?php else : ?>
                                                                                <div class="file-icon"><i
                                                                                        data-lucide="file"></i>
                                                                                    <a href="<?= base_url() ?>client/tasks/download_files/<?= $files_info[$key]->task_id ?>/<?= $v_files->uploaded_files_id ?>"><?= $v_files->file_name ?></a>
                                                                                </div>
                                                                            <?php endif; ?>

                                                                            <a data-toggle="tooltip"
                                                                               data-placement="top"
                                                                               data-original-title="<?= $files_info[$key]->description ?>"
                                                                               class="text-info"
                                                                               href="<?= base_url() ?>client/tasks/download_files/<?= $files_info[$key]->task_id ?>/<?= $v_files->uploaded_files_id ?>">
                                                                                <?= $files_info[$key]->title ?>
                                                                                <?php if ($v_files->is_image == 1) : ?>
                                                                                    <em><?= $v_files->image_width . "x" . $v_files->image_height ?></em>
                                                                                <?php endif; ?>
                                                                            </a>
                                                                            <p class="file-text"><?= $files_info[$key]->description ?></p>
                                                                        </td>
                                                                        <td class=""><?= $v_files->size ?>Kb
                                                                        </td>
                                                                        <td class="col-date"><?= date('Y-m-d' . "<br/> h:m A", safe_strtotime($files_info[$key]->upload_time)); ?></td>
                                                                        <td>
                                                                            <?= $user_info->username ?>
                                                                        </td>
                                                                        <td>
                                                                            <a class="btn btn-xs btn-dark"
                                                                               data-toggle="tooltip"
                                                                               data-placement="top"
                                                                               title="<?= lang('download') ?>"
                                                                               href="<?= base_url() ?>client/tasks/download_files/<?= $files_info[$key]->task_id ?>/<?= $v_files->uploaded_files_id ?>"><i
                                                                                    data-lucide="download"></i></a>
                                                                        </td>

                                                                    </tr>
                                                                    <?php
                                                                }
                                                            } else {
                                                                ?>
                                                                <tr>
                                                                    <td colspan="5">
                                                                        <?= lang('nothing_to_display') ?>
                                                                    </td>
                                                                </tr>
                                                            <?php } ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            <?php } ?>
            <!-- Task Attachment Panel Ends --->

            <section class="client-task-anchor-section" id="activities">
                <?php $this->load->view('client/tasks/partials/activities', get_defined_vars()); ?>
            </section>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var maxAppend = 0;
        $("#add_more").on("click", function(){
            if (maxAppend >= 4) {
                alert(<?= json_encode(lang('max_5_file_allowed')) ?>);
            } else {
                var add_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
                    <label for="field-1" class="col-sm-3 control-label"><?= lang('upload_file') ?></label>\n\
        <div class="col-sm-5">\n\
        <div class="fileinput fileinput-new" data-provides="fileinput">\n\
<span class="btn btn-default btn-file"><span class="fileinput-new" >Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="task_files[]" ></span> <span class="fileinput-filename"></span><a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none;">&times;</a></div></div>\n\<div class="col-sm-2">\n\<strong>\n\
<a href="javascript:void(0);" class="remCF"><i data-lucide="x"></i>&nbsp;Remove</a></strong></div>');
                maxAppend++;
                $("#add_new").append(add_new);
            }
        });

        $("#add_new").on('click', '.remCF', function () {
            $(this).parent().parent().parent().remove();
        });

        function syncTaskAnchorNav() {
            var hash = (window.location.hash || '#task_details').replace('#', '');
            if (hash !== 'task_details' && hash !== 'activities') {
                return;
            }
            $('[data-task-anchor-nav]').removeClass('active');
            $('[data-task-anchor-nav="' + hash + '"]').addClass('active');
            $('.client-task-anchor-section').addClass('hidden');
            $('#' + hash).removeClass('hidden');
        }

        $(window).on('hashchange', syncTaskAnchorNav);
        syncTaskAnchorNav();
    });
</script>
