<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php 
 if (isset($_GET['pro_id'])) {
     $item_id = $_GET['pro_id'];
     $iteqe= mysqli_query($conn,"SELECT item.*,item_group.pgroup_name,item_brand.pbrand_name,item_catagory.pcat_name FROM item 
      left JOIN item_group ON (item_group.pgid=item.pgid) 
      left JOIN item_catagory ON (item_catagory.pcid=item.pcid) 
      left JOIN item_brand ON (item_brand.pbid=item.pbid) 

      WHERE pro_type='P' AND item.coid='$coid' AND item.bid='$bid'  AND item.pro_id='$item_id'") or die (mysqli_error($conn));
     $iter = mysqli_fetch_assoc($iteqe);

     $item_gid=$iter['pgid'];
     $item_cid=$iter['pcid'];
     $item_brnd=$iter['pbid'];



 }
      $custom_field= mysqli_query($conn,"SELECT item FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['item'];
      function input_check($ap)
      {
       if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
      }

        $grp_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pgroup_name FROM item_group  WHERE pgid='$item_gid' LIMIT 1"))['pgroup_name'];
        $cat_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pcat_name FROM item_catagory  WHERE pcid='$item_cid' LIMIT 1"))['pcat_name'];
        $brnd_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pbrand_name FROM item_brand  WHERE pbid='$item_brnd' LIMIT 1"))['pbrand_name'];

?>
<div class="row">
   <div class="col-md-12">
      <div class="card card-danger card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
         <div class="card-header" style="padding: 3px 8px;">
        <h3 class="card-title"  >   <?php echo  language(25);?></h3>
            <div class="card-tools m-0">
              <?php if (!isset($_GET['pro_id'])) { ?>
               <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = isset($_GET['pro_id'])?'':invoice("SOP");?></span>
              <?php } ?>
               <button type="button" class="btn btn-tool" data-card-widget="remove">
               <i class="fas fa-times"></i>
               </button>
            </div>
         </div>
         <form class="mt-2" id="its_form" action="#" method="post" autocomplete="off">
        <?php if (isset($_GET['pro_id'])) { ?>
          <input type="hidden" name="item_id" value="<?php echo $iter['pro_id'];?>">
        <?php } ?>


            <div class="card-body">
               <div class="col-md-12">
                  <div class="row">
          <div class="col-md-4">
             <div class="form-group">
              <label><?php echo language(69)?>  </label>
              <select name="item_gid" class="form-control"  required="true">

              <option value="<?php echo $item_gid ;?>"> <?php echo $pgroup_name=$iter['pgroup_name']; ?> </option>
               <?php 
               $pgroup_query= mysqli_query($conn,"SELECT * FROM item_group  WHERE coid='$coid' AND bid='$bid' AND active='1' ") or die (mysqli_error($conn));
               while($rowgroup= mysqli_fetch_assoc($pgroup_query)){
                 echo '<option value='.$rowgroup["pgid"].'>'.$rowgroup["pgroup_name"].' </option>';
               }?> 
             </select>
           </div>
         </div>
         <div class="col-md-4">
           <div class="form-group">
            <label><?php echo language(70)?></label>
            <select name="item_cid" class="form-control" required="true">
                  <option value="<?php echo $item_cid ;?>"> <?php echo $pcat_name=$iter['pcat_name'];?> </option>
              <?php 
                 $pcatt_query= mysqli_query($conn,"SELECT * FROM item_catagory  WHERE coid='$coid' AND bid='$bid' AND active='1' ") or die (mysqli_error($conn));
                 while($rowcat= mysqli_fetch_assoc($pcatt_query)){
                   echo '<option value='.$rowcat["pcid"].'>'.$rowcat["pcat_name"].' </option>';
                 }
              ?> 
           </select>
         </div>
       </div>

       <div class="col-md-4">
         <div class="form-group">
          <label><?php echo language(71)?>  </label>
          <select name="item_brnd" class="form-control" required="true">
            <option value="<?php echo $item_brnd ;?>"> <?php echo $pbrand_name=$iter['pbrand_name'];?> </option>

           <?php 
           $pbrand_query= mysqli_query($conn,"SELECT * FROM item_brand  WHERE coid='$coid' AND bid='$bid'  AND active='1' ") or die (mysqli_error($conn));
           while($rowbrand= mysqli_fetch_assoc($pbrand_query)){
             echo '<option value='.$rowbrand["pbid"].'>'.$rowbrand["pbrand_name"].' </option>';
           }?> 
         </select>
       </div>
     </div>








                    <div class="col-md-3">
                      <div class="form-group">
                        <label><?php echo language(97)?> <b class="text-danger">*</b></label>
                        <input type="text" name="item_name" class="form-control form-control-sm inputselect" required="true" value="<?php echo @$iter['item_name'];?>">
                      </div>
                    </div>
                   <div class="col-md-3">
                    <div class="form-group">
                      <label><?php echo language(78)?> <b class="text-danger">*</b></label><sss id="item_code_status"></sss><i class="fa fa-refresh float-right" role="button" onclick="$('[name=item_code]').val(Math.floor(Math.random()*1006660045));"></i>
                      <input type="text" name="item_code" class="form-control form-control-sm inputselect text-center" value="<?php echo @$iter['item_code'];?>" required>
                    </div>
                  </div>
                 <div class="col-md-2">
                  <div class="form-group">
                    <label><?php echo language(81)?> <b class="text-danger">*</b></label>
                    <input type="number" step="0.001" name="p_price" class="form-control form-control-sm inputselect text-center" required="true" value="<?php echo @$iter['mrp_purchase'];?>">
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label><?php echo language(83)?> <b class="text-danger">*</b></label>
                    <input type="number" step="0.001" name="min_sale" class="form-control form-control-sm inputselect text-center" oninput="$('[name=max_sale]').val(this.value);" style="text-align:center;" required="true" value="<?php echo @$iter['min_sales'];?>">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <label><?php echo language(84)?> <b class="text-danger"> *</b></label>
                    <input type="number" step="0.001" name="max_sale" class="form-control form-control-sm inputselect text-center" required="true" value="<?php echo @$iter['mrp_sales'];?>">
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group">
                    <label><?php echo language(77)?></label>
                    <input type="text" name="item_details" class="form-control form-control-sm inputselect" value="<?php echo @$iter['item_details'];?>">
                  </div>
                </div>
                <div class="col-md-2 <?php if(input_check('sku') !==1){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(79)?></label>       
                    <input type="text" name="item_sku" class="form-control form-control-sm" value="<?php echo @$iter['item_sku'];?>">
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label><?php echo language(87)?></label>       
                    <input type="text" name="location" class="form-control form-control-sm" value="<?php echo @$iter['location'];?>">
                  </div>
                </div>
                
                
                
                  <?php if(input_check('emei_tracking') !==1 ){ ?>

                <div class="col-md-2 <?php if(input_check('u_type') !==1 ){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(98)?> <b class="text-danger">*</b></label>
                    <select name="u_type" class="form-control" required="true" onchange="op_cal();">
                   <?php 
                     if (@$iter['pro_unit']) {
                       $puid = $iter['pro_unit'];
                       $eut = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pro_unit WHERE coid='$coid' AND puid='$puid'"));
                       echo '<option selected value='.$eut["puid"].','.$eut["unit_convart"].'>'.$eut["unit_name"].'('.$eut["unit_symble"].') ('.$eut["unit_convart"].')</option>';
                     }
                      $punit_query= mysqli_query($conn,"SELECT * FROM pro_unit WHERE coid='$coid'") or die (mysqli_error($conn));
                      while($rotunit= mysqli_fetch_assoc($punit_query)){
                        echo '<option value='.$rotunit["puid"].','.$rotunit["unit_convart"].'>'.$rotunit["unit_name"].'('.$rotunit["unit_symble"].') ('.$rotunit["unit_convart"].')</option>'; 
                      }?> 
                    </select>
                  </div>
                </div>
                
               <?php }else{ ?>
              
                <div class="col-md-2 d-none" >
                  <div class="form-group">
                    <label><?php echo language(98)?> <b class="text-danger">*</b></label>
                    <select name="u_type" class="form-control" required="true" onchange="op_cal();">
                   <?php 
                     if (@$iter['pro_unit']) {
                       $puid = $iter['pro_unit'];
                       $eut = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pro_unit WHERE coid='$coid' AND puid='$puid'"));
                       echo '<option selected value='.$eut["puid"].','.$eut["unit_convart"].'>'.$eut["unit_name"].'('.$eut["unit_symble"].') ('.$eut["unit_convart"].')</option>';
                     }
                      $punit_query= mysqli_query($conn,"SELECT * FROM pro_unit WHERE coid='$coid'") or die (mysqli_error($conn));
                      while($rotunit= mysqli_fetch_assoc($punit_query)){
                        echo '<option value='.$rotunit["puid"].','.$rotunit["unit_convart"].'>'.$rotunit["unit_name"].'('.$rotunit["unit_symble"].') ('.$rotunit["unit_convart"].')</option>'; 
                      }?> 
                    </select>
                  </div>
                </div>               
              <?php }?>
   
                
                
                <div class="col-md-2 <?php if(input_check('p_tax') !==1){ echo 'd-none';}?>" >
                  <div class="form-group">
                    <label><?php echo language(99)?></label>
                    <select name="p_tax" class="form-control">
                      <?php 
                          $purtax_query= mysqli_query($conn,"SELECT * FROM pro_tax  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='purchase' AND active='1'") or die (mysqli_error($conn));
                             while($rotax= mysqli_fetch_assoc($purtax_query)){
                                echo '<option value='.$rotax["ptid"].'>'.$rotax["tax_group"].'('.$rotax["tax_margin"].' %) </option>';
                               }
                            if (@$iter['pro_ptax']) {
                               $ptid = $iter['pro_ptax'];
                               $eppt = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='purchase' AND active='1' AND ptid='$ptid'"));
                               echo '<option selected value='.$eppt["ptid"].'>'.$eppt["tax_group"].'('.$eppt["tax_margin"].' %) </option>';
                            }
                      ?> 
                    </select>
                  </div>
                </div>
                <div class="col-md-2 <?php if(input_check('s_tax') !==1){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(100)?></label>
                    <select name="s_tax" class="form-control">
                    <?php 
                      $saletax_query= mysqli_query($conn,"SELECT * FROM pro_tax  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='sales' AND active='1'  ") or die (mysqli_error($conn));
                      while($rotax= mysqli_fetch_assoc($saletax_query)){
                        echo '<option value='.$rotax["ptid"].'>'.$rotax["tax_group"].'('.$rotax["tax_margin"].' %) </option>';
                      }
                      
                      if (@$iter['pro_stax']) {
                         $stid = $iter['pro_stax'];
                         $est = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='sales' AND active='1' AND ptid='$stid'"));
                         echo '<option selected value='.$est["ptid"].'>'.$est["tax_group"].'('.$est["tax_margin"].' %) </option>';
                       }
                    ?> 
                    </select>
                  </div>
                </div>
                <div class="col-md-2 <?php if(input_check('batch_id') !==1){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(101)?></label>
                    <select name="batch_id" class="form-control">
                  <?php 
                                    
                      $batch_query= mysqli_query($conn,"SELECT * FROM batch_no  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active='1'") or die (mysqli_error($conn));
                      while($rotax= mysqli_fetch_assoc($batch_query)){
                        echo '<option value='.$rotax["batid"].'>'.$rotax["batch_no"].'('.$rotax["mate_code"].') </option>';
                      }
                      
                    if (@$iter['pro_batchid']) {
                         $batid = $iter['pro_batchid'];
                         $ebt = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM batch_no WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active='1' AND batid='$batid'"));
                         echo '<option selected value='.$ebt["batid"].'>'.$ebt["batch_no"].'('.$ebt["mate_code"].') </option>';
                       } 
                      ?> 
                    </select>
                  </div>
                </div>
                <div class="col-md-2 ">
                  <div class="form-group">
                    <label><?php echo language(85)?></label><i class="float-right fa fa-bell"></i>
                    <input type="number" name="item_notif" class="form-control form-control-sm inputselect text-center" 
                    value="<?php if(@$iter['item_notif']){echo $iter['item_notif'];}else{0;}?>">
                  </div>
                </div>
                <div class="col-md-2 <?php if(input_check('weight') !==1){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(102)?></label>
                    <input type="text" name="item_weight"  class="form-control form-control-sm inputselect text-center" value="<?php echo @$iter['item_weight'];?>">
                  </div>
                </div>
                <div class="col-md-2 <?php if(input_check('item_disc') !==1){ echo 'd-none';}?>">
                  <div class="form-group">
                    <label><?php echo language(88)?></label>
                    <input type="number" step="0.001" name="item_disc" class="form-control form-control-sm inputselect text-center" value="<?php echo @$iter['item_disc'];?>">
                  </div>
                </div>
                <div class="col-md-2 ">
                  <div class="form-group">
                    <label><?php echo language(86)?></label>
                    <select name="war_type" class="form-control">
                    <?php if (@$iter['war_type']) {
                          if ($iter['war_type']=='N') {
                            echo '<option value="N">None</option>';
                          } elseif ($iter['war_type']=='D') {
                            echo '<option value="D">Days</option>';
                          } elseif ($iter['war_type']=='M') {
                            echo '<option value="M">Monthly </option>';
                          } elseif ($iter['war_type']=='Y') {
                            echo '<option value="Y">Yearly</option>';
                          } elseif ($iter['war_type']=='L') {
                            echo '<option value="L">Lifetime</option>';
                          }
                    } ?>
                      <option value="N">None</option>
                      <option value="D">Days</option>
                      <option value="M">Monthly </option>
                      <option value="Y">Yearly</option>
                      <option value="L">Lifetime</option>
                    </select> 
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label><?php echo language(103)?></label>
                    <input type="text" name="war_val"  class="form-control form-control-sm inputselect text-center" value="<?php echo @$iter['wa_dmy'];?>">
                  </div>
                </div>
                
                  <div class="col-md-4 <?php if(input_check('emei_tracking') !==1){ echo 'd-none';}?>">
                      <?php if (@$iter['imei_status']==1) {
                         $checkededd="checked";
                      }elseif(@$iter['imei_status']==0){
                         $checkededd="";
                      } ?> <label><?php echo language(386);?></label>
                      <div class="custom-control custom-switch custom-switch-off-danger custom-switch-on-success">
                          
                            <?php 
                                  $count_imei_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM imei_tracker 
                                       WHERE coid='$coid' AND bid='$bid' AND pro_id='$item_id'  "))['count_row'];
                            ?>
                            <?php if($count_imei_row >0){ ?>
                                    <input type="checkbox" disabled id="imei_status" <?php echo $checkededd ;?> class="custom-control-input " value="1">
                         <label class="custom-control-label text-danger" for="imei_status"> Some Serial Number Are Active</label> 

                         
                         <?php } else{ ?>
                         <input type="checkbox" name="imei_status" id="imei_status" <?php echo $checkededd ;?> class="custom-control-input " value="1">
                         <label class="custom-control-label" for="imei_status"></label>
                         <?php } ?>
                      </div>
                </div>
   
              </div>
               </div>
            </div>
            <center>
               <div class="card-footer" style="padding: 8px 5px;">
                <?php if (isset($_GET['pro_id'])) { ?>
                  <button type="submit" id="save" class="btn btn-success btn-xs"><i class="fas fa-save"></i> <?php echo language(25)?></button>
                <?php } ?>
               </div>
            </center>
         </form>
      </div>
    </div>

    <div class="col-md-12 pt-1">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
         <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title"><?php echo language(93)?></h3>
            <div class="card-tools m-0">
               <span class="mark1"><?php echo $grp_name.' >> '.$cat_name.' >> '.$brnd_name; ?></span>
               <button type="button" class="btn btn-tool" data-card-widget="remove">
               <i class="fas fa-times"></i>
               </button>
            </div>
          </div>
              <div class="card-body p-1">
                <table class=" ttable" cellpadding="0" cellspacing="0">
                  <thead>
                    <tr class="sticky-top">
                      <th class="text-center" style="width:2%"><?php echo language(15)?></th>
                      <th class="w-20"><?php echo language(97)?></th>
                      <th class="text-center w-15"><?php echo language(78)?></th>
                      <th class="text-center w-15"><?php echo language(77)?></th>                     
                      <th class="text-center w-5"><?php echo language(80)?></th>
                      <th class="text-center w-10"><?php echo language(107)?></th>
                      <th class="text-center w-10"><?php echo language(83)?></th>
                      <th class="text-center w-10"><?php echo language(84)?></th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                     $iteq= mysqli_query($conn,"SELECT pro_id,item_name,item_code,item_details,item_qty,mrp_purchase,min_sales,mrp_sales FROM item WHERE pro_id='$item_id' AND pro_type='P' AND pgid='$item_gid' AND pcid='$item_cid' AND pbid='$item_brnd' AND coid='$coid' AND $bid='$bid' AND brid='$brid' AND active='1'") or die (mysqli_error($conn));
                     $i=0;
                      while ($iter= mysqli_fetch_assoc($iteq)){

                      $pro_id       = $iter['pro_id'];
                      $item_name    = $iter['item_name'];
                      $item_code    = $iter['item_code'];
                      $item_details = $iter['item_details'];
                      $item_qty     = $iter['item_qty'];
                      $mrp_purchase = $iter['mrp_purchase'];
                      $min_sales    = $iter['min_sales'];
                      $mrp_sales    = $iter['mrp_sales']; 
                    ?>

                       <tr edit_id="<?php echo $pro_id;?>" class="btn-outline-dark">
                        <td class="text-center"><?php echo ++$i ?></td>
                        <td><?php echo $item_name;?></td>
                        <td class="text-center"><?php echo $item_code;?></td>
                        <td><?php echo $item_details;?></td>
                        <td class="text-center"><?php echo $item_qty;?></td>
                        <td class="text-right"><?php echo $mrp_purchase;?></td>
                        <td class="text-right"><?php echo $min_sales;?></td>
                        <td class="text-right"><?php echo $mrp_sales;?></td>
                       </tr>
                    <?php } ?>
                 </tbody>
               </table>
             </div>
        </div>

   </div>
</div>

<script>
    function op_cal(){
      let u_type  = Number($('[name=u_type]').val().split(",")[1]);
      let op_qty  = Number($('#op_qty').val());
      let u_price = Number($('[name=u_price]').val());
      if(!isNaN(u_type) && !isNaN(op_qty) && !isNaN(u_price)){
         $('[name=op_val]').val(u_type*op_qty*u_price);
         $('[name=op_in_qty]').val(u_type*op_qty);

      }
    }
</script>

<script>
   $(document).ready(function(){
     $("select").select2();
     $('#its_form').on('submit', function(e){
      e.preventDefault();
     $('#save').prop('disabled', true);
     $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
     $.post("../itemstock/item_add_db.php",$(this).serialize(),function(data){
      $('#its_form')[0].reset();
      $('#save').prop('disabled', false);
      pagecall('../itemstock/item_profile_update.php?pro_id=<?php echo $pro_id;?>','#pview');
       toastr.success(Showing, yourdata); 
    });
   });
  });  
</script>