<?php

session_start();

$coid = $_SESSION['co_id'];
$uid  =$_SESSION['user_id'];

// ---------------Min Max Date range-------------------//
$stmt = $pdo->prepare("SELECT min_date FROM trans_period WHERE co_id = :coid
");

$stmt->bindValue(':coid', $coid, PDO::PARAM_INT);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);

$min_date = $row['min_date'];     // from database
$max_date = date("Y-m-d");        // today
  
// -------------------Fetch Products----------------------------------------//

$stmt = $pdo->prepare("SELECT id, p_name FROM products WHERE co_id = :coid ORDER BY p_name");
$stmt->execute(['coid'=>$coid]);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

// -------------------Fetch CR----------------------------------------//
$stmt = $pdo->prepare("SELECT id, acc_name FROM accounts WHERE co_id = :coid ORDER BY acc_name");
$stmt->execute(['coid'=>$coid]);
$crs = $stmt->fetchAll(PDO::FETCH_ASSOC);

// -------------------Fetch Store id----------------------------------------//
$stmt = $pdo->prepare("SELECT a.id, a.acc_name FROM accounts a JOIN permissions p ON a.id=p.pm_acc_id JOIN users u ON u.id=p.user_id WHERE a.acc_type='STOCK' AND a.acc_categ='warehouse' AND a.acc_source='internal' AND a.status='active' AND p.co_id= :coid AND p.user_id= :uid");
$stmt->execute(['coid'=>$coid, 'uid'=>$uid]);
$warehs = $stmt->fetch(PDO::FETCH_ASSOC);
$wh_id = $warehs['id'] ?? '';
?>
           <!-- Panel Right -->
        <div class="modal fade panelbox panelbox-right" id="PanelRight" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Action</h5>
                        <a href="#" class="btn btn-link btn-icon sidebar-close" data-dismiss="modal">
                            <ion-icon name="close-outline"></ion-icon>
                        </a>
                    </div>
                    <div class="modal-body">
                        <!-- menu -->
                    <div class="listview-title mt-0 bg-primary">Entry Forms</div>
                    <ul class="listview flush transparent no-line image-listview">
                        <li>
                            <a href="#" class="item" id="deliveryLink">
                                <div class="in">
                                    Delivery Note
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="grLink">
                                <div class="in">
                                    Goods Returned Note
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="prchLink">
                                <div class="in">
                                    Purchases
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="retprchLink">
                                <div class="in">
                                    Purchase Return
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="stkinvretLink">
                                <div class="in">
                                    Invoice Return
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="stfrLink">
                                <div class="in">
                                    Transfer Stock
                                </div>
                            </a>
                        </li>
                     </ul>
                    <!-- * menu -->
                    
                    <!-- Reports -->
                    <div class="listview-title bg-warning">Reports</div>
                    <ul class="listview flush transparent no-line image-listview">
                        <li>
                            <a href="#" class="item" id="salesLinkrep" data-toggle="modal" data-target="#Modalrep" >
                                        Sales Reports
                                
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item"  id="prchLinkrep" data-toggle="modal" data-target="#Modalrep">
                                        Purchase Reports
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item"  id="stktfrLinkrep" data-toggle="modal" data-target="#Modalrep">
                                        Stock Transfer Reports
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item"  id="docnoLink" data-toggle="modal" data-target="#Modalrep">
                                        Document No.
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item" id="deliveryLinkrep" data-toggle="modal" data-target="#Modalrep">
                                        Transaction Report
                            </a>
                        </li>
                        
                    </ul>
                    <!-- * Reports -->
                     
                    </div>
                </div>
            </div>
        </div>
        <!-- * Panel Right -->
        
     <!-- Deposit Action Sheet -->
        <div class="modal fade action-sheet" id="inventoryActionSheet" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Select Action</h5>
                    </div>
                    <div class="modal-body">
                        <div class="action-sheet-content">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- * Deposit Action Sheet -->

 <!-- Modal Basic -->
        <div class="modal fade modalbox" id="Modaldelivery" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header form-group basic balance">
                        <h5 class="modal-title"><input type="text" id="form_name" name="form_name" class="form-control title text-dark"  readonly></h5>
                        <a href="javascript:;" data-dismiss="modal">Close</a>
                    </div>
                    <div class="modal-body">
                     <form  method="POST" id="add_delivery" >
                          <div class="col-6">
                      <div class="form-group basic">
                                        <div class="input-wrapper">
                          <label for="field1" class="label">Date</label>
                          <input required type="date" id="field1" name="field1" class="form-control" min="<?php echo $min_date?>" max="<?php echo $max_date?>"  placeholder="DD/MM/YYY" required>
                             </div>
                         </div>
                    </div>
                     <div class="row">     
                     <div class="col-4">
                          <div class="form-group basic">
                            <div class="input-wrapper">
                        
                              <label for="field2" class="label">Doc No.</label>
                        
                              <input type="number" pattern="[0-9]*" id="field2" name="field2" class="form-control" placeholder="0001" required>
                        
                              <small id="doc_msg"></small>
                        
                              <div id="doc_spinner" style="display:none; margin-top:5px;">
                                <div class="spinner-border spinner-border-sm text-primary"></div>
                                <small>Checking...</small>
                              </div>
                        
                            </div>
                          </div>
                        </div>
                        
                    <div class="col-8" id="Customer_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="customerx" class="label">Customer</label>
                        <select id="customerx" name="customerx" class="form-control custom-select" required>
                            <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div>
                    
                    <div class="col-8" id="field3_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="field3" class="label">Sales person</label>
                        <select id="field3" name="field3" class="form-control custom-select" required>
                            <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div> 
            
           <div class="col-8" id="supplier_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="supp" class="label">Supplier</label>
                        <select id="supplier" name="supplier" class="form-control custom-select" required>
                            <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div> 
            
            <div class="col-8" id="storex_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="storex" class="label">Transfer To</label>
                        <select id="storex" name="storex" class="form-control custom-select" required>
                            <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="col-8" id="cash_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="supp" class="label">From</label>
                        <select id="cash_acc" name="cash_acc" class="form-control custom-select" required>
                           <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div> 
            
                        
           <div class="col-5" id="driver_group">
                <div class="form-group basic">
                    <div class="input-wrapper">
                        <label for="driver" class="label">Driver</label>
                        <select id="driver" name="driver" class="form-control custom-select" required>
                            <option value=""  >Loading</option>
                           
                        </select>
                    </div>
                </div>
            </div> 
            
   <input type="hidden" id="field4" name="field4" class="hidden hidden-fields"  required>
    <input type="hidden" id="field5" name="field5" value="<?php echo $_SESSION['user_id']; ?>" required>
    <input type="hidden" id="comp_id" name="comp_id"  required>
    <input type="hidden" id="whm" name="whm"  required>
            </div>            
            
     <!---------------End of Inventory  table -------------------------> 
     
      <table class="table" id="my-table">
        <thead>
          <tr>
            <th>Item</th>
            <th>Quantity</th>
           
          </tr>
        </thead>
         <tr>
            <td>
         <div class="col-6">
                <div class="form-group basic">
                  <div class="input-wrapper">
                 <select name="column1[]" class="form-control" onchange="populateColumns(this)"  style="width:170px;">
                        <option value="">Loading</option>
                   
        
                 </select> 
               </div>
          </div>
        </div>

            </td>
            <td>
              <div class="col-6">
                <div class="form-group basic">
                  <div class="input-wrapper">
                <input type="number" name="column2[]" placeholder="0"  class="form-control" min="1"  style="width: 110px;">
                <td class="hidden hidden-fields"></td>
                    <input type="hidden" class="form-control"  name="column3[]"  ></td>
                 <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control row-total"  name="column33[]"  ></td>
                 <td class="hidden hidden-fields"><input type="hidden" name="column4[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control row-purch-total"  name="column44[]"  ></td>
                
                 <td class="hidden hidden-fields"><input type="hidden" name="column5[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column55[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column6[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column66[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column7[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column77[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column8[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column88[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column9[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column99[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column10[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column100[]"  ></td>
                 
                 <td class="hidden hidden-fields"><input type="hidden" name="column11[]" class="form-control form-field hidden"></td>
                  <td class="hidden hidden-fields">
                    <input type="hidden" class="form-control"  name="column1110[]"  ></td>
                 
                
                </div>
               
          </div>
        </div>
          
           
          </tr>
        </table>
        
   <!---------------End of Inventory  table -------------------------> 
   
   <!---------------Accounts  table -------------------------> 
        
       <table class="table" id="accounts-table">
    <thead>
        <tr>
            <th>To Account</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
        
            <div class="form-group basic">
                <select name="accs[]" class="form-control" style="width:170px;">
                    <option value="">Loading</option>
                </select>
            </div>
       
            </td>
            <td>
       
            <div class="form-group basic">
                <input type="number" name="amt[]" class="form-control row-rec-total" >
            </div>
       
            </td>
        </tr>
    </tbody>
