@extends('layouts.master')
@section('content')

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Permissions Details</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('users.list') }}">Team Member List</a></li>
                        <li class="breadcrumb-item active">Permission Details</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <!-- Default box -->
                    <div class="card">
                        <div class="card-header">
                            <!-- <h3 class="card-title">Select Members For </h3> -->
                            <div class="card-tools">
                                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                                    <i class="fas fa-minus"></i>
                                </button>
                            </div>
                        </div>
                        <div class="card-body">
                            <form method="post" action="{{ route('users.updatepermission') }}" name="form"
                                enctype="multipart/form-data">
                                @csrf
                                <input type='hidden' name="CustomerID" value="{{$users->CustomerID}}">
                                <div class="form-group">
                                    <div class="row">
                                        
                                        <div class="col-lg-3 col-6">
                                            <div class="inner">
                                                <lable><b>Team Member : </b></lable>&nbsp;{{$users->CustomerName}}
                                                <input type="hidden" name="CustomerID" value="{{$users->CustomerName}}" readonly>
                                            </div>
                                        </div>
                                        <div class="col-lg-3 col-6">
                                            <div class="inner">
                                                <lable><b> Email Id : </b></lable>&nbsp;{{$users->email}}
                                                <input type="hidden" name="email_id" value="{{$users->email}}" readonly>
                                            </div>
                                        </div>                                        
                                        <div class="col-lg-3 col-6">
                                            <div class="inner">
                                                <lable><b> User Id : </b></lable>&nbsp;{{$users->CustomerID}}
                                                <input type="hidden" name="CustomerID" value="{{$users->CustomerID}}" readonly>
                                            </div>
                                        </div>
                                        {{-- <div>
                                            <a class="btn btn-primary" style="float: right;"  onclick="replace_user(this)"  data-CustomerID="{{$users->CustomerID}}">
                                                Merge With Another User
                                            </a>
                                        </div> --}}
                                    </div>
                                    <table id="example2" class="table table-bordered table-hover dataTable dtr-inline"
                                        aria-describedby="example2_info">
                                        <thead>
                                            <tr>
                                                <th>
                                                    Permissions
                                                </th>
                                                <th>

                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Contact_Share">
                                                    Contact_Share
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox"  $selected name="Contact_Share"
                                                        class="btn bg-gradient-success btn-sm" value="1" @if((isset($permissions)) && ($permissions->Contact_Share == "1"))
                                                        checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Call_Contact">
                                                    Call_Contact
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="Call_Contact"
                                                        class="btn bg-gradient-success btn-sm" value="1" @if((isset($permissions)) && ($permissions->Call_Contact == "1"))
                                                        checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Send_Whatsapp">
                                                    Send_Whatsapp
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="Send_Whatsapp"
                                                        class="btn bg-gradient-success btn-sm" value="1" @if((isset($permissions)) && ($permissions->Send_Whatsapp == "1"))
                                                        checked @endif>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Save_Contact">
                                                    Save_Contact
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="Save_Contact"
                                                        class="btn bg-gradient-success btn-sm" value="1" @if((isset($permissions)) && ($permissions->Save_Contact == "1"))
                                                        checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Export_Excel">
                                                    Export_Excel
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="Export_Excel"
                                                        class="btn bg-gradient-success btn-sm" value="1"  @if((isset($permissions)) && ($permissions->Export_Excel == "1"))
                                                        checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="location_config">
                                                    Enable Location
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="location_config"
                                                        class="btn bg-gradient-success btn-sm" value="1"  @if((isset($users)) && ($users->location_config == "1")) checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="send_email">
                                                    Send Email
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="send_email"
                                                        class="btn bg-gradient-success btn-sm" value="1"  @if((isset($permissions)) && ($permissions->send_email == "1")) checked @endif>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0" name="Bulk_Whatsapp">
                                                    Send Bulk Whatsapp
                                                </td>
                                                <td class="dtr-control sorting_1" style="border-bottom: 2px solid;"
                                                    tabindex="0">
                                                    <input type="checkbox" name="Bulk_Whatsapp"
                                                        class="btn bg-gradient-success btn-sm" value="1"  @if((isset($permissions)) && ($permissions->Bulk_Whatsapp == "1")) checked @endif>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <div class="card-footer">
                                        <button type="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Division </h3>
                            <div class="card-tools">
                                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                                    <i class="fas fa-minus"></i>
                                </button>
                            </div>
                        </div>
                        <div class="card-body">
                            <form method="post" action="{{ route('users.divisionUpdate') }}" name="form"
                                  enctype="multipart/form-data">
                                @csrf
                                <input type='hidden' name="CustomerID" value="{{$users->CustomerID}}">
                                <div class="form-group">
                                    <div class="col-6">
                                        <label for="">Division</label>
                                        <select class="form-control custom-select" name="divisions[]" id="division_id">
                                            <option></option>
                                        </select>
                                        @error('divisions')
                                        <div class="error">{{ $message }}</div>
                                        @enderror
                                    </div>
                                </div>
                                <div class="card-footer">
                                    <button type="submit" class="btn btn-primary">Update</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- model replace user --}}
<div class="modal" tabindex="-1" role="dialog" id="modalieventlist">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title">User List</h5>
            </div>
            <div class="modal-body">
                <div class="card-body">
                    <form action="{{ route('users.add_customer_id') }}" method="post">
                        @csrf
                        <input type="hidden" value="customer_id" name="db_customer_id" id="modaldb_CustomerID">
                        <div class="table-responsive text-nowrap">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Customer Id</th>
                                        <th>User Name</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody class="table-border-bottom-0" id="replace_user">
                                </tbody>
                            </table>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function replace_user(elm) {
    // var id = $(elm).attr('data-id');
    var db_CustomerID = $(elm).attr('data-CustomerID');
    $.ajax({
        url: "{{ url('ajax/get_userlist') }}",
        type: "GET",
        data: {
            db_CustomerID: db_CustomerID,
        },

        success: function(response) {
            if (response) {
                $("#replace_user").html('');
                $.each(response['data'], function(i, item) {

                    html =  `<tr>
                                <td>
                                    ` + item['CustomerID'] + `
                                </td>
                                <td>
                                    ` + item['CustomerName'] + `
                                </td>
                                <td>
                                    <input type="radio" name="CustomerID"  id="CustomerName" value=" ` + item['CustomerID'] + `" required>
                                </td>
                             </tr>`;
                    $("#replace_user").append(html);
                    $('#modaldb_CustomerID').val(db_CustomerID);
                    $("#modalieventlist").modal('show');
                });
            } else {
                alert('no data found!')
            }
        },
        error: function(error) {
            console.log(error);
        }
    });
}
</script>
<script>
    $(document).ready(function() {
        $("#division_id").select2({
            placeholder: "Select Division",
            multiple: false,
        });
        setDivisions();
    });

    function setDivisions() {
        var divisionIds = {!! json_encode($selectedDivisionIdsString) !!};
        var divisions = {!! $divisions !!};

        var options = [];
        var divisionId = $('#division_id');
        divisionId.html(''); // Clear existing options

        $.each(divisions, function(index, value) {
            var selected = divisionIds.includes(value.id.toString());
            var option = new Option(value.name, value.id, false, selected);
            divisionId.append(option);
            options.push({id:value.id,text:value.name});
        });

        divisionId.trigger({
            type: 'select2:select',
            params: {
                data: options
            }
        });

        divisionId.val(divisionIds).trigger('change');
    }
</script>
@endsection
