<?php  require_once('../auth/auth.php');
$comid= $_GET['comid']; ?>
<?php
if(isset($_GET['getbrid'])){
$getbrid= $_GET['getbrid'];
$areaquery= mysqli_query($conn,"SELECT * FROM branch WHERE brid='$getbrid' LIMIT 1 ") or die (mysqli_error($conn));
$rowarea= mysqli_fetch_assoc($areaquery);
$com_id=$rowarea['coid'];
$bid_id=$rowarea['bid'];
$bird_id=$rowarea['brid'];
$branch_name=$rowarea['branch_name'];
$branch_number=$rowarea['branch_number'];
$branch_email=$rowarea['branch_email'];
$b_email_smtp=$rowarea['b_email_smtp'];
$branch_code=$rowarea['branch_code'];
$branch_address=$rowarea['branch_address'];
$branch_shipping_add=$rowarea['branch_shipping_add'];
$printer_set=$rowarea['printer_set'];

$branch_opining_d=$rowarea['branch_opining_d'];
}
?>
<section class="content">
   <div class="container-fluid">
      <div class="row">
         <div class="col-md-12">
            <div class="card card-secondary card-outline direct-chat direct-chat-secondary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
               <div class="card-header" style="padding: 5px 8px;">
                  <h3 class="card-title"><i class="fa fa-language"></i>   New Branch  Add  </h3>
                  <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i>  </button> </div>
               </div>
               <form id="branch_from" action="#" method="post" class="form"  autocomplete="off"   >
                  <?php if(isset($_GET['getbrid'])){  ?>
                  <input type="hidden" name="bran_submit" id="bran_submit" value="<?php echo @$getbrid ;?>"/>
                  <?php }else{?>
                  <input type="hidden" name="bran_submit" id="bran_submit" value="0"/>
                  <input type="hidden" name="com_id" value="<?php echo $comid ;?>"/>

                  <?php } ?>
                  <div class="card-body">
                     <div class="col-md-12">
                        <div class="row mt-2">
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(231);?></label>
                                 <select name="bid_id" class="form-control"  required="true">
                                    <option value="<?php echo @$bid_id ?>" selected> <?php echo  @$business_name= mysqli_fetch_array (mysqli_query($conn,"SELECT bid_name FROM business WHERE bid='$bid_id' AND coid='$comid' LIMIT 1"))['bid_name']; ?> </option>
                                    <?php
                                    $bus_qury= mysqli_query($conn,"SELECT bid,bid_name FROM business  WHERE coid='$comid' ") or die (mysqli_error($conn));
                                    while($burow= mysqli_fetch_assoc($bus_qury)){
                                    echo '<option value='.$burow["bid"].'>'.$burow["bid_name"]. ' </option>';
                                    }
                                    ?>
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-6">
                              <div class="form-group">
                                 <label><?php echo language(320);?>   <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_name" value="<?php echo  @$branch_name ?>"  class="form-control form-control-sm " required="true">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(111);?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_number" value="<?php echo  @$branch_number ?>" class="form-control form-control-sm " required="true">
                              </div>
                           </div>
                             <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(112);?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_email" value="<?php echo  @$branch_email; ?>" class="form-control form-control-sm " required="true">
                              </div>
                           </div>
                           
                            <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo 'SMTP Security ';?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="b_email_smtp" value="<?php echo  @$b_email_smtp; ?>" class="form-control form-control-sm " required="true">
                              </div>
                           </div>
                           
                           
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(396);?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_code" value="<?php echo  @$branch_code ?>" class="form-control form-control-sm "  required="true">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label> <?php echo language(248);?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_address" value="<?php echo  @$branch_address ?>" class="form-control form-control-sm "  required="true">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(116);?> <b class="text-danger"> *</b></label>
                                 <input type="text" name="branch_shipping_add" value="<?php echo  @$branch_shipping_add ?>" class="form-control form-control-sm "  required="true">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <center><label><?php echo language(131);?></label></center>
                                 <input type="text" name="branch_opining_d" class="form-control form-control-sm" onmouseover="lcdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo $date;?>">
                              </div>
                           </div>

                           <div class="col-md-3">
                              <div class="form-group">
                                 <label><?php echo language(336);?></label>
                                 <select name="printer" class="form-control select2" style="width: 100%"  required="true">
                                    <option value="<?php echo @$printer_set;?>" selected>
                                     <?php 
                                     if (@$printer_set=='R') {
                                           echo 'Regular Printer';
                                        } else if (@$printer_set=='P') {
                                           echo 'POS Printer';
                                        }?>
                                      </option>
                                      <option value="R">Regular Printer</option>
                                      <option value="P">POS Printer</option>
                                 </select>
                              </div>
                           </div>                           
                           
                        </div>
                        <center>
                        <div class="card-footer" style="padding: 8px 5px;">
                           <button type="reset" class="btn btn-secondary btn-xs">Reset</button>
                           <button type="submit" id="save" class="btn btn-success btn-xs"><?php if (@$_GET['getbrid']) {echo language(25);}else{echo language(23);}?></button>
                           
                        </div>
                        </center>
                     </div>
                  </div>
               </form>
               <div class="card-body p-1">
                  <tr>
                     <th rowspan="3"  class="text-center" >
                        <input type="text" id="search" class="form-control form-control-sm search"  placeholder="searching" required="true" style="border-radius: ;border:1px solid primary;width:100%; text-align:center; ">
                     </th>
                  </tr>
                  <table id="trbl_table" class="searchtable ttable" cellpadding="0" cellspacing="0">
                     <thead>
                        <tr class="sticky-top">
                           <th><?php echo language(15); ?></th>
                           <th title="Company"> <?php echo language(193); ?> </th>
                           <th class="text-center" title="Business Name"> <?php echo language(26); ?>   </th>
                           <th title="Branch Name"> <?php echo language(320);?>  </th>
                           <th class="text-center" title="Number">  <?php echo language(111);?></th>
                           <th class="text-center" title="Number">  <?php echo language(112);?></th>
                           <th class="text-center" title="Address"> <?php echo language(248);?> </th>
                           <th class="text-center" title="Shipping Address"> <?php echo language(116);?> </th>
                           <th class="text-center"><?php echo language(396);?> </th>
                           <th class="text-center"><?php echo language(131); ?> </th>
                           <th class="text-center"> <?php echo language(90); ?> </th>
                           <th class="text-center"> # </th>

                        </tr>
                     </thead>
                     <tbody id="colview">
                        <?php
                        $i=0;
                        $groupaa= mysqli_query($conn,"SELECT * FROM branch WHERE coid='$coid' ORDER BY brid DESC ") or die (mysql_error());
                        while ($rowbr= mysqli_fetch_array ($groupaa) ){
                        ?>
                        <tr class="tr-hover delete_op<?php echo $rowbr['brid'];?>" >
                           <td class="text-center"><?php echo ++$i;?></td>
                           <td> <?php echo $com_name= mysqli_fetch_array (mysqli_query($conn,"SELECT co_name FROM company WHERE coid ='".$rowbr['coid']."' LIMIT 1"))['co_name']; ?></td>
                           <td > <?php echo $bid_name= mysqli_fetch_array (mysqli_query($conn,"SELECT bid_name FROM business WHERE bid='".$rowbr['bid']."' LIMIT 1"))['bid_name']; ?></td>
                           <td  onclick="pagecall('../company_setup/branch_add.php?getbrid='+'<?php echo $rowbr['brid'];?>'+'&comid=<?php echo $comid ?>','#com_profile_view')">
                               <?php echo $rowbr['branch_name'];?> <i class="fa fa-edit float-right"></i></td>
                           <td><?php echo $rowbr['branch_number'];?></td>
                           <td> <?php echo $rowbr['branch_email']; ?></td>
                           <td><?php echo $rowbr['branch_address'];?></td>
                           <td><?php echo $rowbr['branch_shipping_add'];?></td>
                           <td><?php echo $rowbr['branch_code'];?></td>
                           <td><?php echo $rowbr['branch_opining_d'];?></td>
                           <td><?php  $status=$rowbr['active'];
                              if ($status==1) {
                              echo "Active";
                              }else if($status==0){
                              echo "Deactive";
                              }
                           ?></td>

                           <td onclick="pagecall('../company_setup/permissoin.php?getid='+'<?php echo $rowbr['brid']?>'+'&type=branch','#com_profile_view')"> <i class="fa fa-cog"> </i> </td>

                        </tr>
                        <?php } ?>
                     </tbody>
                  </table>
               </div>
            </div>
         </div>
      </div>
   </div>
</section>
<script>
$(document).ready(function() {
$("select").select2();

$('#branch_from').on('submit', function(e){
$('#save').prop('disabled', true);
$('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
e.preventDefault();
$.post("../company_setup/branch_db.php",$(this).serialize(),function(data){
$('#branch_from')[0].reset();
$('#save').prop('disabled', false);
$('#save').html(' Save');
var bran_submit = $("#bran_submit").val();

if (bran_submit ==0) {
$.post('../company_setup/branch_add.php?comid='+'<?php echo $comid;?>',{},function(res){
$('#com_profile_view').html(res);
});
toastr.success(Showing, '<?php echo "New Balance Add Successful" ?>');
}else if(bran_submit >0){
$.post('../company_setup/branch_add.php?comid='+'<?php echo $comid;?>',{},function(res){
$('#com_profile_view').html(res);
});
toastr.success(Showing, '<?php echo "New Update Successful" ?>');
}


});
});

$('.search').on('keyup',function(){
var searchTerm = $(this).val().toLowerCase();
$('.searchtable tr').each(function(){
var lineStr = $(this).text().toLowerCase();
if(lineStr.indexOf(searchTerm) === -1){
$(this).hide();
}else{
$(this).show();
}
});
});


});
</script>