@extends('adminlte::page')
@section('content')
    <?php Fun::lang()?>
	<div class="top-header">
		<ol class="breadcrumb">
			<li><a href="#">{{__('administrator.Dashboard')}}</a></li>
			<li><a href="{{route('users.index')}}">{{__('administrator.setup_user')}}</a></li>
			<li class="active">{{__('administrator.setup_user')}}</li>
		</ol>
	</div>
	<div class="page">
<div class=container-fluid>
		<div class="top-content">
			<div class="row">
				<div class="col-sm-6 col-xs-12">
					<div class="title-add">
						<div class="pull-left">
							<h3>{{__('administrator.setup_user')}}</h3>
						</div>

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

		{!! Form::open(array('route' => 'user.setup-user-store','method'=>'POST')) !!}
		<div class="row">

			<div class="col-sm-12">
				<div class="master-form">
					<div class="title-note">
						<h3><span class="fa fa-exclamation-triangle"></span> {{__('administrator.setup_user')}} <i>({{__('administrator.fields_in_red_are_required')}})</i></h3>
					</div>
					<div class="form-group col-sm-4 col-xs-12">
						<div class="col-sm-4 col-xs-12 ">
							<label class="required" for="name">{{__('administrator.group')}} </label>
						</div>
						<div class="col-sm-8 col-xs-12">
							{!! Form::select('group', $group,null, array('placeholder'=>'Please Select ','class' => 'group form-control select2','required')) !!}
						</div>
					</div>
					<div class="form-group col-sm-4 col-xs-12">
						<div class="col-sm-3 col-xs-12 ">
							<label class="required" for="name">{{__('administrator.user')}} </label>
						</div>
						<div class="col-sm-9 col-xs-12">
							{!! Form::select('user', $user,null, array('class' => 'form-control select2 user','required')) !!}
						</div>
					</div>
					<div class="form-group col-sm-4 col-xs-12">
						<div class="col-sm-4 col-xs-12 ">
							<label class="required" for="name">{{__('administrator.positions')}} </label>
						</div>
						<div class="col-sm-8 col-xs-12">
							{!! Form::select('position', [],null, array('class' => 'form-control select2 position','required')) !!}
						</div>
					</div>
					<div class="col-sm-12 col-xs-12">
						<div class="form-group footer-save">
							<button type="submit" class=" button_primary pull-right"><span class="fa fa-save"></span> {{__('administrator.save')}}</button>
							<button type="button" onClick="history.go(-1)" class=" button_warning pull-right">
								<span class=" fa fa-arrow-circle-left"></span> {{__('administrator.back')}}</button>
						</div>
					</div>
				</div>
			</div>
		</div>
		{!! Form::close() !!}
		<div class="wrapper-table">
			<table id="datatable_user" width="100%" class="table table-striped">
				<thead>
					<tr>
						<th>#</th>
						<th>{{__('administrator.name')}}</th>
						<th>{{__('administrator.email')}}</th>
						<th>{{__('administrator.leader')}}</th>
						<th>{{__('administrator.group')}}</th>
						<th>{{__('administrator.department')}}</th>
						<th>{{__('administrator.action')}}</th>
					</tr>
				</thead>
				<tbody>

				</tbody>
			</table>
		</div>
	</div>
    <div class="modal fade bs-modal-lg" id="form_delete" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-dm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h3 class="modal-title">Are you sure?</h3>
                </div>
                <br/>
                <form role="form" method="delete" id="delete">
                    {{csrf_field()}}
                    <div class="row">
                        <div class="col-md-12">
                            <div class="col-md-10">
                                <h4>Are you want to delete?</h4>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" data-dismiss="modal" class="button_danger"><i class="fa fa-close"></i> No</button>
                        <button type="button" class="button_primary deleteRecord" data-toggle="modal"><i class="fa fa-save"></i> OK</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

@endsection
@section('js')
	<script type="text/javascript">
        $(document).ready(function() {
            $('#datatable_user').DataTable({
                ordering: false,
                serverSide: true,

                ajax: "{{ route('user.datatable-user') }}",
                columns:
                    [
                        {data: 'DT_RowIndex'},
                        {data: 'agent_id'},
                        {data: 'email'},
                        {data: 'leader_id'},
                        {data: 'group_id'},
                        {data: 'department_id'},
                        {data: 'action'},

                    ],

                "initComplete": function(settings, json) {
                }
            });

            $.fn.dataTable.ext.errMode = 'throw';
        });
        $(document).on('click', '.delete_products', function() {
            $('#form_delete').modal('show');
            var id = $(this).data("id");
            $(".deleteRecord").click(function(){
                var token = $("meta[name='csrf-token']").attr("content");
                $.ajax({
                    url: "{{route('user.delete_setup_group')}}",

                    type: 'GET',
                    data: {
                        "id": id,
                        "_token": token,
                    },
                    success: function (){
                        $('#form_delete').modal('hide');
                        location.reload();  
                    }
                });
            });
        });

	</script>



	<script>
        $(document).ready(function () {
            $('.group').on('change', function () {
                id = $(this).val();

                if (id) {
                    $.ajax({
                        url: '{{route('user.get-leader')}}',
                        type: 'GET',
                        data: {id: $(this).val()},
                        success: function (data) {
                            $('.position').html('');
                            var json = $.parseJSON(data);
                            if(json.leader_group_id!=null){
                                $('.position').append('<option value="Staff">Staff</option>');
                            }else{
                                $('.position').append(
                                    '<option value="leader">Leader</option><option value="Staff">Staff</option>');
                            }
                            $('.main-spinner').hide();
                        }
                    });
                }
                else {

                    $('.position').html('');
                }
            });
        });
	</script>
@stop


