<script type="text/javascript">

<?php if($page_name=='work_profile'){?>
  var work_id = "<?php echo $work_data->work_id?>";
  get_labours(work_id);
  <?php } ?>

  function get_labours(id) {
    <?php if($this->config->item('Labour_projet_assigning')==1 && ($this->session->userdata('user_role')=='general_manager' || $this->session->userdata('user_role')=='admin' || $this->session->userdata('user_role')=='madmin')){?>
    $('#table_labour_assign_add').dataTable().fnDestroy();
    $('#table_body_labour_assign_add').html('');
    $.post("<?php echo base_url() ?>labours/select_labour",function(data) {
      var obj = $.parseJSON(data);
      var d = obj.data;
      var count = d.length;
      if(count>0){
        var body = '';
        for (i = 0; i < count; i++) {
          var row = d[i];
          if(row[0]!=''){
            var checked = '';
            var assigned_projects = row[21].split(",");
            if(assigned_projects.indexOf(id)!=-1){
              checked = 'checked';
            }
            var checkbox = '<div class="m-form__group form-group"><div class="m-checkbox-list"><label style="color: white" class="m-checkbox m-checkbox--bold m-checkbox--state-success"><input type="checkbox" '+checked+' id="selected_labours_'+i+'" name="selected_labours['+i+']" value="'+row[0]+'" class="labour_checkboxes" ><span style="top: -8px !important;"></span></label></div></div>';
          }else{
            var checkbox = '';
          }
          body += '<tr>';
          body += '<td>'+checkbox+'</td>';
          body += '<td>'+row[0]+'</td>';
          body += '<td>'+row[2]+'</td>';
          body += '<td>'+row[3]+'</td>';
          body += '<td>'+row[4]+'</td>';
          body += '<td>'+row[5]+'</td>';
          body += '<td>'+row[6]+'</td>';
          body += '</tr>';
        }
      }else{
        var body = '<tr><td class="text-center" colspan="7">No Labours Found</td></tr>';
      }
      $('#table_body_labour_assign_add').append(body);
      $('#table_labour_assign_add').DataTable({
        "paging":   false,
        "ordering": false,
        "info":     false,
      });
    });
    $('#labours_table_div').show();
    <?php } ?>
  }

  $('body').on('change', '#labour_work_view', function() {
    var project_id = $(this).val();
    get_labours(project_id);
  })

  $('body').on('change', '.labour_checkboxes', function() {
    if($(this).prop("checked") == false){
      var labour_id = $(this).val();
      var unchecked_labours = $('#unchecked_labours').val();


      if(unchecked_labours!=''){
        var unchecked_array = unchecked_labours.split(",");
        if(unchecked_array.indexOf(labour_id)==-1){
          var unchecked = unchecked_labours+','+labour_id;
          $('#unchecked_labours').val(unchecked);
        }
      }else{
        var unchecked = labour_id;
        $('#unchecked_labours').val(unchecked);
      }

    }
  })


  $("#labour_work_view").select2({
    placeholder: "Select  Work",
    dropdownParent: $("#form_labour_assign")
  })


</script>




<script>

<?php if($page_name!='work_profile'){?>

  var assigned_labour_view_table = $('#assigned_labour_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'labours/select_labour' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        // d.labour_type = 1;
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',title: 'Assigned Labours' },
      { 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": [ 1,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22],
        className: "hidden"
      }
    ],
    'order': false
  });

  $(".table_filter").on("change", function() {
    assigned_labour_view_table.ajax.reload();
  });


  $(".form_submit_btn").click(function (e) {

    e.preventDefault();
    var a = $(this),
    l = $(this).closest("form"),
    action = l.attr('action');
    var form_id = l.attr('id');
    l.validate({
      rules: {
        "mobile_number": {
          required: !0,
          number: !0,
          minlength: 8,
          maxlength: 12
        }
      }, invalidHandler: function (e, r) {
        var i = $("#" + form_id + "_msg");
        i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
      }, submitHandler: function (e) {
      }
    }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
    l.ajaxSubmit({
      url: action,
      success: function (e, t, r, s) {
        var modal_id = l.attr('data-modal-id');
        var form_type = l.attr('data-form-type');
        $("#" + form_id + "_msg").addClass("m--hide");
        // Block UI
        mApp.blockPage({
          overlayColor: "#000000",
          type: "loader",
          state: "success",
          message: "Please wait..."
        }), setTimeout(function () {
          mApp.unblockPage()
        }, 2e3);
        var obj = $.parseJSON(e);
        if (obj.status == 1) {
          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
          toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);

          $("#"+modal_id).modal('toggle');
          $('#table_body').html('');
          $("."+form_id+"_select").val('').trigger('change');
          $('#labours_table_div').hide();

          assigned_labour_view_table.ajax.reload();

        }
        if (obj == null) {
          setTimeout(function () {
            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
            toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
          }, 2e4)
        }
      },error: function() {
        a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
        toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
      }
    })
  )
});

<?php } ?>

</script>
