<?= form_open('', 'id="menu_form"', ['id_menu' => $id_menu]) ?>
   <div class="modal-body">
      <?= xform_select2('id_modul', $dd_moduls, $id_modul, 'Nama Modul', 3, 9) ?>

      <?= xform_select2('id_parent', $dd_menus, $id_parent, 'Menu Parent', 3, 9) ?>

      <?= xform_input('nama_menu', 'Nama Menu', $nama_menu, 3, 9, true, 100, 'ginputx') ?>

      <?= xform_input('method', 'Method', $method, 3, 9, true, 100, 'ginputx') ?>

      <?= xform_input('urut', 'No. Urut', $urut, 3, 9, true, 2, 'angkax') ?>
   </div><hr>
   <div class="invalid-feedback text-center" id="em_message"></div>
   <div class="modal-footer">
      <button type="button" class="btn btn-secondary waves-effect waves-themed" data-dismiss="modal">Close</button>
      <button type="submit" id="btn_rekam2" class="btn btn-primary waves-effect waves-themed">
         Save Changes
      </button>
   </div>
</form>

<script type="text/javascript">

   $('.select2').select2({
      dropdownCssClass:'increasezindex'
   });

   $('.ginputx').inputmask({
      regex: "^[a-zA-Z0-9\\s.,@-]*$"
   });

   $('.angkax').numeric();

   $('#sel_id_modul').on('change', function() {  
      $('#sel_id_parent').empty();              
      $.ajax({
         type: "POST",
         url: "<?= base_url('admin/modul/get_parent') ?>",
         dataType: 'json',
         data: { 'id_modul': this.value },
         success: function(data){
            var opts = data;//$.parseJSON(data);
            $.each(opts, function(i, d) {
               opts += '<option value="' + d.id_menu + '">' + d.nama_menu + '</option>';
            });

            $('#sel_id_parent').append(opts);
         }
      });
   });

   $("#menu_form").submit(function(e){
      e.preventDefault();
      var url = '<?= $action ?>'
      var formData = $(this).serialize();
      $.ajax({
         url: url,
         type: "post",
         dataType: 'json',
         data: formData,
         beforeSend  : function() {
            $(".form-group").removeClass("is-invalid");
            $(".invalid-feedback").html('').hide();
            BtnLoading($("#btn_rekam2"));
         },
         error: function (xhr, status, message) {
            var res = JSON.parse(xhr.responseText);
            $(res.messages).each(function(i,val){
               var inputname = Object.keys(val)[0];
               $.each(val,function(k,v){
                  $("#"+inputname).focus();
                  $("#"+k).addClass("is-invalid");
                  $("#em_"+k).html(v).fadeIn();
               });
            });
            BtnReset($("#btn_rekam2"));
         },
         success: function(res) {
            $('#modal-menu').modal('hide');
            $('#datagrid_menu').datagrid('load');
            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
         },
         complete: function() {
            BtnReset($("#btn_rekam2"));
         }
      });
   });
</script>