</table>
 <!---------------End of Accounts  table -------------------------> 
 
    <!--------------- Expense table -------------------------> 
        
       <table class="table" id="expense-table">
    <thead>
        <tr>
            <th style="width: 30%;">Expense Item</th>
            <th style="width: 40%;">Description</th>      
            <th style="width: 20%;">Amount</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
        
            <div class="form-group basic">
                <select name="exp[]" class="form-control" required>
                    <option value="">Loading</option>
                </select>
            </div>
            </td>
            
            <td>
            <div class="form-group basic">
               <input type="text" name="desc[]" class="form-control" >
            </div>
            
            </td>
            <td>
       
            <div class="form-group basic">
                <input type="number" name="amt2[]" class="form-control row-exp-total" required min="1">
            </div>
       
            </td>
        </tr>
    </tbody>
</table>
 <!---------------End of Expense  table -------------------------> 
 
    <!---------------Customer  table -------------------------> 
        
       <table class="table" id="customer-table">
    <thead>
        <tr>
            <th>Customer</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
        
            <div class="form-group basic">
                <select name="cust[]" class="form-control" style="width:170px;">
                    <option value="">Loading</option>
                </select>
            </div>
       
            </td>
            <td>
       
            <div class="form-group basic">
                <input type="number" name="amt3[]" class="form-control row-cust-total" >
            </div>
       
            </td>
        </tr>
    </tbody>
