<?php
include("../../packages/require.php");
include("controller/controller_admin.php");
$curpage = "admin";
if($acsa_create_admin == 0){
    header("Location:".$path['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">
                        <h3 class="box-title">Add New Data</h3>
                    </div>
                    <form name="form-insert" action="<?=$path['admin'];?>insert.php?action=add" 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">
                                <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">
                                <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">
                                    <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 value="<?=$roles['role_id'];?>" style="display: none;"><?=$roles['role_title'];?></option>
                                    <?php } else { ?>
                                    <option 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 value="yes">Yes</option>
                                    <option 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">
                                    <option value="">-Choose-</option>
                                    <option value="yes">Yes</option>
                                    <option 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 value="<?=$region['region_id'];?>"><?=ucfirst($region['region_title']);?></option>
                                    <?php } } ?>
                                </select>
                                <div id="error-region" class="error-text"></div>
                            </div>
                            <div class="form-group hide" style="width: 500px;">
                                <label for="input-password">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 hide" style="width: 500px;">
                                <label for="input-repassword">Re Type 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 class="form-group hide" style="width: 500px;">
                                <label for="input-image">Upload Photo *</label>
                                <input type="file" id="input-image" name="image" class="form-control">
                                <div id="error-image" class="error-text"></div>
                                <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">
                                        <a href="<?=$path['admin'];?>" class="btn btn-default">
                                            <i class="fa fa-arrow-left"></i> Back
                                        </a>
                                    </div>
                                </div>
                                <div class="col-sm-6 col-xs-6">
                                    <div class="text-right">
                                        <div class="btn-group">
                                            <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 -->

    <?php include($global['root']."packages/footer.php");?>
    <script type="text/javascript">
        function validateForm(){
            var email = $("#input-email").val();
            var username = $("#input-username").val();
            var password = $("#input-password").val();
            var repassword = $("#input-repassword").val();
            var role = $("#input-role").val();
            var pic = $("#input-pic").val();
            var adminc = $("#input-adminc").val();
            var region = $("#input-region").val();
            var photo = $("#input-image").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;
            }
            if(role != ""){
                $("#error-role").html("");
                $("#error-role").hide();
                $("#input-role").removeClass("input-error");
            } else {
                $("#error-role").show();
                $("#error-role").html("<i class='fa fa-warning'></i> This field is required.");
                $("#input-role").addClass("input-error");
                $("#input-role").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;
            }
            // 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;
            // }
            // if(photo != ""){
            //     $("#error-image").html("");
            //     $("#error-image").hide();
            //     $("#input-image").removeClass("input-error");
            // } else {
            //     $("#error-image").show();
            //     $("#error-image").html("<i class='fa fa-warning'></i> Insert primary photo.");
            //     $("#input-image").addClass("input-error");
            //     $("#input-image").focus();
            //     return false;
            // }
        }
    </script>
</body>
</html>