
<?php $__env->startSection('content'); ?>
    <style>
        @import  url('https://fonts.googleapis.com/css?family=Assistant');

        body {

            font-family: Assistant, sans-serif
        }

        .cell-1 {
            border-collapse: separate;
            border-spacing: 0 4em;

            border-bottom: 5px solid transparent;
            background-clip: padding-box;
            cursor: pointer
        }



        .table-elipse {
            cursor: pointer
        }

        #demo {
            -webkit-transition: all 0.3s ease-in-out;
            -moz-transition: all 0.3s ease-in-out;
            -o-transition: all 0.3s 0.1s ease-in-out;
            transition: all 0.3s ease-in-out
        }


        .table td.collapse.in {
            display: table-cell;
        }
    </style>
    <div class="card card-body pd-10">
        <div class="az-content-breadcrumb">
            <span>Departments</span>
        </div>
        <h2 class="az-content-title" style="display: inline"> Departments List <span>
                
        </h2>
        
    </div>

    
    <div class="row">
        <div class="col-md-12 col-lg-12 col-xl-12">
            <div class="card card-body pd-40">

                <div>
                    <table id="example2" class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="wd-10p">View</th>
                                <th class="wd-20p">Department Name</th>
                                
                                
                                <th class="wd-15p">User Count</th>
                                <th class="wd-15p">Department Head</th>
                                <th class="wd-10p">Status</th>
                                <th class="wd-10p">Created</th>
                                <th class="wd-10p">Updated</th>
                                <th class="wd-10p">Assign Users</th>
                                <th class="wd-10p">Add Teams</th>
                                
                                <th class="wd-10p">Operations</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php $__currentLoopData = $departments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $department): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $counter = App\assign_department_user::where('department_id', $department->id_departments)
                                        ->get()
                                        ->count();
                                    $get_department_head = App\assign_department_user::where('department_id', $department->id_departments)
                                        ->where('is_head', 1)
                                        ->select('user_name')
                                        ->first();
                                    // dd($get_department_head);
                                ?>
                                <tr>
                                <tr class="cell-1" data-toggle="collapse"
                                    data-target="#demo<?php echo e($department->id_departments); ?>">
                                    <td class="table-elipse" data-toggle="collapse" data-target="#demo"><i
                                            class="fa fa-ellipsis-h text-black-50"></i></td>
                                    <td><?php echo e($department->department_name); ?></td>
                                    
                                    
                                    <td><?php echo e($counter); ?></td>
                                    <td><?php echo e($get_department_head?->user_name); ?></td>
                                    <td>
                                        <?php if($department->status == 1): ?>
                                            <button class="btn btn-rounded btn-success" style="color:#fff;">
                                                Active <span class="badge badge-primary"></span>
                                            </button>
                                        <?php else: ?>
                                            <button class="btn btn-rounded btn-danger" style="color:#fff;">
                                                Deactive <span class="badge badge-primary"></span>
                                            </button>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= date('d-m-Y', strtotime($department->created_at)) ?></td>
                                    <td><?= date('d-m-Y', strtotime($department->updated_at)) ?></td>
                                    <td>
                                        <button class="btn btn-rounded  btn-primary"
                                            onclick="assignUser(<?php echo e($department->id_departments); ?>)">Assign User</button>

                                    </td>
                                    <td>
                                        <button class="btn btn-rounded  btn-primary"
                                            onclick="assignUserservices(<?php echo e($department->id_departments); ?>)">Add
                                            Teams</button>

                                    </td>
                                    
                                    <td>
                                        <a class="btn btn-rounded btn-primary"
                                            href="<?php echo e(url('departments/edit/' . Crypt::encrypt($department->id_departments))); ?>">
                                            Edit
                                        </a>
                                        <!--                                    <a class="btn btn-rounded btn-danger" href="<?php echo e(url('departments/destroy/' . Crypt::encrypt($department->id_service_departments))); ?>">
                                                                                                                                                                                                                                                        Delete
                                                                                                                                                                                                                                                        </a>-->
                                    </td>
                                </tr>

                                <?php
                                    $decode = App\department_team::where('department_id', $department->id_departments)->get();
                                    // dd($decode);
                                ?>
                                <tr id="demo<?php echo e($department->id_departments); ?>" class="collapse cell-1 row-child">
                                    <td colspan="1"><i class="fa fa-angle-up"></i></td>
                                    <td colspan="1" style="font-weight:bold;">Team Name&nbsp;</td>
                                    
                                    <td colspan="2" style="font-weight:bold;">Users&nbsp;</td>


                                </tr>
                                <?php if(isset($decode)): ?>
                                <?php $__currentLoopData = $decode; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr id="demo<?php echo e($department->id_departments); ?>" class="collapse cell-1 row-child">
                                        <td colspan="1"></td>
                                        <th><label><?php echo e($value->team_name); ?></label>
                                        </th>
                                        
                                        <th colspan="2">
                                            <?php if(isset($value->user_id)): ?>
                                                <?php $__currentLoopData = json_decode($value->user_id); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user_value_dec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                    <span
                                                        class="<?php if($user_value_dec == $value->head_id): ?> badge badge-success <?php else: ?> badge badge-warning <?php endif; ?>">
                                                        <?php echo e(get_user_name($user_value_dec)); ?></span>
                                                    <a onclick="remove_user('<?php echo e(\Crypt::encrypt($user_value_dec)); ?>','<?php echo e(\Crypt::encrypt($value->id_department_teams)); ?>')"> <label
                                                            class="badge badge-danger">X</label></button>
                                                        <br>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <?php endif; ?>
                                        </th>

                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th class="wd-10p">View</th>
                                <th class="wd-20p">Department Name</th>
                                
                                
                                <th class="wd-15p">User Count</th>
                                <th class="wd-15p">Department Head</th>
                                <th class="wd-10p">Status</th>
                                <th class="wd-10p">Created</th>
                                <th class="wd-10p">Updated</th>
                                <th class="wd-10p">Assign Users</th>
                                <th class="wd-10p">Assign Users Services</th>
                                
                                <th class="wd-10p">Operations</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            <!-- card -->
        </div>
        <!-- col -->
    </div>


    <div id="modaldemo1" class="modal " aria-modal="true" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content modal-content-demo">
                <div class="modal-header">
                    <h6 class="modal-title">Assign Users</h6>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <label for="">Assign Team</label>
                    <form method="POST" action="<?php echo e(url('assign_user_teams')); ?>">
                        <?php echo csrf_field(); ?>

                        <select style="width: 100%;" class="form-control select1" name="team_id" required>
                            <option value="">Select</option>
                            <?php $__empty_1 = true; $__currentLoopData = $department_teams; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <option value="<?php echo e($item->id_department_teams); ?>"><?php echo e($item->team_name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <?php endif; ?>
                        </select>
                        <label for="">Assign User</label>
                        <select style="width: 100%;" class="form-control" name="user_id" required>
                            <option value="">Select</option>
                            <?php $__empty_1 = true; $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <option value="<?php echo e($item->id); ?>"><?php echo e($item->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <?php endif; ?>
                        </select>
                        <label for="">Department Head</label>
                        <input type="checkbox" name="is_head" class="mt-2">
                        <input type="hidden" value="" id="d_id" name="d_id">

                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-indigo">Assign</button>
                    </form>
                    <button type="button" class="btn btn-outline-light" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div><!-- modal-dialog -->
    </div>


    <div id="modaldemo2" class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h6 class="modal-title">Assign Services And Sub Services</h6>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    
                    <form method="POST" action="<?php echo e(url('assign_services_department')); ?>">

                        <div class="row">
                            <div class="col-lg-5 mg-t-20 mg-lg-t-0">
                                <div class="form-group">

                                    <label class="form-control-label">Services: <span style="color:red;">*</span></label>
                                    <select name="services[]" id="services" class="form-control" required="required">
                                        <option>Select Services </option>
                                        <?php $__empty_1 = true; $__currentLoopData = $services; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $service): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <option value="<?php echo e($service->id_other_services); ?>">
                                                <?php echo e($service->service_name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            No Results Found
                                        <?php endif; ?>
                                    </select>
                                </div>
                            </div>
                            <?php echo csrf_field(); ?>
                            <div class="col-lg-6 mg-t-20 mg-lg-t-0">
                                <div class="form-group">

                                    <label class="form-control-label">Sub Services:</label>
                                    <select style="width: 100%" name="sub_services[]" id="sub_services"
                                        class="js-example-basic-multiple" multiple="multiple">
                                        <option>Select Sub Service</option>

                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-1 mg-t-20 mg-md-t-0">
                                
                                <button onclick="add_more()" class="btn btn-az-primary mt-4" type="button">Add
                                    More</button>
                            </div>
                            <input type="hidden" id="d_id2" name="d_id">
                        </div>
                        <div class="row" id="append_services">

                        </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-indigo">Assign</button>
                    </form>
                    <button type="button" class="btn btn-outline-light" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <div id="modaldemo3" class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h6 class="modal-title">Add Teams</h6>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    
                    <form method="POST" action="<?php echo e(url('add_department_teams')); ?>">

                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="">Select Team</label>
                                    <select style="width: 100%;" class="form-control js-example-tags"
                                        name="department_teams" id="department_teams" required>
                                        <option value="">Select</option>
                                        <?php $__empty_1 = true; $__currentLoopData = $department_teams; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <option value="<?php echo e($item->id_department_teams); ?>"><?php echo e($item->team_name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                        <?php endif; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-5 mg-t-20 mg-lg-t-0">
                                <div class="form-group">

                                    <label class="form-control-label">Services: <span style="color:red;">*</span></label>
                                    <select name="services[]" id="services_users" class="form-control"
                                        required="required">
                                        <option>Select Services </option>
                                        <?php $__empty_1 = true; $__currentLoopData = $services; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $service): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                            <option value="<?php echo e($service->id_other_services); ?>">
                                                <?php echo e($service->service_name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                            No Results Found
                                        <?php endif; ?>
                                    </select>
                                </div>
                            </div>
                            <?php echo csrf_field(); ?>
                            <div class="col-lg-6 mg-t-20 mg-lg-t-0">
                                <div class="form-group">

                                    <label class="form-control-label">Sub Services:</label>
                                    <select style="width: 100%" name="sub_services[]" id="sub_services_users"
                                        class="js-example-basic-multiple_new" multiple="multiple">
                                        <option>Select Sub Service</option>

                                    </select>
                                </div>
                            </div>
                            <div class="col-lg-1 mg-t-20 mg-md-t-0">
                                
                                <button onclick="add_more_users()" class="btn btn-az-primary mt-4" type="button">Add
                                    More</button>
                            </div>
                            <input type="hidden" id="d_id3" name="d_id">
                        </div>
                        <div class="row" id="append_services_users">

                        </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-indigo">Assign</button>
                    </form>
                    <button type="button" class="btn btn-outline-light" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
<?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?>
    <script>
        $(document).ready(function() {

            $('#example2 tfoot th').each(function() {
                var title = $(this).text();
                $(this).html('<input type="text" class="form-control" placeholder="' + title + '" />');
            });

            $('#example2').DataTable({
                "ordering": true,
                "dom": 'Blfrtip',
                "buttons": [
                    'excel', 'pdf', 'print'
                ],
                responsive: !0,
                columnDefs: [{
                    className: 'control'
                }],
                initComplete: function() {
                    // Apply the search
                    this.api()
                        .columns()
                        .every(function() {
                            var that = this;

                            $('input', this.footer()).on('keyup change clear', function() {
                                if (that.search() !== this.value) {
                                    that.search(this.value).draw();
                                }
                            });
                        });
                }
            });
        });
    </script>
    <script>
        // alert('sdsds')
        var counti = 0;
        $(document).ready(function() {
            $('.js-example-basic-multiple').select2({
                dropdownParent: $("#modaldemo2")
            });
            $('.select2').select2({
                dropdownParent: $("#modaldemo2")
            });
            $('.select1').select2({
                dropdownParent: $("#modaldemo1")
            });
            $(".js-example-tags").select2({
                tags: true,
                dropdownParent: $("#modaldemo3")
            });

        });

        function remove_user(id, d_id) {
            Swal.fire({
                title: "Do you want to Delete This User?",
                showDenyButton: true,

                confirmButtonText: "Yes",
                denyButtonText: `No`
            }).then((result) => {
                /* Read more about isConfirmed, isDenied below */
                if (result.isConfirmed) {

                    location.href = "<?php echo e(url('remove_department_user/')); ?>/" + id + "/" + d_id
                } else if (result.isDenied) {

                }
            });
        }

        function add_more() {
            // alert('sdsd')
            counti = counti + 1;
            $.ajax({
                url: "<?php echo e(url('add_more_services')); ?>/" + counti,
                type: 'GET',
                success: function(data) {
                    console.log(data.script)
                    $('#append_services').append(data.data);
                    // $('#append_js').append(data.script);
                    $('#count_id').val(counti);
                    $('.js-example-basic-multiple').select2()
                    $('#services' + counti).on('change', function() {
                        var val = $(this).val();
                        $.ajax({
                            url: "<?php echo e(url('get_sub_services')); ?>/" + val,
                            type: "GET",
                            success: function(data) {
                                console.log(data)
                                $('#sub_services' + counti).html(data);
                            }
                        });
                    });
                }
            });

        }

        var counti_users = 0;

        function add_more_users() {
            // alert('sdsd')
            counti_users = counti_users + 1;
            $.ajax({
                url: "<?php echo e(url('add_more_services')); ?>/" + counti_users,
                type: 'GET',
                success: function(data) {
                    console.log(data.script)
                    $('#append_services_users').append(data.data);
                    // $('#append_js').append(data.script);
                    $('#count_id').val(counti_users);
                    $('.js-example-basic-multiple').select2()
                    $('#services_users' + counti_users).on('change', function() {
                        var val = $(this).val();
                        $.ajax({
                            url: "<?php echo e(url('get_sub_services')); ?>/" + val,
                            type: "GET",
                            success: function(data) {
                                console.log(data)
                                $('#sub_services_users' + counti_users).html(data);
                            }
                        });
                    });
                }
            });

        }

        function remove(count_rmv) {
            // alert(counti)
            counti = counti - 1;
            $('.rmv' + count_rmv).remove();
        }
        $(document).ready(function() {
            // $('.select2').select2();
            $(".js-example-basic-multiple_new").select2({
                dropdownParent: $("#modaldemo2")
            });
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo3")
            });
            $('#services').on('change', function() {
                var val = $(this).val();
                $.ajax({
                    url: "<?php echo e(url('get_sub_services')); ?>/" + val,
                    type: "GET",
                    success: function(data) {
                        $('#sub_services').html(data);
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo2")
                        });
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo3")
                        });
                    }
                });
            });
        });
        $(document).ready(function() {
            $('#services_users').on('change', function() {
                var val = $(this).val();
                $.ajax({
                    url: "<?php echo e(url('get_sub_services')); ?>/" + val,
                    type: "GET",
                    success: function(data) {
                        $('#sub_services_users').html(data);
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo2")
                        });
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo3")
                        });
                    }
                });
            });
        });
        $(document).ready(function() {
            // $('.select2').select2();
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo2")
            });
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo3")
            });
            $('#services_users').on('change', function() {
                var val = $(this).val();
                $.ajax({
                    url: "<?php echo e(url('get_sub_services')); ?>/" + val,
                    type: "GET",
                    success: function(data) {
                        $('#sub_services_users').html(data);
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo2")
                        });
                        $(".js-example-basic-multiple").select2({
                            dropdownParent: $("#modaldemo3")
                        });
                    }
                });
            });


        });
    </script>


    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>
    <script>
        function assignUser(user_id) {
            $('#d_id').val(user_id);
            $('#modaldemo1').modal('show');
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo2")
            });
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo3")
            });
        }

        function assignUserservices(dep_id) {
            $('#d_id3').val(dep_id);
            $('#modaldemo3').modal('show');

            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo2")
            });
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo3")
            });
            $.ajax({
                type: "GET",
                url: "<?php echo e(url('get_department_users')); ?>/" + dep_id,
                success: function(response) {
                    // user_id_dep
                    $('#user_id_dep').html(response.dep_users);
                }
            });
        }

        function assignServices(d_id) {
            $('#d_id2').val(d_id);
            // alert(d_id)
            $('#modaldemo2').modal('show');
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo2")
            });
            $(".js-example-basic-multiple").select2({
                dropdownParent: $("#modaldemo3")
            });
        }
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\wamp64\www\Travocom\TVC_PHASE_1\resources\views/departments/index.blade.php ENDPATH**/ ?>