<script>
<?php if($page_name=='user_activities_view'){?>

  $("#table_filter_user").select2({
    placeholder: "Select  User"
  })
  $("#table_filter_work").select2({
    placeholder: "Select  Work",
    templateResult: format_project_dropdown
  })

  function GetSearchUser(){
    return $('#table_filter_user').val();
  }
  function GetSearchwork(){
    return $('#table_filter_work').val();
  }
  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }

  <?php } ?>
  var user_activities_view_table = $('#user_activities_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'user/select_activities' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='user_activities_view'){?>
          // d.date = GetSearchDate();
          d.user = GetSearchUser();
          d.work = GetSearchwork();
d.date = GetSearchDate();
          <?php }elseif($page_name=='work_profile' || $page_name=='projects_profile'){ ?>
            d.work =   "<?php echo $work_data->work_id?>";
            <?php }elseif($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                <?php } ?>

          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'User Activities' },
          { extend: 'csv', className: 'btn purple btn-outline ' },
          { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
        ],
        dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
        "lengthMenu": [
          [25, 50, 100, -1],
          [25, 50, 100, "All"] // change per page values here
        ],
        "columnDefs": [
          {
            "targets": [],
            className: "hidden"
          }
        ],
        'order': false
      });
      $(".table_filter").on("change", function() {
        user_activities_view_table.ajax.reload();
      });
      $(".hide_column").hide();
      <?php if($page_name=='user_activities_view'){?>
        // Date Range Picker
        var start = moment();
        var end = moment();

        function cb(start, end) {
          $('#table_filter_date_range').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
          user_activities_view_table.ajax.reload();
        }

        $('#table_filter_date_range').daterangepicker({
          buttonClasses: "m-btn btn",
          applyClass: "btn-primary",
          cancelClass: "btn-secondary",
          startDate: start,
          endDate: end,
          locale: {
            format: 'MMMM D, YYYY'
          },
          ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
            'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
          }
        }, cb);

        cb(start, end);

      <?php } ?>

      </script>
