@extends('layouts.app-hr')

@section('content')
    <?php Fun::lang()?>
    @include('hr.employee-rotate.modal-show')
    @include('hr.employee-rotate.confirm-delete')
    <div class="top-header">
        <ol class="breadcrumb">
            <li><a href="#">{{__('administrator.Dashboard')}}</a></li>
            <li><a href="#">{{__('administrator.rotate')}}</a></li>
        </ol>
    </div>
    <div class="page">
        <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.rotate')}} </h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="box-body">
            <table id="datatable_products" width="100%" class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.code')}}</th>
                        <th>{{__('administrator.khmer_name')}}</th>
                        <th>{{__('administrator.english_name')}}</th>
                        <th>{{__('administrator.rotate_date')}}</th>
                        <th>{{__('administrator.current_branch')}}</th>
                        <th>{{__('administrator.current_department')}}</th>
                        <th>{{__('administrator.current_position')}}</th>
                        <th>{{__('administrator.to_branch')}}</th>
                        <th>{{__('administrator.to_department')}}</th>
                        <th>{{__('administrator.to_position')}}</th>
                        <th>{{__('administrator.reason')}}</th>
                        <th>{{__('administrator.action')}}</th>
                    </tr>
                </thead>

            </table>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            $(document).on('click', '.btn-delete', function(){
                var id = $(this).data('id');
                $('#delete-id').val(id);
                $('#form-delete').modal('show');
            });
            $(document).on('click', '.btn-show', function(){
                var id = $(this).data('id');
                var route = "{{route('hr.show-rotate',':id')}}";
                route = route.replace(':id', id);
                $.ajax({
                    url: route,
                    type: 'GET',
                    data: {
                        _token: "{{csrf_token()}}",
                        id: id
                    },
                    success: function(data){

                        var old_date = data.rotate_date ? data.rotate_date : "";
                        var arr = old_date.split('-');
                        var new_date = arr[2]+'-'+arr[1]+'-'+arr[0];

                        var current_branch      = data.from_branch != null ? data.from_branch.english_name : "";
                        var current_department  = data.from_department != null ? data.from_department.english_name : "";
                        var current_position    = data.from_position != null ? data.from_position.english_name : "";

                        var to_branch           = data.to_branch != null ? data.to_branch.english_name : "";
                        var to_department       = data.to_department != null ? data.to_department.english_name : "";
                        var to_position         = data.to_position != null ? data.to_position.english_name : "";

                        $('#code').val(data.code);
                        $('#khmer_name').val(data.name_kh);
                        $('#english_name').val(data.name);
                        $('#rotate_date').val(new_date);
                        $('#current_branch').val(current_branch);
                        $('#current_department').val(current_department);
                        $('#current_position').val(current_position);
                        $('#to_branch').val(to_branch);
                        $('#to_department').val(to_department);
                        $('#to_position').val(to_position);
                        $('#reason').val(data.reason);
                        
                        $('#form-show').modal('show');
                    },
                    error: function(){
                        console.log('error');
                    }
                });

            });

            $('#datatable_products').DataTable({
                ordering: false,
                serverSide: true,
                ajax: '{{route("datatable.get-retation")}}',
                lengthMenu: [[20, 30, 50, 100, 1000], [20, 30, 50, 100, 1000]],
                columns:
                    [
                        {data: 'DT_RowIndex', orderable: false, searchable: false},
                        {data: 'code'},
                        {data: 'name_kh'},
                        {data: 'name'},
                        {data: 'rotate_date'},
                        {data: 'current_branch'},
                        {data: 'current_department'},
                        {data: 'current_position'},
                        {data: 'transfer_branch'},
                        {data: 'transfer_department'},
                        {data: 'transfer_position'},
                        {data: 'reason'},
                        {data: 'action'},

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

            $.fn.dataTable.ext.errMode = 'throw';
        });
    </script>
@endsection
