<?php
include 'connection_sql.php';
ini_set('session.gc_maxlifetime', 30 * 60 * 60 * 60);
session_start();
$_SESSION["brand"] = "";
if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

?>


<style>
    .talign {
        text-align: left !important;

    }

    .text {
        font-weight: 600;
        color: black;
    }

    .form-horizontal .form-group {
        margin-top: -10px !important;
    }
</style>
<script language="JavaScript" src="js/category_mas.js?v1"></script>
<section class="content">

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">Category Master</h3>

        </div>

        <form role="form" name="form1" class="form-horizontal">
            <div class="box-body">

                <div class="form-group">
                    <a onclick="new_inv();" id="btn_new" class="btn btn-default btn-sm">
                        <span class="fa fa-user-plus"></span> &nbsp; New
                    </a>


                    <?php
                    $sql_userp = "select * from view_userpermission where username='" . $_SESSION["UserName"] . "' and docname='CATEGORY MASTER' and grp='Master Files' ";
                    $result_userp = $conn->query($sql_userp);
                    if ($row_userp = $result_userp->fetch()) {

                        // ADDED ID: btn_save
                        if ($row_userp['doc_feed'] == 1) {
                            echo " <a onclick='save_inv();' id='btn_save' class='btn btn-success btn-sm'>
                            <span class='fa fa-save'></span> &nbsp; Save
                        </a> ";
                        }

                        // ADDED ID: btn_update
                        if ($row_userp['doc_mod'] == 1) {
                            echo " <a onclick='update_inv();' id='btn_update' class='btn btn-info btn-sm'>
                            <span class='fa fa-edit'></span> &nbsp; Update
                        </a> ";
                        }

                        if ($row_userp['doc_view'] == 1) {

                        }

                        if ($row_userp['doc_print'] == 1) {
                            echo " <a onclick='print_inv();' class='btn btn-default btn-sm'>
                            <span class='fa fa-print'></span> &nbsp; Print
                        </a> ";
                        }

                        if ($row_userp['doc_cancel'] == 1) {
                            echo " <a onclick='cancel_inv();' class='btn btn-danger btn-sm'>
                            <span class='fa fa-trash'></span> &nbsp; Delete
                        </a> ";
                        }
                    }
                    ?>

                </div>

                <div hidden>

                </div>

                <div id="msg_box" class="span12 text-center"></div>
                <div class="col-sm-1">
                    <input type="text" placeholder="Uniq" id="uniq" class="form-control hidden input-sm">
                </div>


                <div class="form-group"></div>


                <div class="form-group">
                    <label class="col-sm-1 control-label" for="invno">Ref No</label>
                    <div class="col-sm-1">
                        <input type="text" name="refno" id="refno" disabled class="form-control   input-sm">
                    </div>
                    <label class="col-sm-2 control-label" for="invno">Category Name</label>
                    <div class="col-sm-3">
                        <input type="text" id="category" name="category" class="form-control   input-sm"
                            placeholder='Category Name'>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-1"></div>
                </div>
                
                <div class="form-group">
                     <label class="col-sm-1 control-label" for="search">Filter</label>
                    <div class="col-sm-3">
                        <input type="text" id="myInput" onkeyup="filterTable()" class="form-control input-sm" placeholder="Search by Name or Code...">
                    </div>
                </div>

                <div id="itemdetails">
                    <div id="bank_table">
                        <table id="myTable" class="table table-bordered" style='width:75%'>
                            <tr class='info'>
                                <td width="12%">Category Code</td>
                                <td width="41%">Category Name</td>
                            </tr>
                            <?php
                            $sql = "select * from s_mas_category where CANCELL='0' order by ID DESC";
                            foreach ($conn->query($sql) as $row) {

                                echo "<tr>
                                    <td align=\"left\" onclick=\"stono('" . $row['REFNO'] . "', '" . $row['name'] . "');\">" . $row["REFNO"] . "</td>
                                    <td align=\"left\" onclick=\"stono('" . $row['REFNO'] . "', '" . $row['name'] . "');\">" . $row["name"] . "</td>
                                    ";

                            }
                            ?>
                        </table>
                    </div>
                </div>


        </form>
    </div>
    </div>
    </div>
</section>

<script>
    new_inv();
    
    // Search Function
    function filterTable() {
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      table = document.getElementById("myTable");
      tr = table.getElementsByTagName("tr");
      for (i = 0; i < tr.length; i++) {
        var tdCode = tr[i].getElementsByTagName("td")[0];
        var tdName = tr[i].getElementsByTagName("td")[1];
        
        if (tdName || tdCode) {
          var txtCode = tdCode ? (tdCode.textContent || tdCode.innerText) : "";
          var txtName = tdName ? (tdName.textContent || tdName.innerText) : "";
          
          if (txtCode.toUpperCase().indexOf(filter) > -1 || txtName.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }       
      }
    }
</script>