<?php
 echo @$_GET['msg'];
  session_start();
  if(isset($_SESSION['userId'])){ 
include 'template/header.php';

?>


<div class="ch-container">
    <div class="row">
        
<!-- SIDEBAR HERE-->
<?php

include 'template/sidebar.php';

?>
       <style type="text/css">
@media only screen and (max-width: 600px) {
           .inp-box{
max-width: 100%;
           }
       }



       </style>



        <div id="content" class="col-lg-10 col-sm-10">
            <!-- content starts -->
            <div>
    <ul class="breadcrumb">
        
        <li>
            <a href="dashboard.php">Dashboard</a>
        </li>
         <li>
            <a href="accrg.php">Accounts</a>
        </li>
    </ul>
</div>

<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well">
                <h2><i class="glyphicon glyphicon-info-sign"></i> Add / Update Accounts</h2>

                <div class="box-icon">
                    <a href="#" class="btn btn-setting btn-round btn-default"><i
                            class="glyphicon glyphicon-cog"></i></a>
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i
                            class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
            <div class="box-content row">
                <div class="col-lg-12 col-md-12">
                 
                                     <p>Select Group of Account</p>
                        
                      <select id="grpid" class="form-control selectpicker"data-live-search="true"> 
                                         <?php
                    require 'connection.php'; 
         $qry = "SELECT * FROM groupreg";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
                                         <option value="<?php echo $row['groupid']; ?>"><?php echo $row['groupname']; ?></option>
                                     <?php } ?>
                                     </select>
<br>
<input class="inp-box" id="accname" autocomplete="off"  type="text" placeholder="Enter Account Title" style="padding: 2px; width: 500px; margin-bottom: 10px; margin-top: 10px;"></input>
<input class="inp-box" id="adrsbox" autocomplete="off"  type="text" placeholder="Enter Address" style="padding: 2px; width: 500px; margin-bottom: 10px; margin-top: 10px;"></input>
<input class="inp-box" id="contbox" autocomplete="off"  type="text" placeholder="Contact No." style="padding: 2px; width: 500px; margin-bottom: 10px; margin-top: 10px;"></input>
<br>
<input class="inp-box" id="opbal" autocomplete="off"  type="number" placeholder="Opening Balance" style="padding: 2px; width: 200px; margin-bottom: 10px; margin-top: 10px;"></input>
     <select  id="balopptype" class=" selectpicker"data-live-search="true"> 
                                    
                                         <option value="0">Debit</option>
                                         <option value="1">Credit</option>
                                                                      
                                     </select>
                   
                                     <br>
                                     <p>if creating shed account please select Type</p>
                                       <select  id="acctype" class=" selectpicker"data-live-search="true"> 
                                    
                                         <option value="0">Other</option>
                                         <option value="1">Chicks</option>
                                         <option value="2">Feed</option>
                                         <option value="3">Medicine</option>
                                         <option value="4">Diesel</option>
                                         <option value="5">Kitchen</option>
                                         <option value="6">Electricity</option>
                                         <option value="7">Miscellaneous</option>
                                         <option value="8">Repairing</option>
                                         <option value="9">Salary</option>
                                         <option value="10">Supervisor</option>
                                         <option value="11">Shed Rent</option>
                                         <option value="12">Brada Bardana</option>
                                         <option value="13">Broiler Sales</option>
                                         <option value="14">Flock Final Account</option>
                                   
                                     </select>
                                        <br>
                                        <br>
                                     <button id="svbtn" class="btn btn-sm btn-primary">Save Record</button>

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


<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well">
                <h2><i class="glyphicon glyphicon-info-sign"></i> List of Accounts</h2>

                <div class="box-icon">
                    <a href="#" class="btn btn-setting btn-round btn-default"><i
                            class="glyphicon glyphicon-cog"></i></a>
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i
                            class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
            <div class="box-content row">
                <div class="col-lg-12 col-md-12">
                  <table class="table table-striped table-bordered bootstrap-datatable datatable responsive">
    <thead>
    <tr>
        <th>ID</th>
        <th>Account Title</th>
        <th>Contact</th>
        <th>Group</th>
        <th>Actions</th> 
    </tr>
    </thead>
    <tbody>

                      <?php
                    require 'connection.php'; 
         $qry = "SELECT accid,accname,groupname,phone,address FROM accreg inner join groupreg on accreg.groupid=groupreg.groupid where activityacc=0 order by accid ASC";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
                                     
           <tr>
        <td class="center"><?php echo $row['accid']; ?></td>
        <td class="center"><?php echo $row['accname']; ?></td>
        <td class="center"><?php echo $row['phone'];?></td>
        <td hidden class="center"><?php echo $row['address'];?></td>
        <td class="center"><?php echo $row['groupname']; ?></td>

  
        <td class="center">
            <!-- <a class="btn btn-sm btn-info" href="#"> -->
                <button class="btn btn-sm btn-info  icon-white edtbtn"><i class="glyphicon glyphicon-edit icon-white"></i> Edit</button>
                <!-- <i class="glyphicon glyphicon-edit icon-white"></i> -->
               <!--    <i  data-toggle="modal" data-target="#myModal" class="glyphicon glyphicon-edit icon-white" style="cursor: pointer;"></i> -->
                         <!-- <i  style="cursor: pointer;"></i> -->
        </td>
    </tr>

                                     <?php } ?>


         </tbody>
    </table>
                       



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

