<?php
function calculateDays($d){
    $now = time();
    $d = strtotime($d);
    $datediff = $d - $now;
    return round($datediff / (60 * 60 * 24));
}
?>
<link href="<?= $base_url ?>css/project-details.css" rel="stylesheet" type="text/css" />
<section class="pcoded-main-container">
    <div class="pcoded-content">
        <div class="page-header">
            <div class="page-block">
                <div class="row align-items-center">
                    <div class="col-md-12">
                        <div class="page-header-title">
                            <h5 class="m-b-10">Project : <?= $project->topic; ?>
                                <button class="btn  btn-secondary pull-right" onclick="window.history.back();"><i class="feather icon-chevron-left"></i> Go Back</button>
                            </h5>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xl-4 col-lg-12 task-detail-right">
                <div class="card">
                    <div class="card-body bg-c-blue">
                        <div class="counter text-center">
                            <h4 id="timer" class="text-white m-0"></h4>
                        </div>
                    </div>
                </div>
                <?php
                $freelancer_class = "display:none;";
                if (isset($project['freelancer']) && !empty($project['freelancer'])) {
                    $freelancer_class = "displa:block;";
                }
                ?>
                <div class="card user-card user-card-1" id="assignedData" style="<?= $freelancer_class; ?>">
                    <div class="card-header border-0 p-2 pb-0">
                        <div class="cover-img-block">
                            <p class="as_task"><i class="feather icon-check-square"></i> Assigned to Freelancer</p>
                            <img src="assets/images/widget/slider5.jpg" alt="" class="img-fluid">
                        </div>
                    </div>
                    <div class="card-body pt-0" id="freelancerDiv">
                        <div class="user-about-block text-center">
                            <div class="row align-items-end">
                                <div class="col"></div>
                                <div class="col"><img class="img-radius img-fluid wid-80" src="<?= $base_url; ?>/img/user.png" alt="User image"></div>
                                <div class="col"></div>
                            </div>
                        </div>
                        <div class="text-center">
                            <h6 class="mb-1 mt-3"> <?= $this->Html->link($project['freelancer']['name'], ['controller' => 'Freelancers', 'action' => 'view', $project['freelancer']['id']]); ?></h6>

                            <p class="mb-3 text-muted"><?php $proffArr = explode(',',$project['freelancer']['proficient']); 
                                foreach ($proffArr as $key => $proff) {
                                    if(isset($subjects[$proff])){
                                        echo $subjects[$proff].', ';
                                    }
                                }
                            ?></p>
                        </div>
                        <hr class="wid-80 b-wid-3 my-4">
                        <p><?= $project['freelancer']['language']; ?></p>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">
                        <h5>Project Details</h5>
                    </div>
                    <div class="card-body task-details">
                        <table class="table">
                            <tbody>
                                <tr>
                                    <td><i class="far fa-calendar-alt m-r-5"></i> Deadline:</td>
                                    <td class="text-right"><?= date('d M, Y', strtotime($project->deadline_date)); ?></td>
                                </tr>
                                <tr>
                                    <td><i class="far fa-calendar-alt m-r-5"></i> Bid Expire:</td>
                                    <td class="text-right"><?= date('d M, Y', strtotime($project->bid_expiry_date)); ?></td>
                                </tr>
                                <tr>
                                    <td><i class="far fa-credit-card m-r-5"></i> Created:</td>
                                    <td class="text-right"><?= date('d M, Y', strtotime($project->created)); ?></td>
                                </tr>
                                <?php if($project->website != ''){ ?>
                                    <tr>
                                        <td><i class="fas fa-chart-line m-r-5"></i> Website:</td>
                                        <td class="text-right">
                                            <?= h($project->website) ?>
                                        </td>
                                    </tr>
                                <?php } ?>
                                <tr>
                                    <td><i class="fas fa-thermometer-half m-r-5"></i> Status:</td>
                                    <td class="text-right"><?php $status = ['Pending', 'Assigned']; echo @$status[$project->order_status] ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

                <?php 
                if(trim($project->file_data) != ''){
                    $fileArr = explode('?|@$',$project->file_data);
                ?>
                    <div class="card">
                        <div class="card-header">
                            <h5>Attached Files</h5>
                        </div>
                        <div class="card-body task-attachment">
                            <ul class="media-list p-0">
                                <?php foreach ($fileArr as $k => $file) { ?>
                                    <li class="media d-flex m-b-15">
                                        <div class="m-r-20 file-attach">
                                            <i class="far fa-file f-28 text-muted"></i>
                                        </div>
                                        <div class="media-body">
                                            <a href="#!" class="m-b-5 d-block text-secondary"><?= $file ?></a>
                                        </div>
                                       
                                        <div class="float-right text-muted">
                                            <a target="_blank" download="<?= $file ?>" href="<?= $s3url.'../'.$file ?>"><i class="fas fa-download f-18"></i></a>
                                        </div>
                                    </li>
                                <?php } ?>
                            </ul>
                        </div>
                    </div>
                <?php } ?>
            </div>


            <div class="col-xl-8 col-lg-12">
                <div class="card section_report">
                    <div class="row border-bottom">
                        <section class="left col-xl-8 col-lg-12">
                            <div class="title"> <?= $project->topic ?> </div>
                            <div class="fix-price"><span>Fixed Price :</span> <strong>$ <?= $project->price ?></strong></div>
                            <ul>
                                <li><span>Type:</span> <?= $project->service ?></li>
                                <li><span>Subject: </span><?= $project->subject ?></li>
                                <li><span>Page/Word: </span><?= $project->pages .'/'.$project->words ?></li>
                                <li><span class="dadline">Deadline: </span><?= date('d M, Y',strtotime($project->deadline_date)) ?></li>
                            </ul>
                        </section>
                        <section class="right col-xl-4 col-lg-12">
                            <div class="deadline-btn">Deadline: <span><?= calculateDays($project->deadline_date) ?> Day Left</span></div>
                            <!-- <button type="button" class="btn chat-btn">Chat Now</button> -->
                        </section>
                    </div>
                    <div class="article">
                        <?= $project->description ?>
                    </div>
                    <div class="bottom-detils border-top">
                        <ul>
                            <li><span>Posted on:</span> <?= date('d M, Y',strtotime($project->created)) ?></li>
                            <li><span>Project ID:</span>#<?= $project->id ?></li>
                        </ul>
                    </div>
                </div>
                <?php if (!isset($project['freelancer']) && empty($project['freelancer']) && !empty($project->bids)) { ?>
                    <div class="card">
                        <div class="card-body table-border-style">
                            <div class="table-responsive dataTables_wrapper dt-bootstrap4" style="overflow: hidden;">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <h4><?= __('Related Bids') ?></h4>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="pull-right">
                                            <div id="colum-rendr_filter" class="dataTables_filter">
                                                <label><input type="search" id="myInput" placeholder='Search by freelancer' name="freelancers" onkeyup="myFunction()" class="form-control" aria-controls="bids"></label>
                                                <!-- <button type="submit" class="btn btn-success"><i class="fas fa-search"></i> Submit</button> -->
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <table cellpadding="0" cellspacing="0" class="table" id="myTable">
                                    <thead>
                                        <th scope="col"><?= __('Id') ?></th>
                                        <th scope="col"><?= __('Freelancer') ?></th>
                                        <th scope="col"><?= __('Placed On') ?></th>
                                        <th scope="col" class="actions"><?= __('Actions') ?></th>
                                    </thead>
                                    <?php $count = 1;
                                    foreach ($project->bids as $bids) :
                                    ?>
                                        <tr>
                                            <td><?= $count++ ?></td>
                                            <td><?= $this->Html->link($bids->freelancer->name, ['target' => '_blank'], ['controller' => 'freelancers', 'action' => 'view', $bids->freelancer_id]); ?>
                                            </td>
                                            <td><?= date('d-M-Y H:i A', strtotime($bids->created)) ?></td>
                                            <td class="actions">

                                                <?= $this->Html->link($this->Html->tag('i', '', array('class' => "fas fa-eye btn btn-success", 'traget' => '_blank')), array('controller' => 'Bids', 'action' => 'view', $bids->id), array('escape' => false, 'target' => '_blank'))
                                                ?>
                                                <a href="javascript:;"><span onclick="assignProject('<?= h($bids->freelancer->name) ?>','<?= $bids->freelancer_id; ?>')"><i class="feather icon-check-circle btn btn-icon btn-success"></i></span></a>

                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </table>
                            </div>
                        </div>
                    </div>
                <?php }  ?>
            </div>

        </div>
    </div>
    <!-- <div class="pcoded-content">
                <div class="page-header">
                    <div class="page-block">
                        <div class="row align-items-center">
                            <div class="col-md-12">
                                <div class="page-header-title">
                                    <h5 class="m-b-10">Project Details<?php echo $this->Html->link('<i class="feather icon-list" ></i> Project List', array('controller' => 'Projects', 'action' => 'index'), array('class' => 'btn btn-primary pull-right', 'escape' => false), array()); ?>
                                    </h5>
                                </div>                   
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xl-12 col-md-12">
                        <div class="card">
                            <div class="card-body table-border-style">
                                <div class="table-responsive panel" style="overflow: hidden;">
                                    <div class="row">
                                        <div class="col-xl-6 col-md-6">
                                            <table class="vertical-table table table-striped">
                                                <tr>
                                                    <th scope="row"><?= __('Topic') ?></th>
                                                    <td> <?= $project->topic; ?>  </td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Pages') ?></th>
                                                    <td><?= $this->Number->format($project->pages) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Price') ?></th>
                                                    <td><?= $this->Number->format($project->price) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Order Id') ?></th>
                                                    <td><?= $this->Number->format($project->order_id) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Order Status') ?></th>
                                                    <td><?php $status = ['Pending', 'Assigned'];
                                                        echo @$status[$project->order_status] ?></td>
                                                </tr>
                                                
                                                <tr>
                                                    <th scope="row"><?= __('Language') ?></th>
                                                    <td><?= h($project->language) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Description') ?></th>
                                                    <td><?= h($project->description) ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                        <div class="col-xl-6 col-md-6">
                                            <table class="vertical-table table table-striped">
                                                <tr>
                                                    <th scope="row"><?= __('Subject') ?></th>
                                                    <td> <?= $project->subject; ?>  </td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Words') ?></th>
                                                    <td><?= $this->Number->format($project->words) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Website') ?></th>
                                                    <td><?= h($project->website) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Deadline Date') ?></th>
                                                    <td><?= date('d,M,Y H:i', strtotime($project->deadline_date)) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('Created') ?></th>
                                                    <td><?= date('d,M,Y H:i', strtotime($project->created)) ?></td>
                                                </tr>
                                                <tr>
                                                    <th scope="row"><?= __('File') ?></th>
                                                    <td>                                        
                                                        <a download href="<?php echo $base_url . 'webroot/img/Projects/' . $project->file_data; ?>" traget="_blank">Attachments</a>
                                                    
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6" id="freelancerDiv">
                                        <?php if (!empty($project['freelancer'])) { ?>
                                            <div class="card">
                                                <div class="card-header">
                                                    <h5> <i class="fas fa-check"></i> Assigned to Freelancer</h5>
                                                </div>
                                                <div class="card-body">
                                                        <form>
                                                        <div class="row form-group">
                                                            <div class="col-sm-3">
                                                            <label class="col-form-label">Name</label>
                                                            </div>
                                                            <div class="col-sm-9">
                                                            <input type="text" class="form-control-plaintext" value="<?php echo $project['freelancer']['name']; ?>"  autocomplete="off" readonly="">
                                                            </div>
                                                        </div>
                                                        <div class="row form-group">
                                                            <div class="col-sm-3">
                                                            <label class="col-form-label">Email</label>
                                                            </div>
                                                            <div class="col-sm-9">
                                                            <input type="text" class="form-control-plaintext"  value="<?php echo $project['freelancer']['email']; ?>" autocomplete="off" readonly>
                                                            </div>
                                                        </div>
                                                        <div class="row">
                                                            <div class="col-sm-3">
                                                            <label class="col-form-label">Mobile</label>
                                                            </div>
                                                            <div class="col-sm-9">
                                                            <input type="text" class="form-control-plaintext"  value="<?php echo $project['freelancer']['mobile']; ?>" autocomplete="off" readonly>
                                                            </div>
                                                        </div>
                                                        </form>
                                                </div>
                                            </div>
                                        <?php } ?>
                                        </div>
                                    </div>
                                </div>
                                
                    </div>
                </div>
                <div class="card">
                    <div class="card-body table-border-style">
                        <div class="table-responsive dataTables_wrapper dt-bootstrap4" style="overflow: hidden;">
                            <div class="row">
                                <div class="col-sm-6">
                                    <h4><?= __('Related Bids') ?></h4></div>
                                    <div class="col-sm-6">
                                        <div class="pull-right">

                                            <div id="colum-rendr_filter" class="dataTables_filter">
                                                <label><input type="search" id="myInput" placeholder='Search by freelancer, allotment_status and ratings' name="freelancers" onkeyup="myFunction()"  class="form-control"  aria-controls="bids"></label>
                                                <button type="submit" class="btn btn-success"><i class="fas fa-search"></i> Submit</button>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                <?php if (!empty($project->bids)) :
                                    //debug($project->bids);
                                ?>
                            <table cellpadding="0" cellspacing="0" class="table" id="myTable">
                                <thead>
                                    <th scope="col"><?= __('Id') ?></th>
                                    <th scope="col"><?= __('Freelancer') ?></th>
                                    <th scope="col"><?= __('Allotment Status') ?></th>
                                    <th scope="col"><?= __('Ratings') ?></th>
                                    <th scope="col"><?= __('Created') ?></th>
                                    <th scope="col" class="actions"><?= __('Actions') ?></th>
                                </thead>
                                <?php $count = 1;
                                    foreach ($project->bids as $bids) :
                                ?>
                                <tr>
                                    <td><?= $count++ ?></td>
                                    <td><?= h($bids->freelancer->name) ?></td>
                                    <td><?php
                                        if ($bids->allotment_status == 0) {
                                            echo "Pending";
                                        } else {
                                            echo "Assigned";
                                        }
                                        ?></td>
                                    <td><?= h(@$orders->ratings) ?></td>
                                    <td><?= date('d-M-Y H:i A', strtotime($bids->created)) ?></td>
                                    <td class="actions">

                                        <?= $this->Html->link($this->Html->tag('i', '', array('class' => "fas fa-user btn btn-success", 'traget' => '_blank')), array('controller' => 'Freelancers', 'action' => 'view', $bids->freelancer_id), array('escape' => false, 'target' => '_blank')) ?>
                                        <?= $this->Html->link($this->Html->tag('i', '', array('class' => "fas fa-eye btn btn-success", 'traget' => '_blank')), array('controller' => 'Bids', 'action' => 'view', $bids->id), array('escape' => false, 'target' => '_blank'))
                                        ?>
                                        <a href="javascript:;"><span onclick="assignProject('<?= h($bids->freelancer->name) ?>','<?= $bids->freelancer_id; ?>')"><i class="feather icon-check-circle btn btn-icon btn-success"></i></span></a>

                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </table>
                    <?php endif; ?>
                    </div> 
                    </div>
                </div>
                    
                </div>
            </div>
        </div> -->
