<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    @include('layout.header')
    <style>
        .select2-container input{
            border: none !important;
        }
    </style>
</head>
<body>
<div class="row" id="x" style="display: none;">
    <div class="col-12">
      <span class="d-flex align-items-center purchase-popup">
        <i class="typcn typcn-delete-outline" id="bannerClose"></i>
      </span>
    </div>
</div>
<div class="container-scroller">
@include('layout.navbar')
<!-- partial -->
    @include('layout.breadscrum')
    <div class="container-fluid page-body-wrapper">

    @include('layout.sidebar')

    <!-- partial -->
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-md-12 grid-margin stretch-card">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title"><i class="mdi mdi-dots-vertical menu-icon"></i> {{__('Messages')}}</h4>
                                <div class="row">
                                    <div class="col-sm-12">

                                        <form method="get" action="{{url('publication/search')}}">
                                            <div class="row">
                                                <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                                    <div class="input-group">
                                                        <input type="text" class="form-control" value="{{isset($_GET['kword']) ? $_GET['kword'] : ''}}" name="kword" id="kword" placeholder="{{__('Message title')}}">
                                                        <div class="input-group-append">
                                                            <button class="btn btn-sm btn-inverse-secondary px-3" type="submit"><i class="fa fa-search"></i> {{__('general.btn_search')}}</button>
                                                            @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_comment']))
                                                            <button class="btn btn-info waves-effect px-3 ml-2" type="button" data-action="new" data-toggle="modal" data-target="#modal-comment" data-backdrop="static"><i class="typcn typcn-plus"></i> {{__('general.btn_add_new')}}</button>
                                                            @endif
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </form> <hr class="my-3 d-none">

                                        <div class="col-sm-12 p-0 mb-1 mt-3">
                                            <div class="table-responsive">
                                                <table class="table table-bordered table-striped table-hover" id="quantity_dt">
                                                    <thead>
                                                    <tr class="bg-gradient-light">
                                                        <th class="py-2" width="50px">{{__('comment.table_no')}}</th>
                                                        <th class="py-2 text-center" width="100px">{{__('comment.table_action')}}</th>
                                                        <th class="py-2">{{__('Title')}}</th>
                                                        <th class="py-2">{{__('Start Date')}}</th>
                                                        <th class="py-2">{{__('End Date')}}</th>
                                                        <th class="py-2 text-center" width="100px">{{__('comment.table_status')}}</th>
                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    @foreach($publications as $k=>$publication)
                                                        <tr>
                                                            <td class="py-2 text-center">{{($k+1) + (($_GET['page'] ?? 1) * 20) - 20}}</td>
                                                            <td class="py-1 text-center">
                                                                <div class="btn-group" role="group" aria-label="Basic example" >
                                                                    @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['update_comment']))
                                                                    <button type="button" class="btn btn-sm btn-inverse-primary mr-2" title="{{__('general.btn_edit')}}" data-action="edit" data-uid="{{$publication->id}}" data-toggle="modal" data-target="#modal-comment" data-backdrop="static">
                                                                        <i class="typcn typcn-edit"></i>
                                                                    </button>
                                                                    @endif
                                                                    @if($publication->record_status_id==0)
                                                                        <button type="button" class="btn btn-sm btn-inverse-primary " title="Restore"  onclick="restore_data({{$publication->id}})"><i class="typcn typcn-plus"></i></button>
                                                                    @else
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['delete_comment']))
                                                                        <button type="button" class="btn btn-sm btn-inverse-danger " title="{{__('general.btn_delete')}}" onclick="delete_data({{$publication->id}})"><i class="mdi mdi-close-circle"></i></button>
                                                                        @endif
                                                                    @endif
                                                                </div>
                                                            </td>
                                                            <td class="py-2">{{$publication->title_en}}</td>
                                                            <td class="py-2">{{$publication->start_date}}</td>
                                                            <td class="py-1">{{$publication->end_date}}</td>
                                                            <td class="py-1 text-center">
                                                                <i class="mdi {{$publication->record_status_id == 1 ? 'mdi-check-circle text-primary' : 'mdi-checkbox-blank-circle-outline'}}"></i>
                                                            </td>
                                                        </tr>
                                                    @endforeach
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>

                                        <div class="float-right mt-3">
                                            {!! $publications->links('pagination.custom') !!}
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <!-- Modal Laboratory -->
            <div class="modal fade modal-primary" id="modal-comment">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h6 class="modal-title new">{{__('comment.form_add_tittle')}}</h6>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" class="mdi mdi-close"></span></button>
                        </div>

                        <div class="modal-body">
                            <form method="post" action="#" id="form" onkeydown="return (event.key != 'Enter' || (event.target.matches('textarea')));">
                                @csrf
                                <input type="hidden" class="form-control" name="uid" id="data-id" value="0" />
                                <div class="form-vertical">

                                    <div class="form-group mb-1">
                                        <label for="lab-code" class="control-label">{{__('Recipient')}}&nbsp;<span class="text-danger">*</span></label>
                                        <div class="form-check" style="width: 250px !important;">
                                            <label class="form-check-label">
                                                <input type="checkbox" class="form-check-input" id="select-all-lab">
                                                {{__('department.form_select_all_lab')}}
                                            </label>
                                        </div>
                                        <select class="form-control select2-multiple" multiple name="lab_ids" id="lab-ids" style="width: 100% !important;">
                                            @foreach($labs as $lab)
                                                <option value="{{$lab->id}}">{{$lab->name_en}}</option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="form-group mb-1">
                                        <label for="lab-code" class="control-label">{{__('Title')}}&nbsp;<span class="text-danger">*</span></label>
                                        <input type="text" class="form-control" name="title_en" id="title-en">
                                    </div>

                                    <div class="row">
                                        <div class="col-sm-6">
                                            <div class="form-group mb-1">
                                                <label for="lab-code" class="control-label">{{__('Start Date')}}&nbsp;<span class="text-danger">*</span></label>
                                                <input type="date" class="form-control" name="start_date" id="start-date">
                                            </div>
                                        </div>
                                        <div class="col-sm-6">
                                            <div class="form-group mb-1">
                                                <label for="lab-code" class="control-label">{{__('End Date')}}&nbsp;</label>
                                                <input type="date" class="form-control" name="end_date" id="end-date">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group mb-1">
                                        <label for="lab-name-en">{{__('Description')}}</label>
                                        <textarea class="form-control" rows="10" style="line-height:15px" name="comment_desc" id="description" placeholder="{{__('Description')}}"></textarea>
                                    </div>

                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-info" id="btnSave"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;<span class="save">{{__('lang.save')}}</span><span class="update" style="display: none">{{__('lang.update')}}</span></button>
                            <button type="button" class="btn btn-light" data-dismiss='modal'>{{__('lang.cancel')}}</button>
                        </div>
                    </div>
                </div>
            </div>

            @include('layout.footer')
        </div>
    </div>
