<style>
    .nav-tabs-fillup>li>a:after {
        background: none repeat scroll 0 0 #eb9220 !important;
        border: 1px solid #eb9220 !important;
    }
</style> 
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage($is_actual == 0 ? 'users_list' : 'act_users_list'); ?></div>
            <?php if ($permissions->hasPermission($is_actual == 0 ? 'add_user' : 'add_actual_user') && smc_session()['default_setting']['sync_type'] != 2) { ?>
                <div class="col-xs-12 p-0 m-b-10">  
                    <div class="floatingContainer ">
                        <a href="" id="new_user" data-target="#add-edit-user-modal" data-toggle="modal" >
                            <div class="actionButton bg-info-light" >
                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage($is_actual == 0 ? 'add_user' : 'add_user_act') ?></span></p>
                            </div>
                        </a>
                    </div>
                </div>
            <?php } ?>
        </div>
        <div class="panel-body">

            <div class="row">
                <div class="col-md-12">
                    <div id="user_list_alert"></div>

                    <div class="table-responsive">
                        <table class="table table-bordered setting-table" id="usersTable">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('username') ?></th>
                                    <th><?php print CommonLanguage::getMessage('role') ?></th>
                                    <th><?php print CommonLanguage::getMessage('user_full_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('is_admin') ?></th>
                                    <th><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="add-edit-user-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width:50%;">
        <div class="modal-content">
            <div class="modal-header">
                <div class="row">
                    <?php echo Forms::AlertDiv('user_alert'); ?>
                </div>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage($is_actual == 0 ? 'add_user' : 'add_user_act'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage($is_actual == 0 ? 'edit_user' : 'edit_user_act'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="user_form" method="POST" onsubmit="return false">
                    <div class="panel panel-transparent ">
                        <ul class="nav nav-tabs nav-tabs-fillup">
                            <li class="active">
                                <a data-toggle="tab" href="#slide1"><span><?php print CommonLanguage::getMessage('user_info'); ?></span></a>
                            </li>
                            <li>
                                <a data-toggle="tab" href="#slide2"><span><?php print CommonLanguage::getMessage('others'); ?></span></a>
                            </li>

                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane slide-left active" id="slide1">
                            <input type='hidden' name='user_id' id='current_user_id'/>
                            <input type='hidden' name='is_actual' id='is_actual' value="<?php echo $is_actual; ?>"/>

                                <div class="row  m-t-10">
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('Employee'); ?></label>
                                        <input class="Employee_ID full-width" id="Employee_ID" name="Employee_ID" type="hidden"  />
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('user_full_name'); ?></label>
                                        <span class="red-star">*</span>
                                        <input class="form-control" id="User_Full_Name" name="User_Full_Name" required />
                                    </div>
                                </div> 
                                <div class="row ">
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('username'); ?></label>
                                        <span class="red-star">*</span>
                                        <input type="text"  placeholder="<?php print CommonLanguage::getMessage('username'); ?>" id="current_user_name" name="User_Name" class="form-control " required>
                                    </div>
                                    <div class="col-md-6 form-group user-secretCode-password">
                                        <label><?php print CommonLanguage::getMessage('password'); ?></label>
                                        <span class="red-star">*</span>
                                        <input type="password"  placeholder="<?php print CommonLanguage::getMessage('password'); ?>" id="current_user_password" name="User_Password" class="form-control empty " required>
                                    </div>
                                </div>

                                <div class="row ">
                                    <div class="col-md-6  form-group ">
                                        <label><?php print CommonLanguage::getMessage('role'); ?></label>
                                        <span class="red-star">*</span>
                                        <input class="Role_ID full-width" id="Role_ID" name="Role_ID" type="hidden" required />
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('landing'); ?></label>
                                        <span class="red-star">*</span>
                                        <input type="hidden" id="landing_id" name="landing_id" class="full-width" required>
                                    </div>
                                </div>
                                <div class="row ">
                                    <div class="col-md-6  form-group ">
                                        <label><?php print CommonLanguage::getMessage('Email'); ?></label>
                                        <input class="form-control empty" id="user_email" name="user_email" type="email"  autocomplete="off" />
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('mobile_phone'); ?></label>
                                        <input type="text" id="user_mobile_phone" name="user_mobile_phone" class="form-control empty" autocomplete="off">
                                    </div>
                                </div>

                                <div class="row form-group">
                                    <div class="col-md-6  form-group ">
                                        <label><?php print CommonLanguage::getMessage('roles_to_control'); ?></label>
                                        <input class="control_role_id full-width" id="control_role_id" name="control_role_id" type="hidden"  />
                                    </div>
                                    <?php // if (smc_session()['default_setting']['activate_smart_diner'] == 1) { ?>
                                        <div class="col-md-6 form-group user-secretCode-password  ">
                                            <label><?php print CommonLanguage::getMessage('secret_code'); ?></label>
                                            <input type="password"  placeholder="<?php print CommonLanguage::getMessage('secret_code'); ?>" id="secret_code" pattern=".{6,}"  name="secret_code" class="form-control empty ">
                                        </div>
                                    <?php // } ?>
                                    
                                    
                                </div> 
                                <div class="row form-group">
                                    <div class="col-md-6">
                                        <label><?php echo CommonLanguage::getMessage('Type'); ?></label>
                                        <select id="is_app" name="is_app_user" class="full-width" data-init-plugin="select2">
                                            <option value="0"><?php echo CommonLanguage::getMessage('normal_user') ?></option>
                                            <option value="1"><?php echo CommonLanguage::getMessage('distribution') ?></option>
                                            <option value="2"><?php echo CommonLanguage::getMessage('attendance') ?></option>
                                            <option value="3"><?php echo CommonLanguage::getMessage('api') ?></option>
                                            <option value="4"><?php echo CommonLanguage::getMessage('sales_crm') ?></option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label><?php echo CommonLanguage::getMessage('login_device'); ?></label>
                                        <select name="login_device" id="login_device" class="full-width" data-init-plugin="select2">
                                            <option value="1"><?php echo CommonLanguage::getMessage('desktop'); ?></option>
                                            <option value="2"><?php echo CommonLanguage::getMessage('tablet'); ?></option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <div class="checkbox check-info">
                                            <input type="checkbox"  name="is_admin"  id="is_admin">
                                            <label for="is_admin"><?php print CommonLanguage::getMessage('is_admin'); ?> </label><br/>
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="checkbox check-info">
                                            <input type="checkbox"  name="limited_access"  id="limited_access">
                                            <label for="limited_access"><?php print CommonLanguage::getMessage('limited_access'); ?>&nbsp;<i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('limited_access_tooltip') ?>"></i> </label><br/>
                                        </div>
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('shift_name'); ?></label>
                                         <input type="hidden" id="user_shifts_id" name="user_shifts_id" class="full-width" >
                                    </div>
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <div class="checkbox check-info">
                                            <input type="checkbox"    id="stop_multi_login">
                                            <label for="stop_multi_login"><?php print CommonLanguage::getMessage('stop_multi_login'); ?> </label><br/>
                                        </div>
                                    </div>

                                    
                                </div>
                                <div class="row form-group">
                                    <div class="col-md-3">
                                        <div class="checkbox check-info">
                                            <input type="checkbox"    id="limit_customer_invoice">
                                            <label for="limit_customer_invoice"><?php print CommonLanguage::getMessage('limit_customer_invoice'); ?> </label><br/>
                                        </div>
                                    </div>

                                    
                                </div>
                                <div class="row form-group <?php echo Session::is_smart_admin() ? '' : 'hidden'?>">
                                    <div class="col-md-12">
                                        <label><?php echo CommonLanguage::getMessage('json_data')?></label>
                                        <textarea id="json_data" name="json_data" class="form-control" style="resize: vertical;" rows="5"></textarea>
                                    </div>

                                </div>
                            </div>
                            <div class="tab-pane slide-left " id="slide2">

                                <div class="row  ">
                                    <div class="col-md-6 form-group">
                                        <label><?php print CommonLanguage::getMessage('login_at'); ?></label><?php echo smc_session()['default_setting']['sync_type'] != 0 ? '<span class="red-star">*</span>' : '' ?>
                                        <select id="login_at" name="login_at" class="full-width <?php echo smc_session()['default_setting']['sync_type'] != 0 ? 'required' : '' ?>" data-init-plugin="select2" <?php echo smc_session()['default_setting']['sync_type'] != 0 ? 'required' : '' ?>>
                                            <option value="0" selected disabled><?php echo CommonLanguage::getMessage('select') ?></option>
                                            <option value="1"><?php echo CommonLanguage::getMessage('server_branch') ?></option>
                                            <option value="2"><?php echo CommonLanguage::getMessage('server') ?></option>
                                            <option value="3"><?php echo CommonLanguage::getMessage('branch') ?></option>
                                        </select>
                                    </div>

                                </div> 
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn bg-info-light text-white" id="submit_user"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('userLoader'); ?>
        </div>
    </div>
</div>
<div class="modal fade" id="enable-disable-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('enable_user'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('disable_user'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('enable_disable_alert'); ?></div>
                <div class="row">
                    <input  type="hidden" id="user_status_id"/>
                    <input  type="hidden" id="enabled_disabled_user_id"/>
                    <div class="col-md-12 m-t-10 form-group">
                        <p class="about_enable_user"><?php echo CommonLanguage::getMessage('about_enable_user'); ?></p>
                        <p class="about_disable_user"><?php echo CommonLanguage::getMessage('about_disable_user'); ?></p>
                        <p><?php echo CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>

                <?php echo Forms::ModalLoader('enableDisableLoader'); ?>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn bg-info-light text-white" id="submit_enable_disable"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" data-backdrop="static" id="show_user_account_modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><i class="pg-close fa-22"></i></span></button>
                <h5 class="modal-title bold"><?php print CommonLanguage::getMessage('user_accounts') ?></h5>
            </div>
            <div class="modal-body">
                <div class="row m-t-10">
                    <?php echo Forms::AlertDiv('user_acccount_alert'); ?>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo json_encode($is_actual) ?>;
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            has_permission_user_accounts = <?php echo json_encode($permissions->hasPermission('addUserAccounts')); ?>;
    $(function () {
        build_select2($("#Employee_ID"), "xhr/employeeForUserSearch?is_actual=" + is_actual);
        build_select2($("#Role_ID"), "xhr/getRelatedRoles?used_role=1", 1);
        build_select2($("#control_role_id"), "xhr/getRelatedRoles"/*"xhr/getAllRoles?without_admin=1"*/, 1);
        build_select2($("#landing_id"), "xhr/CommonSearch?col_id=landing_id&col_name=landing_name&table=tbl_landings");
        build_select2($("#user_shifts_id"), "xhr/CommonSearch?col_id=user_shifts_id&col_name=Shift_Name&table=tbl_user_shifts");

        RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3], 'SettingsAjax/getUsers', {is_actual: is_actual}, '#usersTable');
        $("#Employee_ID").on('change', function () {
            $(this).val() ? $('#User_Full_Name').val($('.Employee_ID .select2-chosen').html()) : $('#User_Full_Name').val('');
        });
        $('#usersTable').on('click', '.delete_user', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {user_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3], 'SettingsAjax/getUsers', {is_actual: is_actual}, '#usersTable');
            }, "SettingsAjax/deleteUser");
        });
        $("#usersTable").on('click', '.enable_disable_user', function () {
            $('#enable-disable-modal .about_enable_user,#enable-disable-modal .about_disable_user').addClass('hidden');
            var status_id = $(this).attr('data-id'), user_id = $(this).parents('div.Action').attr('data-id');
            $('#user_status_id').val(status_id);
            $('#enabled_disabled_user_id').val(user_id);
            status_id == 0 ? $('#enable-disable-modal .add-label,#enable-disable-modal .about_enable_user').removeClass('hidden') : $('#enable-disable-modal .edit-label,#enable-disable-modal .about_disable_user').removeClass('hidden');
        });
        $("#new_user").on("click", function () {
            $("#current_user_id").val(0);
            $('#login_device').val('1').select2();
            $("#add-edit-user-modal .add-label,#add-edit-user-modal .user-secretCode-password").removeClass('hidden');
            $("#current_user_password").attr('required', true);
        });
        $('#show_user_account_modal').on('hidden.bs.modal', function () {
            $('#user_acccount_alert').html('');
        });
        $('#add-edit-user-modal').on('hidden.bs.modal', function () {
            $('#User_Full_Name').val('');
        });
        $("#usersTable").on("click", ".empty_unique_id", function () {
            $.ajax({
                url: "SettingsAjax/empty_unique_id_ajax",
                method: "POST",
                data: {user_id: $(this).parents('div.Action').attr('data-id')},
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseText == "OK") {
                            $("#user_list_alert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        } else {
                            $("#user_list_alert").attr("class", "alert alert-danger").html(Failed_Submit)

                        }
                    }
                }
            })
        })
        $("#usersTable").on("click", ".edit_user", function () {
            var user_id = $(this).parents('div.Action').attr('data-id');
            $("#current_user_id").val(user_id);
            $("#add-edit-user-modal .edit-label").removeClass('hidden');
            $("#add-edit-user-modal .user-secretCode-password").addClass('hidden');
            $("#current_user_password").attr('required', false);
            CommonViewById('SettingsAjax/getUserData', {user_id: user_id},
                    '#userLoader', function (response) {
                        var user = response['user'][0], role = response['role'], control_role = response['control_role']
                        $('#Role_ID').select2('val', []);
                        $('#Role_ID').data().select2.updateSelection(role);
                        $('#control_role_id').select2('val', []);
                        $('#control_role_id').data().select2.updateSelection(control_role);
                        $('#landing_id').val(user['landing_id']);
                        $('#landing_id').attr('data-select-name', user['landing_name']);
                        if(user['u_user_shifts_id']!=0  ){
                        $('#user_shifts_id').val(user['u_user_shifts_id']);
                        $('#user_shifts_id').attr('data-select-name', user['Shift_Name']);
                        build_select2($("#user_shifts_id"), "xhr/CommonSearch?col_id=user_shifts_id&col_name=Shift_Name&table=tbl_user_shifts");
                        }
                        build_select2($("#landing_id"), "xhr/CommonSearch?col_id=landing_id&col_name=landing_name&table=tbl_landings");
                        $("#current_user_name").val(user['User_Name']);
                        $("#User_Full_Name").val(user['User_Full_Name']);
                        $('#user_email').val(user['user_email']);
                        $('#user_mobile_phone').val(user['user_mobile_phone']);
                        $('#login_device').val(user['login_device']).select2();
                        $("#Employee_ID").val(user['Employee_ID']);
                        $("#Employee_ID").attr('data-select-name', user['Employee_Name']);
                        $('#login_at').val(user['login_at']).select2();
                        build_select2($("#Employee_ID"), "xhr/employeeForUserSearch?is_actual=" + is_actual);
                        $("#is_app").val(user['is_app']).select2()
//                        user['is_app'] == "1" ? $('#is_app').prop('checked', true) : $('#is_app').prop('checked', false);
                        user['is_admin'] == "1" ? $('#is_admin').prop('checked', true) : $('#is_admin').prop('checked', false);
                        user['stop_multi_login'] == "1" ? $('#stop_multi_login').prop('checked', true) : $('#stop_multi_login').prop('checked', false);
                        user['limit_customer_invoice'] == "1" ? $('#limit_customer_invoice').prop('checked', true) : $('#limit_customer_invoice').prop('checked', false);
                        user['limited_access'] == "1" ? $('#limited_access').prop('checked', true) : $('#limited_access').prop('checked', false);
                        $('#json_data').val(user['json_data']);
                    });
        });
        $("#submit_user").on("click", function () {
            if (!$("#user_form").valid())
                return;
            //            if($("#login_at").hasClass("required") && $("#login_at").val()==0)
            //                return;
            var Data = new FormData(document.getElementById("user_form"));
            Data.append('is_actual', is_actual);
            Data.append('is_admin_user', $('#is_admin').is(":checked") ? 1 : 0)
            Data.append('stop_multi_login', $('#stop_multi_login').is(":checked") ? 1 : 0)
            Data.append('limit_customer_invoice', $('#limit_customer_invoice').is(":checked") ? 1 : 0)
//            Data.append('is_app_user', $('#is_app').is(":checked") ? 1 : 0)
            Data.append('limited_access', $('#limited_access').is(":checked") ? 1 : 0)
            $("#userLoader").removeClass('hidden');
            $.ajax({
                url: "SettingsAjax/submitUser",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3], 'SettingsAjax/getUsers', {is_actual: is_actual}, '#usersTable');
                            hideModalAlert('add-edit-user-modal', 'user_alert');
                            if ($('#current_user_id').val() == 0 && has_permission_user_accounts) {
                                $('#user_acccount_alert').attr('class', 'alert alert-info').html(jqXHR.responseJSON[2]);
                                $('#show_user_account_modal').modal('show');
                            }
                        }
                        AddAlert("#user_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#userLoader").addClass('hidden');
                }
            });
        });
        $("#submit_enable_disable").on("click", function () {
            $("#enableDisableLoader").removeClass('hidden');
            $.ajax({
                url: "SettingsAjax/enableDisableUser",
                method: "POST",
                data: {status_id: $('#user_status_id').val(), user_id: $('#enabled_disabled_user_id').val()},
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3], 'SettingsAjax/getUsers', {is_actual: is_actual}, '#usersTable');
                            hideModalAlert('enable-disable-modal', 'enable_disable_alert');
                        }
                        AddAlert("#enable_disable_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#enableDisableLoader").addClass('hidden');
                }
            });
        });
    });
</script>
<?php
include 'Views/Settings/change-secret-code-modal.php';