<!-- *****************************modal start*************************** -->

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div style="background-color: darkblue" class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h5 style="color: white" class="modal-title">Account Edit / Update<i style="margin-left: 10px; font-size: 16px; font-weight: bold; color: white" id="ordrlbl" ></i></h5>
<label hidden id="accidlbl"></label>
          
        </div>
        <div class="modal-body">
            <label>Select Group</label>
            <br>
              <select id="selgrp" class="form-control selectpicker"data-live-search="true">
                 <?php
                    require 'connection.php';

              $qry = "SELECT * FROM groupreg";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option  value="<?php echo $row['groupid']; ?>"><?php echo $row['groupname']; ?></option>
        <?php } ?>
            </select>
          
            <br>
 <label>Account Title</label>
            <br>
<input class="form-control" type="text" id="edtaccname" name="">
<br>
 <label>Contact No</label>
            <br>
<input class="form-control" type="text" id="mobno" name="">
 <label>Address</label>
            <br>
<input class="form-control" type="text" id="edtaddrs" name="">

        </div>
        <div class="modal-footer">
            <i class="pull-left" style=" color: red; font-size: 26px; font-weight: bold;" id="itmnamelbl"></i>

          <button  id="btndlt"  class="btn btn-sm btn-primary pull-left" style="background-color: red">Delete account</button>

          <button id="btnupdt"  class="btn btn-sm btn-warning" style="background-color: red">Save / Update</button>

          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
</div>
<!-- **** -->

<!-- ******************************modal end********************** -->

    <!-- content ends -->
    </div><!--/#content.col-md-0-->
</div><!--/fluid-row-->


    <hr>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">

        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h3>Settings</h3>
                </div>
                <div class="modal-body">
                    <p>Here settings can be configured...</p>
                </div>
                <div class="modal-footer">
                    <a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
                    <a href="#" class="btn btn-primary" data-dismiss="modal">Save changes</a>
                </div>
            </div>
        </div>
    </div>

<?php
include 'template/footer.php';
?>

<script>
    $("#svbtn").click(function(){

      var grpid = $('#grpid').val();
      var accname = $('#accname').val();
      var adrsbox = $('#adrsbox').val();
      var contbox = $('#contbox').val();
      var acctype = $('#acctype').val();
    var opbal =   $('#opbal').val();
    var baltype =   $('#balopptype').val();
      $('#svbtn').hide();
      event.preventDefault();
 
            $.ajax({
        url: 'adaccrg.php',
        method: 'POST',
       data: {opbal: opbal, baltype: baltype, grpid: grpid, grpid: grpid, accname: accname, adrsbox: adrsbox, contbox: contbox, acctype: acctype},
        success: function(data){
            // $('#prate').val(data);
            alert(data);
      $('#svbtn').show();

        }
      })

    });

</script>

     <script type="text/javascript">
  $('.table tbody').on('click','.edtbtn',function(){
    var currow = $(this).closest('tr');
    var acid = currow.find('td:eq(0)').text();
    var acname = currow.find('td:eq(1)').text();
    var addrs = currow.find('td:eq(3)').text();
    var contct = currow.find('td:eq(2)').text();
    $("#accidlbl").html(acid);  
    $("#edtaccname").val(acname);
    $("#edtaddrs").val(addrs);
    $("#mobno").val(contct);

  $('#myModal').modal('toggle');

  });
</script>

     <script type="text/javascript">
    $("#btnupdt").click(function(){

      var accid = $('#accidlbl').html();
      var grpid = $('#selgrp').val();
      var accnam = $('#edtaccname').val();
      var adrsbox = $('#edtaddrs').val();
      var contbox = $('#mobno').val();

      event.preventDefault();
 
            $.ajax({
        url: 'adaccrg.php',
        method: 'POST',
       data: {accid: accid, grpid: grpid, grpid: grpid, accnam: accnam, adrsbox: adrsbox, contbox: contbox},
        success: function(data){
            // $('#prate').val(data);
            alert(data);
            location.reload();


        }
      })

    });

</script>

     <script type="text/javascript">
    $("#btndlt").click(function(){

      var acciddlt = $('#accidlbl').html();


      event.preventDefault();
 
            $.ajax({
        url: 'adaccrg.php',
        method: 'POST',
       data: {acciddlt: acciddlt},
        success: function(data){
            // $('#prate').val(data);
            alert(data);
            location.reload();


        }
      })

    });

</script>

<?php } ?>