<?php //$wo['genders'] = Dataset::gender();

$lang = GetActiveLang();
$genders = $db->where('ref','gender')->get('langs',null,array('lang_key',$lang,'id'));
$wo['genders'] = array();
foreach ($genders as $key => $value) {
    if(isset($value[$lang])) {
        $wo['genders'][$value['id']] = $value[$lang];
    }
}

 ?>
<div class="container-fluid">
    <div>
        <h3>Manage Genders</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Users</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Manage Genders</li>
            </ol>
        </nav>
    </div>
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="card-body">
					<div class="pull-right">
                                  <button class="btn btn-primary" id="add_new_gender">Add new gender</button>
                              </div>
                    <h6 class="card-title">Manage & Edit Genders</h6>
                    <div class="gender-settings-alert"></div>
                    <div class="row">
                       <div class="col-md-12" style="margin-bottom:0;">
                        <div class=" add_category_form_alert"></div>
                              <div class="clearfix"></div>
                       </div>
                   </div>
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                    <th><input type="checkbox" id="check-all" class="filled-in check-all" ><label for="check-all"></label></th>
                                    <th>ID</th>
                                    <th>Key Name</th>
                                    <th>Value</th>
                                    <th>Free
                                        <span class="black" data-toggle="popover" data-trigger="hover" data-content="If you click enable, all website service will be free to specific gender" data-original-title="" title=""><i class="fa fa-question-circle fa-fw"></i></span>
                                    </th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                $wo['gender_id'] = 1;
                                foreach ($wo['genders'] as $wo['gender_key'] => $wo['gender']) {
                                    echo Wo_LoadAdminPage('manage-genders/list');
                                    $wo['gender_id'] = $wo['gender_id'] + 1;
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="clearfix"></div>
                      <div class="row">
                          <div class="col-lg-2 col-md-2">
                              <span>Action</span>
                              <select class="form-control show-tick" id="action_type">
                                  <option value="enable">Enable</option>
                                  <option value="disable">Disable</option>
                                  <option value="delete">Delete</option>
                              </select>
                          </div>
                          <div class="col-lg-3 col-md-3">
                              <span>&nbsp;</span>
                              <button type="button" class="btn btn-primary waves-effect delete-selected d-block" disabled>Submit<span></span></button>
                          </div>
                      </div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    </div>
    <!-- #END# Vertical Layout -->
<div class="modal fade" id="DeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Gender?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete this Gender?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">Delete</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="SelectedDeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Gender?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure that you want to remove the selected Gender(s)?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="DeleteSelected()" data-dismiss="modal">Submit</button>
            </div>
        </div>
    </div>
</div>
<script>
$('.check-all').on('click', function(event) {
  $('input:checkbox').not(this).prop('checked', this.checked);
});
$('.delete-checkbox, .check-all').change(function(event) {
    $('.delete-selected').attr('disabled', false);
    $('.delete-selected').find('span').text(' (' + $('.delete-checkbox:checked').length + ')');
});
$('.delete-selected').on('click', function(event) {
    event.preventDefault();
    action_type = $('#action_type').val();
    $('#SelectedDeleteModal').find('.modal-body').html('Are you sure that you want to '+action_type+' the selected gender(s)?');
    $('#SelectedDeleteModal').find('#exampleModal1Label').html(action_type+' gender(s)');
    $('#SelectedDeleteModal').modal('show');
});
function DeleteSelected() {
    action_type = $('#action_type').val();
    data = new Array();
    $('td .delete-checkbox:checked').parents('tr').each(function () {
        data.push($(this).attr('data_selected'));
    });
    $('.delete-selected').attr('disabled', true);
    $('.delete-selected').text('Please wait..');
    $.post(Wo_Ajax_Requests_File()+'?f=admin_setting&s=delete_multi_gender', {ids: data,type: action_type}, function () {
        if (action_type == 'delete') {
            $.each( data, function( index, value ){
                $('#' + value).remove();
            });
        }
        else{
            location.reload();
        }
        $('.delete-selected').text('Submit');
    });
}

$('.btn-lang').on('click', function(event) {
    $('#defaultModal .modal-body form .data').html('<div class="preloader pl-size-xl "><div class="spinner-layer pl-teal"><div class="circle-clipper left"><div class="circle"></div></div><div class="circle-clipper right"><div class="circle"></div></div></div></div>');
    var lang_id = $(this).attr('data-id');
    $.get(Wo_Ajax_Requests_File() + '?f=get_lang_key&p=manage-genders', {id: lang_id, lang_name: lang_id}, function(data, textStatus, xhr) {
        $('#defaultModal .modal-title').html('Edit key: ' + lang_id);
        $('#id_of_key').val(lang_id);
        $('#defaultModal .modal-body form .data').html(data.html);
    });
});
function Wo_SubmitLangForm() {
    $('.edit-key-settings').submit();
}
function Wo_SubmitAddGenderForm() {
    $('.langsModalForm').submit();
}
$(function () {
  var form_lang_settings = $('form.edit-key-settings');
    form_lang_settings.ajaxForm({
        url: Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_lang_key&p=manage-genders&hash=' + $('.main_session').val(),
        beforeSend: function() {
            $('.btn-save').text('Please wait..');
        },
        success: function(data) {
            if (data.status == 200) {
                $('.btn-save').text('SAVE CHANGES');
                var value_to_use = $('[data-editable=1]').val();
                var id_of_key = $('#id_of_key').val();
                $('#edit_' + id_of_key).text(value_to_use);
                $('#defaultModal').modal('hide');
                location.reload();
            }
        }
    });

    var form_gender_settings = $('form.langsModalForm');
    form_gender_settings.ajaxForm({
        url: Wo_Ajax_Requests_File() + '?f=admin_setting&s=add_new_gender&p=manage-genders&hash=' + $('.main_session').val(),
        beforeSend: function() {
            $('.btn-save').text('Please wait..');
        },
        success: function(data) {
            
            if (data.status == 200) {
                $('.btn-save').text('SAVE CHANGES');
                $('#langsModal').modal('hide');
                location.reload();
            }
            else{
                $('.btn-save').text('SAVE CHANGES');
                $('.langsModalAlert').html(data.message);
            }
            $("#langsModal").animate({ scrollTop: 0 }, 100);

        }
    });
});

$(document).on('click', '#add_new_gender', function(event) {
    $('#langsModal').modal('show');
});
function Wo_DeleteGender(key,type = 'show') {
  if (key == '') {
    return false;
  }
  if (type == 'hide') {
    $('#DeleteModal').find('.btn-primary').attr('onclick', "Wo_DeleteGender('"+key+"')");
    $('#DeleteModal').modal('show');
    return false;
  }

  $.get(Wo_Ajax_Requests_File(), {f: 'admin_setting', s:'delete_gender', p: 'manage-genders', key:key}, function (data) {
      if(data.status == 300){
        $('.gender-settings-alert').html('<div class="alert alert-danger">You can not delete this gender.</div>');
        setTimeout(function () {
            $('.gender-settings-alert').empty();
        }, 2000);
      }else{
          $('#' + key).fadeOut(300, function () {
              $(this).remove();
          });
      }
  });
}


function Wo_FreeGenderEnable(key) {
    if (key == '') {
        return false;
    }
    $.get(Wo_Ajax_Requests_File(), {f: 'admin_setting', s:'free_gender_enable', p: 'manage-genders', key:key});
}
function Wo_FreeGenderDisable(key) {
    if (key == '') {
        return false;
    }
    $.get(Wo_Ajax_Requests_File(), {f: 'admin_setting', s:'free_gender_disable', p: 'manage-genders', key:key});
}

</script>
<div class="modal fade" id="defaultModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content ">
            <div class="modal-header">
                <h4 class="modal-title" id="defaultModalLabel"></h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="alert alert-success edit-lang-settings-alert"></div>
                <form class="edit-key-settings" method="POST">
                    <div class="data"></div>
                    <input type="hidden" name="hash_id" value="<?php echo Wo_CreateSession();?>">
                    <input type="hidden" name="id_of_key" id="id_of_key" value="">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">CLOSE</button>
                <button type="button" onclick="Wo_SubmitLangForm();" class="btn-save btn btn-primary waves-effect">SAVE CHANGES</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="langsModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content ">
            <div class="modal-header">
                <h4 class="modal-title" id="langsModalLabel">Add New Gender</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="alert alert-danger langsModalAlert"></div>
                <form class="langsModalForm" method="POST">
                    <?php foreach (Wo_LangsNamesFromDB() as $wo['key_']) { ?>
                        <div class="form-group">
                            <div class="form-lins">
                                <label class="form-lasbel"><?php echo ucfirst($wo['key_']); ?></label>
                                <textarea style="resize: none;" name="<?php echo $wo['key_']; ?>" id="<?php echo $wo['key_']; ?>" class="form-control" cols="20" rows="2"></textarea>
                            </div>
                        </div>
                    <?php } ?>
                    <div class="data"></div>
                    <input type="hidden" name="hash_id" value="<?php echo Wo_CreateSession();?>">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">CLOSE</button>
                <button type="button" onclick="Wo_SubmitAddGenderForm();" class="btn-save btn btn-primary waves-effect">SAVE CHANGES</button>
            </div>
        </div>
    </div>
</div>