<?php
include("../../packages/require.php");
include("controller/controller_unit.php");
$curpage = "unit";
$page_name = "module/unit/index.php";
if($acsg_unit == 0){
    header("Location:".$path['dashboard']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?=$title['unit'];?></title>
    <?php include($global['root']."packages/head.php");?>
</head>
<body class="<?=$global['body-css'];?>">
    <div class="wrapper">

        <?php include($global['root']."section/header.php");?>
      
        <?php include($global['root']."section/sidebar.php");?>

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                  <h1>
                    Module Unit
                    <small>Adding and editing your unit</small>
                  </h1>
                  <ol class="breadcrumb">
                        <li><a href="<?=$path['dashboard'];?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Unit</li>
                  </ol>
            </section>

            <!-- Main content -->
            <section class="content">
                <!-- Default box -->
                <div class="box">
                    <div class="box-header with-border">
                        <div class="row">
                            <div class="col-xs-12 col-sm-6"></div>
                            <div class="col-xs-12 col-sm-6">
                                <div class="head-btn text-right">
                                    <?php if($acsun_create == 1){ ?>
                                    <a href="#modal-insert" data-toggle="modal" data-target="#modal-insert" class="btn btn-primary">
                                        <i class="fa fa-plus"></i> Add New Data
                                    </a>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="box-body">
                        <div class="body-total text-right" style="margin-bottom: 15px;">
                            <span class="numbadge">
                                Total Data <span class="badge bg-light-blue"><?=$total_data;?></span>
                            </span> 
                        </div>
                        <div class="body-content">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-middle">
                                    <tr>
                                        <th class="text-center">#</th>
                                        <th>Title</th>
                                        <th class="text-center">Create Date</th>
                                        <th class="text-center">Action</th>
                                    </tr>
                                    <?php if(is_array($datas)) { $num=1; foreach($datas as $data){ ?>
                                    <tr>
                                        <td class="text-center"><?=($O_page-1)*10+$num;?>.</td>
                                        <td style="text-transform: uppercase;"><?=correctDisplay($data['unit_title']);?></td>
                                        <td class="text-center">
                                            <?=date("d-m-Y H:i", strtotime($data['unit_create_date']));?>
                                        </td>
                                        <td class="text-center">
                                            <div class="btn-group">
                                                <?php if($acsun_edit == 1){ ?>
                                                <a href="javascript:void(0);" onclick="openEdit('<?=$data['unit_id'];?>', '<?=correctDisplay($data['unit_title']);?>');" class="btn btn-info btn-xs btn-flat">
                                                    <i class="fa fa-edit"></i> Edit
                                                </a>
                                                <?php } else { ?>
                                                <a href="#" class="btn btn-default btn-xs btn-flat disable-state">
                                                    <i class="fa fa-edit"></i> Edit
                                                </a>
                                                <?php } ?>
                                                <?php if($acsun_delete == 1){ ?>
                                                <a href="javascript:void(0);" onclick="confirmDelete('<?=$data['unit_id'];?>', '<?=correctDisplay($data['unit_title']);?>');" class="btn btn-danger btn-xs btn-flat">
                                                    <i class="fa fa-trash"></i> Delete
                                                </a>
                                                <?php } else { ?>
                                                <a href="#" class="btn btn-default btn-xs btn-flat disable-state">
                                                    <i class="fa fa-trash"></i> Delete
                                                </a>
                                                <?php } ?>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php $num++; } } else { ?>
                                    <tr class="warning">
                                        <td colspan="5" class="bold" align="center">There is no data right now!</td>
                                    </tr>
                                    <?php } ?>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer clearfix">
                        <div class="row">
                            <div class="col-sm-5">
                                <?php if($total_data > 0){ ?>
                                <div class="dataTables_info">
                                    Showing <?=($O_page-1)*10+1;?> to <?=($O_page-1)*10+$total_show;?> of <?=$total_data;?> entries
                                </div>
                                <?php } ?>
                            </div>
                            <div class="col-sm-7">
                                <?php
                                $batch = getBatch($O_page);
                                if($batch < 1){$batch = 1;}
                                $prevLimit = 1 +(10*($batch-1));
                                $nextLimit = 10 * $batch;

                                if($nextLimit > $total_page){
                                    $nextLimit = $total_page;
                                }
                                ?>
                                <ul class="pagination pagination-sm no-margin pull-right">
                                    <?php if($O_page > 1){?>
                                    <li>
                                        <a href="<?php echo $page_name."?page=".($O_page-1);?>">Previous</a>
                                    </li>
                                    <?php } ?>
                                    <?php for($mon = $prevLimit; $mon <= $nextLimit;$mon++){ ?>
                                    <li class="<?php if($mon == $O_page){echo 'active';}?>">
                                        <a href="<?php echo $page_name."?page=".$mon;?>"><?=$mon;?></a>
                                    </li>
                                    <?php } ?>
                                    <?php if($total_page > 1 && ($O_page < $total_page)){?>
                                    <li>
                                        <a href="<?php echo $page_name."?page=".($O_page+1);?>">Next</a>
                                    </li>
                                    <?php } ?>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /.box-footer-->
            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

        <?php include($global['root']."section/footer.php");?>

    </div>
    <!-- ./wrapper -->
    <?php if($acsun_create == 1){ ?>
    <div id="modal-insert" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog">
        <div class="modal-dialog mde-dialog modal-sm" role="document">
            <div class="modal-content mde-content">
                <div class="modal-header mde-header">
                    <h4 class="modal-title mde-title">Insert New Data</h4>
                </div>
                <form id="form-insert" name="form-insert" action="<?=$path['unit'];?>?action=add" enctype="multipart/form-data" method="post" onsubmit="return processForm();">
                    <div class="modal-body mde-body">
                        <div class="form-group" style="width: 500px;">
                            <label for="input-title">Title *</label>
                            <input type="text" class="form-control" name="title" id="input-title" placeholder="Title" style="text-transform: lowercase;">
                            <div id="error-title" class="error-text"></div>
                        </div>
                    </div>
                    <div class="modal-footer mde-footer">
                        <div class="row">
                            <div class="col-sm-6 col-xs-6">
                                <div class="text-left">
                                    <a href="javascript:void(0);" data-dismiss="modal" class="btn btn-default">
                                        <i class="fa fa-arrow-left"></i> Close
                                    </a>
                                </div>
                            </div>
                            <div class="col-sm-6 col-xs-6">
                                <div class="text-right">
                                    <div class="btn-group">
                                        <button type="submit" id="btn-submit" class="btn btn-success">
                                            <i class="fa fa-save"></i> Save Data
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php } ?>
    <?php if($acsun_edit == 1){ ?>
    <div id="modal-edit" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog">
        <div class="modal-dialog mde-dialog modal-sm" role="document">
            <div class="modal-content mde-content">
                <div class="modal-header mde-header">
                    <h4 class="modal-title mde-title">Edit Data</h4>
                </div>
                <form id="form-edit" name="form-edit" action="<?=$path['unit'];?>?action=edit" enctype="multipart/form-data" method="post" onsubmit="return processFormEdit();">
                    <div class="modal-body mde-body">
                        <div class="form-group" style="width: 500px;">
                            <label for="input-etitle">Title *</label>
                            <input type="text" class="form-control" name="title" id="input-etitle" placeholder="Title" style="text-transform: lowercase;">
                            <div id="error-etitle" class="error-text"></div>
                        </div>
                    </div>
                    <div class="modal-footer mde-footer">
                        <div class="row">
                            <div class="col-sm-6 col-xs-6">
                                <div class="text-left">
                                    <a href="javascript:void(0);" data-dismiss="modal" class="btn btn-default">
                                        <i class="fa fa-arrow-left"></i> Close
                                    </a>
                                </div>
                            </div>
                            <div class="col-sm-6 col-xs-6">
                                <div class="text-right">
                                    <input type="hidden" value="" id="input-item_id" name="item_id">
                                    <div class="btn-group">
                                        <button type="submit" id="btn-esubmit" class="btn btn-success">
                                            <i class="fa fa-save"></i> Save Changes
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php } ?>

    <?php include($global['root']."packages/footer.php");?>
    <script type="text/javascript">
        $(document).ready(function() {
            <?php if($message != "") { ?>
            //use session here for alert success/failed
            var alertText = "<?=$message;?>";
                <?php if($alert != "success"){ ?>
                errorAlert(alertText);// error alert
                <?php } else { ?>
                successAlert(alertText);// success alert
                <?php } ?>
            <?php } ?>
        });
        //function confirmation delete
        function confirmDelete(num, text){
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Delete ! ",
                cancelButtonText: "Cancel !",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "<?=$path['unit'];?>index.php?action=delete&id="+num+"&title="+text;
                } else {
                    //nothing
                }
            });
        }
        function processForm(){
            var title = $("#input-title").val();
            $("#btn-submit").addClass("disable-state");
            $("#btn-submit").html("<i class='fa fa-spinner fa-spin'></i> Loading");
            $("#form-insert .form-control").addClass("disable-input");
            if(title != ""){
                $("#error-title").html("");
                $("#error-title").hide();
                $("#input-title").removeClass("input-error");
            } else {
                $("#error-title").show();
                $("#error-title").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-title").addClass("input-error");
                $("#input-title").focus();

                $("#btn-submit").removeClass("disable-state");
                $("#btn-submit").html("<i class='fa fa-save'></i> Save Data");
                $("#form-insert .form-control").removeClass("disable-input");

                return false;
            }
        }
        function processFormEdit(){
            var title = $("#input-etitle").val();
            $("#btn-esubmit").addClass("disable-state");
            $("#btn-esubmit").html("<i class='fa fa-spinner fa-spin'></i> Loading");
            $("#form-edit .form-control").addClass("disable-input");
            if(title != ""){
                $("#error-etitle").html("");
                $("#error-etitle").hide();
                $("#input-etitle").removeClass("input-error");
            } else {
                $("#error-etitle").show();
                $("#error-etitle").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-etitle").addClass("input-error");
                $("#input-etitle").focus();

                $("#btn-esubmit").removeClass("disable-state");
                $("#btn-esubmit").html("<i class='fa fa-save'></i> Save Changes");
                $("#form-edit .form-control").removeClass("disable-input");

                return false;
            }
        }
        $('#modal-insert').on('hidden.bs.modal', function (e) {
            $("#error-title").html("");
            $("#error-title").hide();
            $("#input-title").removeClass("input-error");
        });
        function openEdit(item_id,title){
            $("#input-item_id").val(item_id);
            $("#input-etitle").val(title);
            $('#modal-edit').modal('show');
        }
        $('#modal-edit').on('hidden.bs.modal', function (e) {
            $("#error-etitle").html("");
            $("#error-etitle").hide();
            $("#input-etitle").removeClass("input-error");
            $("#input-item_id").val("");
            $("#input-etitle").val("");
        });
    </script>
</body>
</html>