<style>
a{
    color:#ed9421;
}
.bg-primary-darker{
    color:white !important;
}
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-body">
            <div class="row">
                <?php if ($permissions->hasPermission('add_archive')) { ?>
                <div class="col-md-12">
                    <a style="color:white !important;" class="btn bg-primary-darker text-white" id="add_archive_btn" data-toggle="modal" data-target="#archive_modal">
                        <?php print CommonLanguage::getMessage('add_archive'); ?>
                    </a> 
                </div>
                <?php } ?>
                <div class="col-md-12 m-t-10">
                    <table class="table table-bordered demo-table-search general-table" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center"><?php print CommonLanguage::getMessage('archive'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('archive_name'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('archive_details'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('date_created'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('date_time'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" role="dialog" id="archive_modal" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header text-left">
                <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('add_archive'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_archive'); ?></h5>
            </div>
            <div class="modal-body">
                <form method="post" action="javascript:void(0);" id="archive_form" >
                    <input type="hidden" id="archive_id" name="archive_id" />
                    <input type="hidden" id="old_archive_name" name="old_archive_name" />

                    <div class="row"><?php echo Forms::AlertDiv('archive_alert'); ?></div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="alert hidden alert-danger" id="prevent_edit_arch">
                                <?php  echo CommonLanguage::getMessage('prevent_edit_arch');?>
                            </div>
                        </div>
                    </div>
                    <div class="row form-group archive_details " >   
                        <label>
                            <?php print CommonLanguage::getMessage('archive_name'); ?>
                        </label>
                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('archive_name'); ?>" id="archive_title" name="archive_title" class="form-control " >
                    </div>
                    <div class="row form-group archive_details " >   
                        <label>
                            <?php print CommonLanguage::getMessage('archive_details'); ?>
                        </label>
                        <textarea id="archive_details" name="archive_details" class="form-control" style="resize: vertical" rows="4"></textarea>
                    </div>

                    <div class="row form-group archive_name_div archive_details" >   
                        <label>
                            <?php print CommonLanguage::getMessage('archive_name'); ?>
                        </label>
                        <a target="_blank" href="" class="current_archive_name"><i class="fa fa-file fa-2x"></i></a>
                    </div>
                    <div class="row form-group archive_details" >   
                        <label>
                            <?php print CommonLanguage::getMessage('archive'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="file" onchange="ValidateForNewAttachment(this,['.jpg','.png','.jpeg','.docx','.pdf'])" name="archive_name" class="form-control"  required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-primary-darker white"  id="submit_archive"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader("archive_loader"); ?>

        </div>
    </div>
</div>
<script>
    var prevent_delete_arch = <?php echo json_encode(CommonLanguage::getMessage('prevent_delete_arch'));?>;
    $(function () {
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], "GeneralAjax/archive_list_ajax")
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id'),
                archive_origin = $(this).attr('data-origin');
            console.log(archive_origin)
            IncludeDeleteModal(common_delete_modal, {archive_id: entry_value,
                                                     archive_name:$(this).attr('data-archive-name')}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], "GeneralAjax/archive_list_ajax")
            }, "GeneralAjax/delete_archive",archive_origin == 'gen' ? '' : prevent_delete_arch,
                               archive_origin == 'gen' ? '' : 1);
        });
        $('#tableWithSearch').on('click', '.edit', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id'),
                archive_origin = $(this).attr('data-origin'),
                archive_title = $(this).attr('data-archive-title'),
                archive_details = $(this).attr('data-archive-details');
                console.log("$(this).attr('data-archive-title')")
                console.log($(this).attr('data-archive-title'))
            if(archive_origin != 'gen'){
                $('.archive_details,#submit_archive').addClass('hidden');
                $('#prevent_edit_arch').removeClass('hidden')
            }else{
                $('.archive_details,#archive_modal .archive_name_div,#submit_archive').removeClass('hidden');
                $('#prevent_edit_arch').addClass('hidden')
            }
            $('#archive_id').val(entry_value);
            $('#archive_title').val(archive_title);
            $('#archive_details').val(archive_details);
            $('#old_archive_name').val($(this).attr('data-archive-name'));
            $('.current_archive_name').attr('href',site_link+'uploads/archive/'+$(this).attr('data-archive-name'));
            $('#archive_modal .edit-label').removeClass('hidden');
        });
        $('#add_archive_btn').on('click',function(){
            $('#archive_id').val(0);
            $('#old_archive_name').val('');
            $('.current_archive_name').attr('href','');
            $('#archive_modal .add-label,.archive_details,#submit_archive').removeClass('hidden');
            $('#archive_modal .archive_name_div,#prevent_edit_arch').addClass('hidden');
        });
        $('#archive_modal').on('hidden.bs.modal',function(){
            $("#submit_archive").prop('disabled', false);
            $("#archive_alert").addClass("hidden");
        });
        $('#submit_archive').on('click',function(){
            if (!$("#archive_form").valid())
                return;
            var Data = new FormData(document.getElementById("archive_form"));
            $("#archive_loader").removeClass('hidden');
            $.ajax({
                url: "GeneralAjax/submit_archive",
                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], "GeneralAjax/archive_list_ajax");
                            hideModalAlert('archive_modal', 'archive_alert');
                        }
                        AddAlert("#archive_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#archive_loader").addClass('hidden');
                }
            }); 
        });

    });
</script>