<?php
include("../../packages/require.php");
include("controller/controller_admin_detail.php");
$curpage = "admin";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?=$title['admin'];?></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 Admin
                    <small>Adding and editing your admin list</small>
                  </h1>
                  <ol class="breadcrumb">
                        <li><a href="<?=$path['dashboard'];?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="<?=$path['admin'];?>"> Admin</a></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-sm-6">
                                <h3 class="box-title">Edit Data ( <?=correctDisplay($data[0]['admin_name']);?> ) </h3>
                            </div>
                            <div class="col-sm-6">
                                <?php if($acsa_edit_admin == 1){?>
                                <div class="text-right">
                                    <a href="#modal-password" data-toggle="modal" data-target="#modal-password" class="btn btn-info">
                                        <i class="fa fa-lock"></i> Change Password
                                    </a>
                                </div>
                                <?php } else { ?>
                                <?php if($_SESSION['admin_id'] == $data[0]['admin_id']){ ?>
                                <div class="text-right">
                                    <a href="#modal-password" data-toggle="modal" data-target="#modal-password" class="btn btn-info">
                                        <i class="fa fa-lock"></i> Change Password
                                    </a>
                                </div>
                                <?php } ?>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <form name="form-edit" action="<?=$path['admin'];?>edit.php?action=edit" enctype="multipart/form-data" method="post" onsubmit="return validateForm();">
                        <div class="box-body">
                            <div class="form-group" style="width: 500px;">
                                <label for="input-email">Email *</label>
                                <input type="text" class="form-control" name="email" id="input-email" placeholder="Email Address" value="<?=correctDisplay($data[0]['admin_email']);?>">
                                <div id="error-email" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-username">Username *</label>
                                <input type="text" class="form-control" name="username" id="input-username" placeholder="Username" value="<?=correctDisplay($data[0]['admin_name']);?>">
                                <div id="error-username" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-role">Admin Role *</label>
                                <select id="input-role" name="role" class="form-control <?=$disable_role;?>">
                                    <option value="">-Choose Role-</option>
                                    <?php if(is_array($data_role)){ foreach($data_role as $roles){ ?>
                                    <?php if($roles['role_title'] == 'Developer' && $_SESSION['admin_role_name'] != 'Developer'){ ?>
                                    <option <?php if($data[0]['admin_role'] == $roles['role_id']){ echo "selected"; } ?> value="<?=$roles['role_id'];?>" style='display: none;'><?=$roles['role_title'];?></option>
                                    <?php } else { ?>
                                    <option <?php if($data[0]['admin_role'] == $roles['role_id']){ echo "selected"; } ?> value="<?=$roles['role_id'];?>"><?=$roles['role_title'];?></option>
                                    <?php } ?>
                                    <?php } } ?>
                                </select>
                                <div id="error-role" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-pic">Active PIC RMS*</label>
                                <select id="input-pic" name="pic" class="form-control <?php if($acsa_pic != 1){ ?>disable-input<?php } ?>">
                                    <option value="">-Choose-</option>
                                    <option <?php if($data[0]['admin_pic'] == 'yes'){ echo "selected"; } ?> value="yes">Yes</option>
                                    <option <?php if($data[0]['admin_pic'] == 'no'){ echo "selected"; } ?> value="no">No</option>
                                </select>
                                <div id="error-pic" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-adminc">Admin Client*</label>
                                <select id="input-adminc" name="adminc" class="form-control <?php if($acsa_pic != 1){ ?>disable-input<?php } ?>">
                                    <option value="">-Choose-</option>
                                    <option <?php if($data[0]['admin_pic_client'] == 'yes'){ echo "selected"; } ?> value="yes">Yes</option>
                                    <option <?php if($data[0]['admin_pic_client'] == 'no'){ echo "selected"; } ?> value="no">No</option>
                                </select>
                                <div id="error-adminc" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-region">Region *</label>
                                <select class="form-control" id="input-region" name="region">
                                    <option value="0">All Region</option>
                                    <?php if(is_array($data_region)){ foreach($data_region as $region){ ?>
                                    <option <?php if($data[0]['admin_ref_region'] == $region['region_id']){ echo "selected"; } ?> value="<?=$region['region_id'];?>"><?=ucfirst($region['region_title']);?></option>
                                    <?php } } ?>
                                </select>
                                <div id="error-region" class="error-text"></div>
                            </div>
                            <div class="form-group" style="width: 500px;">
                                <label for="input-image">Upload Photo *</label>
                                <div>
                                <?php if($data[0]['admin_img'] != ""){ ?>
                                <a href="<?=$global['absolute-url'].$data[0]['admin_img'];?>" class="image-link fancybox">
                                    <div class="img-bg" style="background-image: url('<?=$global['absolute-url'].$data[0]['admin_img'];?>');"></div>
                                </a>
                                <?php } else { ?>
                                <a href="<?=$global['absolute-url'].'img/dummy.jpg';?>" class="image-link fancybox">
                                    <div class="img-bg" style="background-image: url('<?=$global['absolute-url'].'img/dummy.jpg';?>');"></div>
                                </a>
                                <?php } ?>
                                </div>
                                <input type="file" id="input-image" name="image" class="form-control">
                                <p class="help-block">Image will be best shown in 400px by 400px.<br>image format has to be jpg, jpeg, gif, png.</p>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer clearfix">
                            <div class="row">
                                <div class="col-sm-6 col-xs-6">
                                    <div class="text-left">
                                        <div class="btn-group">
                                            <?php if($_SESSION['admin_role_name'] == 'Developer' || $_SESSION['admin_role_name'] == 'Master'){ ?>
                                            <?php if($_SESSION['admin_id'] != $data[0]['admin_id']){ ?>
                                            <a href="javascript:void(0);" onclick="confirmDelete('<?=$data[0]['admin_id'];?>', '<?=correctDisplay($data[0]['admin_name']);?>');" id="delete-data" class="btn btn-danger">
                                                <i class="fa fa-trash"></i> Delete
                                            </a>
                                            <?php } ?>
                                            <?php } ?>
                                            <?php if($data[0]['admin_active'] == 'active'){ ?>
                                            <a href="javascript:void(0);" onclick="confirmDeactive('<?=$data[0]['admin_id'];?>', '<?=correctDisplay($data[0]['admin_name']);?>');" id="deactive-data" class="btn btn-danger">
                                                <i class="fa fa-trash"></i> Deactive
                                            </a>
                                            <?php } else { ?>
                                            <a href="javascript:void(0);" onclick="confirmActive('<?=$data[0]['admin_id'];?>', '<?=correctDisplay($data[0]['admin_name']);?>');" id="deactive-data" class="btn btn-info">
                                                <i class="fa fa-check"></i> Activate
                                            </a>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6 col-xs-6">
                                    <div class="text-right">
                                        <div class="btn-group">
                                            <input type="hidden" name="admin_id" value="<?=$data[0]['admin_id'];?>">
                                            <input type="hidden" name="old_email" value="<?=correctDisplay($data[0]['admin_email']);?>">
                                            <button type="submit" class="btn btn-success">
                                                <i class="fa fa-save"></i> Save Data
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-footer-->
                    </form>
                </div>
            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

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

    </div>
    <!-- ./wrapper -->

    <!-- Modal Photo-->
    <div class="modal fade" id="modal-password" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Change Password</h4>
                </div>
                <form name="form-edit" action="<?=$path['admin'];?>edit.php?action=password" enctype="multipart/form-data" method="post" onsubmit="return validatePassword();">
                <div class="modal-body">
                    <?php if($data[0]['admin_password'] != ""){ ?>
                    <div class="form-group" style="width: 500px;">
                        <label for="input-opassword">Old Password *</label>
                        <input type="text" class="form-control disable-input" name="opassword" id="input-opassword" placeholder="Old Password" value="<?=decrypt_key($data[0]['admin_password']);?>">
                        <div id="error-opassword" class="error-text"></div>
                    </div>
                    <?php } ?>
                    <div class="form-group" style="width: 500px;">
                        <label for="input-password">New Password *</label>
                        <input type="password" class="form-control" name="password" id="input-password" placeholder="Password">
                        <div id="error-password" class="error-text"></div>
                    </div>
                    <div class="form-group" style="width: 500px;">
                        <label for="input-repassword">Re-Type New Password *</label>
                        <input type="password" class="form-control" name="repassword" id="input-repassword" placeholder="Re Type Password">
                        <div id="error-repassword" class="error-text"></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="row">
                        <div class="col-sm-6 text-left">
                            <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">
                                <i class="fa fa-times"></i> Close
                            </button>
                        </div>
                        <div class="col-sm-6 text-right">
                            <div class="btn-group">
                                <input type="hidden" name="radmin_id" value="<?=$data[0]['admin_id'];?>">
                                <button type="submit" class="btn btn-success btn-flat">
                                    <i class="fa fa-save"></i> Change Password
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                </form>
            </div>
        </div>
    </div>

    <?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 validateForm(){
            var email = $("#input-email").val();
            var username = $("#input-username").val();
            var pic = $("#input-pic").val();
            var adminc = $("#input-adminc").val();
            var region = $("#input-region").val();

            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            var numFormat = /^\+?[\d+ ]*$/;
            var urlFormat = /^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/;

            if(email != ""){
                if(email.match(mailformat)){
                    $("#error-email").html("");
                    $("#error-email").hide();
                    $("#input-email").removeClass("input-error");
                } else {
                    $("#error-email").show();
                    $("#error-email").html("<i class='fa fa-warning'></i> Incorrect email format.");
                    $("#input-email").addClass("input-error");
                    $("#input-email").focus();
                    return false;
                }
            } else {
                $("#error-email").show();
                $("#error-email").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-email").addClass("input-error");
                $("#input-email").focus();
                return false;
            }
            if(username != ""){
                $("#error-username").html("");
                $("#error-username").hide();
                $("#input-username").removeClass("input-error");
            } else {
                $("#error-username").show();
                $("#error-username").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-username").addClass("input-error");
                $("#input-username").focus();
                return false;
            }
            <?php if($acsa_pic == 1){ ?>
            if(pic != ""){
                $("#error-pic").html("");
                $("#error-pic").hide();
                $("#input-pic").removeClass("input-error");
            } else {
                $("#error-pic").show();
                $("#error-pic").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-pic").addClass("input-error");
                $("#input-pic").focus();
                return false;
            }
            <?php } ?>
            if(adminc != ""){
                $("#error-adminc").html("");
                $("#error-adminc").hide();
                $("#input-adminc").removeClass("input-error");
            } else {
                $("#error-adminc").show();
                $("#error-adminc").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-adminc").addClass("input-error");
                $("#input-adminc").focus();
                return false;
            }
            if(region != ""){
                $("#error-region").html("");
                $("#error-region").hide();
                $("#input-region").removeClass("input-error");
            } else {
                $("#error-region").show();
                $("#error-region").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-region").addClass("input-error");
                $("#input-region").focus();
                return false;
            }
        }
        function validatePassword(){
            //var opassword = $("#input-opassword").val();
            var password = $("#input-password").val();
            var repassword = $("#input-repassword").val();

            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            var numFormat = /^\+?[\d+ ]*$/;
            var urlFormat = /^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/;

            // if(opassword != ""){
            //     $("#error-opassword").html("");
            //     $("#error-opassword").hide();
            //     $("#input-opassword").removeClass("input-error");
            // } else {
            //     $("#error-opassword").show();
            //     $("#error-opassword").html("<i class='fa fa-warning'></i> This field is required.");
            //     $("#input-opassword").addClass("input-error");
            //     $("#input-opassword").focus();
            //     return false;
            // }
            if(password != ""){
                $("#error-password").html("");
                $("#error-password").hide();
                $("#input-password").removeClass("input-error");
            } else {
                $("#error-password").show();
                $("#error-password").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-password").addClass("input-error");
                $("#input-password").focus();
                return false;
            }
            if(repassword != ""){
                $("#error-repassword").html("");
                $("#error-repassword").hide();
                $("#input-repassword").removeClass("input-error");
            } else {
                $("#error-repassword").show();
                $("#error-repassword").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-repassword").addClass("input-error");
                $("#input-repassword").focus();
                return false;
            }
            if(password == repassword){
                $("#error-repassword").html("");
                $("#error-repassword").hide();
                $("#input-repassword").removeClass("input-error");
            } else {
                $("#error-repassword").show();
                $("#error-repassword").html("<i class='fa fa-warning'></i> Your re type password not match.");
                $("#input-repassword").addClass("input-error");
                $("#input-repassword").focus();
                return false;
            }
        }
        //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['admin'];?>index.php?action=delete&id="+num+"&title="+text;
                } else {
                    //nothing
                }
            });
        }
        //function confirmation deactivate
        function confirmDeactive(num, text){
            swal({
                title: "Are you sure?",
                text: "You will deactivate account "+text+" !",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Deactivate ! ",
                cancelButtonText: "Cancel !",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "<?=$path['admin'];?>edit.php?action=deactive&id="+num+"&title="+text;
                } else {
                    //nothing
                }
            });
        }
        //function confirmation activate
        function confirmActive(num, text){
            swal({
                title: "Are you sure?",
                text: "You will activate account "+text+" !",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#00a65a",
                confirmButtonText: "Activate ! ",
                cancelButtonText: "Cancel !",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    window.location.href = "<?=$path['admin'];?>edit.php?action=active&id="+num+"&title="+text;
                } else {
                    //nothing
                }
            });
        }
    </script>

</body>
</html>