<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1>
                    
                </h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="<?php echo base_url();?>">Dashboard</a></li>
                    <li class="breadcrumb-item"><?php echo $parent;?></li>
                    <li class="breadcrumb-item"><a href="<?php echo base_url()."index.php/".$link;?>"><?php echo $titlebefore;?></a></li>
                    <li class="breadcrumb-item active"><?php echo $title;?></li>
                </ol>
            </div>
        </div>
    </div>
</section>
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="card card-default">
                    <?php 
                        $setdisabled    = "";
                        $setid          = "";
                        $setname        = "";
                        $setlink        = "";
                        $seticon        = "";
                        $setparent      = "";
                        $setlevel       = "";
                        $setaction      = "";
                        $setsort        = "";
                        $setstatus      = 1;

                        $setnumberfield = 0;

                        if ($actionform=="prosesview"){
                            $setdisabled="disabled='disabled'";
                        }
                        if ($actionform!="prosesadd"){
                            $setid      = $mst->row('mm_id');
                            $setname    = $mst->row('mm_name');
                            $setlink    = $mst->row('mm_link');
                            $seticon    = $mst->row('mm_icon');
                            $setparent  = $mst->row('mm_parent');
                            $setlevel   = $mst->row('mm_level');
                            $setaction  = $mst->row('mm_action');
                            $setsort    = $mst->row('mm_sort');
                            $setstatus  = $mst->row('mm_status');
                        }
                    ?>
                    <form action="<?php echo(base_url()); ?>index.php/<?php echo $link;?>/<?php echo $actionform; ?>" method="post" autocomplete="off">
                        <div class="card-header">
                            <h3 class="card-title"><?php echo $title;?></h3>
                        </div>
                        <div class="card-body">
                            <?php if(validation_errors()){ ?>
                                <div id="alert" class="alert alert-danger alert-dismissable">
                                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                    <i class="icon fa fa-ban"></i> Alert!
                                        <?php echo validation_errors() ?>
                                </div>
                                <script> 
                                    setTimeout(function() {
                                        $('#alert').hide('fast');
                                    }, 5000);
                                </script>
                            <?php } ?>
                            <div class="form-group row">
                                <label for="nama" class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-3">
                                    <input type="hidden" name="id" value="<?php echo $setid;?>"/>
                                    <input id="nama" name="nama" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Nama" value="<?php echo $setname;?>" />
                                </div>
                                <div class="col-sm-2">
                                    <input id="link" name="link" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Link" value="<?php echo $setlink;?>" />
                                </div>
                                <div class="col-sm-3">
                                    <input id="icon" name="icon" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Icon" value="<?php echo $seticon;?>" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="parent" class="col-sm-2 col-form-label">Parent</label>
                                <div class="col-sm-3">
                                    <select <?php echo $setdisabled; ?> id="parent" name="parent" class="form-control select" >
                                        <option value="">Pilih Parent</option>
                                        <?php 
                                            foreach ($ma as $dat) {
                                                $select="";
                                                if($setparent==$dat->mm_id){$select="selected='selected'";}
                                                echo "<option value='".$dat->mm_id."'".$select.">".$dat->mm_name."</option>";
                                            }
                                        ?>
                                  </select>
                                </div>
                                <div class="col-sm-2">
                                    <select <?php echo $setdisabled; ?> id="level" name="level" class="form-control select" >
                                        <option value="">Pilih Level</option>
                                        <option <?php if($setlevel=="1"){echo "selected='selected'";}?> value="1">Parent</option>
                                        <option <?php if($setlevel=="2"){echo "selected='selected'";}?> value="2">Child 2</option>
                                        <option <?php if($setlevel=="3"){echo "selected='selected'";}?> value="3">Child 3</option>
                                    </select>
                                </div>
                                <div class="col-sm-3">
                                    <select <?php echo $setdisabled; ?> id="action" name="action" class="form-control select" >
                                        <option <?php if($setaction==""){echo "selected='selected'";}?> value="">Form Action False</option>
                                        <option <?php if($setaction=="1"){echo "selected='selected'";}?> value="1">Form Action True</option>
                                    </select>
                                </div>

                            </div>
                            <div class="form-group row">
                                <label for="sort" class="col-sm-2 col-form-label">Sort Menu</label>
                                <div class="col-sm-3">
                                    <input id="sort" name="sort" type="number" <?php echo $setdisabled;?> class="form-control" placeholder="Sort" value="<?php echo $setsort;?>" />
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-sm-2"></div>
                                <div class="col-sm-2">
                                    <input id="status" name="status" type="checkbox" value="1" <?php echo $setdisabled;?> <?php if($setstatus==1){echo "checked";}?> data-bootstrap-switch data-off-color="danger" data-on-color="success"/>
                                </div>
                            </div>
                        </div>
                        <div class="card-header">
                            <h3 class="card-title">Field Data</h3>
                            <div class="card-tools">
                                <button <?php echo $setdisabled;?> onclick="addfield()" type="button" class="btn btn-success"><i class="fa fa-plus"></i> Add Field Data</button>
                            </div>
                        </div>
                        <script>
                            function addfield(){
                                var num = parseInt($("#numadd").val())+1;
                                $("#numadd").val(num);
                                $("#addfieldhere").append(" <div id='fieldnya"+num+"' class='form-group row'> "+
                                                                "<div class='col-sm-2'></div>"+
                                                                "<div class='col-sm-3'>"+
                                                                    "<input id='fieldlabel"+num+"' name='fieldlabel[]' type='text' class='form-control' placeholder='Field Label' value='' />"+
                                                                    "<input type='hidden' id='delfield"+num+"' name='delfield[]' value='2'/>"+
                                                                "</div>"+
                                                                "<div class='col-sm-2'>"+
                                                                    "<input id='fieldid"+num+"' name='fieldid[]' type='text' class='form-control' placeholder='Field ID' value='' />"+
                                                                "</div>"+
                                                                "<div class='col-sm-3'>"+
                                                                    "<input id='fieldname"+num+"' name='fieldname[]' type='text' class='form-control' placeholder='Field Name' value='' />"+
                                                                "</div>"+
                                                                "<div class='col-sm-2'>"+
                                                                    "<button onclick='delappfield("+num+")' type='button' class='btn btn-warning'><i class='fa fa-ban'></i> Delete</button>"+
                                                                "</div>"+
                                                            "</div>");
                            }

                            function delappfield(num){
                                $("#fieldnya"+num).remove();
                            }
                            function delfield(num){
                                $("#delfield"+num).val(1);
                                var element = document.getElementById("fieldlabel"+num);
                                    element.classList.add("is-invalid");
                                var element = document.getElementById("fieldid"+num);
                                    element.classList.add("is-invalid");
                                var element = document.getElementById("fieldname"+num);
                                    element.classList.add("is-invalid");
                                $("#btndelfield"+num).remove();
                                $("#divdelfield"+num).append("<button id='btndelfield"+num+"' onclick='cancelfield("+num+")' type='button' class='btn btn-warning'><i class='fa fa-times'></i> Cancel</button>");
                            }

                            function cancelfield(num){
                                $("#delfield"+num).val(0);
                                var element = document.getElementById("fieldlabel"+num);
                                    element.classList.remove("is-invalid");
                                var element = document.getElementById("fieldid"+num);
                                    element.classList.remove("is-invalid");
                                var element = document.getElementById("fieldname"+num);
                                    element.classList.remove("is-invalid");
                                $("#btndelfield"+num).remove();
                                $("#divdelfield"+num).append("<button id='btndelfield"+num+"' onclick='delfield("+num+")' type='button' class='btn btn-danger'><i class='fa fa-ban'></i> Delete Field Data</button>");
                            }
                        </script>

                        <div id="addfieldhere" class="card-body"></div>
                        <div class="card-body">
                            <?php
                                if ($actionform!="prosesadd"){
                                    foreach ($fa as $dat){
                                        $setnumberfield++;
                            ?>
                                        <div id="divid<?php echo $setnumberfield;?>" class="form-group row">
                                            <div class="col-sm-2"></div>
                                            <div class="col-sm-3">
                                                <input type="hidden" id="idfield<?php echo $setnumberfield;?>" name="idfield[]" value="<?php echo $dat->mf_id;?>" />
                                                <input type="hidden" id="delfield<?php echo $setnumberfield;?>" name="delfield[]" value="0">
                                                <input id="fieldlabel<?php echo $setnumberfield;?>" name="fieldlabel[]" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Field Label" value="<?php echo $dat->mf_label;?>" />
                                            </div>
                                            <div class="col-sm-2">
                                                <input id="fieldid<?php echo $setnumberfield;?>" name="fieldid[]" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Field ID" value="<?php echo $dat->mf_field_id;?>" />
                                            </div>
                                            <div class="col-sm-3">
                                                <input id="fieldname<?php echo $setnumberfield;?>" name="fieldname[]" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Field Name" value="<?php echo $dat->mf_name;?>" />
                                            </div>
                                            <div id="divdelfield<?php echo $setnumberfield;?>" class="col-sm-2">
                                                <button <?php echo $setdisabled;?> id="btndelfield<?php echo $setnumberfield;?>" onclick="delfield(<?php echo $setnumberfield;?>)" type="button" class="btn btn-danger"><i class="fa fa-ban"></i> Delete Field Data</button>
                                            </div>
                                        </div>
                            <?php
                                    }
                                }
                            ?>
                        </div>
                        <input type="hidden" id="numadd" name="numadd" value="<?php echo $setnumberfield;?>">

                        <div class="card-footer">
                            <?php if ($actionform!="prosesview"){ ?>
                                <button type="submit" class="btn btn-info">Simpan</button>
                            <?php } ?>
                            <a href="<?php echo base_url()."index.php/".$link;?>" class="btn btn-default float-right">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>