</div>
@include('layout.common_script')

<script src="{{url(env("APP_URL").'storage/assets/js/admin/calculation_results/underscore-min.js')}}"></script>
<script>

    let base_comment_url = "{{url('publication')}}";
    let modal_add_title = "{{__('New Message')}}";
    let modal_edit_title = "{{__('Edit Message')}}";

</script>
<script>


    $("#btnSave").on('click',function(e){
        e.preventDefault();
        data_id = $('#data-id').val();
        let url = base_comment_url.toString() + (data_id < 1 ? '.save' : '.update').toString();
        $.ajax({
            url: url,
            type:"POST",
            data :{
                "_token": csrf_token,
                uid : data_id,
                title_en: $('#title-en').val(),
                start_date: $('#start-date').val(),
                end_date: $('#end-date').val(),
                description: $('#description').val(),
                lab_ids: $('#lab-ids').val()
            },
            success:function(res){
                handleMessage(res.success, res.message.replace('Comment', 'Message'))
                if(res.success) setTimeout(function () {location.reload()}, messageDuration)
            }
        });
    });

    function delete_data(uid){
        Swal.fire({
            text: confirm_delete_record,
            icon: 'question',
            showCancelButton: true,
            cancelButtonClass:'danger',
            confirmButtonText: confirm_ok_delete,
            cancelButtonText: confirm_cancel
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: base_comment_url.toString() + '.delete',
                    method:'POST',
                    data:{
                        "_token": csrf_token,
                        uid : uid,
                    },
                    success:function(res){
                        handleMessage(res.success, res.message.replace('Comment', 'Message'))
                        if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                    }
                })

            }
        });
    }

    function restore_data(uid){
        Swal.fire({
            text: confirm_restore_record,
            icon: 'question',
            showCancelButton: true,
            cancelButtonClass:'danger',
            confirmButtonText: confirm_ok_restore,
            cancelButtonText: confirm_cancel_restore,
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: base_comment_url.toString() + '.restore',
                    method:'POST',
                    data:{
                        "_token": csrf_token,
                        uid : uid,
                    },
                    success:function(res){
                        handleMessage(res.success, res.message.replace('Comment', 'Message'))
                        if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                    }
                })

            }
        });
    }

    $('#modal-comment').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var action = button.data('action') // Extract info from data-* attributes
        var modal = $(this);
        $('#comment-desc').val("");
        if(action === "new"){
            // Clear inputs
            modal.find('.modal-body input[name="uid"]').val(0);
            modal.find('.modal-body input').val("");
            modal.find('.modal-body #description').val("");
            modal.find('.modal-title').html(modal_add_title);
        }else if(action === "edit"){
            uid = button.data('uid');
            modal.find('.modal-body input[name="uid"]').val(uid);
            modal.find('.modal-title').html(modal_edit_title);
            $.ajax({
                url: base_comment_url.toString() + '.get',
                type:"POST",
                data:{
                    "_token": csrf_token,
                    uid: uid,
                },
                success:function(res){
                    let data = res.data;
                    $('#title-en').val(data.title_en)
                    modal.find('.modal-body input[name="start_date"]').val(data.start_date);
                    modal.find('.modal-body input[name="end_date"]').val(data.end_date);
                    $('#description').val(data.description)
                    modal.find('.modal-body #lab-ids').val(_.pluck(data.receivers, 'lab_id')).change();

                    if(!res.success) handleMessage(res.success, res.message.replace('Comment', 'Message'))
                }
            });
        }
    })

    console.log('your browser is ', checkBrowser())

    function checkBrowser(){
        let browser = "";
        let c = navigator.userAgent.search("Chrome");
        let f = navigator.userAgent.search("Firefox");
        let m8 = navigator.userAgent.search("MSIE 8.0");
        let m9 = navigator.userAgent.search("MSIE 9.0");
        if (c > -1) {
            browser = "Chrome";
        } else if (f > -1) {
            browser = "Firefox";
        } else if (m9 > -1) {
            browser ="MSIE 9.0";
        } else if (m8 > -1) {
            browser ="MSIE 8.0";
        }
        return browser;
    }

    $('#select-all-lab').click(function () {
        if( $('#select-all-lab').is(':checked')){
            $('#lab-ids > option').prop("selected", true)
            $('#lab-ids').trigger('change')
        }else{
            $('#lab-ids > option').prop("selected", false)
            $('#lab-ids').trigger('change')
        }
    })


</script>
</body>

</html>