</section>
<button type="button" id="modal_open" data-toggle="modal" data-target="#exampleModal" style="display: none;"></button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" style="display: none;" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Assign Project(<?= $project->id; ?>)</span></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
            </div>
            <div class="modal-body">
                <form id="projectAssignForm" method="post">
                    <div class="form-group">
                        <label for="recipient-name" class="col-form-label">Freelancer:</label>
                        <input type="text" class="form-control" id="fr_name" name="freelancer_id" readonly>
                    </div>
                    <div class="form-group">
                        <label for="message-text" class="col-form-label">Remarks:</label>
                        <textarea class="form-control" id="message-text" name="remarks"></textarea>
                    </div>
                    <input type="hidden" name="freelancer_id" id="fr_id">
                    <input type="hidden" id="" name="project_id" value="<?= $project->id; ?>">
                    <input type="hidden" id="" name="project_name" value="<?= $project->topic; ?>">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn  btn-secondary close_modal" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn  btn-primary" onclick="saveProject()">Submit</button>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    var target = $('#assignedData');
    function assignProject(fr_name, fr_id) {
        console.log(fr_name);
        $('#fr_name').val(fr_name);
        $('#fr_id').val(fr_id);
        $('#modal_open').trigger('click');
    }

    function saveProject() {
        $("#assignedData").show();
        $(".as_task").html('Assigning ...');
        $('#freelancerDiv').html('<div class="spinner-grow" role="status"><span class="sr-only">Loading...</span></div>');
        $('.close_modal').trigger('click');
        $.ajax({
            url: '<?php echo $this->Url->build(["controller" => "Projects", "action" => "assignProject"]); ?>',
            type: 'post',
            data: $("#projectAssignForm").serialize(),
            success: function(data) {
                setTimeout(function() {
                    $('#assignedData').html('').html(data);
                }, 3000);
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                    return false;
                }
            }
        });
    }

    function myFunction() {
        var input, filter, table, tr, td, i, txtValue;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("myTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[1];
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }


    // Set the date we're counting down to
    var d = new Date('<?= $project->deadline_date; ?>');
    console.log(d);
    d.setDate(d.getDate());
    var countDownDate = new Date(d).getTime();
    //console.log(countDownDate)
    // Update the count down every 1 second
    var x = setInterval(function() {
        // Get todays date and time
        var now = new Date().getTime();
        // Find the distance between now and the count down date
        var distance = countDownDate - now;
        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Output the result in an element with id="demo"
        document.getElementById("timer").innerHTML = "<b>" + days + "</b>days : <b>" + hours + "</b>h : <b>" +
            minutes + "</b>m : <b>" + seconds + "</b>s ";

        // If the count down is over, write some text
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("timer").innerHTML = "Times over";
        }
    }, 1000);
</script>