</table>
 <!---------------End of Accounts  table -------------------------> 
 
          <!-- Add this code after the table -->
     <div id="total-container" class="col-6">
                <div class="form-group basic">
                  <div class="input-wrapper">

  <label for="total-field">Total Amount:</label>
  <input type="text" id="total-field" class="form-control" readonly required style="width: 210px;" placeholder="0">
  
   <input type="text" id="total-purch-field" class="form-control" readonly style="width: 210px;" placeholder="0">

 </div>
               
          </div>
        </div>
<br></br>
      
      <button id="remove-inv-button" type="button" class="btn btn-danger" onclick="removeRow()">Remove Row</button>
      <button id="remove-acc-button" type="button" class="btn btn-danger" onclick="removeRECRow()">Remove Row</button>
      <button id="remove-exp-button" type="button" class="btn btn-danger" onclick="removeEXPRow()">Remove Row</button>
      <button id="remove-cust-button" type="button" class="btn btn-danger" onclick="removeCUSTRow()">Remove Row</button>
      
      <button id="add-inv-button" type="button" class="btn btn-primary" onclick="addRow()">Add Row</button>
      <button id="add-acc-button" type="button" class="btn btn-primary" onclick="addRECRow()">Add Row</button>
      <button id="add-exp-button" type="button" class="btn btn-primary" onclick="addEXPRow()">Add Row</button>
      <button id="add-cust-button" type="button" class="btn btn-primary" onclick="addCUSTRow()">Add Row</button>
      <br></br>
      <input type="submit" id="submitBtn" class="btn btn-success square me-1 btn-lg btn-block" value="Submit">
      <br></br>
         
                     </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- * Modal Basic -->
        
         <!-- Reports Modal Basic -->
<div class="modal fade modalbox" id="Modalrep" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <div class="col-9">
                         <div class="form-group basic balance">
                      <div class="input-wrapper">
                        <h5 class="modal-title">
                        <input type="text" id="form_name1" name="form_name1" class="form-control title text-dark" value="<?php echo $form_name1 ?>" readonly></h5>
                        </div>
                    </div>
                </div>
                <a href="javascript:;" data-dismiss="modal">Close</a>
            </div>
            <div class="modal-body mt-1">
                <input style="width: 30%;" type="text" id="trans_type" name="trans_type" placeholder="Del,GR,PRCH, Ret Prch" >
              <input  style="width: 40%;" type="text"  id="search-input" name="search-input" placeholder="Search by CR" > 
              <input  style="width: 40%;" type="text"  id="docnox" name="docnox" placeholder="Search by Doc No." > 
              <div class="mt-1"></div>
              <div>
                   <input style="width: 40%;" type="date" id="date" name="date3"  >
                <input style="width: 40%;" type="date" id="date2" name="date4"  >
                 <input style="width: 40%;" type="date" id="date3" name="date3"  >
                <input style="width: 40%;" type="date" id="date4" name="date4"  >
                
                 <input style="width: 40%;" type="date" id="date5" name="date5"  >
                <input style="width: 40%;" type="date" id="date6" name="date6"  >
                
                 <input style="width: 40%;" type="date" id="date7" name="date7"  >
                <input style="width: 40%;" type="date" id="date8" name="date8"  >
                <input style="width: 40%;" type="date" id="dater1" name="dater1"  >
                <input style="width: 40%;" type="date" id="dater2" name="dater2"  >
                <input style="width: 40%;" type="date" id="dater3" name="dater3"  >
                <input style="width: 40%;" type="date" id="dater4" name="dater4"  >
                 <input style="width: 40%;" type="date" id="dateb" name="dateb"  >
                <input style="width: 40%;" type="date" id="dateb2" name="dateb2"  >
               </div> 
                
                <input style="width: 40%;" type="hidden" id="user3_id" name="user3_id" value="<?php echo $user_id ?>" >
        
                  <div class="card mt-1">

                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                            
                            </tr>
                        </thead>
                       <tbody id="currentsales">
                          
                        </tbody>
                    </table>
                </div>

            </div>
                    
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- * Reports Modal Basic -->