@extends('incharge.layout.page-app')
@section('page_title', __('label.sub_in_charge'))
@section('tab_title', __('label.sub_in_charge'))

@section('content')
    @include('incharge.layout.sidebar')

    <!-- Select2 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" />

    <div class="right-content">
        @include('incharge.layout.header')

        <div class="body-content">
            <!-- mobile title -->
            <h1 class="page-title-sm">{{__('label.sub_in_charge')}}</h1>

            <div class="border-bottom row">
                <div class="col-sm-10">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('incharge.dashboard') }}">{{__('label.dashboard')}}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{__('label.sub_in_charge')}}</li>
                    </ol>
                </div>
                <div class="col-sm-2 d-flex align-items-center justify-content-end">
                    <a href="{{ route('insub_incharge.create') }}" class="btn btn-default mw-130" style="margin-top: -14px;">{{__('label.add_sub_in_charge')}}</a>
                </div>
            </div>
            
            <!-- Search -->
            <div class="page-search mb-3">
                <div class="input-group" title="Search">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1"><i class="fa-solid fa-magnifying-glass fa-xl light-gray"></i></span>
                    </div>
                    <input type="text" id="input_search" class="form-control" placeholder="{{__('label.search_sub_in_charge')}}" aria-label="Search" aria-describedby="basic-addon1">
                </div>
                <div class="sorting w-50">
                    <label>{{__('label.sort_by')}}</label>
                    <select class="form-control" id="input_status">
                        <option value="all">{{__('label.all_status')}}</option>
                        <option value="0">{{__('label.inactive')}}</option>
                        <option value="1">{{__('label.active')}}</option>
                    </select>
                </div>
            </div>

            <div class="table-responsive">
                <table class="table table-striped text-center table-bordered" id="datatable">
                    <thead>
                        <tr style="background: #F9FAFF;">
                            <th>{{__('label.#')}}</th>
                            <th>{{__('label.image')}}</th>
                            <th>{{__('label.full_name')}}</th>
                            <th>{{__('label.transfer')}}</th>
                            <th>{{__('label.status')}}</th>
                            <th>{{__('label.action')}}</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

            <!-- Transfer Model-->
            <div class="modal fade" id="edit_incharge_model" tabindex="-1" data-backdrop="static" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="ModalLabel">{{__('label.transfer_sub_in_charge')}}</h5>
                            <button type="button" class="close text-dark" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <form id="in_charge_update" autocomplete="off">
                            <div class="modal-body">
                                <div class="form-row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>{{__('label.service')}}</label>
                                            <select class="form-control" name="service_id[]" multiple id="edit_service_id" style="width: 100% !important;">
                                                @foreach ($service as $key => $value)
                                                <option value="{{ $value->id}}">
                                                    {{ $value->name }}
                                                </option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <input type="hidden" name="id" id="edit_incharge_id">
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default mw-120" onclick="in_charge_update()">{{__('label.update')}}</button>
                                <button type="button" class="btn btn-cancel mw-120" data-dismiss="modal">{{__('label.close')}}</button>
                                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('pagescript')
    <!-- Select2 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

    <script>
        $("#edit_service_id").select2({
			placeholder: "{{__('label.select_service')}}",
		});

        $(document).ready(function() {
            var table = $('#datatable').DataTable({
                ...dataTableDefaults,
                ajax: {
                    url: "{{ route('insub_incharge.index') }}",
                    data: function(d) {
                        d.input_status = $('#input_status').val();
                        d.input_department = $('#input_department').val();
                        d.input_search = $('#input_search').val();
                    },
                },
                columns: [
                    { data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false },
                    {
						data: 'image',
						name: 'image',
						orderable: false,
						searchable: false,
						render: function(data) {
                            return `<a href='${data}' target='_blank'>
                                        <img src='${data}' class='rounded-circle' style='height:55px; width:55px'>
                                    </a>`;
						},
					},
                    {
						data: 'full_name',
						name: 'full_name',
						render: function(data) {
                            return data ? data : "-";
                        }
					},
                    {
                        data: 'transfer',
                        name: 'transfer'
                    },
                    {
                        data: 'status',
                        name: 'status',
                        orderable: false,
						searchable: false
                    },
                    { data: 'action', name: 'action', orderable: false, searchable: false },
                ],
            });

            $('#input_status, #input_department').change(function() {
                table.draw();
            });
            $('#input_search').keyup(function() {
                table.draw();
            });
        });

        // make transfer
        $(document).on("click", ".in_charge_update", function() {

            var incharge_id = $(this).data('id');

            $(".modal-body #edit_incharge_id").val(incharge_id);
        });

        function change_status(id, status) {

            var Demo_Mode = '<?php echo Demo_Mode(); ?>';
            if(Demo_Mode == 1){

                $("#dvloader").show();
                var url = `{{ route('insub_incharge.show', '') }}/${id}`;

                $.ajax({
                    type: "GET",
                    url: url,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(resp) {
                        $("#dvloader").hide();

                        if (resp.status == 200) {
                            if (resp.status_code == 1) {
                                $('#' + id).text('{{__("label.active")}}').removeClass('hide-btn').addClass('show-btn');
                            } else {
                                $('#' + id).text('{{__("label.inactive")}}').removeClass('show-btn').addClass('hide-btn');
                            }
                            toastr.success(resp.success);
                        } else {
                            toastr.error(resp.errors);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        };

        // Transfer 
        function in_charge_update() {

            var Check_Admin = '<?php echo Demo_Mode(); ?>';
            if (Check_Admin == 1) {

                $("#dvloader").show();
                var formData = new FormData($("#in_charge_update")[0]);
                $.ajax({
                    type: 'POST',
                    url: '{{ route("insub_incharge.transfer") }}',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(resp) {
                        $("#dvloader").hide();
                        get_responce_message(resp, 'in_charge_update', '{{ route("insub_incharge.index") }}');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        }
    </script>
@endsection