<?php $this->load->view('site/layout'); ?>
<link href="<?php echo base_url() ?>public/plugins/custom/datatables/datatables.bundle.css" rel="stylesheet" type="text/css" />
<style>
    .btn i {
        padding-left: 0rem;
    }
</style>
<?php $this->load->view('site/navbar'); ?>

<div class="kt-content  kt-grid__item kt-grid__item--fluid kt-grid kt-grid--hor" id="kt_content">

    <!-- begin:: Content Head -->
    <div class="kt-subheader  kt-grid__item" id="kt_subheader">
        <div class="kt-container  kt-container--fluid ">
            <div class="kt-subheader__main">
                <h3 class="kt-subheader__title"><a href="<?php echo base_url() ?>Dashboard/"><?php echo $this->lang->line('dashboard'); ?></a></h3>
                <span class="kt-subheader__separator kt-subheader__separator--v"></span>
                <span class="kt-subheader__desc"><?php echo $this->lang->line('categories'); ?></span>
            </div>

        </div>
    </div>
</div>

<div class="kt-portlet kt-portlet--mobile">
    <?php if ($this->session->flashdata('success')) { ?>
        <div class="alert alert-success">
            <a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a>
            <?= $this->session->flashdata('success') ?>
        </div>
    <?php } ?>

    <?php if ($this->session->flashdata('error')) { ?>
        <div class="alert alert-danger">
            <a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a>
            <?= $this->session->flashdata('error') ?>
        </div>
    <?php } ?>

    <div class="kt-portlet__head kt-portlet__head--lg">
        <div class="kt-portlet__head-label">
            <span class="kt-portlet__head-icon">
                <i class="kt-font-brand flaticon2-line-chart"></i>
            </span>
            <h3 class="kt-portlet__head-title">
                <?php echo $this->lang->line('categories'); ?> (<?= $dalel_name ?>)
            </h3>
        </div>

        <div class="kt-portlet__head-toolbar">
            <div class="kt-portlet__head-wrapper">
                <div class="kt-portlet__head-actions">

                    &nbsp;
                    <?php if (get_ap('kw_add_category', $this->operator_id)) { ?>
                        <a href="<?= base_url() ?>knowledge_base/addcategory/<?= $dalel_id ?>" class="btn btn-brand btn-elevate btn-icon-sm">
                            <i class="la la-plus"></i>
                            <?php echo $this->lang->line('add-a-category') ?>
                        </a>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
    <div class="kt-portlet__body">

        <table id="dataTable" class="table table-striped table-bordered table-hover">

            <thead>
                <tr>
                    <td style="width: 10%">
                        <?= $this->lang->line('arrangement') ?>
                    </td>
                    <td>
                        <?= $this->lang->line('title') ?>
                    </td>
                    <td>
                        <?= $this->lang->line('guide') ?>
                    </td>
                    <td>
                        <?= $this->lang->line('action') ?>
                    </td>
                </tr>
            </thead>

            <tbody>



                <?php
                $order_arr = array();
                if (isset($categories) && $categories != null) {
                    for ($i = 0; $i < count($categories); ++$i) {
                        //  $groups = $cont->getusergroups($users[$i]->userid);
                        ?>

                        <tr>
                            <td>
                                <?php //echo form_open('dashboard/reordercategories');  ?>
                                <!--<form>-->
                                <input type="number" name="cat_order" cat_id="<?php echo $categories[$i]->id; ?>" class="form-control" value="<?php echo $categories[$i]->category_order; ?>"  />
                                <!--<input type="hidden" name="cat_id" value="<?php //echo $categories[$i]->id;            ?>" />
                                <button type="submit" id="cat_btn<?php //echo $i;            ?>" class="btn btn-info cat_btn" disabled="disabled">حفظ</button>-->
                                <!--</form>-->
                                <?php
                                $order_arr[$categories[$i]->id] = $categories[$i]->category_order;
                                ?>
                            </td>
                            <td>
                                <?php echo $categories[$i]->name; ?>
                            </td>
                            <td>
                                <?php
                                $dalel = $this->db->get_where('kw_parent_categories', array('id' => $categories[$i]->parent_id))->row();
                                if ($dalel) {
                                    echo $dalel->name;
                                } else {
                                    echo 'غير مرتبط';
                                }
                                ?>
                            </td>


                            <td>
                                <?php if (get_ap('kw_edit_category', $this->operator_id)) { ?>
                                    <a href="<?php echo base_url("knowledge_base/editcategories/") . $categories[$i]->id . '/' . $dalel_id ?>" class="btn btn-small btn-primary" title="تعديل">
                                        <i class="fa fa-edit"></i>
                                    </a>
                                <?php } ?>

                                <?php if (get_ap('kw_delete_category', $this->operator_id)) { ?>
                                    <a class="btn btn-danger btn-small" onclick="return confirm('are you sure?');" title="<?= $this->lang->line('delete') ?>" href="<?= base_url("knowledge_base/deletecategories/") . $categories[$i]->id . '/' . $dalel_id ?>">
                                        <i class="fa fa-minus-circle"></i>
                                    </a>
                                <?php } ?>

                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>

            </tbody>
        </table>

        <?php echo form_open('knowledge_base/reorderallcategories'); ?>
        <form>
            <input type="hidden" id="cat_json" name="cat_json" value='<?php echo json_encode($order_arr); ?>' />
            <input type="hidden" id="dalel_id" name="dalel_id" value='<?php echo $dalel_id; ?>' />
            <button type="submit" id="cat_btn" class="btn btn-info cat_btn mt" disabled="disabled"><?= $this->lang->line('save_order') ?></button>
        </form>
       <!--<a href="<?php //echo base_url("dashboard/reorderallcategories");            ?>" cat_order_json="<?php //echo json_encode($order_arr);            ?>" id="cat_btn" class="btn btn-small btn-success mt" title="حفظ الترتيب" disabled="disabled">
           حفظ الترتيب
       </a>-->
        <div id="cattt"></div>
    </div>



</div>







<!-- page end-->

</section>

<!--/wrapper -->

</section><!-- /MAIN CONTENT -->

<?php $this->load->view('site/footer_2'); ?>

<script src="<?php echo base_url() ?>public/js/pages/crud/datatables/basic/basic.js" type="text/javascript"></script>

<script>
                            $("#dataTable").DataTable();
                            function enable_cat_btn() {
                                $("#cat_btn").removeAttr("disabled");

                            }

//$(document).ready(function() {
                            //$("input[name='cat_order']").change(function() {
                            $(document).on('change', 'input[name="cat_order"]', function () {

                                $("#cat_btn").removeAttr("disabled");
                                cat_id = $(this).attr('cat_id');
                                cat_order = $(this).val();
                                new_json_string = '{';
                                cat_order_json = $('#cat_json').val();
                                cat_order_string = cat_order_json.replace('{', '');
                                cat_order_string = cat_order_string.replace('}', '');
                                cat_order_string = cat_order_string.replace(/\"/g, '');
                                string_arr = cat_order_string.split(',');
                                for (i = 0; i < string_arr.length; i++) {
                                    string_item = string_arr[i];
                                    arr_item = string_item.split(':');
                                    if (arr_item[0] == cat_id) {
                                        if (i == 0) {
                                            new_json_string += '\"' + arr_item[0] + '\":\"' + cat_order + '\"';
                                        } else {
                                            new_json_string += ',\"' + arr_item[0] + '\":\"' + cat_order + '\"';
                                        }
                                    } else {
                                        if (i == 0) {
                                            new_json_string += '\"' + arr_item[0] + '\":\"' + arr_item[1] + '\"';
                                        } else {
                                            new_json_string += ',\"' + arr_item[0] + '\":\"' + arr_item[1] + '\"';
                                        }
                                    }
                                }
                                new_json_string += '}'
                                //$("#cattt").append(new_json_string);
                                $("#cat_json").val(new_json_string);
                            });
//});
</script>