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

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

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

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

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

            <div class="border-bottom row">
                <div class="col-sm-10">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">{{__('label.dashboard')}}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{__('label.in_charge')}}</li>
                    </ol>
                </div>
            </div>

            <div class="card custom-border-card mt-3 mb-2">
                <h5 class="card-header">{{__('label.add_in_charge')}}</h5>
                <div class="card-body">
                    <form id="save_in_charge" enctype="multipart/form-data">
                        <input type="hidden" name="id" value="">
                        <div class="form-row">
                            <div class="col-md-10">
                                <div class="form-row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>{{__('label.full_name')}}<span class="text-danger">*</span></label>
                                            <input name="full_name" type="text" class="form-control" placeholder="{{__('label.full_name_here')}}">
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>{{__('label.email')}}<span class="text-danger">*</span></label>
                                            <input name="email" type="email" class="form-control" placeholder="{{__('label.email_here')}}">
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>{{__('label.password')}}<span class="text-danger">*</span></label>
                                            <input name="password" type="password" class="form-control" placeholder="{{__('label.password_here')}}">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="col-md-2">
										<div class="form-group">
											<label>{{__('label.country_code')}}<span class="text-danger">*</span></label>
											<input name="country_code" type="text" class="form-control" placeholder="{{__('label.+91')}}">
										</div>
									</div>
									<div class="col-md-2">
										<div class="form-group">
											<label>{{__('label.country_name')}}<span class="text-danger">*</span></label>
											<input name="country_name" type="text" class="form-control" placeholder="{{__('label.ind')}}">
										</div>
									</div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>{{__('label.mobile_number')}}<span class="text-danger">*</span></label>
                                            <input name="mobile_number" type="text" class="form-control" placeholder="{{__('label.mobile_number_here')}}">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label>{{__('label.image')}}</label>
                                    <div class="avatar-upload">
                                        <div class="avatar-edit">
                                            <input type='file' name="image" id="imageUpload" accept=".png, .jpg, .jpeg" />
                                            <label for="imageUpload" title="{{__('label.upload_file')}}"></label>
                                        </div>
                                        <div class="avatar-preview">
                                            <img src="{{asset('assets/imgs/upload_img.png')}}" id="imagePreview">
                                        </div>
                                    </div>
                                    <label class="mt-3 text-gray">{{__('label.max_size_5mb')}}</label>
                                </div>
                            </div>
                        </div>
                        
                        <div class="border-top pt-3 text-right">
                            <button type="button" class="btn btn-default mw-120" onclick="save_incharge()">{{__('label.save')}}</button>
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        </div>
                    </form>
                </div>
            </div>

            <div class="card custom-border-card mt-3">
                <!-- Export Files -->
                <div class="page-search mb-3 mt-3">
                    <div class="col-8">
                        <label class="text-gray pt-2 font-weight-bold"><i class="fa-solid fa-circle-info fa-2xl mr-3"></i>{{__('label.only_the_following_data_will_be_captured_in_this_file')}}</label>
                    </div>
                    <div class="col-4">
                        <div class="d-flex justify-content-around">
                            <button id="ms_excel" class="btn btn-default"><i class="fa-sharp fa-solid fa-file-excel mr-2 font-weight-bold"></i>{{__('label.ms_excel')}}</button>
                            <button id="csv" class="btn btn-default"><i class="fa-solid fa-file-csv mr-2 font-weight-bold" style="font-size:18px"></i>{{__('label.csv')}}</button>
                            <button id="pdf" class="btn btn-default"><i class="fa-solid fa-file-pdf mr-2 font-weight-bold" style="font-size:18px"></i>{{__('label.pdf')}}</button>
                        </div>
                    </div>
                </div>

                <!-- Search -->
                <div class="page-search mb-3">
                    <div class="input-group">
                        <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_in_charge')}}" aria-label="Search" aria-describedby="basic-addon1">
                    </div>
                    <div class="sorting mr-4 w-75">
                        <label>{{__('label.sort_by')}}</label>
                        <select class="form-control" name="input_department" id="input_department">
                            <option value="all">{{__('label.all_department')}}</option>
                            @foreach ($department as $key => $value)
                                <option value="{{$value->id}}">
                                    {{ $value->name }}
                                </option>
                            @endforeach
                        </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.department')}}</th>
                                <th>{{__('label.full_name')}}</th>
                                <th>{{__('label.email')}}</th>
                                <th>{{__('label.mobile')}}</th>
                                <th>{{__('label.register_date')}}</th>
                                <th>{{__('label.transfer')}}</th>
                                <th>{{__('label.action')}}</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>

            <!-- Edit Model -->
            <div class="modal fade" id="EditModel" tabindex="-1" data-backdrop="static" role="dialog" aria-labelledby="EditModelLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="EditModelLabel">{{__('label.edit_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="edit_incharge" enctype="multipart/form-data">
                            <div class="modal-body">
                                <div class="form-row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>{{__('label.full_name')}}<span class="text-danger">*</span></label>
                                            <input name="full_name" type="text" id="edit_full_name" class="form-control" placeholder="{{__('label.full_name_here')}}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>{{__('label.email')}}<span class="text-danger">*</span></label>
                                            <input name="email" type="email" id="edit_email" class="form-control" placeholder="{{__('label.email_here')}}">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>{{__('label.country_code')}}<span class="text-danger">*</span></label>
                                            <input name="country_code" type="text" id="edit_country_code" class="form-control" placeholder="{{__('label.+91')}}">
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label>{{__('label.country_name')}}<span class="text-danger">*</span></label>
                                            <input name="country_name" type="text" id="edit_country_name" class="form-control" placeholder="{{__('label.ind')}}">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>{{__('label.mobile_number')}}<span class="text-danger">*</span></label>
                                            <input name="mobile_number" type="text" id="edit_mobile_number" class="form-control" placeholder="{{__('label.mobile_number_here')}}">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group ">
                                        <label class="">{{__('label.image')}}</label>
                                        <div class="avatar-upload">
                                            <div class="avatar-edit">
                                                <input type='file' name="image" id="imageUploadModel" accept=".png, .jpg, .jpeg" />
                                                <label for="imageUploadModel" title="{{__('label.upload_file')}}"></label>
                                            </div>
                                            <div class="avatar-preview">
                                                <img src="" alt="upload_img.png" id="imagePreviewModel">
                                            </div>
                                        </div>
                                        <label class="mt-3 text-gray">{{__('label.max_size_5mb')}}</label>
                                    </div>
                                </div>
                                <input type="hidden" name="id" id="edit_id">
                                <input type="hidden" name="old_image" id="edit_old_image">
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default mw-120" onclick="update_incharge()">{{__('label.update')}}</button>
                                <button type="button" class="btn btn-cancel mw-120" data-dismiss="modal">{{__('label.close')}}</button>
                                <input type="hidden" name="_method" value="PATCH">
                            </div>
                        </form>
                    </div>
                </div>
            </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_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.department')}}</label>
                                            <select class="form-control" name="department_id" id="edit_department_id" style="width: 100%;">
                                                <option value="0">{{__('label.select_department')}}</option>
                                                @foreach ($department 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')
    <!-- Export Files LInk (PDF, CSV, MS-Excel) -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
    <!-- Select2 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

    <script>
        $("#input_department").select2();
        $('#edit_department_id').select2({
            dropdownParent: $('#edit_incharge_model') 
        });

        $(document).ready(function() {
            var table = $('#datatable').DataTable({
                ...dataTableDefaults,
                ajax: {
                    url: "{{ route('in_charge.index') }}",
                    data: function(d) {
                        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, type, full, meta) {
                            return `<a href='${data}' target='_blank'>
                                        <img src='${data}' class='img-thumbnail' style='height:55px; width:55px'>
                                    </a>`;
						},
					},
                    {
						data: 'department.name',
						name: 'department.name',
						render: function(data) {
                            return data ? data : "-";
                        }
					},
                    {
						data: 'full_name',
						name: 'full_name',
						render: function(data) {
                            return data ? data : "-";
                        }
					},
                    {
                        data: 'email',
                        name: 'email',
                        render: function(data) {
                            return data ? data : "-";
                        }
                    },
                    {
                        data: 'mobile_number',
                        name: 'mobile_number',
                        render: function(data, type, full) {
                            return data ? (full.country_code ? `${full.country_code} ${data}` : data) : "-";
                        }
                    },
                    {
                        data: 'register_date',
                        name: 'register_date'
                    },
                    {
                        data: 'transfer',
                        name: 'transfer'
                    },
                    { data: 'action', name: 'action', orderable: false, searchable: false },
                ],
                buttons: [{
                        extend: 'excel',
                        filename: "{{App_Name()}} - {{__('label.department')}} {{__('label.in_charge')}}",
                        exportOptions: {
                            columns: [0, 2, 3, 4, 5]
                        },
                        customize: function(xlsx) {
                            var sheet = xlsx.xl.worksheets['sheet1.xml'];
                            $('row:first c', sheet).attr('s', '2');
                        },
                    },
                    {
                        extend: 'csv',
                        filename: "{{App_Name()}} - {{__('label.department')}} {{__('label.in_charge')}}",
                        exportOptions: {
                            columns: [0, 2, 3, 4, 5]
                        },
                    },
                    {
                        extend: 'pdf',
                        title: "{{App_Name()}} - {{__('label.department')}} {{__('label.in_charge')}}",
                        filename: "{{App_Name()}} - {{__('label.department')}} {{__('label.in_charge')}}",
                        pageSize: 'A4',
                        exportOptions: {
                            columns: [0, 2, 3, 4, 5]
                        },
                        customize: function(doc) {
                            doc.styles.tableHeader.fontSize = 10;
                            doc.defaultStyle.fontSize = 8;
                            doc.content[1].table.widths = ['4%', '18%', '18%', '20%', '20%', '20%'];
                            doc.content[1].layout = "borders";
                            doc.styles.title.fontSize = 22;
                            doc.styles.title.alignment = 'center';
                            doc.defaultStyle.alignment = 'center';

                            // Create a header
                            doc['header'] = (function(page, pages) {
                                return {
                                    columns: [{
                                            alignment: 'left',
                                            bold: true,
                                            text: "{{App_Name()}}",
                                        },
                                        {
                                            alignment: 'right',
                                            bold: true,
                                            text: ['Total Page ', {
                                                text: pages.toString()
                                            }],
                                        }
                                    ],
                                    margin: [20, 20],
                                }
                            });
                            // Create a footer
                            doc['footer'] = (function(page, pages) {
                                return {
                                    columns: [{
                                        alignment: 'center',
                                        bold: true,
                                        text: ['Page ', {
                                            text: page.toString()
                                        }, ' of ', {
                                            text: pages.toString()
                                        }],
                                    }],
                                }
                            });
                        }
                    }
                ],
            });

            $('#ms_excel').on('click', function() {
                
                var Demo_Mode = '{{ Demo_Mode() }}';
                if(Demo_Mode == 1){
                    var table = $('#datatable').DataTable();
                    table.button('0').trigger();
                } else {
                    showError();
                }
            });
            $('#csv').on('click', function() {

                var Demo_Mode = '{{ Demo_Mode() }}';
                if(Demo_Mode == 1){
                    var table = $('#datatable').DataTable();
                    table.button('1').trigger();
                } else {
                    showError();
                }
            });
            $('#pdf').on('click', function() {

                var Demo_Mode = '{{ Demo_Mode() }}';
                if(Demo_Mode == 1){
                    var table = $('#datatable').DataTable();
                    table.button('2').trigger();
                } else {
                    showError();
                }
            });

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

        function save_incharge(){

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

                $("#dvloader").show();
                var formData = new FormData($("#save_in_charge")[0]);
                $.ajax({
                    type:'POST',
                    url:'{{ route("in_charge.store") }}',
                    data:formData,
                    cache:false,
                    contentType: false,
                    processData: false,
                    success:function(resp){
                        $("#dvloader").hide();
                        get_responce_message(resp, 'save_in_charge', '{{ route("in_charge.index") }}');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        }

        var id;
        $(document).on("click", ".edit_incharge", function() {
            id = $(this).data('id');
            var full_name = $(this).data('full_name');
            var email = $(this).data('email');
            var country_code = $(this).data('country_code');
            var mobile_number = $(this).data('mobile_number');
            var country_name = $(this).data('country_name');
            var image = $(this).data('image');

            $(".modal-body #edit_id").val(id);
            $(".modal-body #edit_full_name").val(full_name);
            $(".modal-body #edit_email").val(email);
            $(".modal-body #edit_country_code").val(country_code);
            $(".modal-body #edit_mobile_number").val(mobile_number);
            $(".modal-body #edit_country_name").val(country_name);
            $(".modal-body #imagePreviewModel").attr("src", image);
            $(".modal-body #edit_old_image").val(image);
        });

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

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

            $(".modal-body #edit_incharge_id").val(incharge_id);
            $(".modal-body #edit_department_id").val(departmentid).trigger('change');
        });

        function update_incharge() {

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

                $("#dvloader").show();
                var formData = new FormData($("#edit_incharge")[0]);
                $.ajax({
                    type:'POST',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    url: '{{ route("in_charge.update", ":id") }}'.replace(':id', id),
                    data:formData,
                    cache:false,
                    contentType: false,
                    processData: false,
                    success:function(resp){
                        $("#dvloader").hide();
                        get_responce_message(resp, 'edit_incharge', '{{ route("in_charge.index") }}');
                    },
                    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("in_charge.transfer") }}',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(resp) {
                        $("#dvloader").hide();
                        get_responce_message(resp, 'in_charge_update', '{{ route("in_charge.index") }}');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        }
    </script>
@endsection